Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在DQN(深度Q网络)中分配状态?_C#_Python_Unity3d_Neural Network_Q Learning - Fatal编程技术网

C# 如何在DQN(深度Q网络)中分配状态?

C# 如何在DQN(深度Q网络)中分配状态?,c#,python,unity3d,neural-network,q-learning,C#,Python,Unity3d,Neural Network,Q Learning,我正在用自动驾驶仪进行飞行模拟,所以我需要制作一个DQN(深度Q网络)来控制自动驾驶仪,但我不知道最佳状态数 模拟是统一完成的,所有的环境和物理也都完成了,DQN只需要输出(W,A,S,D)来控制飞机,我发现了一个控制CARTPOLE的代码,理论上应该能够训练和控制飞机,唯一的问题是我不知道我选择的州是否正确 代码如下: import os import random import gym import numpy as np from collecti

我正在用自动驾驶仪进行飞行模拟,所以我需要制作一个DQN(深度Q网络)来控制自动驾驶仪,但我不知道最佳状态数

模拟是统一完成的,所有的环境和物理也都完成了,DQN只需要输出(W,A,S,D)来控制飞机,我发现了一个控制CARTPOLE的代码,理论上应该能够训练和控制飞机,唯一的问题是我不知道我选择的州是否正确

代码如下:


    import os
    import random
    import gym
    import numpy as np
    from collections import deque
    from keras.models import Sequential
    from keras.layers import Dense
    from keras.optimizers import Adam



    class DQNAGENT:

        def __init__(self,state_size,_action_size):

            self.state_size = state_sizes
            self.action_size = actions_sizes
            self.memory = deque(maxlen=2000)
            self.gamma = 0.95
            self.epsilon = 1.00
            self.epsilon_decay_rate = 0.995
            self.epsilon_min = 0.01
            self.learning_rate = 0.001
            self.model = self.build_model()

        def buildmodel(self):

            model = Sequential()
            model.add(Dense(24, input_dim=self.state_size, activation='relu'))
            model.add(Dense(24, activation='relu'))
            model.add(Dense(self.action_size, activation='linear'))
            model.compile(loss='mse',optimizer=Adam(lr=self.learning_rate))
            return model

        def remember(self, state, action, reward, next_state, done):

            self.memory.append((state, action, reward, next_state, done))

        def act(self, state):

            if np.random.rand()  self.epsilon_min:
                self.epsilon *= self.epsilon_decay_rate

        def load(self, name):

            self.model.load_weights(name)

        def save(self, name):

            self.model.save_weights(name)


    def main():
        #environemnet variables
        state_sizes=0
        actions_sizes=4
        #training Variables
        batch_size=32
        n_episodeds=100
        output_directory= 'model_output/autopilot'
        if not os.path.exists(output_directory):
            os.makedirs(output_directory)

        agent = DQNAGENT(state_sizes,actions_sizes)
        done = False

        for e in range(n_episodeds):
            state = #states of the game
            for time in range(5000):
                action = agent.act(state)
                #next_state, reward, done, _ = ##env.step(action)
                #put the next state from unity
                reward = reward if not done else -10
                agent.remember(state, action, reward, next_state, done)
                state = next_state
                if len(agent.memory) > batch_size:
                    agent.replay(batch_size)
    
如果代理类是将被训练的代理,那么这些函数是可以的,但是在Main中,状态大小设置为,因为我还不知道这三行的数量,我无法转换为能够在我的项目上运行

state = #states of the game
action = agent.act(state)
next_state, reward, done, _ = ##env.step(action)
原始代码有以下几行代码:

env = gym.make('CartPole-v1')
state_size = env.observation_space.shape[0]
state = env.reset()
next_state, reward, done, _ = env.step(action)
因为它从健身房软件包中获取这些变量,但我需要手动输入这些变量,我的环境将包括空速、飞机位置、机场位置、,etc这就是我想写的,所以如果有人能帮我弄清楚这是否正确,或者更好,告诉我什么是最佳状态,我会非常感激

例外的结果是这样的

statesizes = 4
states= "how to write those states in this variable"

这些状态只是您的代理在当前“帧/步骤”中拥有的信息。
这是代理选择操作所需的
agent.act(state)

CartPole示例中,状态是一个包含4个值的框:

  • 大车位置
  • 小车速度
  • 极角
  • 尖端极速度
在您的飞行自动驾驶仪中,状态是您需要您的代理拥有的信息,以便做出决策,例如:

  • 电流格度
  • 当前速度