robots統計

先述の乱数発生とgnurobotsおよびMySQLを組み合わせて、結果をデータベース化できるようにしてみました。

gnurobotsはエネルギー100、盾5で実行しています。

f:id:S_E_Hyphen:20170420153658p:plain

 

f:id:S_E_Hyphen:20170420155013p:plain

たくさん移動できた時は、盾が残っていてエネルギー切れで終了している傾向があるようです。逆に言えば、あまり移動できていない時は壁などにぶつかりまくって盾を失って終了しているということです。

 


 #!/bin/bash
# 乱数で発生させたgnurobotsのコマンドと
# その実行結果をMySQLに登録する

temp=`mktemp ./com.XXXX`
mysql robots << _SQL_
/* テーブルの初期化 */
create table if not exists commands(
 scm_num int,
 seq int,
 command varchar(50) );
create table if not exists results(
 scm_num int,
 Shields int,
 Energy int,
 walk int,
 Shots int,
 Score int);
_SQL_

# マップの生成
cat << _MAP_ >test.map
##########################
#              @         #
#             @@         #
#   #####                #
#             #          #
#     ++      #          #
#    +++      #          #
#    +++      #     $    #
#   ++++                 #
##########################
_MAP_

scm_num=`mysql robots -N << _SQL_ | sed -e "s/NULL//g"
select max(scm_num) from results;
_SQL_`
for ( ( iter=0; iter<5; iter++ ) )
do
scm_num=`expr $scm_num + 1`

# コマンドの生成
./make_scm.sh |\
 awk '{print $2}' |\
 sed -e "s/_/ /g" |\
 tee command.txt |\
 awk -v scm_num="$scm_num" '{printf "%d,%d,%s\n",scm_num,NR,$0}' \
 > $temp
# gnurobotsの実行
gnurobots -e 100 -s 5 command.txt -f test.map \
 2>/dev/null > result.txt
# 結果の整理
Shields=`cat result.txt |  awk '/Shields/ {print $2}' | head -1`
Energy=`cat result.txt |  awk '/Energy/ {print $2}' | head -1`
walk=`cat result.txt |  awk '/Units walked/ {print $3}' | head -1`
Shots=`cat result.txt |  awk '/Shots/ {print $2}' | head -1`
Score=`cat result.txt |  awk '/Score/ {print $2}' | head -1`
echo $scm_num $Shields $Energy $walk $Shots $Score

mysql robots << _SQL_
/* command.txt の登録 */
 load data local infile '$temp'
  into table commands
  fields terminated by ','
  (scm_num,seq,command);
/* result.txt の登録 */
 insert into results
  (scm_num,Shields,Energy,walk,Shots,Score)
  value
  ($scm_num,$Shields,$Energy,$walk,$Shots,$Score);
_SQL_

done
rm $temp


make_scm.sh

