強化学習でファンネルの攻撃から避けるガンダムを作る(準備1)
去年ぐらいから人工知能を勉強し始めまして、技術書とかは一通り触ったりしていて次に何やろうかなーと思ってたら半年が経過していました。
最近ちょうどガンダムUCを見ていたらファンネルがあったので、今回からファンネルとガンダムで人工知能を作ってみようと思います。
アムロが乗ったガンダムがジオン軍のファンネルを避けるという構図で作って、避けるのは強化学習でできればと思います。
強化学習はchainerかTensorflowどっちで作るかはまだ決めてませんが。。。
初回からいきなり強化学習とはいかないので、最初はガンダムとファンネル、ファンネルからの攻撃を作ろうと思います。
開発環境は以下の通りです。
OS : win10
言語 : python3.6
結果gif:
座標[0,0]のガンダム(赤丸)が周りのファンネルから攻撃受けるところまでです
以下プログラム:
・ガンダムは座標[0,0]固定
・ファンネルはランダム配置
・ガンダム、ファンネルの座標から角度を出してビーム座標を計算
・while文中のplt.pause()でウェイトをかけている
・plt.clfで描画リセット
*プログラムのコメントは後で追加するかもです
#coding: UTF-8 from __future__ import unicode_literals, print_function import numpy as np from numpy import array import matplotlib.pyplot as plt import math import copy from time import sleep def pause_plot(): fig, ax = plt.subplots(1, 1) fannelNum = 10 time = 0 maxStep = 15 step = [] for i in range(maxStep): step.append(0.0) oldX = copy.deepcopy(step) oldY = copy.deepcopy(step) diffX = copy.deepcopy(step) diffY = copy.deepcopy(step) beamA = copy.deepcopy(step) beamB = copy.deepcopy(step) fannelPointX = copy.deepcopy(step) fannelPointY = copy.deepcopy(step) first = 0 while True: gundamX = 0.0 gundamY = 0.0 plt.plot(gundamX, gundamY, "ro") i = 0 for i in range(fannelNum): if time == 0: print("start") x = np.random.randn(fannelNum) y = np.random.randn(fannelNum) fannelPointX[i] = x[i] fannelPointY[i] = y[i] diffX[i] = gundamX-x[i] diffY[i] = gundamY-y[i] oldX[i] = x[i] oldY[i] = y[i] else: print(time) diffY[i] = gundamY-oldY[i] diffX[i] = gundamX-oldX[i] tht = math.atan2(diffY[i],diffX[i]) #print(tht) len = 0.1 beamA[i] = len * math.cos(tht) beamB[i] = len * math.sin(tht) plt.plot([oldX[i], (beamA[i]-diffX[i])], [oldY[i], (beamB[i]-diffY[i])], 'y-', lw=2) oldX[i] = (beamA[i]-diffX[i]) oldY[i] = (beamB[i]-diffY[i]) for i in range(fannelNum): plt.plot(fannelPointX[i], fannelPointY[i], "o") plt.pause(0.005) if time < maxStep: time += 1 for i in range(maxStep): step[i] = time else: time = 0 for i in range(maxStep): step[i] = 0 if first == 0: sleep(15) first =1 plt.clf() print("=======") if __name__ == "__main__": pause_plot()