Fluent NHibernate:马尔可夫链模型

发布于 2024-08-21 08:56:54 字数 2268 浏览 3 评论 0原文

我想使用 Fluent NHibernate 来建模 马尔可夫链。它基本上是一组不同的状态,并且状态之间具有转换概率。

我想将转移概率作为字典映射到 MarkovState.TransitionProbabilities 中。我想使用 NEXT 状态作为键(使用 MarkovState 或 int 作为键),以便我可以进行如下查找:

float probability = currentState.TransitionProbabilities[nextState.Id].Probability;

我的数据库模型:

CREATE TABLE MarkovState
(
    Id int IDENTITY(1,1) NOT NULL,
    StateContent varchar(10) NOT NULL
);

CREATE TABLE TransitionProbability
(        
    Id int IDENTITY(1,1) NOT NULL,
    CurrentState_Id int NOT NULL,
    NextState_Id int NOT NULL,
    Probability float NOT NULL
 );

我的课程:

public class MarkovState
{
    public virtual int Id { get; private set; }
    public virtual string StateContent { get; set; }
    public virtual IDictionary<int, TransitionProbability> TransitionProbabilities { get; set; }
}

public class TransitionProbability
{
    public virtual int Id { get; private set; }
    public virtual MarkovState CurrentState { get; set; }
    public virtual MarkovState NextState { get; set; }
    public virtual float Probability { get; set; }
}

和我的地图:

class MarkovStateMap : ClassMap<MarkovState>
{
    public MarkovStateMap()
    {
        Id(x => x.Id);
        Map(x => x.StateContent);
        HasMany<TransitionProbability>(x => x.TransitionProbabilities)
            .AsMap(x => x.NextState.Id)
            .KeyColumn("CurrentState_Id")
            .Not.LazyLoad()
            .Inverse();            
    }
}

class TransitionProbabilityMap : ClassMap<TransitionProbability>
{
    public TransitionProbabilityMap()
    {
        Id(x => x.Id);
        Map(x => x.Probability);
        References(x => x.CurrentState);
        References(x => x.NextState);
    }
}

但是,此映射似乎使用 CurrentState 作为键,而不是 NextState。我什至不知道这个模型是否可行。如果您认为此模型无效,请提出替代方案。

I want to use Fluent NHibernate to model a Markov chain. It's basically a set of different states with transition probabilities between the states.

I want to map the transition probabilities into MarkovState.TransitionProbabilities as a Dictionary. I want to use the NEXT state as key (using either MarkovState or int as key), so that I can do lookups like:

float probability = currentState.TransitionProbabilities[nextState.Id].Probability;

My database model:

CREATE TABLE MarkovState
(
    Id int IDENTITY(1,1) NOT NULL,
    StateContent varchar(10) NOT NULL
);

CREATE TABLE TransitionProbability
(        
    Id int IDENTITY(1,1) NOT NULL,
    CurrentState_Id int NOT NULL,
    NextState_Id int NOT NULL,
    Probability float NOT NULL
 );

My classes:

public class MarkovState
{
    public virtual int Id { get; private set; }
    public virtual string StateContent { get; set; }
    public virtual IDictionary<int, TransitionProbability> TransitionProbabilities { get; set; }
}

public class TransitionProbability
{
    public virtual int Id { get; private set; }
    public virtual MarkovState CurrentState { get; set; }
    public virtual MarkovState NextState { get; set; }
    public virtual float Probability { get; set; }
}

And my maps:

class MarkovStateMap : ClassMap<MarkovState>
{
    public MarkovStateMap()
    {
        Id(x => x.Id);
        Map(x => x.StateContent);
        HasMany<TransitionProbability>(x => x.TransitionProbabilities)
            .AsMap(x => x.NextState.Id)
            .KeyColumn("CurrentState_Id")
            .Not.LazyLoad()
            .Inverse();            
    }
}

class TransitionProbabilityMap : ClassMap<TransitionProbability>
{
    public TransitionProbabilityMap()
    {
        Id(x => x.Id);
        Map(x => x.Probability);
        References(x => x.CurrentState);
        References(x => x.NextState);
    }
}

However, this mapping seems to use CurrentState as the key, and not the NextState. I don't even know if this model is possible. Please suggest alternatives if you think this model isn't valid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凯凯我们等你回来 2024-08-28 08:56:54

也许我误解了这个问题,但您的 HasMany 映射指向 CurrentState_Id。我认为你的映射应该是这样的:

class MarkovStateMap : ClassMap<MarkovState>
{
    public MarkovStateMap()
    {
        Id(x => x.Id);
        Map(x => x.StateContent);
        HasMany<TransitionProbability>(x => x.TransitionProbabilities)
            .AsMap(x => x.NextState.Id)
            .KeyColumn("NextState_Id")
            .Not.LazyLoad()
            .Inverse();            
    }
}

class TransitionProbabilityMap : ClassMap<TransitionProbability>
{
    public TransitionProbabilityMap()
    {
        Id(x => x.Id);
        Map(x => x.Probability);
        References(x => x.CurrentState, "Id");
        References(x => x.NextState, "Id");
    }
}

Maybe I'm misunderstanding the question, but your HasMany mapping points to CurrentState_Id. I think you're mappings should look like:

class MarkovStateMap : ClassMap<MarkovState>
{
    public MarkovStateMap()
    {
        Id(x => x.Id);
        Map(x => x.StateContent);
        HasMany<TransitionProbability>(x => x.TransitionProbabilities)
            .AsMap(x => x.NextState.Id)
            .KeyColumn("NextState_Id")
            .Not.LazyLoad()
            .Inverse();            
    }
}

class TransitionProbabilityMap : ClassMap<TransitionProbability>
{
    public TransitionProbabilityMap()
    {
        Id(x => x.Id);
        Map(x => x.Probability);
        References(x => x.CurrentState, "Id");
        References(x => x.NextState, "Id");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文