当然可以! 下面是一个简单的 Q-learning 智能体的 Python 代码例子:
import numpyas np
class QLearningAgent:
def __init__(self, alpha, epsilon, discount, get_legal_actions):
self.get_legal_actions = get_legal_actions
self._qvalues = {}
self.alpha = alpha
self.epsilon = epsilon
self.discount = discount
def get_qvalue(self, state, action):
return self._qvalues.get((state, action), 0.0)
def set_qvalue(self, state, action, value):
self._qvalues[(state, action)] = value
def get_value(self, state):
possible_actions = self.get_legal_actions(state)
if not possible_actions:
return 0.0
return max(self.get_qvalue(state, action) for action in possible_actions)
def update(self, state, action, reward, next_state):
gamma = self.discount
learning_rate = self.alpha
q_next = self.get_value(next_state)
q_current = self.get_qvalue(state, action)
self.set_qvalue(state, action, q_current + learning_rate * (reward + gamma * q_next - q_current))
def get_best_action(self, state):
possible_actions = self.get_legal_actions(state)
if not possible_actions:
return None
best_action = max(possible_actions, key=lambda x: self.get_qvalue(state, x))
return best_action
def get_action(self, state):
possible_actions = self.get_legal_actions(state)
if not possible_actions:
return None
if np.random.rand() < self.epsilon:
return np.random.choice(possible_actions)
return self.get_best_action(state)
希望这个代码能帮助到你!