先述の乱数発生とgnurobotsおよびMySQLを組み合わせて、結果をデータベース化できるようにしてみました。
gnurobotsはエネルギー100、盾5で実行しています。
たくさん移動できた時は、盾が残っていてエネルギー切れで終了している傾向があるようです。逆に言えば、あまり移動できていない時は壁などにぶつかりまくって盾を失って終了しているということです。
#!/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