改进这段代码,change = [[0, -step], [0, step], [-step, 0], [step, 0]] F_actions = [] for i in range(4): next_state_action = s[0] + np.array(change[i]) # 有点问题np.array把列表转为数组 (x1, y1) = next_state_action F_next_state__actions = x12 + y12 F_actions.append(F_next_state__actions) action = np.argmin(F_actions) print('动作值', F_actions),
时间: 2024-02-13 08:05:54 浏览: 60
这段代码可以进行如下改进:
1. 将 F_next_state_actions 的计算过程中的 **2 变为平方函数 pow(x1, 2) + pow(y1, 2),这样更加清晰易懂。
2. 将 x1 和 y1 的计算与 next_state_action 放在同一行,减少代码行数,提高代码可读性。
3. 将 F_actions 的计算和 action 的计算分离开来,代码更加清晰易懂。
改进后的代码如下:
```
change = [[0, -step], [0, step], [-step, 0], [step, 0]]
F_actions = []
for i in range(4):
next_state_action = s[0] + np.array(change[i])
F_next_state_actions = pow(next_state_action[0], 2) + pow(next_state_action[1], 2)
F_actions.append(F_next_state_actions)
action = np.argmin(F_actions)
print('动作值', F_actions)
```
这样代码更加简洁,易于理解和维护。
相关问题
改进代码,def take_action( s): # 选取下一步的操作 if np.random.random() < epsilon: action = np.random.randint(n_action) # def take_action(self, s): # 选取下一步的操作 # (x0,y0)=s0 # a0为向下,a1为向上,a2为向左,a3为向右 # change = [[0, -step], [0, step], [-step, 0], [step, 0]] # F_actions = [] else: change = [[0, -step_action], [0, step_action], [-step_action, 0], [step_action, 0]] F_actions = [] for i in range(4): next_state_action = np.array(s[0]) + np.array(change[i]) # 有点问题np.array把列表转为数组 (x1, y1) = next_state_action F_next_state__actions = x1 ** 2 + y1 ** 2 F_actions.append(F_next_state__actions) action = np.argmin(F_actions) print('动作值', F_actions) print('动作:', action) return action
这里是您现有的代码:
```python
def take_action(s): # 选取下一步的操作
if np.random.random() < epsilon:
action = np.random.randint(n_action)
else:
change = [[0, -step_action], [0, step_action], [-step_action, 0], [step_action, 0]]
F_actions = []
for i in range(4):
next_state_action = np.array(s[0]) + np.array(change[i])
(x1, y1) = next_state_action
F_next_state__actions = x1 ** 2 + y1 ** 2
F_actions.append(F_next_state__actions)
action = np.argmin(F_actions)
print('动作值', F_actions)
print('动作:', action)
return action
```
这里有一些需要改进的地方:
1. 函数的参数 `s` 是一个状态,应该是一个列表或者数组,但是在函数中却使用了 `s[0]`,这会导致下一步的动作只考虑了状态的第一个元素。应该使用 `s` 直接进行计算。
2. 计算下一步状态的代码中应该使用 `step_action` 而不是 `step`。
3. 在计算动作值时,因为 `F_actions` 是一个列表,最小值的索引可以使用 `np.argmin` 方法来获取。
4. `print` 语句应该在函数的最后,因为函数的主要任务是返回下一步动作,而不是输出信息。
改进后的代码如下:
```python
def take_action(s):
if np.random.random() < epsilon:
action = np.random.randint(n_action)
else:
change = [[0, -step_action], [0, step_action], [-step_action, 0], [step_action, 0]]
F_actions = []
for i in range(4):
next_state_action = np.array(s) + np.array(change[i])
(x1, y1) = next_state_action
F_next_state_actions = x1 ** 2 + y1 ** 2
F_actions.append(F_next_state_actions)
action = np.argmin(F_actions)
print('动作:', action)
print('动作值', F_actions)
return action
```
<builders> <org.jenkinsci.plugins.conditionalbuildstep.singlestep.SingleConditionalBuilder plugin="conditional-buildstep@1.4.2"> <condition class="org.jenkins_ci.plugins.run_condition.contributed.ShellCondition" plugin="run-condition@1.5"> <command>if git show --summary HEAD | grep -q -E "\(cherry picked from commit \b[0-9a-f]{5,40}\b\)"; then exit 1 fi</command> </condition> <buildStep class="hudson.tasks.Shell"> <command>./scripts/checkpatch.pl --min-conf-desc-length=1 --ignore GERRIT_CHANGE_ID -g HEAD </command> <configuredLocalRules/> </buildStep> <runner class="org.jenkins_ci.plugins.run_condition.BuildStepRunner$Run" plugin="run-condition@1.5"/> </org.jenkinsci.plugins.conditionalbuildstep.singlestep.SingleConditionalBuilder> <hudson.tasks.Shell> <command>NCPU=$(expr $(nproc --all ) / 2) make O=build-${BUILD_ID} -s defconfig make O=build-${BUILD_ID} -j$NCPU -s Image make O=build-${BUILD_ID} -j$NCPU -s modules make O=build-${BUILD_ID} INSTALL_MOD_PATH=../install-${BUILD_ID} -s modules_install mkdir -p install-${BUILD_ID}/boot/ KERNELVER=$(cat build-${BUILD_ID}/include/config/kernel.release 2> /dev/null) cat build-${BUILD_ID}/arch/arm64/boot/Image > install-${BUILD_ID}/boot/Image-$KERNELVER cp build-${BUILD_ID}/System.map install-${BUILD_ID}/boot/System.map-$KERNELVER cp build-${BUILD_ID}/.config install-${BUILD_ID}/boot/config-$KERNELVER tar -cJf install-${BUILD_ID}/linux-$KERNELVER.tar.xz -C install-${BUILD_ID}/ boot lib</command> <configuredLocalRules/> </hudson.tasks.Shell> <hudson.tasks.Shell> <command>cd $HOME/src/linux git fetch -p -t git pull </command> <configuredLocalRules/> </hudson.tasks.Shell> </builders> 这是一段Jenkins job的定义,请把它转成对应的JJB的yaml格式配置文件
对应的JJB的yaml格式配置文件如下所示:
```yaml
- builders:
- conditional-step:
conditional-buildstep@1.4.2:
condition:
shell-condition:
run-condition@1.5:
command: "if git show --summary HEAD | grep -q -E \"\\(cherry picked from commit \\b[0-9a-f]{5,40}\\b\\)\"; then exit 1 fi"
buildStep:
shell:
command: "./scripts/checkpatch.pl --min-conf-desc-length=1 --ignore GERRIT_CHANGE_ID -g HEAD"
configuredLocalRules: ""
runner:
run-build-step-runner:
run-condition@1.5: ""
- shell:
command: "NCPU=$(expr $(nproc --all ) / 2) make O=build-${BUILD_ID} -s defconfig make O=build-${BUILD_ID} -j$NCPU -s Image make O=build-${BUILD_ID} -j$NCPU -s modules make O=build-${BUILD_ID} INSTALL_MOD_PATH=../install-${BUILD_ID} -s modules_install mkdir -p install-${BUILD_ID}/boot/ KERNELVER=$(cat build-${BUILD_ID}/include/config/kernel.release 2> /dev/null) cat build-${BUILD_ID}/arch/arm64/boot/Image > install-${BUILD_ID}/boot/Image-$KERNELVER cp build-${BUILD_ID}/System.map install-${BUILD_ID}/boot/System.map-$KERNELVER cp build-${BUILD_ID}/.config install-${BUILD_ID}/boot/config-$KERNELVER tar -cJf install-${BUILD_ID}/linux-$KERNELVER.tar.xz -C install-${BUILD_ID}/ boot lib"
configuredLocalRules: ""
- shell:
command: "cd $HOME/src/linux git fetch -p -t git pull"
configuredLocalRules: ""
```
阅读全文