开发者

Fluent NHibernate: Model Markov chain

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 vir开发者_如何学Pythontual 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.


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");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