流利的NHibernate:模型马尔可夫链

时间:2010-02-09 09:56:53

标签: nhibernate fluent-nhibernate statistics

我想使用Fluent NHibernate来建模Markov chain。它基本上是一组具有各州之间转移概率的不同状态。

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

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

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
 );

我的课程:
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; }
}

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);
    }
}
。我甚至不知道这种模式是否可行。如果您认为此模型无效,请提供替代方案。

1 个答案:

答案 0 :(得分:1)

也许我误解了这个问题,但你的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");
    }
}