Lerning Entity Framework 6 ------ Introduction to TPH


Sometimes, you have created two models. They have the same parent class like this:

public class Person
{
    public int PersonId { get; set; }

    public string PersonName { get; set; }
}

public class InsidePerson : Person
{
    public string Title { get; set; }

    public string Department { get; set; }
}

public class OutsidePerson : Person
{
    public string CompanyName { get; set; }
}

After you execute the command Update-Database in nuget command line, Entity Framework will create one table named people:

图片.png-6.6kB

The Discriminator column is created for discriminating what model dose current row represent. this model creating type is called TPH(Table per Hierarchy Inheritance). Let's do a test. Add some codes in main function:

static void Main(string[] args)
{
    using (MyDbContext db = new MyDbContext())
    {
        Person insidePerson1 = new InsidePerson()
        {
            PersonName = "InsidePerson1",
            Title = "Manager",
            Department = "development"
        };
        db.People.Add(insidePerson1);

        InsidePerson insidePerson2 = new InsidePerson()
        {
            PersonName = "InsidePerson2",
            Title = "Manager",
            Department = "development"
        };
        db.People.Add(insidePerson2);

        InsidePerson insidePerson3 = new InsidePerson()
        {
            PersonName = "InsidePerson3",
            Title = "Manager",
            Department = "development"
        };
        db.InsidePeople.Add(insidePerson3);

        Person outsidePerson1 = new OutsidePerson()
        {
            PersonName = "outsidePerson1",
            CompanyName = "Tencent"
        };
        db.People.Add(outsidePerson1);

        db.SaveChanges();
    }
}

Let's look at the database:

图片.png-6.6kB

If you don't like the discriminator column which entity framework auto create, you can define your column by adding these codes in OnModelCreating function of DbContext class:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Person>()
        .Map<InsidePerson>(p => p.Requires("PersonType").HasValue(1))
        .Map<OutsidePerson>(p => p.Requires("PersonType").HasValue(2));
}

Then, execute the command Add-Migration AddPesonTypeColumn2PeopleTable and Update-Database in nuget command line. Now, look at the database again:

图片.png-6.5kB

We can find the Entity Framework can't insert any value into PersonType column of existed rows. It's a little sad. Now, We insert some new data by coding:

static void Main(string[] args)
{
    using (MyDbContext db = new MyDbContext())
    {
        Person insidePerson4 = new InsidePerson()
        {
            PersonName = "InsidePerson4",
            Title = "Manager",
            Department = "development"
        };
        db.People.Add(insidePerson4);

        Person outsidePerson2 = new OutsidePerson()
        {
            PersonName = "outsidePerson2",
            CompanyName = "Baidu"
        };
        db.People.Add(outsidePerson2);

        db.SaveChanges();
    }
}

Look at the databas again:

图片.png-9.1kB

That's all.

转载于:https://www.cnblogs.com/zzy0471/p/6811269.html

Q-learning算法是强化学习领域的重要算法,为后续算法的发展奠定了基础。近年来基于深度神经网络的Q-Learning扩展算法如DQN、Double DQN、Dueling DQN等,提升了其表示能力和稳定性 [^2]。 Q-learning算法的框架原理基于马尔可夫决策过程。其核心目标是学习一个最优的动作价值函数Q(s, a),这个函数表示在状态s下采取动作a,然后遵循某一策略后续所能获得的最大累积奖励。Q-learning使用迭代更新的方式来不断逼近最优的Q值。其更新公式为: $Q(s_t, a_t) = Q(s_t, a_t) + \alpha [r_{t+1} + \gamma \max_{a} Q(s_{t+1}, a) - Q(s_t, a_t)]$ 其中,$s_t$ 是当前状态,$a_t$ 是当前采取的动作,$r_{t+1}$ 是执行动作后获得的即时奖励,$s_{t+1}$ 是转移后的下一个状态,$\alpha$ 是学习率,控制每次更新的步长,$\gamma$ 是折扣因子,体现了对未来奖励的重视程度 [^1]。 以下是一个简单的Q-learning算法的Python代码示例: ```python import numpy as np # 定义环境参数 num_states = 5 num_actions = 2 gamma = 0.9 # 折扣因子 alpha = 0.1 # 学习率 episodes = 1000 # 初始化Q表 Q = np.zeros((num_states, num_actions)) # 定义奖励矩阵 rewards = np.array([ [-1, -1], [-1, -1], [-1, 10], [-1, -1], [-1, -1] ]) # Q-learning算法 for episode in range(episodes): state = np.random.randint(0, num_states) # 随机初始化状态 done = False while not done: # 选择动作 if np.random.uniform(0, 1) < 0.1: # 探索率 action = np.random.randint(0, num_actions) else: action = np.argmax(Q[state, :]) # 获取奖励和下一个状态 next_state = state reward = rewards[state, action] # 更新Q表 Q[state, action] = Q[state, action] + alpha * (reward + gamma * np.max(Q[next_state, :]) - Q[state, action]) state = next_state if reward == 10: done = True print("最终的Q表:") print(Q) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值