#!/bin/bash
# 配列の定義
# 配分表のhirituの割合でitemが出力される
declare -a item=(\
 `cat distribution.tab |\
  awk '{print $1}'`\
)
declare -a hiritu=(\
 `cat distribution.tab |\
  awk '{print $2}'`\
)
declare -a ruiseki
# n_arrayは配列の数
n_array=${#hiritu[*]}

ruiseki[0]=0
for ( ( i=0; i<$n_array; i++ ) )
do
 ruiseki[`expr $i + 1`]=`expr ${ruiseki[$i]} + ${hiritu[$i]}`
done
for ( ( i=0; i<=$n_array; i++ ) )
do
 ruiseki[$i]=`bc  << _BC_
  32767 * ${ruiseki[$i]} / ${ruiseki[${n_array}]}
_BC_`
done
# 配列ruisekiの中には0〜32767をhirituに応じて配分している

for ( ( count=0; count<50; count++ ) )
do
# 一様乱数randを発生させ、reuiseki(i)<=rand<(i+1)なら
# item(i)を表示する
 rand=$( ( $RANDOM ) )
 for ( ( i=0; i<$n_array; i++ ) )
 do
 if [ ${ruiseki[$i]} -le $rand ] && [ $rand -lt ${ruiseki[`expr $i + 1`]} ]
 then
  echo $rand ${item[$i]}
 fi
 done
done

 


distribution.tab

(robot-turn_1)    2
(robot-turn_-1)    2
(robot-move_1)    5
(robot-zap)    1
(robot-grab)    1

ubuntu16とMySQL5.7

OSとMYSQLのバージョンを同時に上げてしまったので、どっちの影響なのかわからないのですが、インストールしても

user@PC-UBUNTU64:~$ mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

となってMySQLが起動しなくなってしまいました。

mysqld_safeも上手く動かないし困っていたところ、

$ sudo mysql

で、あっさりと起動してしまいました。

このときMYSQLは管理者権限でログインしていることになっているので、すかさず別のユーザーを作成することで、従来通り

$ mysql

だけで起動できるようになりました。

近い将来メインPCをバージョンアップするときのための備忘録です。

比率に応じた乱数の発生

distribution.tabファイルを

0 1
1 1
2 1
3 1
4 1

とすると、1:1:1:1:1の割合で0、1、2、3、4が出力される。ヒストグラムにすると

f:id:S_E_Hyphen:20170414111552j:plain

同じく

0 1
1 1
2 2
3 1
4 1

にすると、1:1:2:1:1となる。

f:id:S_E_Hyphen:20170414111704j:plain

0 1
1 1
2 1
3 1
4 3
9 1

みたいに間が飛んでいても大丈夫。

 

f:id:S_E_Hyphen:20170414111926j:plain



 


 

#!/bin/bash
# 配列の定義
# 配分表のhirituの割合でitemが出力される
declare -a item=(\
 `cat distribution.tab |\
  awk '{print $1}'`\
)
declare -a hiritu=(\
 `cat distribution.tab |\
  awk '{print $2}'`\
)
declare -a ruiseki
# n_arrayは配列の数
n_array=${#hiritu[*]}

ruiseki[0]=0
for ( ( i=0; i<$n_array; i++ ) )
do
 ruiseki[`expr $i + 1`]=`expr ${ruiseki[$i]} + ${hiritu[$i]}`
done
for ( ( i=0; i<=$n_array; i++ ) )
do
 ruiseki[$i]=`bc  << _BC_
  32767 * ${ruiseki[$i]} / ${ruiseki[${n_array}]}
_BC_`
done
# 配列ruisekiの中には0〜32767をhirituに応じて配分している

for ( ( count=0; count<5000; count++ ) )
do
# 一様乱数randを発生させ、reuiseki(i)<=rand<reuiseki(i+1)なら
# item(i)を表示する
 rand=$( ( $RANDOM ) )
 for ( ( i=0; i<$n_array; i++ ) )
 do
 if [ ${ruiseki[$i]} -le $rand ] && [ $rand -lt ${ruiseki[`expr $i + 1`]} ]
 then
  echo $rand ${item[$i]}
 fi
 done
done |\
 awk '{print $2}' |\
 gmt pshistogram /dev/stdin -W0.2 -L -JX10c/3c -R-1/10/0/2000 -B1/a500g500 > histgram.ps
gmt ps2raster histgram.ps -E100 -P -A

# 最後2行はGMTを使って描画している

 

gnurobots

Scheme言語でロボットを操って得点を競うゲームだそうです。

f:id:S_E_Hyphen:20170408162321p:plain

「報酬」を捕ったらScoreが1加算されます。

「餌」を食ったらEnergyが10?加算されます。

逆に「壁」や「敵」に突っ込んだらShieldが1枚減るようです。

 

とりあえず乱数で酔歩運動させてみました。

効率は無茶苦茶悪いけど、一応「報酬」や「餌」だけを食べているみたいです。

pythonの前にSchemeを覚えなくてはならなくなりました。


 

;;; 乱数 ;;;;;;;;;;;;;;
(define obj " ")
; 種 (seed)
(define *seed* 1)
; シードの設定
(define (srand x)
    (set! *seed* x))
; 整数の一様乱数
(define (irand)
    (set! *seed* (modulo (+ (* 69069 *seed*) 1) #x100000000))
    *seed*)
; 実数の一様乱数
(define (random)
    (* (/ 1.0 #x100000000) (irand)))
(define (make-number n)
    (+ (modulo (quotient (irand) #x10000) n) 1))
;;;;;;;;;;;;;;;;;;;;;;;;

;;; 目前の物体を確認する;;
(define freq '("space" "wall" "baddie" "food" "prize"))
(define (grope-things freq)
  (if (null? freq) #f
    (if (robot-feel (car freq))
      (car freq)
      (grope-things (cdr freq))
    )
  )
)
;;;;;;;;;;;;;;;;;;;;;;;;

;;; 処理の定義 ;;;;;;;;;;
(define (grab-prize)
  (robot-grab)
  (robot-move 1)
  (robot-turn (make-number 4))
)
(define (grab-food)
  (robot-grab)
  (robot-move 1)
  (robot-turn (make-number 4))
)
(define (turn-wall)
  (robot-turn (make-number 4))
)
(define (go-space)
  (robot-move 1)
  (robot-turn (make-number 4))
)
(define (zap-baddie)
  (robot-zap)
  (robot-move 1)
  (robot-turn (make-number 4))
)
;;;;;;;;;;;;;;;;;;;;;;;;

;; メインループ ;;;;;;;;;;
(
define (main-loop)
(set! obj (grope-things freq))
(
cond
 ( (equal? obj "space") (go-space))
 ( (equal? obj "wall") (turn-wall))
 ( (equal? obj "baddie") (zap-baddie))
 ( (equal? obj "food") (grab-food))
 ( (equal? obj "prize") (grab-prize))
)
(main-loop))

(robot-turn 1)
(main-loop)
;;;;;;;;;;;;;;;;;;;;;;;;

 

人工知能tensorflowを飼ってみる

まずはグラフィックボードのドライバー設定をしました。

f:id:S_E_Hyphen:20170407161748p:plain

続いてCUDAのインストールです。

超低火力DeepLeaning環境をUbuntu16 & 750ti & Chainerで構築したよ | Foolean

が、とても参考になりました。

 

cuDNNは登録メールが一向に返って来ないので、とりあえず後回しにしました。

 

sudo apt-get install python3-pip

sudo pip3 install --upgrade pip

export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.9.0-cp35-cp35m-linux_x86_64.whl

sudo pip3 install --upgrade $TF_BINARY_URL

 

で、transflowのインストールです。TF_BINARY_URL

https://www.tensorflow.org/install/install_linux#TF_PYTHON_URLが公式となります。

 

さて何ができるようになるのでしょう?道のりは長そうです。


 

user@desktop-ubuntu64:~$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcublas.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:126] Couldn't open CUDA library libcudnn.so.5. LD_LIBRARY_PATH:
I tensorflow/stream_executor/cuda/cuda_dnn.cc:3517] Unable to load cuDNN DSO
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcufft.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcurand.so.8.0 locally
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:910] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
I tensorflow/core/common_runtime/gpu/gpu_device.cc:885] Found device 0 with properties:
name: GeForce GTX 550 Ti
major: 2 minor: 1 memoryClockRate (GHz) 1.9
pciBusID 0000:01:00.0
Total memory: 959.06MiB
Free memory: 695.19MiB
I tensorflow/core/common_runtime/gpu/gpu_device.cc:906] DMA: 0
I tensorflow/core/common_runtime/gpu/gpu_device.cc:916] 0:   Y
I tensorflow/core/common_runtime/gpu/gpu_device.cc:948] Ignoring visible gpu device (device: 0, name: GeForce GTX 550 Ti, pci bus id: 0000:01:00.0) with Cuda compute capability 2.1. The minimum required Cuda capability is 3.0.
>>> sess.run(hello)
b'Hello, TensorFlow!'
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> sess.run(a+b)
42
>>>