【问题标题】:Define action values in keras-rl在 keras-rl 中定义动作值
【发布时间】:2020-07-18 08:45:06
【问题描述】:

我在keras-rl中有一个自定义环境,在构造函数中有如下配置

def __init__(self, data):

    #Declare the episode as the first episode
    self.episode=1

    #Initialize data      
    self.data=data

    #Declare low and high as vectors with -inf values 
    self.low = numpy.array([-numpy.inf])
    self.high = numpy.array([+numpy.inf])

    self.observation_space = spaces.Box(self.low, self.high, dtype=numpy.float32)

    #Define the space of actions as 3 (I want them to be 0, 1 and 2)
    self.action_space = spaces.Discrete(3) 

    self.currentObservation = 0

    self.limit = len(data)      

    #Initiates the values to be returned by the environment
    self.reward = None

如您所见,我的代理将执行 3 个动作,根据动作,将在下面的函数 step() 中计算不同的奖励:

def step(self, action):

    assert self.action_space.contains(action)

    #Initiates the reward
    self.reward=0

    #get the reward 
    self.possibleGain = self.data.iloc[self.currentObservation]['delta_next_day']

    #If action is 1, calculate the reward 
    if(action == 1):
        self.reward = self.possibleGain-self.operationCost

    #If action is 2, calculate the reward as negative     
    elif(action==2):
        self.reward = (-self.possibleGain)-self.operationCost

    #If action is 0, no reward     
    elif(action==0):
        self.reward = 0

    #Finish episode 
    self.done=True 

    self.episode+=1   
    self.currentObservation+=1

    if(self.currentObservation>=self.limit):
        self.currentObservation=0

    #Return the state, reward and if its done or not
    return self.getObservation(), self.reward, self.done, {}

问题在于,如果我打印每一集的动作,它们是 0、2 和 4。我希望它们是 0、1 和 2。我怎样才能强制代理只识别这 3 个使用 keras-rl 的操作?

【问题讨论】:

  • 您是否尝试过使用:spaces.Box(self.low, self.high, shape=(1,), dtype=numpy.float32)?它似乎是大多数示例中的流行格式

标签: python keras reinforcement-learning keras-rl


【解决方案1】:

我不知道为什么self.action_space = spaces.Discrete(3) 会以0,2,4 的身份为您提供操作,因为我无法使用您发布的代码 sn-p 重现您的错误,所以我建议您使用以下方法来定义您的操作

self.action_space = gym.spaces.Box(low=np.array([1]),high= np.array([3]), dtype=np.int)

这是我从动作空间中采样时得到的。

actions= gym.spaces.Box(low=np.array([1]),high= np.array([3]), dtype=np.int)
for i in range(10):
    print(actions.sample())

[1]
[3]
[2]
[2]
[3]
[3]
[1]
[1]
[2]
[3]

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多