RaspberryPiとUbuntu16.04にpipでtensorflowをインストールする

RaspberryPi3(Raspbian)とUbuntu16.04にtensorflowをインストールしていきます。

[python3系デフォルト設定]
*python2の人はここの作業は不要です。
すでに以下のpython3系のインストールをやっているとして

sudo apt-get install  python3 python3-pip python3-dev python3-numpy python3-scipy python3-matplotlib

まず僕の環境はpython3なので、
python3をデフォルトに設定させました

sudo nano .bashrc

一番下に記入

alias python=python3
$ . ~/.bashrc
$ python --version

確認できたらok

[tensorflowインストール]
次にネイティブpipでtensorflowをインストール
tensorflowでgpuとかpython2系3系があるので以下を参照

$ pip install tensorflow      # Python 2.7; CPU support (no GPU support)
$ pip3 install tensorflow     # Python 3.n; CPU support (no GPU support)
$ pip install tensorflow-gpu  # Python 2.7;  GPU support
$ pip3 install tensorflow-gpu # Python 3.n; GPU support

もしできなかった場合は、.whlでインストール
下のコマンドの最後にある”tfBinaryURL”の部分を自分の環境に合うURLに置き換える。

Python 2.7の場合
$ sudo pip  install --upgrade tfBinaryURL

# Python 3系の場合
$ sudo pip3 install --upgrade tfBinaryURL   

Python 2.7
CPU only:
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.3.0-cp27-none-linux_x86_64.whl
GPU support:
https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.3.0-cp27-none-linux_x86_64.whl

Python 3.4
CPU only:
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.3.0-cp34-cp34m-linux_x86_64.whl
GPU support:
https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.3.0-cp34-cp34m-linux_x86_64.whl

Python 3.5
CPU only:
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.3.0-cp35-cp35m-linux_x86_64.whl
GPU support:
https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.3.0-cp35-cp35m-linux_x86_64.whl

Python 3.6
CPU only:
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.3.0-cp36-cp36m-linux_x86_64.whl
GPU support:
https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.3.0-cp36-cp36m-linux_x86_64.whl



ちなみにtensorflowのアンインストールは

$ sudo pip uninstall tensorflow  # for Python 2.7
$ sudo pip3 uninstall tensorflow # for Python 3.n

おわり

VMware12にubuntu16.04を入れたときのメモ

まずvmware12を入れます
https://www.vmware.com/jp/products/player/playerpro-evaluation.html
DL&インストール

次にubuntu16.04を入れます
http://www.ubuntulinux.jp/download/ja-remix
DLしてvmware立ち上げてubuntuのisoを指定してOSを入れる


ubuntuが入ったら端末を起動して
リポジトリがUSで遅いので日本(JAIST)に変更

$ sudo sed -i".bak" -e 's/\/\/us.archive.ubuntu.com/\/\/ftp.jaist.ac.jp/g'  /etc/apt/sources.list

次に

$ sudo apt-get update
$ sudo apt-get upgrade

したいんですけど、以下のエラーがでたので解決、

E: ロック /var/lib/dpkg/lock が取得できませんでした - open (11: リソースが一時的に利用できません)
E: 管理用ディレクトリ (/var/lib/dpkg/) をロックできません。これを使う別のプロセスが動いていませんか?
$ sudo rm /var/lib/apt/lists/lock 
$ sudo rm /var/lib/dpkg/lock

で対象を消去して先ほどの2つを実行

おわり
2017/09/07 追記:
python環境入れます

sudo apt-get install  python3 python3-pip python3-dev python3-numpy python3-scipy python3-matplotlib
pip3 install chainer
pip3 install tensorflow


ちなみにnvidiaのドライバ入れたらログインできなくなってみんなその事象に遭遇しているみたいです。
(なので面倒だから今回は入れませんでした)


参考:
http://pc.casey.jp/archives/153900744
http://qiita.com/koara-local/items/e63b7e4ff3cea803bdea
https://www.tensorflow.org/install/install_linux#installing_with_native_pip

温湿度センサ(HDC1080)について

HDC1080は温湿度センサです。
今回、環境センサーモジュール【MM-ENV01】に乗ってるHDC1080をArduinoで使いましたが、標準ライブラリでは温度、湿度ともに
きちんとした数値が取得できない問題が発生しました。

f:id:weekendproject9:20170822191438p:plain
www.marutsu.co.jp

