这是完整代码import math import random import numpy as np import matplotlib.pyplot as plt #import self as self epsilon = 0.5 gamma = 0.1 lr = 0.1 zeros_vector=[] x = []; y = []; X = []; Y = []; agent=[x,y]; object=[X,Y]; random.seed(70) for i in range(10): x.append(random.uniform(0, 1)) y.append(random.uniform(0, 1)) X.append(random.uniform(1, 10)) Y.append(random.uniform(1, 10)) distance = [] for i in range(len(agent[0])): distance_vector = [] for j in range(len(object[0])): dx = agent[0][i] - object[0][j] dy = agent[1][i] - object[1][j] distance_vector.append(math.sqrt(dx * dx + dy * dy)) distance.append(distance_vector) R_table = np.zeros((10, 10)) for i in range(len(agent[0])): for j in range(len(object[0])): R_table[i,j] = 20-distance[i][j] space = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] #Q_table = [] Q_table = np.zeros((10, 10)) # 进行训练同时测试训练成果 iterate_results = [] # 保存每次测试结果 for i in range(500): print(f"开始第{i + 1}回合。。。") # 初始位置 path = [] # 每个回合要获取10个位置 for j in range(10): remain = set(space) - set(path) # 剩余节点 # s = path[0] # 当前位置 # s_row = Q_table[s] # 当前位置对应的Q表中的行 max_value = -1000 # 在剩余动作中遍历最大值 for rm in remain: Q = Q_table[j][rm] if Q > max_value: max_value = Q a = rm # 随机选择下一行动 if np.random.uniform() < epsilon: a = np.random.choice(np.array(list(set(space) - set(path)))) # 更新Q表 if j != 10: Q_table[j][a] = (1 - lr) * Q_table[j][a] + lr * (R_table[j][a] + gamma * max_value) else: Q_table[j][a] = (1 - lr) * Q_table[j][a] + lr * R_table[j][a] path.append(a) # print(Q_table[j][a]) # 根据当前Q表获取最佳路径距离 result = [] lengths=[] final_length=1000 final_result=[] for k in range(10): loc = k remain1 = set(space) - set(result) # 剩余节点 max_value1 = -1000 # 在剩余节点中遍历最大值 # a = 0 # 利用贪婪策略选择下一行动 for v in remain1: Q_ = Q_table[loc][v] if Q_ > max_value1: a = v max_value1 = Q_ result.append(a) length = 0 for v in range(1, 10): length += distance[result[v - 1]][v-1] print(f"距离为:{length}") if length < final_length: final_length=length final_result=result iterate_results.append(length)
时间: 2024-02-28 07:56:04 浏览: 25
根据您提供的代码,if条件句应该是可以生效的。代码中有一个变量叫做`final_length`,它的初始值为1000。在代码的最后,有一个循环用于计算路径长度,并与`final_length`进行比较,如果路径长度小于`final_length`,则将`final_length`更新为当前路径长度,并将路径保存为`final_result`。所以,如果路径长度小于`final_length`,if条件句应该会被执行。
具体来说,下面这段代码就是if条件句:
```python
if length < final_length:
final_length=length
final_result=result
```
这段代码的作用是,如果当前路径长度`length`小于`final_length`,则将当前路径长度赋值给`final_length`,并将当前路径保存为`final_result`。
如果您有任何其他问题,请随时问我。
相关问题
import numpy as np import matplotlib.pyplot as plt import sympy from scipy.interpolate import interp1d gamma = 1.2 R = 8.314 T0 = 500 Q = 50 * R * T0 a0 = np.sqrt(gamma * R * T0) M0 = 6.216 P_P0 = sympy.symbols('P_P0') num = 81 x0 = np.linspace(0,1,num) t_t0 = np.linspace(0,15,num) x = x0[1:] T_T0 = t_t0[1:] h0 = [] h1 = []#创建拉姆达为1的空数组 r = [] t = [] c = [] s = [] i = 0 for V_V0 in x: n1 = sympy.solve(1 / (gamma-1) * (P_P0 * V_V0 - 1) - 0.5 * (P_P0 + 1) * (1 - V_V0)- gamma * 0 * Q / a0 ** 2,P_P0)#lamuda=0的Hugoniot曲线方程 n2 = sympy.solve(1 / (gamma-1) * (P_P0 * V_V0 - 1) - 0.5 * (P_P0 + 1) * (1 - V_V0)- gamma * 1 * Q / a0 ** 2,P_P0)#lamuda=1的Hugoniot曲线方程 n3 = sympy.solve(-1 * P_P0 + 1 - gamma * M0 ** 2 * (V_V0 - 1),P_P0)#Reyleigh曲线方程 n4 = 12.014556 / V_V0#等温线 n5 = sympy.solve((P_P0 - 1 / (gamma+1) )* (V_V0-gamma / (gamma + 1)) - gamma / ((gamma + 1) ** 2),P_P0)#声速线 n6 = 10.6677 / np.power(V_V0,1.2)#等熵线 h0.append(n1) h1.append(n2) r.append(n3) t.append(n4) c.append(n5) s.append(n6) i = i+1 h0 = np.array(h0) h1 = np.array(h1) r = np.array(r) t = np.array(t) c = np.array(c) s = np.array(s) plt.plot(x,r,label='Rayleigh') plt.plot(x,t,color='purple',label='isothermal') plt.plot(x,s,color='skyblue',label='isentropic') a = np.where(h0 < 0) b = np.where(c < 0) h0 = np.delete(h0,np.where(h0 < 0)[0],axis = 0)#去除解小于0的值 h1 = np.delete(h1,np.where(h1 < 0)[0],axis = 0)#去除解小于0的值 c = np.delete(c,np.where(c < 0)[0],axis = 0)#去除解小于0的值 x0 = np.delete(x,a,axis = 0)#对应去除x轴上错误值的坐标 x1 = np.delete(x,b,axis = 0) plt.plot(x0,h0,label='Hugoniot(lambda=0)') plt.plot(x0,h1,label='Hugoniot(lambda=1)') plt.plot(x1,c,color='yellow',label='soniclocus') plt.ylim((0,50)) plt.legend() # 显示图例 plt.xlabel('V/V0') plt.ylabel('P/P0') f1 = interp1d(x1, c.T, kind='cubic') f2 = interp1d(x,r.T,kind='cubic') f3 = interp1d(x, t.T, kind='cubic') epsilon = 0.0001 x0 = 0.56 y0 = f1(x0) - f2(x0) while abs(y0) > epsilon: df = (f1(x0 + epsilon) - f2(x0 + epsilon) - y0) / epsilon x0 -= y0 / df y0 = f1(x0) - f2(x0) plt.scatter(x0, y0, 50, color ='red') plt.show()
这是一个 Python 代码,我可以解读出来。这段代码主要是用来绘制气体动力学中的一些图形,包括 Hugoniot 曲线、Rayleigh 曲线、等温线、等熵线和声速线等等。其中用到了一些科学计算库,比如 numpy、matplotlib、sympy 和 scipy.interpolate。这段代码还包括了一些数据处理的操作,比如删除数组中小于0的值、插值等等。最后,这段代码还用牛顿迭代法求解了两条曲线的交点。
DQN Pong Pytorch的完整代码
下面是一个使用Pytorch实现DQN算法解决Pong游戏的完整代码:
```
import gym
import random
import math
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from collections import deque
# 定义神经网络模型
class DQN(nn.Module):
def __init__(self, input_shape, num_actions):
super(DQN, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(input_shape[0], 32, kernel_size=8, stride=4),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=4, stride=2),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, stride=1),
nn.ReLU()
)
conv_out_size = self._get_conv_out(input_shape)
self.fc = nn.Sequential(
nn.Linear(conv_out_size, 512),
nn.ReLU(),
nn.Linear(512, num_actions)
)
def _get_conv_out(self, shape):
o = self.conv(torch.zeros(1, *shape))
return int(np.prod(o.size()))
def forward(self, x):
conv_out = self.conv(x).view(x.size()[0], -1)
return self.fc(conv_out)
# 定义DQN算法
class DQNAgent:
def __init__(self, env):
self.env = env
self.replay_buffer = deque(maxlen=10000)
self.gamma = 0.99
self.epsilon = 1.0
self.epsilon_decay = 0.995
self.epsilon_min = 0.01
self.batch_size = 32
self.model = DQN(env.observation_space.shape, env.action_space.n).to(device)
self.target_model = DQN(env.observation_space.shape, env.action_space.n).to(device)
self.target_model.load_state_dict(self.model.state_dict())
self.optimizer = optim.Adam(self.model.parameters(), lr=0.00025)
def act(self, state):
if np.random.rand() <= self.epsilon:
return self.env.action_space.sample()
state = torch.FloatTensor(state).unsqueeze(0).to(device)
q_values = self.model(state)
return q_values.max(1)[1].item()
def replay(self):
if len(self.replay_buffer) < self.batch_size:
return
batch = random.sample(self.replay_buffer, self.batch_size)
states, actions, rewards, next_states, dones = zip(*batch)
states = torch.FloatTensor(states).to(device)
actions = torch.LongTensor(actions).to(device)
rewards = torch.FloatTensor(rewards).to(device)
next_states = torch.FloatTensor(next_states).to(device)
dones = torch.FloatTensor(dones).to(device)
q_values = self.model(states).gather(1, actions.unsqueeze(-1)).squeeze(-1)
next_q_values = self.target_model(next_states).max(1)[0]
expected_q_values = rewards + self.gamma * next_q_values * (1 - dones)
loss = F.mse_loss(q_values, expected_q_values.detach())
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
def train(self, num_episodes):
rewards = []
for i in range(num_episodes):
state = env.reset()
done = False
episode_reward = 0
while not done:
action = self.act(state)
next_state, reward, done, _ = env.step(action)
self.replay_buffer.append((state, action, reward, next_state, done))
state = next_state
episode_reward += reward
self.replay()
self.update_target_model()
rewards.append(episode_reward)
self.epsilon = max(self.epsilon_min, self.epsilon * self.epsilon_decay)
print("Episode {}: reward = {}, epsilon = {}".format(i, episode_reward, self.epsilon))
return rewards
def update_target_model(self):
self.target_model.load_state_dict(self.model.state_dict())
# 设置超参数和设备
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
env = gym.make('Pong-v0')
num_episodes = 1000
# 创建代理并训练
agent = DQNAgent(env)
rewards = agent.train(num_episodes)
# 绘制训练曲线
plt.plot(rewards)
plt.xlabel('Episode')
plt.ylabel('Reward')
plt.title('Training Curve')
plt.show()
```
注意,在运行代码之前需要安装好Pytorch和Gym库。代码中使用了一个双重Q网络和经验回放的技巧来提高算法的性能和稳定性。在训练结束后,会输出每个回合的奖励和探索率,并绘制训练曲线。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)