そこできちんと数値が取得できるように以下のことをしました。
ClosedCube HDC1080 - Arduino Libraries
ここの最新版ライブラリをダウンロードする。
解凍後、ファイルをprogramfile\Arduino\libariesにコピーする。

ArduinoIDEを起動させ、ファイル→スケッチ例→ClosedCube_HDC1080→demoを選択
アドレスが標準(0x40)のままならそのまま書き込み
シリアルモニタで温度、湿度がきちんと取得できているか確認。

確認できたら、MM-ENV01に移植する形で解決。

プログラム(C#)で2リンクの逆運動学を解く

数年前に作ったC#で2リンクの逆運動学を解いたプログラムを載せようと思います。
(計算とか作ったときの記憶とかが曖昧なところがありますがそのまま載せます)

[pythonの方はこちらの記事を参考にしてください]
pythonで2リンク逆運動学を解いて表示する - ロボット、電子工作、IoT、AIなどの開発記録
pythonで3リンク逆運動学を解いて表示する - ロボット、電子工作、IoT、AIなどの開発記録


環境
win7 64bit
Visualstudio15
opencvsharp-x86

f:id:weekendproject9:20170821232710p:plain


描画系でopencvを利用しています。
opencvの入れ方は、visualstudioでC#formアプリケーションでまずプロジェクトを制作して、
ツール→Nugetパッケージマネージャ→ソリューションのパッケージの管理で検索窓にOpenCvSharp-x86を選択します。
(2017/08/21現在はOpenCVShapしかないようですね。。。)

cs本体は以下になります。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Blob;

namespace scalarobo1
{
    public partial class Form1 : Form
    {
        public static double deg1, deg2;
        public static double width = 640;
        public static double height = 480;
        int widthP2 = (int)width / 2;
        int heightP2 = (int)height / 2;
        static int oldX, oldY;
        const double radToDeg = 57.295779513082320876798154814105;
        public IplImage vis = new IplImage(new CvSize((int)width, (int)height), BitDepth.U8, 3);
        

        public Form1()
        {

            InitializeComponent();
            init();


        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void init()
        {
            //軸描写
            Cv.Zero(vis);
            Cv.Line(vis, new CvPoint(0, heightP2), new CvPoint((int)width, heightP2), new CvScalar(255, 255, 255), 1);
            Cv.Line(vis, new CvPoint(widthP2, 0), new CvPoint(widthP2, (int)height), new CvScalar(255, 255, 255), 1);

            //描画実行
            pictureBoxIpl2.ImageIpl = vis;

            l1TextBox.Text = "80";
            l2TextBox.Text = "80";
            xTextbox.Text = "100";
            ytextbox.Text = "80";

        
        }

        //逆運動学計算
        public void scala_calc2(double x, double y, double l1, double l2)
        {

            double tht1, tht2;
            double fai1, fai2, fai0, l3;
            double operationability;

            l3 = Math.Sqrt((x * x) + (y * y));
            fai2 = Math.Acos(((l1 * l1) + (l2 * l2) - (l3 * l3)) / (2 * l1 * l2));
            tht2 = Math.PI - fai2;
            fai1 = Math.Acos(((l1 * l1) + (l3 * l3) - (l2 * l2)) / (2 * l1 * l3));
            fai0 = Math.Atan((y / x));
            //tht1 = fai0 - fai1; //これでも良いがata2にする
            tht1 = Math.Atan2(y, x) - fai1;

            deg1 = tht1 * (180 / Math.PI);
            deg2 = tht2 * (180 / Math.PI);

            operationability = l1 * l2 * Math.Sin(tht2);
            view(l1, l2 , x, y, tht1, tht2);

        }


        public void view(double l1, double l2,double x,double y,double tht1,double tht2)
        {
            double fowerd_x0,fowerd_y0,fowerd_x1,fowerd_y1;
            double inv_x0, inv_y0;
            double late;
            Cv.Line(vis, new CvPoint(0, heightP2), new CvPoint((int)width, heightP2), new CvScalar(255, 255, 255), 1);
            Cv.Line(vis, new CvPoint(widthP2, 0), new CvPoint(widthP2, (int)height), new CvScalar(255, 255, 255), 1);
            //順運動学計算
            fowerd_x0 = (l1 * Math.Cos(tht1)) + widthP2;
            fowerd_y0 = (l1 * Math.Sin(tht1)) + heightP2;
            fowerd_x1 = fowerd_x0 + (l2 * Math.Cos(tht1+tht2));
            fowerd_y1 = fowerd_y0 + (l2 * Math.Sin(tht1+tht2));


            //可操作性楕円対体
            //% 関節速度の最大値
            double wmax = 0.20;//%[rad/s]
            int ii = 101;
            double[] w1 = new double[ii];
            double[] w2 = new double[ii];
            double[] v_x = new double[ii];
            double[] v_y = new double[ii];
            //%リンクの長さ
            //L=1.0;%[m]
            //% 関節速度円の生成
            for (int nn = 1; nn < ii; nn++)
            {
                w1[nn] = wmax * Math.Cos(2 * Math.PI / (ii - 1) * (nn - 1));
                w2[nn] = wmax * Math.Sin(2 * Math.PI / (ii - 1) * (nn - 1));
            }

            //ヤコビ 可操作性楕円体
            double[,] J = new double[3, 3];
            J[1, 1] = -l1 * Math.Sin(tht1) - l2 * Math.Sin(tht1 + tht2);
            J[1, 2] = -l2 * Math.Sin(tht1 + tht2);
            J[2, 1] = l1 * Math.Cos(tht1) + l2 * Math.Cos(tht1 + tht2);
            J[2, 2] = l2 * Math.Cos(tht1 + tht2);
            //可操作楕円体の生成
            for (int nnn = 1; nnn < ii; nnn++)
            {
                v_x[nnn] = J[1, 1] * w1[nnn] + J[1, 2] * w2[nnn] + fowerd_x1;
                v_y[nnn] = J[2, 1] * w1[nnn] + J[2, 2] * w2[nnn] + fowerd_y1;
                if (nnn == 25 || nnn == 50 ||nnn == 100 )
                    Cv.Circle(vis, new CvPoint((int)v_x[nnn], (int)v_y[nnn]), 1, new CvScalar(0, 205, 255), 1, Cv.AA);
                else if(nnn == 75)
                    Cv.Circle(vis, new CvPoint((int)v_x[nnn], (int)v_y[nnn]), 2, new CvScalar(0, 255, 5), 1, Cv.AA);
                else
                    Cv.Circle(vis, new CvPoint((int)v_x[nnn], (int)v_y[nnn]), 1, new CvScalar(100, 105, 105), 1, Cv.AA);
            }

            
            Cv.Line(vis, new CvPoint(widthP2, heightP2), new CvPoint((int)fowerd_x0, (int)fowerd_y0), new CvScalar(0, 255, 0), 2);
            Cv.Line(vis, new CvPoint((int)fowerd_x0, (int)fowerd_y0), new CvPoint((int)fowerd_x1, (int)fowerd_y1), new CvScalar(0, 255, 255), 2);
            oldX = (int)fowerd_x1;
            oldY = (int)fowerd_y1;


            inv_x0 = fowerd_x0 + (l2 * Math.Cos(tht1 + tht2));
            inv_y0 = fowerd_y0 + (l2 * Math.Sin(tht1 + tht2));
            Cv.Circle(vis, new CvPoint((int)inv_x0, (int)inv_y0), 3, new CvScalar(250, 255, 255));

            Cv.Circle(vis, new CvPoint((int)x+widthP2, (int)y+heightP2), 2, new CvScalar(0, 255, 255));
            pictureBoxIpl2.ImageIpl = vis;

            tht1label.Text = Convert.ToString(0+(-tht1* radToDeg));
            tht2label.Text = Convert.ToString(tht2 * radToDeg);


        }





        private void button2_Click(object sender, EventArgs e)
        {
            double l1,l2,x, y;

            l1 = Convert.ToDouble(l1TextBox.Text);
            l2 = Convert.ToDouble(l2TextBox.Text);
            x = Convert.ToDouble(xTextbox.Text);
            y = Convert.ToDouble(ytextbox.Text);
            Cv.Zero(vis);
            //稼動範囲
            Cv.Circle(vis, new CvPoint(widthP2, heightP2), (int)(l1 + l2), new CvScalar(155, 55, 55), 1);

            scala_calc2(x, y*-1, l1, l2);


            pictureBoxIpl2.ImageIpl = vis;
                
            
        }
    }
}

formデザイン文は以下になります。

namespace scalarobo1
{
    partial class Form1
    {
        /// <summary>
        /// 必要なデザイナー変数です。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 使用中のリソースをすべてクリーンアップします。
        /// </summary>
        /// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows フォーム デザイナーで生成されたコード

        /// <summary>
        /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
        /// コード エディターで変更しないでください。
        /// </summary>
        private void InitializeComponent()
        {
            this.xTextbox = new System.Windows.Forms.TextBox();
            this.ytextbox = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.pictureBoxIpl2 = new OpenCvSharp.UserInterface.PictureBoxIpl();
            this.button2 = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.tht1label = new System.Windows.Forms.Label();
            this.tht2label = new System.Windows.Forms.Label();
            this.l2label = new System.Windows.Forms.Label();
            this.l1label = new System.Windows.Forms.Label();
            this.l2TextBox = new System.Windows.Forms.TextBox();
            this.l1TextBox = new System.Windows.Forms.TextBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBoxIpl2)).BeginInit();
            this.SuspendLayout();
            // 
            // xTextbox
            // 
            this.xTextbox.Location = new System.Drawing.Point(69, 206);
            this.xTextbox.Name = "xTextbox";
            this.xTextbox.Size = new System.Drawing.Size(100, 19);
            this.xTextbox.TabIndex = 2;
            // 
            // ytextbox
            // 
            this.ytextbox.Location = new System.Drawing.Point(69, 256);
            this.ytextbox.Name = "ytextbox";
            this.ytextbox.Size = new System.Drawing.Size(100, 19);
            this.ytextbox.TabIndex = 3;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 209);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(42, 12);
            this.label1.TabIndex = 4;
            this.label1.Text = "座標X:";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(12, 259);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(42, 12);
            this.label2.TabIndex = 5;
            this.label2.Text = "座標Y:";
            // 
            // pictureBoxIpl2
            // 
            this.pictureBoxIpl2.Location = new System.Drawing.Point(218, 89);
            this.pictureBoxIpl2.Name = "pictureBoxIpl2";
            this.pictureBoxIpl2.Size = new System.Drawing.Size(640, 480);
            this.pictureBoxIpl2.TabIndex = 8;
            this.pictureBoxIpl2.TabStop = false;
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(69, 310);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 15;
            this.button2.Text = "calc";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(23, 392);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(31, 12);
            this.label3.TabIndex = 16;
            this.label3.Text = "tht1:";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(23, 421);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(31, 12);
            this.label4.TabIndex = 17;
            this.label4.Text = "tht2:";
            // 
            // tht1label
            // 
            this.tht1label.AutoSize = true;
            this.tht1label.Location = new System.Drawing.Point(67, 392);
            this.tht1label.Name = "tht1label";
            this.tht1label.Size = new System.Drawing.Size(11, 12);
            this.tht1label.TabIndex = 18;
            this.tht1label.Text = "0";
            // 
            // tht2label
            // 
            this.tht2label.AutoSize = true;
            this.tht2label.Location = new System.Drawing.Point(67, 421);
            this.tht2label.Name = "tht2label";
            this.tht2label.Size = new System.Drawing.Size(11, 12);
            this.tht2label.TabIndex = 19;
            this.tht2label.Text = "0";
            // 
            // l2label
            // 
            this.l2label.AutoSize = true;
            this.l2label.Location = new System.Drawing.Point(23, 162);
            this.l2label.Name = "l2label";
            this.l2label.Size = new System.Drawing.Size(23, 12);
            this.l2label.TabIndex = 23;
            this.l2label.Text = "L2:";
            // 
            // l1label
            // 
            this.l1label.AutoSize = true;
            this.l1label.Location = new System.Drawing.Point(23, 112);
            this.l1label.Name = "l1label";
            this.l1label.Size = new System.Drawing.Size(23, 12);
            this.l1label.TabIndex = 22;
            this.l1label.Text = "L1:";
            // 
            // l2TextBox
            // 
            this.l2TextBox.Location = new System.Drawing.Point(69, 159);
            this.l2TextBox.Name = "l2TextBox";
            this.l2TextBox.Size = new System.Drawing.Size(100, 19);
            this.l2TextBox.TabIndex = 21;
            // 
            // l1TextBox
            // 
            this.l1TextBox.Location = new System.Drawing.Point(69, 109);
            this.l1TextBox.Name = "l1TextBox";
            this.l1TextBox.Size = new System.Drawing.Size(100, 19);
            this.l1TextBox.TabIndex = 20;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(935, 707);
            this.Controls.Add(this.l2label);
            this.Controls.Add(this.l1label);
            this.Controls.Add(this.l2TextBox);
            this.Controls.Add(this.l1TextBox);
            this.Controls.Add(this.tht2label);
            this.Controls.Add(this.tht1label);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.pictureBoxIpl2);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.ytextbox);
            this.Controls.Add(this.xTextbox);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBoxIpl2)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion
        private System.Windows.Forms.TextBox xTextbox;
        private System.Windows.Forms.TextBox ytextbox;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private OpenCvSharp.UserInterface.PictureBoxIpl pictureBoxIpl2;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label tht1label;
        private System.Windows.Forms.Label tht2label;
        private System.Windows.Forms.Label l2label;
        private System.Windows.Forms.Label l1label;
        private System.Windows.Forms.TextBox l2TextBox;
        private System.Windows.Forms.TextBox l1TextBox;
    }
}

STMマイコンがパソコンに挿しても認識されない問題

秋月でSTM32 Nucleo Board STM32F303K8を購入しました。
STM32 Nucleo Board STM32F303K8: マイコン関連 秋月電子通商 電子部品 ネット通販
f:id:weekendproject9:20170820013212p:plain

STMマイコンをPC(win7,64bit)に挿したところ、マイコンボードの赤色LEDが点滅してしまいます。
通常ですとすぐにPCにmbedのウインドウが出てくるのですが、デバイスマネージャーで確認しても認識されていませんでした。

[解決法]
まず、PCに認識されるようにマイコンボードのリセットスイッチを押したままPCに接続します。
そうするとLEDが赤、緑と交互に点灯し、PCにはウインドウが出てくると思います。

その状態で、次にNUCLEO-F303K8 | Mbed
ここの”Nucleo ST-LINK/V2 driver installation and firmware upgrade”にあるように、
STSW-LINK009 - ST-LINK, ST-LINK/V2, ST-LINK/V2-1 USB driver signed for Windows7, Windows8, Windows10 - STMicroelectronics
に行き、下にあるGET SOFTWAREの”STSW-LINK009”をダウンロードします。(アカウントがない場合は名前とメールアドレスを記入して登録してください)

ダウンロードできたら、解凍して、osが32bitなら”dpinst_x86.exe”、64bitなら”dpinst_amd64.exe”をインストールします。
終了するとデバイスが認識されるはずです。

また、STMマイコンのファームが古いのでついでにアップデートします。

Nucleo Firmware - | Mbed
に行き、
”Download the latest ST-LINK/V2-1 firmware upgrade.”のリンク先に行きます。
下にあるGET SOFTWAREの”STSW-LINK007”をダウンロードします。
解凍して、Windowsフォルダの”ST-LinkUpgrade.exe”を起動します。
ウインドウが出るので、マイコンボードを繋いだまま”Device Connect”をクリックして、マイコンが認識されたらUpdateの”Yes”をクリックしてアップデートさせます。
終了するとアップデート完了です。

win10,win7にcondaでtensorflowをインストール

題名通りwin10,win7にcondaでtensorflowをインストールします。
(pip install tensorflowで入ればそっちの方が単純でいいかな?)

[Anaconda仮想環境でtensorflowをインストール]

と言ってもここにあるままやればいいですが、
Installing TensorFlow on Windows  |  TensorFlow

> conda create -n tensorflow python=3.5 

> activate tensorflow


CPU版をインストール

(tensorflow)C:> pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.3.0-cp35-cp35m-win_amd64.whl

GPU版をインストール

(tensorflow)C:> pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-1.3.0-cp35-cp35m-win_amd64.whl


インストールできたか確認です

$ python
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))

結果で以下がでればokです

Hello, TensorFlow!


僕の環境ではTensorflowに引っ張られてpython3.6から3.5に下がりました。

また、同じ方法でopencvもインストールしました。
(通常pipでは入らなかった)

強化学習でファンネルの攻撃から避けるガンダムを作る(準備1)

去年ぐらいから人工知能を勉強し始めまして、技術書とかは一通り触ったりしていて次に何やろうかなーと思ってたら半年が経過していました。
最近ちょうどガンダムUCを見ていたらファンネルがあったので、今回からファンネルとガンダム人工知能を作ってみようと思います。

アムロが乗ったガンダムジオン軍のファンネルを避けるという構図で作って、避けるのは強化学習でできればと思います。
強化学習はchainerかTensorflowどっちで作るかはまだ決めてませんが。。。

初回からいきなり強化学習とはいかないので、最初はガンダムとファンネル、ファンネルからの攻撃を作ろうと思います。

開発環境は以下の通りです。
OS : win10
言語 : python3.6

結果gif:
f:id:weekendproject9:20170818145903g:plain

座標[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()