├── .gitignore ├── .gitignore.bak ├── feature ├── __init__.py ├── Board.pyc ├── utility.pyc ├── Plays_RL.pyc ├── __init__.pyc └── utility.py ├── selfplay ├── __init__.py ├── game_RL_policy.py ├── make_datasets_RL_value.py ├── make_datasets_RL_policy.py ├── make_datasets_RL_value2.py └── make_datasets_RL_value3.py ├── play_DCL ├── mcts │ ├── test_.save │ ├── states │ │ ├── __init__.py │ │ └── toy_world_state.py │ ├── graph.py │ ├── mcts.py │ ├── mcts.pyc │ ├── backups.py │ ├── graph.pyc │ ├── utils.pyc │ ├── __init__.pyc │ ├── backups.pyc │ ├── __init__.py │ ├── tree_policies.pyc │ ├── default_policies.py │ ├── default_policies.pyc │ ├── utils.py │ └── tree_policies.py ├── gomill │ ├── __init__.py │ ├── sgf.pyc │ ├── boards.pyc │ ├── common.pyc │ ├── utils.pyc │ ├── __init__.pyc │ ├── sgf_moves.pyc │ ├── sgf_grammar.pyc │ ├── sgf_properties.pyc │ ├── handicap_layout.py │ ├── utils.py │ ├── ascii_boards.py │ ├── common.py │ ├── terminal_input.py │ ├── sgf_moves.py │ ├── compact_tracebacks.py │ ├── ringmaster_command_line.py │ ├── ascii_tables.py │ └── ringmaster_presenters.py ├── go.py ├── jobmgr.py ├── network.py ├── evaluator.py ├── mcts_test3.py ├── mcts_test5.py ├── mcts_test6.py ├── play_gtp.py ├── version.py ├── feature │ ├── __init__.py │ ├── Board.pyc │ ├── Plays.pyc │ ├── __init__.pyc │ ├── FeatureMap.pyc │ └── utility.py ├── evaluator_aleum.py ├── kill_eval.sh ├── modelinterface.py ├── make_datasets_RL_policy.py ├── make_datasets_RL_policy2.py ├── make_datasets_RL_policy3.py ├── network │ ├── slpolicy_model_0.h5 │ ├── value_net_weights_5.h5 │ ├── 2nd_rlpolicy_model_21.h5 │ ├── rollout_policy_net_weights.h5 │ ├── rollout_policy_net_model.json │ ├── slpolicy_model_0.json │ ├── value_net_model.json │ └── 2nd_rlpolicy_model_21.json ├── __init__.py ├── .idea │ ├── modules.xml │ ├── minigo.iml │ └── misc.xml ├── prog5.py ├── .settings │ └── org.eclipse.core.resources.prefs ├── test_minigo_single.sh ├── .project ├── chain.py ├── gothread.py ├── test_minigo_eval.sh ├── scored_move.py ├── test_minigo_SL_FR.sh ├── test_minigo.sh ├── test_simplego_minigo.sh ├── eye.py ├── mcts_test7.py ├── socket2.py ├── mcts_test2.py ├── idiot_bot.py ├── mcts_test4.py ├── maze_.py ├── pos_cache.py ├── graphvizresult.py ├── mcts_test.py ├── utility.py ├── big_game.py ├── block.py ├── const.py ├── pass_live.py ├── load_sgf.py └── config.py ├── tests ├── __init__.py ├── test_model_all.py ├── test_model.py └── test_prob.py ├── train ├── __init__.py ├── policy_net_weights_h03_499.h5 ├── load_test.py ├── policy_net_model_h03.json ├── 2nd-value_net.py ├── RL_policy_net.py ├── SL_policy_net_load.py ├── 1st-value_net.py ├── SL_policy_net_h03.py ├── rollout_policy.py ├── SL_policy_net_h04.py ├── SL_policy_net_h05.py ├── SL_policy_net_h06.py ├── SL_policy_net_h07.py ├── SL_policy_net_h12.py ├── SL_policy_net_h02.py ├── SL_policy_net_h08.py ├── SL_policy_net_h09.py ├── SL_policy_net_h10.py ├── SL_policy_net_h11.py ├── SL_policy_net_h13.py ├── SL_policy_net_h01.py └── SL_policy_net.py ├── utils ├── __init__.py ├── base.py ├── make_same_datasets.py ├── extract_tests_from_dataset.py ├── extract_dataset_from_sgf_valuenet.py ├── extract_dataset_from_sgf_policy.py └── extract_dataset_from_sgf_valuenet_pred.py ├── README.md ├── .project ├── .pydevproject └── .settings └── org.eclipse.core.resources.prefs /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc -------------------------------------------------------------------------------- /.gitignore.bak: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /feature/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /selfplay/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /play_DCL/mcts/test_.save: -------------------------------------------------------------------------------- 1 | * 2 | -------------------------------------------------------------------------------- /play_DCL/gomill/__init__.py: -------------------------------------------------------------------------------- 1 | __version__ = "0.7.4" 2 | -------------------------------------------------------------------------------- /play_DCL/mcts/states/__init__.py: -------------------------------------------------------------------------------- 1 | __author__ = 'johannes' 2 | -------------------------------------------------------------------------------- /play_DCL/go.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/go.py -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | __author__ = 'Aleum' 4 | -------------------------------------------------------------------------------- /train/__init__.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | __author__ = 'Aleum' 4 | -------------------------------------------------------------------------------- /feature/Board.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/feature/Board.pyc -------------------------------------------------------------------------------- /feature/utility.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/feature/utility.pyc -------------------------------------------------------------------------------- /play_DCL/jobmgr.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/jobmgr.py -------------------------------------------------------------------------------- /play_DCL/network.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/network.py -------------------------------------------------------------------------------- /feature/Plays_RL.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/feature/Plays_RL.pyc -------------------------------------------------------------------------------- /feature/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/feature/__init__.pyc -------------------------------------------------------------------------------- /play_DCL/evaluator.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/evaluator.py -------------------------------------------------------------------------------- /play_DCL/mcts/graph.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/mcts/graph.py -------------------------------------------------------------------------------- /play_DCL/mcts/mcts.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/mcts/mcts.py -------------------------------------------------------------------------------- /play_DCL/mcts/mcts.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/mcts/mcts.pyc -------------------------------------------------------------------------------- /play_DCL/mcts_test3.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/mcts_test3.py -------------------------------------------------------------------------------- /play_DCL/mcts_test5.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/mcts_test5.py -------------------------------------------------------------------------------- /play_DCL/mcts_test6.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/mcts_test6.py -------------------------------------------------------------------------------- /play_DCL/play_gtp.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/play_gtp.py -------------------------------------------------------------------------------- /play_DCL/version.py: -------------------------------------------------------------------------------- 1 | number = "0.0.0" 2 | name = "MiniGo" 3 | message = ": message : " 4 | -------------------------------------------------------------------------------- /play_DCL/feature/__init__.py: -------------------------------------------------------------------------------- 1 | from . import * 2 | 3 | __version__ = 0.1 4 | __author__ = "js" 5 | -------------------------------------------------------------------------------- /play_DCL/gomill/sgf.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/gomill/sgf.pyc -------------------------------------------------------------------------------- /play_DCL/mcts/backups.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/mcts/backups.py -------------------------------------------------------------------------------- /play_DCL/mcts/graph.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/mcts/graph.pyc -------------------------------------------------------------------------------- /play_DCL/mcts/utils.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/mcts/utils.pyc -------------------------------------------------------------------------------- /play_DCL/evaluator_aleum.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/evaluator_aleum.py -------------------------------------------------------------------------------- /play_DCL/feature/Board.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/feature/Board.pyc -------------------------------------------------------------------------------- /play_DCL/feature/Plays.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/feature/Plays.pyc -------------------------------------------------------------------------------- /play_DCL/gomill/boards.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/gomill/boards.pyc -------------------------------------------------------------------------------- /play_DCL/gomill/common.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/gomill/common.pyc -------------------------------------------------------------------------------- /play_DCL/gomill/utils.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/gomill/utils.pyc -------------------------------------------------------------------------------- /play_DCL/kill_eval.sh: -------------------------------------------------------------------------------- 1 | ps -ef | grep 5000 | grep python | awk '{ print "kill -9 " $2 }' > t 2 | ./t 3 | -------------------------------------------------------------------------------- /play_DCL/mcts/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/mcts/__init__.pyc -------------------------------------------------------------------------------- /play_DCL/mcts/backups.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/mcts/backups.pyc -------------------------------------------------------------------------------- /play_DCL/modelinterface.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/modelinterface.py -------------------------------------------------------------------------------- /selfplay/game_RL_policy.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/selfplay/game_RL_policy.py -------------------------------------------------------------------------------- /play_DCL/feature/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/feature/__init__.pyc -------------------------------------------------------------------------------- /play_DCL/gomill/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/gomill/__init__.pyc -------------------------------------------------------------------------------- /play_DCL/gomill/sgf_moves.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/gomill/sgf_moves.pyc -------------------------------------------------------------------------------- /play_DCL/mcts/__init__.py: -------------------------------------------------------------------------------- 1 | from . import * 2 | 3 | __version__ = 0.1 4 | __author__ = "Johannes Kulick" 5 | -------------------------------------------------------------------------------- /play_DCL/feature/FeatureMap.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/feature/FeatureMap.pyc -------------------------------------------------------------------------------- /play_DCL/gomill/sgf_grammar.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/gomill/sgf_grammar.pyc -------------------------------------------------------------------------------- /play_DCL/mcts/tree_policies.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/mcts/tree_policies.pyc -------------------------------------------------------------------------------- /play_DCL/gomill/sgf_properties.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/gomill/sgf_properties.pyc -------------------------------------------------------------------------------- /play_DCL/mcts/default_policies.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/mcts/default_policies.py -------------------------------------------------------------------------------- /play_DCL/mcts/default_policies.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/mcts/default_policies.pyc -------------------------------------------------------------------------------- /selfplay/make_datasets_RL_value.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/selfplay/make_datasets_RL_value.py -------------------------------------------------------------------------------- /play_DCL/make_datasets_RL_policy.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/make_datasets_RL_policy.py -------------------------------------------------------------------------------- /play_DCL/make_datasets_RL_policy2.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/make_datasets_RL_policy2.py -------------------------------------------------------------------------------- /play_DCL/make_datasets_RL_policy3.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/make_datasets_RL_policy3.py -------------------------------------------------------------------------------- /play_DCL/network/slpolicy_model_0.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/network/slpolicy_model_0.h5 -------------------------------------------------------------------------------- /selfplay/make_datasets_RL_policy.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/selfplay/make_datasets_RL_policy.py -------------------------------------------------------------------------------- /selfplay/make_datasets_RL_value2.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/selfplay/make_datasets_RL_value2.py -------------------------------------------------------------------------------- /selfplay/make_datasets_RL_value3.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/selfplay/make_datasets_RL_value3.py -------------------------------------------------------------------------------- /train/policy_net_weights_h03_499.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/train/policy_net_weights_h03_499.h5 -------------------------------------------------------------------------------- /play_DCL/network/value_net_weights_5.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/network/value_net_weights_5.h5 -------------------------------------------------------------------------------- /play_DCL/network/2nd_rlpolicy_model_21.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/network/2nd_rlpolicy_model_21.h5 -------------------------------------------------------------------------------- /play_DCL/network/rollout_policy_net_weights.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aleum/AlphaGo/HEAD/play_DCL/network/rollout_policy_net_weights.h5 -------------------------------------------------------------------------------- /utils/__init__.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | __author__ = 'Aleum' 4 | 5 | # extract_dataset_other: 바둑 프로그램 self-play 기보에서 데이터셋 추출, 메모리 크기 고려 6 | # extract_dataset_pro: pro 기보, 메모리 크기 고려 x -------------------------------------------------------------------------------- /play_DCL/__init__.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | __author__ = 'Aleum' 4 | 5 | # extract_dataset_other: 바둑 프로그램 self-play 기보에서 데이터셋 추출, 메모리 크기 고려 6 | # extract_dataset_pro: pro 기보, 메모리 크기 고려 x -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 9x9 GO Game Player referenced by AlphaGo, SimpleGo and MCTS codes 2 | 3 | including training codes, making data codes for 4 | SL Policy Net 5 | RL Policy Net 6 | RL Value Net 7 | Customized Rollout Policy Net 8 | 9 | used deep learning library 10 | Keras.io with Theano 11 | 12 | -------------------------------------------------------------------------------- /play_DCL/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /play_DCL/prog5.py: -------------------------------------------------------------------------------- 1 | ''' 2 | python prog5.py -mode RL 3 | python prog5.py 4 | ''' 5 | 6 | 7 | import argparse 8 | parser = argparse.ArgumentParser() 9 | parser.add_argument('-mode', nargs=1, required=False, dest='mode') 10 | args = parser.parse_args() 11 | 12 | if args.mode is not None: 13 | print 'args.mode : ' + args.mode[0] 14 | else: 15 | print 'args.mode is none: ' 16 | -------------------------------------------------------------------------------- /play_DCL/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//feature/FeatureMap.py=utf-8 3 | encoding//feature/Plays.py=utf-8 4 | encoding//feature/utility.py=utf-8 5 | encoding//mcts/graph.py=ms949 6 | encoding//mcts/mcts.py=ms949 7 | encoding/go.py=ms949 8 | encoding/mcts_test3.py=ms949 9 | encoding/mcts_test4.py=ms949 10 | encoding/play_gtp.py=ms949 11 | -------------------------------------------------------------------------------- /play_DCL/test_minigo_single.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #PATH=usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/cuda-7.0:/usr/local/cuda-7.0/bin:/usr/lib/jvm/java-7-oracle/bin:/usr/lib/jvm/java-7-oracle/db/bin 3 | #THEANO_FLAGS='device=gpu0' 4 | #KERAS_BACKEND=theano 5 | CUDA_LAUNCH_BLOCKING=1 THEANO_FLAGS='device=gpu0' python /home/sclab/dclee/minigo/play_gtp.py 6 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | SCGo 4 | 5 | 6 | 7 | 8 | 9 | org.python.pydev.PyDevBuilder 10 | 11 | 12 | 13 | 14 | 15 | org.python.pydev.pythonNature 16 | 17 | 18 | -------------------------------------------------------------------------------- /play_DCL/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | minigo 4 | 5 | 6 | 7 | 8 | 9 | org.python.pydev.PyDevBuilder 10 | 11 | 12 | 13 | 14 | 15 | org.python.pydev.pythonNature 16 | 17 | 18 | -------------------------------------------------------------------------------- /play_DCL/chain.py: -------------------------------------------------------------------------------- 1 | class Chain: 2 | def __init__(self): 3 | self.blocks = {} 4 | 5 | def add_block(self, block): 6 | self.blocks[block.get_origin()] = block 7 | block.chain = self 8 | 9 | def has_block(self, block): 10 | return block.get_origin() in self.blocks 11 | 12 | def get_color(self): 13 | return self.blocks[self.get_origin()].color 14 | 15 | def get_origin(self): 16 | return min(self.blocks) 17 | -------------------------------------------------------------------------------- /utils/base.py: -------------------------------------------------------------------------------- 1 | 2 | import numpy as np 3 | from operator import itemgetter 4 | import os 5 | import copy 6 | import math 7 | import json 8 | import sys 9 | import time 10 | import csv 11 | import random 12 | 13 | def get_file_names(path, postfix): 14 | res = [] 15 | 16 | for root, dirs, files in os.walk(path): 17 | 18 | for file in files: 19 | if file[-len(postfix):] == postfix: 20 | res.append(file) 21 | 22 | return res 23 | -------------------------------------------------------------------------------- /utils/make_same_datasets.py: -------------------------------------------------------------------------------- 1 | 2 | if __name__ == "__main__": 3 | x = open(r'C:\Users\Aleum\Desktop\original_dataset\game_033.sgf_65_x.csv').read() 4 | y = open(r'C:\Users\Aleum\Desktop\original_dataset\game_033.sgf_65_y.csv').read() 5 | 6 | for i in range(0, 1000): 7 | open(r'C:\Users\Aleum\Desktop\same_dataset\\'+str(i)+"_x2.csv","w+").write(x) 8 | open(r'C:\Users\Aleum\Desktop\same_dataset\\'+str(i)+"_y2.csv","w+").write(y) 9 | 10 | print "finish" 11 | -------------------------------------------------------------------------------- /play_DCL/gothread.py: -------------------------------------------------------------------------------- 1 | class Thread: 2 | def __init__(self, pos1, pos2, length, color): 3 | self.pos1 = pos1 4 | self.pos2 = pos2 5 | self.length = length 6 | self.color = color 7 | self.strength = 1.0 8 | 9 | def key(self): 10 | return self.pos1, self.pos2 11 | 12 | ## def strength(self): 13 | ## return 1.0 / self.length 14 | 15 | ## def __cmp__(self, other): 16 | ## return cmp(self.strength(), other.strength()) 17 | 18 | def __cmp__(self, other): 19 | return cmp(self.strength, other.strength) 20 | 21 | -------------------------------------------------------------------------------- /.pydevproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | /${PROJECT_DIR_NAME} 5 | 6 | python 2.7 7 | Default 8 | 9 | E:\gomill-0.7.4 10 | 11 | 12 | -------------------------------------------------------------------------------- /play_DCL/.idea/minigo.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 15 | -------------------------------------------------------------------------------- /play_DCL/test_minigo_eval.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #PATH=/home/sclab/anaconda2/bin:/usr/local/cuda-7.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/java-7-oracle/bin:/usr/lib/jvm/java-7-oracle/db/bin:/usr/lib/jvm/java-7-oracle/jre/bin 3 | CUDA_LAUNCH_BLOCKING=1 THEANO_FLAGS='device=gpu1' KERAS_BACKEND=theano python evaluator_aleum.py 50001 & 4 | CUDA_LAUNCH_BLOCKING=1 THEANO_FLAGS='device=gpu2' KERAS_BACKEND=theano python evaluator_aleum.py 50002 & 5 | CUDA_LAUNCH_BLOCKING=1 THEANO_FLAGS='device=gpu3' KERAS_BACKEND=theano python evaluator_aleum.py 50003 & 6 | CUDA_LAUNCH_BLOCKING=1 THEANO_FLAGS='device=gpu0' KERAS_BACKEND=theano python evaluator_aleum.py 50004 & 7 | 8 | -------------------------------------------------------------------------------- /train/load_test.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from keras.models import Sequential 4 | from keras.layers.core import Activation, Dense, Flatten 5 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 6 | from keras.optimizers import SGD 7 | from keras.callbacks import Callback 8 | from keras import backend as K 9 | from keras.models import model_from_json 10 | import sys 11 | 12 | if __name__ == "__main__": 13 | 14 | MODEL_FILE_JSON = sys.argv[1] 15 | MODEL_FILE_H5 = sys.argv[2] 16 | 17 | model = model_from_json(open(MODEL_FILE_JSON).read()) 18 | model.load_weights(MODEL_FILE_H5) 19 | 20 | model.save_weights(MODEL_FILE_H5+".v3") 21 | 22 | -------------------------------------------------------------------------------- /play_DCL/scored_move.py: -------------------------------------------------------------------------------- 1 | from utils import move_as_string 2 | 3 | from random import random 4 | 5 | class ScoredMove: 6 | def __init__(self, move, score): 7 | self.move = move 8 | self.score = score 9 | self.rand = random() / 1000 10 | self.nodes = 0 11 | 12 | def __cmp__(self, other): 13 | return cmp(self.score+self.rand, other.score+other.rand) 14 | 15 | def __str__(self): 16 | s = "(%s, %s" % (move_as_string(self.move), self.score) 17 | if hasattr(self, "tactical_status"): 18 | s = s + ", %s" % (self.tactical_status,) 19 | if self.nodes: 20 | s = s + ", %s" % (self.nodes,) 21 | return s + ")" 22 | 23 | -------------------------------------------------------------------------------- /play_DCL/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /play_DCL/test_minigo_SL_FR.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | today=`date +%Y%m%d_%H%M%S` 4 | 5 | #rm -rf 2016* 6 | 7 | mkdir $today 8 | 9 | chmod 700 $today 10 | 11 | cd $today 12 | #PATH=/home/sclab/anaconda2/bin:/usr/local/cuda-7.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/java-7-oracle/bin:/usr/lib/jvm/java-7-oracle/db/bin:/usr/lib/jvm/java-7-oracle/jre/bin 13 | #filename=$today 14 | c=0 15 | for ((b=0;b<1;b++)) 16 | do 17 | for ((a=0;a<1;a++,c++)) 18 | do 19 | #echo -e "\t" $a 20 | filename=$today'_'$c 21 | echo -e "filename=" $filename 22 | THEANO_FLAGS='device=gpu0' 23 | KERAS_BACKEND=theano 24 | gogui-twogtp -black 'python /home/sclab/dclee/minigo/play_gtp.py -mode SL -randomcount 5' -white 'python /home/sclab/dclee/minigo/play_gtp.py -mode FR -randomcount 5' -size 9 -verbose -xml -auto -sgffile ttt -force -sgffile $filename -games 100 25 | #sleep 600 26 | done 27 | sleep 3 28 | done 29 | 30 | echo -e "ended zzzzzzzzz" 31 | -------------------------------------------------------------------------------- /play_DCL/test_minigo.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | mv 2016* old 3 | mv result/* old 4 | mv g*log old 5 | mv mini*log old 6 | 7 | today=`date +%Y%m%d_%H%M%S` 8 | 9 | #rm -rf 2016* 10 | 11 | mkdir $today 12 | 13 | chmod 700 $today 14 | 15 | cd $today 16 | #PATH=/home/sclab/anaconda2/bin:/usr/local/cuda-7.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/java-7-oracle/bin:/usr/lib/jvm/java-7-oracle/db/bin:/usr/lib/jvm/java-7-oracle/jre/bin 17 | #filename=$today 18 | c=0 19 | for ((b=0;b<1;b++)) 20 | do 21 | for ((a=0;a<1;a++,c++)) 22 | do 23 | #echo -e "\t" $a 24 | filename=$today'_'$c 25 | echo -e "filename=" $filename 26 | CUDA_LAUNCH_BLOCKING=1 27 | THEANO_FLAGS='device=gpu0' 28 | KERAS_BACKEND=theano 29 | gogui-twogtp -black 'python /home/sclab/dclee/simplego/play_gtp.py' -white 'python /home/sclab/dclee/minigo/play_gtp.py' -size 9 -verbose -xml -auto -sgffile ttt -force -sgffile $filename -games 10 30 | #sleep 600 31 | done 32 | sleep 3 33 | done 34 | 35 | echo -e "ended zzzzzzzzz" 36 | -------------------------------------------------------------------------------- /play_DCL/test_simplego_minigo.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | mv 2016* old 3 | mv result/* old 4 | mv g*log old 5 | mv mini*log old 6 | 7 | today=`date +%Y%m%d_%H%M%S` 8 | 9 | #rm -rf 2016* 10 | 11 | mkdir $today 12 | 13 | chmod 655 $today 14 | 15 | cd $today 16 | #PATH=/home/sclab/anaconda2/bin:/usr/local/cuda-7.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/java-7-oracle/bin:/usr/lib/jvm/java-7-oracle/db/bin:/usr/lib/jvm/java-7-oracle/jre/bin 17 | #filename=$today 18 | c=0 19 | for ((b=0;b<10;b++)) 20 | do 21 | for ((a=0;a<1;a++,c++)) 22 | do 23 | #echo -e "\t" $a 24 | filename=$today'_'$c 25 | echo -e "filename=" $filename 26 | CUDA_LAUNCH_BLOCKING=1 27 | THEANO_FLAGS='device=gpu1' 28 | KERAS_BACKEND=theano 29 | gogui-twogtp -black 'python /home/sclab/dclee/simplego/play_gtp.py' -white 'python /home/sclab/dclee/minigo/play_gtp.py' -size 9 -verbose -xml -auto -sgffile ttt -force -sgffile $filename -games 1 30 | #sleep 600 31 | done 32 | sleep 3 33 | done 34 | 35 | echo -e "ended zzzzzzzzz" 36 | -------------------------------------------------------------------------------- /play_DCL/mcts/utils.py: -------------------------------------------------------------------------------- 1 | import random 2 | import numpy as np 3 | 4 | 5 | def rand_max(iterable, key=None): 6 | """ 7 | A max function that tie breaks randomly instead of first-wins as in 8 | built-in max(). 9 | :param iterable: The container to take the max from 10 | :param key: A function to compute tha max from. E.g.: 11 | >>> rand_max([-2, 1], key=lambda x:x**2 12 | -2 13 | If key is None the identity is used. 14 | :return: The entry of the iterable which has the maximum value. Tie 15 | breaks are random. 16 | """ 17 | if key is None: 18 | key = lambda x: x 19 | 20 | max_v = -np.inf 21 | max_l = [] 22 | 23 | for item, value in zip(iterable, [key(i) for i in iterable]): 24 | #print "temp : " + str(item) + ", " + str(value) 25 | if value == max_v: 26 | max_l.append(item) 27 | elif value > max_v: 28 | max_l = [item] 29 | max_v = value 30 | ''' 31 | print "--------------" 32 | for tt in max_l: 33 | print "result : " + str(tt) 34 | ''' 35 | 36 | return random.choice(max_l) -------------------------------------------------------------------------------- /play_DCL/eye.py: -------------------------------------------------------------------------------- 1 | from const import * 2 | from utils import * 3 | 4 | 5 | class Eye: 6 | """Eye: collection of empty blocks and either black or white blocks 7 | 8 | Attributes: 9 | parts: list of blocks forming this eye 10 | """ 11 | def __init__(self): 12 | self.parts = [] 13 | 14 | def iterate_stones(self): 15 | """Go through all stones in all blocks in eye 16 | """ 17 | for block in self.parts: 18 | for stone in block.stones: 19 | yield stone 20 | 21 | def mark_status(self, live_color): 22 | """Go through all stones in all blocks in eye 23 | All opposite colored blocks are marked dead. 24 | Empty blocks are marked as territory for live_color. 25 | """ 26 | for block in self.parts: 27 | if block.color == live_color: 28 | block.status = UNCONDITIONAL_LIVE 29 | elif block.color == other_side[live_color]: 30 | block.status = UNCONDITIONAL_DEAD 31 | else: 32 | block.status = live_color + UNCONDITIONAL_TERRITORY 33 | 34 | -------------------------------------------------------------------------------- /play_DCL/mcts_test7.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | import os, math, random 3 | from feature.FeatureMap import * 4 | from feature.Plays import * 5 | from types import NoneType 6 | import sys 7 | 8 | from keras.models import model_from_json 9 | from keras.optimizers import SGD 10 | from keras import backend as K 11 | import numpy as np 12 | 13 | 14 | if __name__ == "__main__": 15 | # 모델 파일 지정 16 | RL_VALUE_JSON = "network/value_net_model.json" 17 | RL_VALUE_H5 = "network/value_net_weights_5.h5" 18 | 19 | # 모델 로드 20 | sgd = SGD(lr=0.003, decay=0.0, momentum=0.0, nesterov=False) 21 | rl_value = model_from_json(open(RL_VALUE_JSON).read()) 22 | rl_value.load_weights(RL_VALUE_H5) 23 | rl_value.compile(loss='MSE', optimizer=sgd) 24 | 25 | plays = Plays() 26 | plays.load_from_sgf("sgfs/test.sgf") 27 | 28 | # value net feature 추출 (policy net보다 1개 많음) 29 | features = FeatureMap(plays, len(plays)) 30 | inputs = features.input_planes_valuenet 31 | # policy net의 경우, inputs = featuers.input_planes_policynet 32 | 33 | # value net output 34 | predicted_value = rl_value.predict(np.asarray([inputs], dtype=np.float)) 35 | 36 | print predicted_value 37 | 38 | -------------------------------------------------------------------------------- /utils/extract_tests_from_dataset.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import random 3 | from utils.base import * 4 | import os 5 | 6 | DATA_FOLDER = "20160608/datasets/phase01/" 7 | TEST_FOLDER = "20160608/datasets/test/" 8 | POSTFIX = "_x.csv" 9 | 10 | RATIO = 9527 11 | 12 | if __name__ == "__main__": 13 | all_fnames = get_file_names(DATA_FOLDER, POSTFIX) 14 | f_idx = [i for i in range(0, len(all_fnames))] 15 | random.shuffle(f_idx) 16 | 17 | print "start" 18 | 19 | num = 0 20 | 21 | for idx in f_idx[:RATIO]: 22 | fname = all_fnames[idx] 23 | f_x = open(DATA_FOLDER + fname, 'r+') 24 | X = f_x.read() 25 | f_x.close() 26 | 27 | f_y = open(DATA_FOLDER + fname[:-len(POSTFIX)] + "_y.csv", 'r+') 28 | Y = f_y.read() 29 | f_y.close() 30 | 31 | f_x = open(TEST_FOLDER + fname, 'w') 32 | f_x.write(X) 33 | f_x.close() 34 | 35 | f_y = open(TEST_FOLDER + fname[:-len(POSTFIX)] + "_y.csv", 'w') 36 | f_y.write(Y) 37 | f_y.close() 38 | 39 | os.remove(DATA_FOLDER + fname) 40 | os.remove(DATA_FOLDER + fname[:-len(POSTFIX)] + "_y.csv") 41 | 42 | num += 1 43 | if num % 100 == 0: 44 | print "." 45 | 46 | print "finish.." 47 | -------------------------------------------------------------------------------- /play_DCL/socket2.py: -------------------------------------------------------------------------------- 1 | # Echo client program 2 | import socket 3 | 4 | def test1(): 5 | HOST = '127.0.0.1' # The remote host 6 | PORT = 50000 # The same port as used by the server 7 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 8 | s.connect((HOST, PORT)) 9 | 10 | for i in range(0, 10): 11 | s.sendall('Hello, world' + str(i)) 12 | data = s.recv(1024) 13 | print 'Received', repr(data) 14 | 15 | s.close() 16 | 17 | def test2(): 18 | HOST = '127.0.0.1' # The remote host 19 | PORT = 50001 # The same port as used by the server 20 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 21 | s.connect((HOST, PORT)) 22 | 23 | for i in range(0, 10): 24 | s.send('Hello, world' + str(i)) 25 | data = s.recv(1024) 26 | print 'Received', repr(data) 27 | 28 | s.close() 29 | 30 | 31 | def test3(): 32 | HOST = '127.0.0.1' # The remote host 33 | PORT = 50001 # The same port as used by the server 34 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 35 | s.connect((HOST, PORT)) 36 | 37 | s.send('eval:sgfs/test.sgf') 38 | data = s.recv(1024) 39 | print 'Received', repr(data) 40 | 41 | s.close() 42 | 43 | 44 | 45 | if __name__=="__main__": 46 | test3() 47 | 48 | -------------------------------------------------------------------------------- /feature/utility.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | utility.py 4 | """ 5 | import os 6 | import os.path 7 | 8 | def filenames_in(path, extensions): 9 | for entryname in os.listdir(path): 10 | entrypath = os.path.abspath(os.path.join(path, entryname)) 11 | entry_ext = os.path.splitext(entrypath)[1][1:] 12 | if os.path.isfile(entrypath) and (entry_ext in [ext.replace(".", "") for ext in extensions.split(";")]): 13 | yield entrypath 14 | 15 | 16 | def print_features(feature_map): 17 | for row_index in range(feature_map.rows): 18 | row = "".join([(value or ".") for value in feature_map.board[row_index]]) 19 | row += "\t" 20 | row += "\t".join(["".join(["{0}".format(value or ".") for value in feature[row_index]]) for feature in feature_map.input_planes]) 21 | print(row) 22 | 23 | def print_board(board): 24 | for row_index in range(board.rows): 25 | print("".join([(value or ".") for value in board[row_index]])) 26 | 27 | def print_feature(feature): 28 | for row_index in range(len(feature)): 29 | print("".join(["{0}".format(value or ".") for value in feature[row_index]])) 30 | 31 | def print_int_feature(board, feature): 32 | for row_index in range(board.rows): 33 | row = "".join([(value or ".") for value in board[row_index]]) 34 | row += "\t" 35 | row += " ".join(["{0:3}".format(value or "...") for value in feature[row_index]]) 36 | print(row) 37 | -------------------------------------------------------------------------------- /play_DCL/mcts_test2.py: -------------------------------------------------------------------------------- 1 | from play_gtp import * 2 | 3 | ''' 4 | name 5 | boardsize 9 6 | clear_board 7 | komi 6.5 8 | play B A1 9 | genmove w 10 | genmove b 11 | showboard 12 | 13 | ''' 14 | 15 | if __name__=="__main__": 16 | player = GTP_player() 17 | 18 | #name 19 | player.master.set_result("= " + player.name + "\n\n") 20 | 21 | #boardsize 9 22 | player.boardsize(9) 23 | 24 | #clear_board 25 | player.clear_board() 26 | 27 | #komi 6.5 28 | player.komi = 6.5 29 | player.engine.set_komi(player.komi + player.handicap) 30 | 31 | #showboard 32 | player.master.set_result(player.showboard()) 33 | 34 | #play B A1 35 | player.play('B', 'A1') 36 | player.master.set_result(player.showboard()) 37 | 38 | player.play('W', 'A2') 39 | player.master.set_result(player.showboard()) 40 | 41 | player.engine.make_move((2, 2)) 42 | player.master.set_result(player.showboard()) 43 | 44 | player.engine.make_move((2, 3)) 45 | player.master.set_result(player.showboard()) 46 | 47 | 48 | player2 = GTP_player() 49 | player2.engine = load_sgf.load_file("sgfs\\0323_result01-0.sgf") 50 | player2.master.set_result(player2.showboard()) 51 | 52 | player3 = GTP_player() 53 | player3 = player2 54 | player3.master.set_result(player3.showboard()) 55 | 56 | for move in player3.engine.iterate_moves(): 57 | a = move 58 | print move 59 | 60 | print "ended" 61 | -------------------------------------------------------------------------------- /play_DCL/feature/utility.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | utility.py 4 | """ 5 | import os 6 | import os.path 7 | 8 | from SGFGame import SGFGame 9 | 10 | def filenames_in(path, extensions): 11 | for entryname in os.listdir(path): 12 | entrypath = os.path.abspath(os.path.join(path, entryname)) 13 | entry_ext = os.path.splitext(entrypath)[1][1:] 14 | if os.path.isfile(entrypath) and (entry_ext in [ext.replace(".", "") for ext in extensions.split(";")]): 15 | yield entrypath 16 | 17 | 18 | def print_features(feature_map): 19 | for row_index in range(feature_map.rows): 20 | row = "".join([(value or ".") for value in feature_map.board[row_index]]) 21 | row += "\t" 22 | row += "\t".join(["".join(["{0}".format(value or ".") for value in feature[row_index]]) for feature in feature_map.features]) 23 | print(row) 24 | 25 | def print_board(board): 26 | for row_index in range(board.rows): 27 | print("".join([(value or ".") for value in board[row_index]])) 28 | 29 | def print_feature(feature): 30 | for row_index in range(len(feature)): 31 | print("".join(["{0}".format(value or ".") for value in feature[row_index]])) 32 | 33 | def print_int_feature(board, feature): 34 | for row_index in range(board.rows): 35 | row = "".join([(value or ".") for value in board[row_index]]) 36 | row += "\t" 37 | row += " ".join(["{0:3}".format(value or "...") for value in feature[row_index]]) 38 | print(row) 39 | -------------------------------------------------------------------------------- /play_DCL/idiot_bot.py: -------------------------------------------------------------------------------- 1 | # -*- coding: cp1252 -*- 2 | #! /usr/bin/env python 3 | 4 | #IdiotBot at KGS 5 | 6 | #This plays mostly randomly using these rules: 7 | #1) If can capture/extend group in atari: select one randomly 8 | #2) If not, then select move that doesn't fill single space 'eye' 9 | #3) If not, then pass 10 | #basically, but for scoring and potentially few moves before it: Uses SimpleBot v0.1.X engine 11 | #For handicap placement also using SimpleBot v0.1.X engine 12 | 13 | import sys, os 14 | import play_gtp 15 | import simple_go 16 | 17 | class IdiotBot(play_gtp.GTP_player): 18 | # Class members: 19 | # slave GTP_connection 20 | # master GTP_controller 21 | 22 | def __init__(self): 23 | play_gtp.GTP_player.__init__(self) 24 | self.engine = simple_go.Game(19) 25 | self.version = "0.0.3" 26 | self.name = "IdiotBot: I play mostly randomly: if I'm too easy, try WeakBot50k. Its maybe 60 stones stronger. 30k humans are often 80 stones stronger. My info contains links to my source code: " 27 | 28 | 29 | def genmove_plain(self, color, remove_opponent_dead=True, pass_allowed=True): 30 | self.check_side2move(color) 31 | move = self.engine.select_random_no_eye_fill_move(remove_opponent_dead, pass_allowed) 32 | move = simple_go.move_as_string(move, self.engine.size) 33 | self.play_plain(color, move) 34 | return move 35 | 36 | 37 | if __name__=="__main__": 38 | player = IdiotBot() 39 | play_gtp.parse_options(player) 40 | player.loop() 41 | 42 | -------------------------------------------------------------------------------- /utils/extract_dataset_from_sgf_valuenet.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | import os, math, random 3 | from feature.FeatureMap import * 4 | from feature.Plays import * 5 | from utils.base import get_file_names 6 | from types import NoneType 7 | import sys 8 | 9 | def load_dataset(): 10 | 11 | print "start loading go dataset files" 12 | 13 | # sgf에서 plays 추출 14 | file_names = get_file_names(GO_FOLDER, POSTFIX) 15 | for name in file_names: 16 | plays = Plays() 17 | plays.load_from_sgf(GO_FOLDER+"/"+name) 18 | splitted_name = name.split("_") 19 | play_num = int(splitted_name[1]) 20 | features = FeatureMap(plays, play_num) 21 | f = open(OTHER_TRAIN_FOLDER+"/"+name+"_"+str(play_num)+"_x"+".csv", 'w') 22 | txt = "" 23 | for feature in features.input_planes_valuenet: 24 | for rows in feature: 25 | for r in range(0, len(rows)): 26 | if r == len(rows)-1: 27 | txt += str(rows[r]) 28 | else: 29 | txt += str(rows[r]) + "," 30 | txt+="\n" 31 | txt+="\n" 32 | f.write(txt) 33 | f.close() 34 | f = open(OTHER_TRAIN_FOLDER+"/"+name+"_"+str(play_num)+"_y"+".csv", 'w') 35 | cur_label = splitted_name[2][0] 36 | f.write(cur_label) 37 | f.close() 38 | 39 | print "finish.." 40 | 41 | if __name__ == "__main__": 42 | POSTFIX = ".sgf" 43 | 44 | GO_FOLDER = sys.argv[1] 45 | OTHER_TRAIN_FOLDER = sys.argv[2] 46 | load_dataset() 47 | 48 | -------------------------------------------------------------------------------- /play_DCL/mcts_test4.py: -------------------------------------------------------------------------------- 1 | # -*- coding: ms949 -*- 2 | 3 | from feature.Plays import * 4 | from feature.FeatureMap import * 5 | from go import * 6 | from mcts.mcts import * 7 | from mcts.tree_policies import * 8 | from mcts.default_policies import * 9 | from mcts.backups import * 10 | from mcts.graph import * 11 | 12 | if __name__ == "__main__": 13 | 14 | game = simple_go.Game(9) 15 | 16 | komi = 6.5 17 | game.set_komi(komi) 18 | 19 | #showboard 20 | print str(game.current_board) 21 | 22 | #test 23 | game.make_move((1, 1)) 24 | print str(game.current_board) 25 | game.make_move((2, 2)) 26 | print str(game.current_board) 27 | 28 | 29 | #make argument to call model.predict 30 | i = 1 31 | playlist = [] 32 | for move in game.move_history: 33 | if i%2 == 1: 34 | playlist.append(('b', (move[0]-1, move[1]-1))) 35 | else: 36 | playlist.append(('w', (move[0]-1, move[1]-1))) 37 | i = i + 1 38 | 39 | plays = Plays(playlist) 40 | features = FeatureMap(plays, len(plays)) 41 | print "first : " 42 | print features.input_planes # <-- use this value 43 | 44 | #another way 45 | print "second : " 46 | print game.getargumenttopredict() 47 | 48 | 49 | #mcts test. reference only interface. inner logic will be modified. 50 | root = StateNode(None, GoState(game)) 51 | mcts = MCTS(tree_policy=UCB1(c=1.41), 52 | default_policy=evaluation, 53 | backup=monte_carlo) 54 | 55 | best_action = mcts(root) 56 | 57 | 58 | print "ended" 59 | 60 | -------------------------------------------------------------------------------- /play_DCL/maze_.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | class MazeAction(object): 4 | def __init__(self, move): 5 | self.move = np.asarray(move) 6 | 7 | def __eq__(self, other): 8 | return all(self.move == other.move) 9 | def __hash__(self): 10 | return 10*self.move[0] + self.move[1] 11 | def __str__(self): 12 | return str(self.move[0]) + "," + str(self.move[1]) 13 | def tostr(self): 14 | return str(self.move[0]) + "," + str(self.move[1]) 15 | 16 | class MazeState(object): 17 | def __init__(self, pos):f 18 | self.pos = np.asarray(pos) 19 | self.actions = [MazeAction([1, 0]), 20 | MazeAction([0, 1]), 21 | MazeAction([-1, 0]), 22 | MazeAction([0, -1])] 23 | 24 | def perform(self, action): 25 | pos = self.pos + action.move 26 | ''' 27 | pos[0] = min(pos[0], 2) 28 | pos[1] = min(pos[1], 2) 29 | pos[0] = max(pos[0], 0) 30 | pos[1] = max(pos[1], 0) 31 | ''' 32 | return MazeState(pos) 33 | 34 | def reward(self, parent, action): 35 | if all(self.pos == np.array([2, 2])): 36 | return 10 37 | else: 38 | return -1 39 | 40 | def is_terminal(self): 41 | return False 42 | 43 | def __eq__(self, other): 44 | return all(self.pos == other.pos) 45 | def __hash__(self): 46 | return 10 * self.pos[0] + self.pos[1] 47 | def __str__(self): 48 | return str(self.pos[0]) + ", " + str(self.pos[1]) 49 | 50 | def tostr(self): 51 | return str(self.pos[0]) + ", " + str(self.pos[1]) 52 | -------------------------------------------------------------------------------- /play_DCL/pos_cache.py: -------------------------------------------------------------------------------- 1 | import copy 2 | 3 | class PositionCache: 4 | def __init__(self, key, score, move, depth, exact): 5 | self.key = key 6 | self.score = score 7 | self.move = move 8 | self.depth = depth 9 | self.exact = exact 10 | 11 | def __str__(self): 12 | return "%s %s %s %s %s" % (self.key, self.score, self.move, self.depth, self.exact) 13 | 14 | def __cmp__(self, other): 15 | return cmp((self.exact, self.depth), (other.exact, other.depth)) 16 | 17 | 18 | LOWERBOUND = "lowerbound" 19 | UPPERBOUND = "upperbound" 20 | EXACTSCORE = "exactscore" 21 | 22 | class AlphaBetaPositionCache: 23 | def __init__(self, key, score, move, depth, alpha, beta, shadow): 24 | self.key = key 25 | self.score = score 26 | self.move = move 27 | self.depth = depth 28 | self.alpha = alpha 29 | self.beta = beta 30 | if score >= beta: 31 | self.flag = LOWERBOUND 32 | elif score <= alpha: 33 | self.flag = UPPERBOUND 34 | else: 35 | self.flag = EXACTSCORE 36 | self.shadow = copy.copy(shadow) 37 | 38 | 39 | def __str__(self): 40 | return "%s %s %s %s %s %s %s" % (self.key, self.score, self.move, self.depth, self.alpha, self.beta, self.flag) 41 | 42 | #def __cmp__(self, other): 43 | # return cmp((self.exact, self.depth), (other.exact, other.depth)) 44 | 45 | 46 | class LambdaPositionCache: 47 | def __init__(self, key, score, move, shadow, n): 48 | self.key = key 49 | self.score = score 50 | self.move = move 51 | self.shadow = copy.copy(shadow) 52 | self.n = n 53 | 54 | def __str__(self): 55 | return "%s %s %s %s" % (self.key, self.score, self.move, self.n) 56 | 57 | -------------------------------------------------------------------------------- /play_DCL/graphvizresult.py: -------------------------------------------------------------------------------- 1 | # -*- coding: ms949 -*- 2 | 3 | 4 | from graphviz import Digraph 5 | from go import * 6 | 7 | 8 | styles = { 9 | 'digraph': { 10 | 'size': '1000,1000' 11 | }, 12 | 'nodes': { 13 | 'fontname': 'courier new', 14 | 'fontsize': '12' 15 | }, 16 | 'edges': { 17 | 'fontname': 'courier new', 18 | 'fontsize': '12' 19 | } 20 | } 21 | 22 | def apply_styles(graph, styles): 23 | graph.node_attr.update( 24 | ('digraph' in styles and styles['digraph']) or {} 25 | ) 26 | graph.node_attr.update( 27 | ('nodes' in styles and styles['nodes']) or {} 28 | ) 29 | graph.edge_attr.update( 30 | ('edges' in styles and styles['edges']) or {} 31 | ) 32 | return graph 33 | 34 | 35 | def printnode(dot, node, parent): 36 | if node.type == 'StateNode': 37 | a = node.state.tostr() 38 | a_key = node.state.tostr2() 39 | #print "a_key : " + a_key 40 | #dot.node(a_key, a + " \: " + str(node.q)) 41 | dot.node(a_key, a) 42 | if parent is not None: 43 | b = parent.parent.state.tostr() 44 | b_key = parent.parent.state.tostr2() 45 | #print "b : " + b 46 | #c = parent.action.tostr() 47 | c = parent.tostr() 48 | dot.edge(b_key, a_key, label=c) 49 | #dot.edge(a_key, b, label=c) 50 | 51 | for child in node.child: 52 | printnode(dot, child, node) 53 | 54 | 55 | def printgraph(filepath, node): 56 | #print "drawing started" 57 | 58 | dot = Digraph(comment='The Round Table') 59 | dot = apply_styles(dot, styles) 60 | printnode(dot, node, None) 61 | dot.render(filepath, view=False) 62 | 63 | #print "ended" 64 | 65 | -------------------------------------------------------------------------------- /play_DCL/mcts_test.py: -------------------------------------------------------------------------------- 1 | from maze_ import * 2 | #import numpy as np 3 | from mcts.mcts import * 4 | from mcts.tree_policies import * 5 | from mcts.default_policies import * 6 | from mcts.backups import * 7 | from mcts.graph import * 8 | 9 | from graphviz import Digraph 10 | 11 | def printnode(dot, node, parent): 12 | if node.type == 'StateNode': 13 | a = node.state.tostr() 14 | #print a 15 | dot.node(a, a + " \: " + str(node.q)) 16 | if parent is not None: 17 | b = parent.parent.state.tostr() 18 | c = parent.action.tostr() 19 | dot.edge(b, a, label=c) 20 | 21 | for child in node.child: 22 | printnode(dot, child, node) 23 | 24 | if __name__=="__main__": 25 | mcts =MCTS(tree_policy=UCB1(c=1.41), 26 | default_policy=immediate_reward, 27 | backup=monte_carlo) 28 | 29 | root = StateNode(None, MazeState([0, 0])) 30 | best_action = mcts(root) 31 | a = 1 32 | print best_action.move[0], best_action.move[1] 33 | 34 | ''' 35 | dot = Digraph(comment='The Round Table') 36 | dot #doctest: +ELLIPSIS 37 | dot.node('A', 'King Arthur') 38 | dot.node('B', 'Sir Bedevere the Wise') 39 | dot.node('L', 'Sir Lancelot the Brave') 40 | 41 | dot.edges(['AB', 'AL']) 42 | dot.edge('B', 'L', constraint='false') 43 | print(dot.source) # doctest: +NORMALIZE_WHITESPACE 44 | dot.render('test.gv', view=True) 45 | ''' 46 | 47 | print "started" 48 | 49 | dot = Digraph(comment='The Round Table') 50 | dot #doctest: +ELLIPSIS 51 | printnode(dot, root, None) 52 | print "writing" 53 | print(dot.source) # doctest: +NORMALIZE_WHITESPACE 54 | print "drawing" 55 | dot.render('test2.gv', view=True) 56 | 57 | print "ended" 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /play_DCL/utility.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | utility.py 4 | """ 5 | import os 6 | import os.path 7 | 8 | #from SGFGame import SGFGame 9 | 10 | def filenames_in(path, extensions): 11 | for entryname in os.listdir(path): 12 | entrypath = os.path.abspath(os.path.join(path, entryname)) 13 | entry_ext = os.path.splitext(entrypath)[1][1:] 14 | if os.path.isfile(entrypath) and (entry_ext in [ext.replace(".", "") for ext in extensions.split(";")]): 15 | yield entrypath 16 | 17 | 18 | def print_features(feature_map): 19 | for row_index in range(feature_map.rows): 20 | row = "".join([(value or ".") for value in feature_map.board[row_index]]) 21 | row += "\t" 22 | row += "\t".join(["".join(["{0}".format(value or ".") for value in feature[row_index]]) for feature in feature_map.input_planes_policynet]) 23 | print(row) 24 | 25 | print('') 26 | for row_index in range(feature_map.rows): 27 | row = "".join([(value or ".") for value in feature_map.board[row_index]]) 28 | row += "\t" 29 | row += "\t".join(["".join(["{0}".format(value or ".") for value in feature[row_index]]) for feature in feature_map.input_planes_valuenet]) 30 | print(row) 31 | 32 | def print_board(board): 33 | for row_index in range(board.rows): 34 | print("".join([(value or ".") for value in board[row_index]])) 35 | 36 | def print_feature(feature): 37 | for row_index in range(len(feature)): 38 | print("".join(["{0}".format(value or ".") for value in feature[row_index]])) 39 | 40 | def print_int_feature(board, feature): 41 | for row_index in range(board.rows): 42 | row = "".join([(value or ".") for value in board[row_index]]) 43 | row += "\t" 44 | row += " ".join(["{0:3}".format(value or "...") for value in feature[row_index]]) 45 | print(row) 46 | -------------------------------------------------------------------------------- /play_DCL/gomill/handicap_layout.py: -------------------------------------------------------------------------------- 1 | """Standard layout of fixed handicap stones. 2 | 3 | This follows the rules from the GTP spec. 4 | 5 | """ 6 | 7 | def max_free_handicap_for_board_size(board_size): 8 | """Return the maximum number of stones for place_free_handicap command.""" 9 | return board_size * board_size - 1 10 | 11 | def max_fixed_handicap_for_board_size(board_size): 12 | """Return the maximum number of stones for fixed_handicap command.""" 13 | if board_size <= 7: 14 | return 0 15 | if board_size > 25: 16 | raise ValueError 17 | if board_size % 2 == 0 or board_size == 7: 18 | return 4 19 | else: 20 | return 9 21 | 22 | handicap_pattern = [ 23 | ['00', '22'], 24 | ['00', '22', '20'], 25 | ['00', '22', '20', '02'], 26 | ['00', '22', '20', '02', '11'], 27 | ['00', '22', '20', '02', '10', '12'], 28 | ['00', '22', '20', '02', '10', '12', '11'], 29 | ['00', '22', '20', '02', '10', '12', '01', '21'], 30 | ['00', '22', '20', '02', '10', '12', '01', '21', '11'], 31 | ] 32 | 33 | def handicap_points(number_of_stones, board_size): 34 | """Return the handicap points for a given number of stones and board size. 35 | 36 | Returns a list of pairs (row, col), length 'number_of_stones'. 37 | 38 | Raises ValueError if there isn't a placement pattern for the specified 39 | number of handicap stones and board size. 40 | 41 | """ 42 | if number_of_stones > max_fixed_handicap_for_board_size(board_size): 43 | raise ValueError 44 | if number_of_stones < 2: 45 | raise ValueError 46 | if board_size < 13: 47 | altitude = 2 48 | else: 49 | altitude = 3 50 | pos = {'0' : altitude, 51 | '1' : (board_size - 1) / 2, 52 | '2' : board_size - altitude - 1} 53 | return [(pos[s[0]], pos[s[1]]) 54 | for s in handicap_pattern[number_of_stones-2]] 55 | -------------------------------------------------------------------------------- /play_DCL/mcts/tree_policies.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | import numpy as np 3 | from const import * 4 | from play_gtp import * 5 | 6 | class UCB1(object): 7 | """ 8 | The typical bandit upper confidence bounds algorithm. 9 | """ 10 | def __init__(self, c): 11 | self.c = c 12 | 13 | def __call__(self, action_node): 14 | if self.c == 0: # assert that no nan values are returned 15 | # for action_node.n = 0 16 | return action_node.q 17 | 18 | return (action_node.q + 19 | self.c * np.sqrt(2 * np.log(action_node.parent.n) / 20 | action_node.n)) 21 | 22 | 23 | def flat(_): 24 | """ 25 | All actions are considered equally useful 26 | :param _: 27 | :return: 28 | """ 29 | return 0 30 | 31 | 32 | class PUCT(object): 33 | """ 34 | The typical bandit upper confidence bounds algorithm. 35 | """ 36 | def __init__(self, c): 37 | self.c = c 38 | 39 | def __call__(self, action_node): 40 | if self.c == 0: 41 | return action_node.Q 42 | 43 | ''' 44 | col = action_node.action.move[0] 45 | row = action_node.action.move[1] 46 | simulgame = action_node.parent.state.state 47 | ms = simulgame.score_move((col, row)) 48 | ''' 49 | 50 | if action_node.active == 0: 51 | return 0 52 | 53 | sumofNv = 0 54 | for sibling in action_node.parent.children.values(): 55 | sumofNv += sibling.Nv 56 | 57 | u = self.c*action_node.Pa*np.sqrt(sumofNv)/(1+action_node.Nv) 58 | 59 | sum = action_node.Q + u 60 | print action_node.action.tostr() + ', sum : ' + str(sum) + ', u : ' + str(u) + ', Q : ' + str(action_node.Q) 61 | 62 | #return (action_node.Q + u) 63 | #return (action_node.Q + u + g_mcts_msweight*action_node.Wms) 64 | return (action_node.Q + u) 65 | 66 | -------------------------------------------------------------------------------- /play_DCL/gomill/utils.py: -------------------------------------------------------------------------------- 1 | """Domain-independent utility functions for gomill. 2 | 3 | This module is designed to be used with 'from utils import *'. 4 | 5 | This is for generic utilities; see common for Go-specific utility functions. 6 | 7 | """ 8 | 9 | from __future__ import division 10 | 11 | __all__ = ["format_float", "format_percent", "sanitise_utf8", "isinf", "isnan"] 12 | 13 | def format_float(f): 14 | """Format a Python float in a friendly way. 15 | 16 | This is intended for values like komi or win counts, which will be either 17 | integers or half-integers. 18 | 19 | """ 20 | if f == int(f): 21 | return str(int(f)) 22 | else: 23 | return str(f) 24 | 25 | def format_percent(n, baseline): 26 | """Format a ratio as a percentage (showing two decimal places). 27 | 28 | Returns a string. 29 | 30 | Accepts baseline zero and returns '??' or '--'. 31 | 32 | """ 33 | if baseline == 0: 34 | if n == 0: 35 | return "--" 36 | else: 37 | return "??" 38 | return "%.2f%%" % (100 * n/baseline) 39 | 40 | 41 | def sanitise_utf8(s): 42 | """Ensure an 8-bit string is utf-8. 43 | 44 | s -- 8-bit string (or None) 45 | 46 | Returns the sanitised string. If the string was already valid utf-8, returns 47 | the same object. 48 | 49 | This replaces bad characters with ascii question marks (I don't want to use 50 | a unicode replacement character, because if this function is doing anything 51 | then it's likely that there's a non-unicode setup involved somewhere, so it 52 | probably wouldn't be helpful). 53 | 54 | """ 55 | if s is None: 56 | return None 57 | try: 58 | s.decode("utf-8") 59 | except UnicodeDecodeError: 60 | return (s.decode("utf-8", 'replace') 61 | .replace(u"\ufffd", u"?") 62 | .encode("utf-8")) 63 | else: 64 | return s 65 | 66 | try: 67 | from math import isinf, isnan 68 | except ImportError: 69 | # Python < 2.6 70 | def isinf(f): 71 | return (f == float("1e500") or f == float("-1e500")) 72 | def isnan(f): 73 | return (f != f) 74 | 75 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//feature/Board.py=utf-8 3 | encoding//feature/Plays.py=utf-8 4 | encoding//play_DCL/const.py=ms949 5 | encoding//play_DCL/evaluator_aleum.py=ms949 6 | encoding//play_DCL/go.py=ms949 7 | encoding//play_DCL/graphvizresult.py=ms949 8 | encoding//play_DCL/gtp1_gtp2.py=cp1252 9 | encoding//play_DCL/idiot_bot.py=cp1252 10 | encoding//play_DCL/jobmgr.py=ms949 11 | encoding//play_DCL/mcts/backups.py=ms949 12 | encoding//play_DCL/mcts/default_policies.py=ms949 13 | encoding//play_DCL/mcts/graph.py=ms949 14 | encoding//play_DCL/mcts/mcts.py=ms949 15 | encoding//play_DCL/modelinterface.py=ms949 16 | encoding//play_DCL/network.py=ms949 17 | encoding//play_DCL/play_gtp.py=ms949 18 | encoding//selfplay/game_RL_policy.py=ms949 19 | encoding//selfplay/make_datasets_RL_policy.py=ms949 20 | encoding//selfplay/make_datasets_RL_value.py=ms949 21 | encoding//selfplay/make_datasets_RL_value2.py=ms949 22 | encoding//selfplay/make_datasets_RL_value3.py=ms949 23 | encoding//tests/__init__.py=utf-8 24 | encoding//tests/test_model.py=utf-8 25 | encoding//tests/test_model_all.py=utf-8 26 | encoding//tests/test_prob.py=utf-8 27 | encoding//train/1st-value_net.py=utf-8 28 | encoding//train/2nd-value_net.py=utf-8 29 | encoding//train/RL_policy_net.py=utf-8 30 | encoding//train/SL_policy_net.py=utf-8 31 | encoding//train/SL_policy_net_h01.py=utf-8 32 | encoding//train/SL_policy_net_h02.py=utf-8 33 | encoding//train/SL_policy_net_h03.py=utf-8 34 | encoding//train/SL_policy_net_h04.py=utf-8 35 | encoding//train/SL_policy_net_h05.py=utf-8 36 | encoding//train/SL_policy_net_h06.py=utf-8 37 | encoding//train/SL_policy_net_h07.py=utf-8 38 | encoding//train/SL_policy_net_h08.py=utf-8 39 | encoding//train/SL_policy_net_h09.py=utf-8 40 | encoding//train/SL_policy_net_h10.py=utf-8 41 | encoding//train/SL_policy_net_h11.py=utf-8 42 | encoding//train/SL_policy_net_h12.py=utf-8 43 | encoding//train/SL_policy_net_h13.py=utf-8 44 | encoding//train/SL_policy_net_load.py=utf-8 45 | encoding//train/load_test.py=utf-8 46 | encoding//train/rollout_policy.py=utf-8 47 | encoding//utils/extract_dataset_from_sgf_policy.py=utf-8 48 | encoding//utils/extract_dataset_from_sgf_valuenet.py=utf-8 49 | encoding//utils/extract_dataset_from_sgf_valuenet_pred.py=utf-8 50 | -------------------------------------------------------------------------------- /utils/extract_dataset_from_sgf_policy.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | import os, math, random 3 | from feature.FeatureMap import * 4 | from feature.Plays import * 5 | import numpy as np 6 | from utils.base import get_file_names 7 | from types import NoneType 8 | import sys 9 | 10 | def load_dataset(): 11 | 12 | print "start" 13 | 14 | num_saves = 0 15 | file_names = get_file_names(SGF_FOLDER, POSTFIX) 16 | for name in file_names: 17 | plays = Plays() 18 | plays.load_from_sgf(SGF_FOLDER+"/"+name) 19 | if plays.total_nonpass_plays < 10: 20 | continue 21 | for play_num in range(0, plays.total_plays+1): 22 | features = FeatureMap(plays, play_num) 23 | if type(features) is NoneType: 24 | continue 25 | f = open(SAVE_FOLDER+"/"+name+"_"+str(play_num)+"_x"+".csv", 'w') 26 | txt = "" 27 | inputs = features.input_planes_policynet 28 | for feature in inputs: 29 | for rows in feature: 30 | for r in range(0, len(rows)): 31 | if r == len(rows)-1: 32 | txt += str(rows[r]) 33 | else: 34 | txt += str(rows[r]) + "," 35 | txt+="\n" 36 | txt+="\n" 37 | f.write(txt) 38 | f.close() 39 | 40 | txt = "" 41 | f = open(SAVE_FOLDER+"/"+name+"_"+str(play_num)+"_y"+".csv", 'w') 42 | for rows in features.label: 43 | for r in range(0, len(rows)): 44 | r_value = rows[r] 45 | if r == len(rows)-1: 46 | txt += str(r_value) 47 | else: 48 | txt += str(r_value) + "," 49 | txt+="\n" 50 | f.write(txt) 51 | f.close() 52 | num_saves += 1 53 | if num_saves % 100 == 0: 54 | print "finish "+str(num_saves)+" sgf files.." 55 | 56 | print "finish all.." 57 | 58 | if __name__ == "__main__": 59 | POSTFIX = ".sgf" 60 | 61 | SGF_FOLDER = sys.argv[1] 62 | SAVE_FOLDER = sys.argv[2] 63 | 64 | if not os.path.exists(SAVE_FOLDER): 65 | os.mkdir(SAVE_FOLDER) 66 | 67 | load_dataset() 68 | -------------------------------------------------------------------------------- /play_DCL/big_game.py: -------------------------------------------------------------------------------- 1 | import string, random, sys, math 2 | import config 3 | from const import * 4 | from utils import * 5 | 6 | from block import Block 7 | from eye import Eye 8 | from gothread import Thread 9 | from board import Board 10 | from pos_cache import PositionCache 11 | from game import Game 12 | import play_gtp 13 | 14 | def main(size): 15 | """Play game against itself on given size board. 16 | Make sgf and images of resulting game. 17 | """ 18 | idiot_flag = True 19 | random.seed(2) 20 | g = Game(size) 21 | g.init_fast_select_random_no_eye_fill_move() 22 | for remove_opponent_dead in (False, True): 23 | g.undo_move() 24 | g.undo_move() 25 | while True: 26 | if idiot_flag: 27 | move = g.fast_select_random_no_eye_fill_move() 28 | else: 29 | move = g.generate_move(remove_opponent_dead=remove_opponent_dead) 30 | g.make_move(move) 31 | #if size<=max_size: 32 | # print move_as_string(move) 33 | #print g.current_board 34 | if idiot_flag: 35 | lst_lst = g.move_history, g.atari_moves, g.available_moves[WHITE], g.available_moves[BLACK] 36 | else: 37 | lst_lst = g.move_history, g.available_moves 38 | for lst in lst_lst: 39 | sys.stderr.write("%i " % len(lst)) 40 | if len(g.atari_moves) >= 2: 41 | sys.stderr.write("\n") 42 | if size<=max_size: 43 | sys.stderr.write("%s\n" % g.move_list_as_string(g.atari_moves)) 44 | else: 45 | sys.stderr.write(" \r") 46 | if g.has_2_passes(): 47 | break 48 | #print g.move_history 49 | #print g.current_board.goban 50 | args = (size, size, remove_opponent_dead) 51 | fp = open("%ix%i_%s.sgf" % args, "w") 52 | fp.write(str(g)) 53 | fp.close() 54 | fp = open("%ix%i_bw_%s.pgm" % args, "w") 55 | fp.write(g.as_image()) 56 | fp.close() 57 | fp = open("%ix%i_color_%s.ppm" % args, "w") 58 | fp.write(g.as_color_image()) 59 | fp.close() 60 | return g 61 | 62 | if __name__=="__main__": 63 | #If this file is executed directly it will play game against itself. 64 | size = int(sys.argv[1]) 65 | main(size) 66 | -------------------------------------------------------------------------------- /play_DCL/gomill/ascii_boards.py: -------------------------------------------------------------------------------- 1 | """ASCII board representation.""" 2 | 3 | from gomill.common import * 4 | from gomill import boards 5 | from gomill.common import column_letters 6 | 7 | def render_grid(point_formatter, size): 8 | """Render a board-shaped grid as a list of strings. 9 | 10 | point_formatter -- function (row, col) -> string of length 2. 11 | 12 | Returns a list of strings. 13 | 14 | """ 15 | column_header_string = " ".join(column_letters[i] for i in range(size)) 16 | result = [] 17 | if size > 9: 18 | rowstart = "%2d " 19 | padding = " " 20 | else: 21 | rowstart = "%d " 22 | padding = "" 23 | for row in range(size-1, -1, -1): 24 | result.append(rowstart % (row+1) + 25 | " ".join(point_formatter(row, col) 26 | for col in range(size))) 27 | result.append(padding + " " + column_header_string) 28 | return result 29 | 30 | _point_strings = { 31 | None : " .", 32 | 'b' : " #", 33 | 'w' : " o", 34 | } 35 | 36 | def render_board(board): 37 | """Render a gomill Board in ascii. 38 | 39 | Returns a string without final newline. 40 | 41 | """ 42 | def format_pt(row, col): 43 | return _point_strings.get(board.get(row, col), " ?") 44 | return "\n".join(render_grid(format_pt, board.side)) 45 | 46 | def interpret_diagram(diagram, size, board=None): 47 | """Set up the position from a diagram. 48 | 49 | diagram -- board representation as from render_board() 50 | size -- int 51 | 52 | Returns a Board. 53 | 54 | If the optional 'board' parameter is provided, it must be an empty board of 55 | the right size; the same object will be returned. 56 | 57 | """ 58 | if board is None: 59 | board = boards.Board(size) 60 | else: 61 | if board.side != size: 62 | raise ValueError("wrong board size, must be %d" % size) 63 | if not board.is_empty(): 64 | raise ValueError("board not empty") 65 | lines = diagram.split("\n") 66 | colours = {'#' : 'b', 'o' : 'w', '.' : None} 67 | if size > 9: 68 | extra_offset = 1 69 | else: 70 | extra_offset = 0 71 | try: 72 | for (row, col) in board.board_points: 73 | colour = colours[lines[size-row-1][3*(col+1)+extra_offset]] 74 | if colour is not None: 75 | board.play(row, col, colour) 76 | except Exception: 77 | raise ValueError 78 | return board 79 | 80 | 81 | -------------------------------------------------------------------------------- /play_DCL/block.py: -------------------------------------------------------------------------------- 1 | import config 2 | from const import * 3 | 4 | class Block: 5 | """Solidly connected group of stones or empy points as defined in Go rules 6 | 7 | Attributes: 8 | stones: position of stones or empty points 9 | empty points are like trasparent stones 10 | liberties: position of liberties 11 | color: color of stones 12 | """ 13 | def __init__(self, color): 14 | self.stones = {} 15 | self.neighbour = {} 16 | self.color = color 17 | self.chain = None 18 | self.eye_count = None 19 | 20 | def add_stone(self, pos): 21 | """add stone or empty point at given position 22 | """ 23 | self.stones[pos] = True 24 | 25 | def remove_stone(self, pos): 26 | """remove stone or empty point at given position 27 | """ 28 | if pos in self.stones: 29 | del self.stones[pos] 30 | 31 | def add_block(self, other_block): 32 | """add all stones and neighbours to this block 33 | """ 34 | self.stones.update(other_block.stones) 35 | self.neighbour.update(other_block.neighbour) 36 | 37 | def mark_stones(self, mark): 38 | """mark all stones with given value 39 | """ 40 | for stone in self.stones: 41 | self.stones[stone] = mark 42 | 43 | def size(self): 44 | """returns block size 45 | """ 46 | return len(self.stones) 47 | 48 | def max_liberties(self): 49 | """returns maximum amount liberties possible for this block size 50 | 44 51 | 4334 52 | 432234 53 | 43211234 54 | 4321XX1234 55 | 43211234 56 | 432234 57 | 4334 58 | 44 59 | """ 60 | if config.use_nth_order_liberties>1: 61 | res = 0.0 62 | for i in range(1, config.use_nth_order_liberties+1): 63 | res += self.size() * 2 + 2 + 4*(i-1) / float(i) 64 | return res 65 | ## return self.size() * 2 + 2 +\ 66 | ## 0.5 * (self.size() * 2 + 6) +\ 67 | ## 1.0/3.0 * (self.size() * 2 + 10) 68 | else: 69 | return self.size() * 2 + 2 70 | 71 | def iterate_threads(self): 72 | for pos1 in self.threads: 73 | for pos2 in self.threads[pos1]: 74 | yield self.threads[pos1][pos2] 75 | 76 | def get_origin(self): 77 | """return origin pos of block 78 | """ 79 | return min(self.stones) 80 | 81 | -------------------------------------------------------------------------------- /tests/test_model_all.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | import numpy as np 4 | from keras.models import model_from_json 5 | from keras.optimizers import SGD 6 | import time, csv, os, sys 7 | 8 | 9 | POSTFIX = ".csv" 10 | lrate = 0.003 11 | bsize = 16 12 | 13 | def get_file_names(path, postfix): 14 | res = [] 15 | 16 | for root, dirs, files in os.walk(path): 17 | 18 | for file in files: 19 | if file[-len(postfix):] == postfix: 20 | res.append(file) 21 | 22 | return res 23 | 24 | def load_dataset(): 25 | xs, ys = list(), list() 26 | file_names = get_file_names(DATA_FOLDER, POSTFIX) 27 | 28 | for name in file_names: 29 | datas = list() 30 | cur_rows = list() 31 | csvfile = open(DATA_FOLDER+name) 32 | row_reader = csv.reader(csvfile) 33 | if name[-5] == 'x': 34 | for row in row_reader: 35 | if len(row) == 0: 36 | datas.append(cur_rows) 37 | cur_rows = list() 38 | continue 39 | c = list() 40 | for a in row: 41 | c.append(a) 42 | cur_rows.append(c) 43 | xs.append(datas) 44 | datas = list() 45 | fname = name[:len(name)-5]+"y.csv" 46 | f = open(DATA_FOLDER + fname) 47 | rreader = csv.reader(f) 48 | for row in rreader: 49 | for a in row: 50 | datas.append(a) 51 | ys.append(datas) 52 | xs = np.asarray(xs, dtype=np.float) 53 | ys = np.asarray(ys, dtype=np.float) 54 | print "xs", xs.shape, "ys", ys.shape 55 | return xs, ys 56 | 57 | 58 | if __name__ == "__main__": 59 | 60 | MODEL_JSON = sys.argv[1] 61 | MODEL_H5 = sys.argv[2] 62 | DATA_FOLDER = sys.argv[3] 63 | 64 | cor, uncor = 0, 0 65 | 66 | X, Y = load_dataset() 67 | model = model_from_json(open(MODEL_JSON).read()) 68 | model.load_weights(MODEL_H5) 69 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 70 | model.compile(loss="categorical_crossentropy", optimizer=sgd) 71 | before = time.time() 72 | pResult = model.predict(X) 73 | after = time.time() 74 | print "걸린 시간", after-before 75 | for i in range(0, len(pResult)): 76 | pred = pResult[i].argmax() 77 | real = Y[i].argmax() 78 | if pred == real: 79 | cor += 1 80 | else: 81 | uncor += 1 82 | ACC = 100.0 * cor / len(Y) 83 | print "맞은 개수:", cor 84 | print "틀린 개수:", uncor 85 | print "정확도: %.5f" % ACC 86 | -------------------------------------------------------------------------------- /play_DCL/const.py: -------------------------------------------------------------------------------- 1 | # -*- coding: ms949 -*- 2 | 3 | EMPTY = "." 4 | BLACK = "X" 5 | WHITE = "O" 6 | EDGE = "#" 7 | 8 | BOTH = BLACK + WHITE 9 | 10 | colors = [BLACK, WHITE] 11 | 12 | other_side = {BLACK: WHITE, WHITE: BLACK} 13 | 14 | CEMPTY = 0 15 | CWHITE = 1 16 | CBLACK = 2 17 | color2ccolor = {BLACK: CBLACK, WHITE: CWHITE, EMPTY: CEMPTY} 18 | 19 | PASS_MOVE = (-1, -1) 20 | UNDO_MOVE = (-2, -2) 21 | NO_MOVE = (-3, -3) 22 | RESIGN_MOVE = (-4, -4) 23 | 24 | BEST_SCORE = 1000000000 25 | WORST_SCORE = -BEST_SCORE 26 | 27 | INFINITE_DISTANCE = 1000000 28 | 29 | normal_stone_value_ratio = 0.9 #how much value is given to stones that are not unconditionally alive and have max liberties 30 | dead_stone_value_ratio = -0.8 31 | our_critical_stone_value = 0.6 32 | other_critical_stone_value = -0.6 33 | 34 | x_coords_string = "ABCDEFGHJKLMNOPQRSTUVWXYZ" 35 | #x_coords_string = "12345678901234567890" 36 | max_size = len(x_coords_string) 37 | 38 | UNCONDITIONAL_LIVE = "alive" 39 | UNCONDITIONAL_DEAD = "dead" 40 | UNCONDITIONAL_UNKNOWN = "unknown" 41 | UNCONDITIONAL_TERRITORY = " territory" 42 | WHITE_UNCONDITIONAL_TERRITORY = WHITE + UNCONDITIONAL_TERRITORY 43 | BLACK_UNCONDITIONAL_TERRITORY = BLACK + UNCONDITIONAL_TERRITORY 44 | 45 | TACTICALLY_UNKNOWN = "tactically unknown" #capture; life&death 46 | TACTICALLY_LIVE = "tactically alive" #life&death 47 | TACTICALLY_DEAD = "tactically dead" #capture; life&death 48 | TACTICALLY_CRITICAL = "tactically critical" #capture; life&death 49 | 50 | PN_UNKNOWN = 0.5 51 | PN_OR = "or" 52 | PN_AND = "and" 53 | PN_INF = 1000000000 54 | 55 | 56 | 57 | #custom setting 58 | g_mcts_threshold = 15 59 | g_mcts_legalmove_restrict = 100 60 | g_mcts_Cpuck = 10 61 | g_mcts_msweight = 0.5 62 | g_mcts_lamda = 0.5 #for Q value calculation 63 | g_mcts_calctime = 10 64 | g_mcts_graphviz = False 65 | 66 | g_mcts_threadcount_eval = 1 #evaluation thread 67 | g_mcts_calccount = 100 #count of selection 68 | g_mcts_vl = 5 #virtual loss 69 | 70 | SLPOLICY_JSON = "network/slpolicy_model_0.json" 71 | SLPOLICY_H5 = "network/slpolicy_model_0.h5" 72 | ROLLOUT_JSON = "network/rollout_policy_net_model.json" 73 | ROLLOUT_H5 = "network/rollout_policy_net_weights.h5" 74 | VALUE_JSON = "network/value_net_model.json" 75 | VALUE_H5 = "network/value_net_weights_5.h5" 76 | RLPOLICY_JSON = "network/2nd_rlpolicy_model_21.json" 77 | RLPOLICY_H5 = "network/2nd_rlpolicy_model_21.h5" 78 | 79 | IS_BLACK = False 80 | 81 | 82 | #EVALUATOR_PORTS = 50001,50002,50003 83 | EVALUATOR_PORTS = 50001 84 | #EVALUATOR_INFOS = "165.132.122.42:50001,165.132.122.41:50001" 85 | #EVALUATOR_INFOS = "165.132.122.42:50001" 86 | #EVALUATOR_INFOS = "127.0.0.1:50001" 87 | EVALUATOR_INFOS = "165.132.122.42:50001,165.132.122.41:50001,165.132.122.41:50002,165.132.122.41:50003,165.132.122.41:50004,165.132.122.43:50001,165.132.122.43:50002" 88 | 89 | -------------------------------------------------------------------------------- /play_DCL/gomill/common.py: -------------------------------------------------------------------------------- 1 | """Domain-dependent utility functions for gomill. 2 | 3 | This module is designed to be used with 'from common import *'. 4 | 5 | This is for Go-specific utilities; see utils for generic utility functions. 6 | 7 | """ 8 | 9 | __all__ = ["opponent_of", "colour_name", "format_vertex", "format_vertex_list", 10 | "move_from_vertex"] 11 | 12 | _opponents = {"b":"w", "w":"b"} 13 | def opponent_of(colour): 14 | """Return the opponent colour. 15 | 16 | colour -- 'b' or 'w' 17 | 18 | Returns 'b' or 'w'. 19 | 20 | """ 21 | try: 22 | return _opponents[colour] 23 | except KeyError: 24 | raise ValueError 25 | 26 | def colour_name(colour): 27 | """Return the (lower-case) full name of a colour. 28 | 29 | colour -- 'b' or 'w' 30 | 31 | """ 32 | try: 33 | return {'b': 'black', 'w': 'white'}[colour] 34 | except KeyError: 35 | raise ValueError 36 | 37 | 38 | column_letters = "ABCDEFGHJKLMNOPQRSTUVWXYZ" 39 | 40 | def format_vertex(move): 41 | """Return coordinates as a string like 'A1', or 'pass'. 42 | 43 | move -- pair (row, col), or None for a pass 44 | 45 | The result is suitable for use directly in GTP responses. 46 | 47 | """ 48 | if move is None: 49 | return "pass" 50 | row, col = move 51 | if not 0 <= row < 25 or not 0 <= col < 25: 52 | raise ValueError 53 | return column_letters[col] + str(row+1) 54 | 55 | def format_vertex_list(moves): 56 | """Return a list of coordinates as a string like 'A1,B2'.""" 57 | return ",".join(map(format_vertex, moves)) 58 | 59 | def move_from_vertex(vertex, board_size): 60 | """Interpret a string representing a vertex, as specified by GTP. 61 | 62 | Returns a pair of coordinates (row, col) in range(0, board_size) 63 | 64 | Raises ValueError with an appropriate message if 'vertex' isn't a valid GTP 65 | vertex specification for a board of size 'board_size'. 66 | 67 | """ 68 | if not 0 < board_size <= 25: 69 | raise ValueError("board_size out of range") 70 | try: 71 | s = vertex.lower() 72 | except Exception: 73 | raise ValueError("invalid vertex") 74 | if s == "pass": 75 | return None 76 | try: 77 | col_c = s[0] 78 | if (not "a" <= col_c <= "z") or col_c == "i": 79 | raise ValueError 80 | if col_c > "i": 81 | col = ord(col_c) - ord("b") 82 | else: 83 | col = ord(col_c) - ord("a") 84 | row = int(s[1:]) - 1 85 | if row < 0: 86 | raise ValueError 87 | except (IndexError, ValueError): 88 | raise ValueError("invalid vertex: '%s'" % s) 89 | if not (col < board_size and row < board_size): 90 | raise ValueError("vertex is off board: '%s'" % s) 91 | return row, col 92 | 93 | -------------------------------------------------------------------------------- /play_DCL/gomill/terminal_input.py: -------------------------------------------------------------------------------- 1 | """Support for non-blocking terminal input.""" 2 | 3 | import os 4 | 5 | try: 6 | import termios 7 | except ImportError: 8 | termios = None 9 | 10 | class Terminal_reader(object): 11 | """Check for input on the controlling terminal.""" 12 | 13 | def __init__(self): 14 | self.enabled = True 15 | self.tty = None 16 | 17 | def is_enabled(self): 18 | return self.enabled 19 | 20 | def disable(self): 21 | self.enabled = False 22 | 23 | def initialise(self): 24 | if not self.enabled: 25 | return 26 | if termios is None: 27 | self.enabled = False 28 | return 29 | try: 30 | self.tty = open("/dev/tty", "w+") 31 | os.tcgetpgrp(self.tty.fileno()) 32 | self.clean_tcattr = termios.tcgetattr(self.tty) 33 | iflag, oflag, cflag, lflag, ispeed, ospeed, cc = self.clean_tcattr 34 | new_lflag = lflag & (0xffffffff ^ termios.ICANON) 35 | new_cc = cc[:] 36 | new_cc[termios.VMIN] = 0 37 | self.cbreak_tcattr = [ 38 | iflag, oflag, cflag, new_lflag, ispeed, ospeed, new_cc] 39 | except Exception: 40 | self.enabled = False 41 | return 42 | 43 | def close(self): 44 | if self.tty is not None: 45 | self.tty.close() 46 | self.tty = None 47 | 48 | def stop_was_requested(self): 49 | """Check whether a 'keyboard stop' instruction has been sent. 50 | 51 | Returns true if ^X has been sent on the controlling terminal. 52 | 53 | Consumes all available input on /dev/tty. 54 | 55 | """ 56 | if not self.enabled: 57 | return False 58 | # Don't try to read the terminal if we're in the background. 59 | # There's a race here, if we're backgrounded just after this check, but 60 | # I don't see a clean way to avoid it. 61 | if os.tcgetpgrp(self.tty.fileno()) != os.getpid(): 62 | return False 63 | try: 64 | termios.tcsetattr(self.tty, termios.TCSANOW, self.cbreak_tcattr) 65 | except EnvironmentError: 66 | return False 67 | try: 68 | seen_ctrl_x = False 69 | while True: 70 | c = os.read(self.tty.fileno(), 1) 71 | if not c: 72 | break 73 | if c == "\x18": 74 | seen_ctrl_x = True 75 | except EnvironmentError: 76 | seen_ctrl_x = False 77 | finally: 78 | termios.tcsetattr(self.tty, termios.TCSANOW, self.clean_tcattr) 79 | return seen_ctrl_x 80 | 81 | def acknowledge(self): 82 | """Leave an acknowledgement on the controlling terminal.""" 83 | self.tty.write("\rCtrl-X received; halting\n") 84 | -------------------------------------------------------------------------------- /play_DCL/network/rollout_policy_net_model.json: -------------------------------------------------------------------------------- 1 | {"class_name": "Sequential", "config": [{"class_name": "ZeroPadding2D", "config": {"padding": [1, 1], "batch_input_shape": [null, 38, 9, 9], "trainable": true, "name": "zeropadding2d_1", "input_dtype": "float32"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_1", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 5, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 32, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 5}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_1"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [2, 2], "trainable": true, "name": "zeropadding2d_2"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_2", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 3, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 32, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 3}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_2"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [2, 2], "trainable": true, "name": "zeropadding2d_3"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_3", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 3, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 32, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 3}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_3"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [1, 1], "trainable": true, "name": "zeropadding2d_4"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_4", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 1, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 1, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 1}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_4"}}, {"class_name": "Flatten", "config": {"trainable": true, "name": "flatten_1"}}, {"class_name": "Dense", "config": {"W_constraint": null, "b_constraint": null, "name": "dense_1", "activity_regularizer": null, "trainable": true, "init": "glorot_uniform", "bias": true, "input_dim": null, "b_regularizer": null, "W_regularizer": null, "activation": "linear", "output_dim": 81}}, {"class_name": "Activation", "config": {"activation": "softmax", "trainable": true, "name": "activation_5"}}]} -------------------------------------------------------------------------------- /train/policy_net_model_h03.json: -------------------------------------------------------------------------------- 1 | {"class_name": "Sequential", "config": [{"class_name": "ZeroPadding2D", "config": {"padding": [1, 1], "batch_input_shape": [null, 38, 9, 9], "trainable": true, "name": "zeropadding2d_1", "input_dtype": "float32"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_1", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 3, "subsample": [1, 1], "init": "glorot_uniform", "bias": true, "nb_filter": 32, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 3}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_1"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [1, 1], "trainable": true, "name": "zeropadding2d_2"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_2", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 1, "subsample": [1, 1], "init": "glorot_uniform", "bias": true, "nb_filter": 32, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 1}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_2"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [1, 1], "trainable": true, "name": "zeropadding2d_3"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_3", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 1, "subsample": [1, 1], "init": "glorot_uniform", "bias": true, "nb_filter": 32, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 1}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_3"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [1, 1], "trainable": true, "name": "zeropadding2d_4"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_4", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 1, "subsample": [1, 1], "init": "glorot_uniform", "bias": true, "nb_filter": 32, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 1}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_4"}}, {"class_name": "Flatten", "config": {"trainable": true, "name": "flatten_1"}}, {"class_name": "Dense", "config": {"W_constraint": null, "b_constraint": null, "name": "dense_1", "activity_regularizer": null, "trainable": true, "init": "glorot_uniform", "bias": true, "input_dim": null, "b_regularizer": null, "W_regularizer": null, "activation": "linear", "output_dim": 81}}, {"class_name": "Activation", "config": {"activation": "softmax", "trainable": true, "name": "activation_5"}}]} -------------------------------------------------------------------------------- /utils/extract_dataset_from_sgf_valuenet_pred.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | import os, math, random 3 | from feature.FeatureMap_RL import * 4 | from feature.Plays_RL import * 5 | from utils.base import get_file_names 6 | from types import NoneType 7 | import sys 8 | 9 | from keras.models import model_from_json 10 | from keras.optimizers import SGD 11 | from keras import backend as K 12 | import numpy as np 13 | 14 | RL_VALUE_H5 = "RL_value_models/value_net_weights_6.h5" 15 | RL_VALUE_JSON = "RL_value_models/value_net_model.json" 16 | 17 | def load_dataset(): 18 | 19 | print "start loading go dataset files" 20 | 21 | file_names = get_file_names(GO_FOLDER, POSTFIX) 22 | for name in file_names: 23 | plays = Plays_RL() 24 | plays.load_from_sgf(GO_FOLDER+"/"+name) 25 | if plays.total_nonpass_plays < 10: 26 | continue 27 | for play_num in range(1, plays.total_plays+1): 28 | features = FeatureMap_RL(plays, play_num) 29 | if type(features) is NoneType: 30 | continue 31 | f = open(OTHER_TRAIN_FOLDER+"/"+name+"_"+str(play_num)+"_x"+".csv", 'w') 32 | txt = "" 33 | inputs = features.input_planes_policynet 34 | for feature in inputs: 35 | for rows in feature: 36 | for r in range(0, len(rows)): 37 | if r == len(rows)-1: 38 | txt += str(rows[r]) 39 | else: 40 | txt += str(rows[r]) + "," 41 | txt+="\n" 42 | txt+="\n" 43 | f.write(txt) 44 | f.close() 45 | inputs = features.input_planes_valuenet 46 | predicted_value = rl_value_net.predict(np.asarray([inputs], dtype=np.float)) 47 | 48 | f = open(OTHER_TRAIN_FOLDER+"/"+name+"_"+str(play_num)+"_y"+".csv", 'w') 49 | txt = "" 50 | for rows in features.label: 51 | for r in range(0, len(rows)): 52 | r_value = rows[r] 53 | if r_value == 1 or r_value == -1: 54 | r_value = r_value - predicted_value[0][0] 55 | if r == len(rows)-1: 56 | txt += str(r_value) 57 | else: 58 | txt += str(r_value) + "," 59 | txt+="\n" 60 | f.write(txt) 61 | f.close() 62 | 63 | print "finish.." 64 | 65 | if __name__ == "__main__": 66 | POSTFIX = ".sgf" 67 | 68 | sgd = SGD(lr=0.003, decay=0.0, momentum=0.0, nesterov=False) 69 | rl_value_net = model_from_json(open(RL_VALUE_JSON).read()) 70 | rl_value_net.load_weights(RL_VALUE_H5) 71 | rl_value_net.compile(loss='MSE', optimizer=sgd) 72 | 73 | GO_FOLDER = sys.argv[1] 74 | OTHER_TRAIN_FOLDER = sys.argv[2] 75 | RLMODEL = sys.argv[3] 76 | load_dataset() 77 | -------------------------------------------------------------------------------- /tests/test_model.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | import numpy as np 4 | from keras.models import model_from_json 5 | from keras.optimizers import SGD 6 | import time, csv, os, sys 7 | 8 | 9 | POSTFIX = ".csv" 10 | lrate = 0.003 11 | bsize = 16 12 | 13 | def get_file_names(path, postfix): 14 | res = [] 15 | 16 | for root, dirs, files in os.walk(path): 17 | 18 | for file in files: 19 | if file[-len(postfix):] == postfix: 20 | res.append(file) 21 | 22 | return res 23 | 24 | def load_dataset(): 25 | xs, ys = list(), list() 26 | file_names = get_file_names(DATA_FOLDER, POSTFIX) 27 | 28 | for name in file_names: 29 | datas = list() 30 | cur_rows = list() 31 | csvfile = open(DATA_FOLDER+name) 32 | row_reader = csv.reader(csvfile) 33 | if name[-5] == 'x': 34 | for row in row_reader: 35 | if len(row) == 0: 36 | datas.append(cur_rows) 37 | cur_rows = list() 38 | continue 39 | c = list() 40 | for a in row: 41 | c.append(a) 42 | cur_rows.append(c) 43 | xs.append(datas) 44 | datas = list() 45 | fname = name[:len(name)-5]+"y.csv" 46 | f = open(DATA_FOLDER + fname) 47 | rreader = csv.reader(f) 48 | for row in rreader: 49 | for a in row: 50 | datas.append(a) 51 | ys.append(datas) 52 | xs = np.asarray(xs, dtype=np.float) 53 | ys = np.asarray(ys, dtype=np.float) 54 | print "xs", xs.shape, "ys", ys.shape 55 | return xs, ys 56 | 57 | 58 | if __name__ == "__main__": 59 | 60 | MODEL_FOLDER = sys.argv[1] 61 | DATA_FOLDER = sys.argv[2] 62 | 63 | MODEL_JSONs = get_file_names(MODEL_FOLDER, ".json") 64 | 65 | X, Y = load_dataset() 66 | 67 | for MODEL_JSON in MODEL_JSONs: 68 | 69 | MODEL_H5 = "policy_net_weights_" + MODEL_JSON[len("policy_net_model_"):-len(".json")] + ".h5" 70 | print MODEL_JSON, MODEL_H5 71 | 72 | cor, uncor = 0, 0 73 | 74 | model = model_from_json(open(MODEL_FOLDER + MODEL_JSON).read()) 75 | model.load_weights(MODEL_FOLDER + MODEL_H5) 76 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 77 | model.compile(loss="categorical_crossentropy", optimizer=sgd) 78 | before = time.time() 79 | pResult = model.predict(X) 80 | after = time.time() 81 | print "걸린 시간", after-before 82 | for i in range(0, len(pResult)): 83 | pred = pResult[i].argmax() 84 | real = Y[i].argmax() 85 | if pred == real: 86 | cor += 1 87 | else: 88 | uncor += 1 89 | ACC = 100.0 * cor / len(Y) 90 | print "맞은 개수:", cor 91 | print "틀린 개수:", uncor 92 | print "정확도: %.5f" % ACC 93 | -------------------------------------------------------------------------------- /train/2nd-value_net.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from keras.models import Sequential 4 | from keras.layers.core import Activation, Dense, Flatten 5 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 6 | from keras.optimizers import SGD 7 | from keras.callbacks import Callback 8 | from keras import backend as K 9 | 10 | from keras.models import model_from_json 11 | from utils.base import * 12 | 13 | lrate = 0.003 14 | k = 192 15 | bsize = 64 16 | epoch = 500 17 | 18 | SAVE_FOLDER_NAME = "20160531" 19 | MODEL_FILE_JSON = "RL_value_models/value_net_model.json" 20 | MODEL_FILE_H5 = "RL_value_models/value_net_weights_6.h5" 21 | POSTFIX = ".csv" 22 | OTHER_TRAIN_FOLDER = "RL_value_features_2nd/" 23 | 24 | def hidden_layers(m, k): 25 | m.add(ZeroPadding2D(padding=(2, 2))) 26 | m.add(Convolution2D(k, 3, 3)) 27 | m.add(Activation('relu')) 28 | 29 | return m 30 | 31 | def load_dataset(): 32 | FOLDER = list() 33 | FOLDER.append(OTHER_TRAIN_FOLDER) 34 | xs, ys = list(), list() 35 | for folder in FOLDER: 36 | file_names = get_file_names(folder, POSTFIX) 37 | for name in file_names: 38 | datas = list() 39 | cur_rows = list() 40 | csvfile = open(folder+name) 41 | row_reader = csv.reader(csvfile) 42 | if name[-5] == 'x': 43 | for row in row_reader: 44 | if len(row) == 0: 45 | datas.append(cur_rows) 46 | cur_rows = list() 47 | continue 48 | c = list() 49 | for a in row: 50 | c.append(a) 51 | cur_rows.append(c) 52 | xs.append(datas) 53 | datas = list() 54 | fname = name[:len(name)-5]+"y.csv" 55 | f = open(folder + fname) 56 | y_label = f.read() 57 | if y_label == "0": 58 | ys.append(-1) 59 | else: 60 | ys.append(1) 61 | xs = np.asarray(xs, dtype=np.float) 62 | ys = np.asarray(ys, dtype=np.float) 63 | print "xs", xs.shape, "ys", ys.shape 64 | return xs, ys 65 | 66 | class save_callback(Callback): 67 | 68 | def on_epoch_end(self, epoch, logs={}): 69 | self.model.save_weights(SAVE_FOLDER_NAME+"/value_net_weights_"+str(epoch)+".h5") 70 | 71 | if __name__ == "__main__": 72 | 73 | 74 | print "load dataset.." 75 | train_x, train_y = load_dataset() 76 | print "..finish" 77 | 78 | print "load model.." 79 | model = model_from_json(open(MODEL_FILE_JSON).read()) 80 | model.load_weights(MODEL_FILE_H5) 81 | print "..finish" 82 | 83 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 84 | model.compile(loss='MSE', optimizer=sgd, metrics=["accuracy"]) 85 | stop = save_callback() 86 | model.fit(train_x, train_y, batch_size=bsize, nb_epoch=epoch, callbacks=[stop]) 87 | -------------------------------------------------------------------------------- /train/RL_policy_net.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from keras.models import Sequential 4 | from keras.layers.core import Activation, Dense, Flatten 5 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 6 | from keras.optimizers import SGD 7 | from keras.callbacks import Callback 8 | from keras import backend as K 9 | from keras.models import model_from_json 10 | 11 | from utils.base import * 12 | 13 | k = 32 14 | bsize = 32 15 | epoch = 125 16 | lrate = 0.1 17 | 18 | POSTFIX = ".csv" 19 | def load_dataset(): 20 | xs, ys = list(), list() 21 | file_names = get_file_names(TRAIN_FOLDER, POSTFIX) 22 | for name in file_names: 23 | datas = list() 24 | cur_rows = list() 25 | csvfile = open(TRAIN_FOLDER+"/"+name) 26 | row_reader = csv.reader(csvfile) 27 | if name[-5] == 'x': 28 | for row in row_reader: 29 | if len(row) == 0: 30 | datas.append(cur_rows) 31 | cur_rows = list() 32 | continue 33 | c = list() 34 | for a in row: 35 | c.append(a) 36 | cur_rows.append(c) 37 | xs.append(datas) 38 | datas = list() 39 | fname = name[:len(name)-5]+"y.csv" 40 | f = open(TRAIN_FOLDER +"/"+ fname) 41 | rreader = csv.reader(f) 42 | for row in rreader: 43 | for a in row: 44 | datas.append(a) 45 | ys.append(datas) 46 | xs = np.asarray(xs, dtype=np.float) 47 | ys = np.asarray(ys, dtype=np.float) 48 | print "xs", xs.shape, "ys", ys.shape 49 | return xs, ys 50 | 51 | def RL_policy_loss(y_true, y_pred): 52 | ''' y_true: 이겼을 때 1, 졌을 때 -1, bsize: 게임 횟수 * 착수 횟수 ''' 53 | #return K.categorical_crossentropy(y_pred * K.sum(y_true) * bsize , K.abs(y_true)) / 32 54 | #return K.sum(K.log(K.max(y_pred, axis=0)) * K.sum(y_true, axis=0), axis=-1) / 32.0 55 | return K.categorical_crossentropy(y_pred, y_true) / 32.0 * float(bsize) 56 | 57 | if __name__ == "__main__": 58 | 59 | SAVE_FOLDER_NAME = "opp_pool/" 60 | 61 | SAVE_DATA_FOLER = sys.argv[1] 62 | TRAIN_FOLDER = sys.argv[2] 63 | RLMODEL = sys.argv[3] 64 | 65 | prefix_fname = "rlpolicy_model_" 66 | MODEL_FILE_JSON = RLMODEL+".json" 67 | MODEL_FILE_H5 = RLMODEL+".h5" 68 | RLMODEL = prefix_fname + str((int(RLMODEL[len(prefix_fname):])+1)) 69 | 70 | print "load dataset.." 71 | train_x, train_y = load_dataset() 72 | print "..finish" 73 | 74 | bsize = len(train_y) 75 | 76 | print "load model.." 77 | model = model_from_json(open(SAVE_FOLDER_NAME+MODEL_FILE_JSON).read()) 78 | model.load_weights(SAVE_FOLDER_NAME+MODEL_FILE_H5) 79 | print "..finish" 80 | 81 | json_string = model.to_json() 82 | open(SAVE_FOLDER_NAME+RLMODEL+".json", "w").write(json_string) 83 | 84 | bsize = len(train_y) 85 | 86 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 87 | model.compile(loss="categorical_crossentropy", optimizer=sgd, metrics=['accuracy']) 88 | hist = model.fit(train_x, train_y, batch_size=bsize, nb_epoch=epoch) 89 | model.save_weights(SAVE_FOLDER_NAME+RLMODEL+".h5") 90 | -------------------------------------------------------------------------------- /train/SL_policy_net_load.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from keras.models import Sequential 4 | from keras.layers.core import Activation, Dense, Flatten 5 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 6 | from keras.optimizers import SGD 7 | from keras.callbacks import Callback 8 | from keras import backend as K 9 | from keras.models import model_from_json 10 | 11 | from utils.base import * 12 | import numpy as np 13 | 14 | k = 32 15 | bsize = 16 16 | epoch = 100 17 | lrate = 0.05 18 | 19 | POSTFIX = "_x.csv" 20 | TRAIN_DATA_FOLDER = "20160608/datasets/phase01/" 21 | SAVE_MODEL_FOLDER = "20160608/h08" 22 | 23 | def hidden_layers(m, k): 24 | m.add(ZeroPadding2D(padding=(1, 1))) 25 | m.add(Convolution2D(k, 3, 3)) 26 | m.add(Activation('relu')) 27 | 28 | return m 29 | 30 | def load_dataset(): 31 | xs, ys = list(), list() 32 | file_names = get_file_names(TRAIN_DATA_FOLDER, POSTFIX) 33 | n_idx = 0 34 | for name in file_names: 35 | datas = list() 36 | cur_rows = list() 37 | csvfile = open(TRAIN_DATA_FOLDER+name) 38 | row_reader = csv.reader(csvfile) 39 | for row in row_reader: 40 | if len(row) == 0: 41 | datas.append(cur_rows) 42 | cur_rows = list() 43 | continue 44 | c = list() 45 | for a in row: 46 | c.append(a) 47 | cur_rows.append(c) 48 | xs.append(datas) 49 | datas = list() 50 | fname = name[:len(name)-5]+"y.csv" 51 | f = open(TRAIN_DATA_FOLDER + fname) 52 | rreader = csv.reader(f) 53 | for row in rreader: 54 | for a in row: 55 | datas.append(float(a)) 56 | ys.append(datas) 57 | n_idx += 1 58 | if n_idx % 100 == 0: 59 | print n_idx 60 | 61 | xs = np.asarray(xs, dtype=np.float) 62 | ys = np.asarray(ys, dtype=np.float) 63 | print "xs", xs.shape, "ys", ys.shape 64 | return xs, ys 65 | 66 | 67 | class save_callback(Callback): 68 | 69 | def on_epoch_end(self, epoch, logs={}): 70 | self.model.save_weights(SAVE_MODEL_FOLDER+"/"+SAVE_FILE+str(epoch)+".h5") 71 | self.model.optimizer.lr = float(open(SAVE_MODEL_FOLDER+"/lr.txt").read()) 72 | 73 | if __name__ == "__main__": 74 | 75 | MODEL_FILE_JSON = "20160608/h08/policy_net_model_h08.json" 76 | MODEL_FILE_H5 = "20160608/h08/policy_net_weights_h08_86.h5" 77 | SAVE_FILE = "policy_net_weights_h08_2_" 78 | # 79 | # if not os.path.exists(SAVE_MODEL_FOLDER): 80 | # os.mkdir(SAVE_MODEL_FOLDER) 81 | # 82 | print "load dataset.." 83 | train_x, train_y = load_dataset() 84 | print "..finish" 85 | 86 | print "make model.." 87 | 88 | model = model_from_json(open(MODEL_FILE_JSON).read()) 89 | model.load_weights(MODEL_FILE_H5) 90 | 91 | print "..finish" 92 | 93 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 94 | model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=["accuracy"]) 95 | stop = save_callback() 96 | model.fit(train_x, train_y, batch_size=bsize, nb_epoch=epoch, callbacks=[stop], verbose=1) 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /train/1st-value_net.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from keras.models import Sequential 4 | from keras.layers.core import Activation, Dense, Flatten 5 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 6 | from keras.optimizers import SGD 7 | from keras.callbacks import Callback 8 | from keras import backend as K 9 | 10 | from utils.base import * 11 | 12 | lrate = 0.003 13 | k = 192 14 | bsize = 64 15 | epoch = 500 16 | 17 | SAVE_FOLDER_NAME = "20160531" 18 | 19 | POSTFIX = ".csv" 20 | OTHER_TRAIN_FOLDER = "RL_value_features_2nd/" 21 | 22 | def hidden_layers(m, k): 23 | m.add(ZeroPadding2D(padding=(2, 2))) 24 | m.add(Convolution2D(k, 3, 3)) 25 | m.add(Activation('relu')) 26 | 27 | return m 28 | 29 | def load_dataset(): 30 | FOLDER = list() 31 | FOLDER.append(OTHER_TRAIN_FOLDER) 32 | xs, ys = list(), list() 33 | for folder in FOLDER: 34 | file_names = get_file_names(folder, POSTFIX) 35 | for name in file_names: 36 | datas = list() 37 | cur_rows = list() 38 | csvfile = open(folder+name) 39 | row_reader = csv.reader(csvfile) 40 | if name[-5] == 'x': 41 | for row in row_reader: 42 | if len(row) == 0: 43 | datas.append(cur_rows) 44 | cur_rows = list() 45 | continue 46 | c = list() 47 | for a in row: 48 | c.append(a) 49 | cur_rows.append(c) 50 | xs.append(datas) 51 | datas = list() 52 | fname = name[:len(name)-5]+"y.csv" 53 | f = open(folder + fname) 54 | y_label = f.read() 55 | if y_label == "0": 56 | ys.append(-1) 57 | else: 58 | ys.append(1) 59 | xs = np.asarray(xs, dtype=np.float) 60 | ys = np.asarray(ys, dtype=np.float) 61 | print "xs", xs.shape, "ys", ys.shape 62 | return xs, ys 63 | 64 | class save_callback(Callback): 65 | 66 | def on_epoch_end(self, epoch, logs={}): 67 | self.model.save_weights(SAVE_FOLDER_NAME+"/value_net_weights_"+str(epoch)+".h5") 68 | 69 | if __name__ == "__main__": 70 | 71 | 72 | print "load dataset.." 73 | train_x, train_y = load_dataset() 74 | print "..finish" 75 | 76 | print "make model.." 77 | model = Sequential() 78 | 79 | model.add(ZeroPadding2D(padding=(1, 1), input_shape=(39, 9, 9))) 80 | model.add(Convolution2D(k, 5, 5)) 81 | model.add(Activation('relu')) 82 | 83 | for i in range(0, 4): 84 | model = hidden_layers(model, k) 85 | 86 | model.add(ZeroPadding2D(padding=(1, 1))) 87 | model.add(Convolution2D(1, 1, 1)) 88 | model.add(Activation('relu')) 89 | 90 | model.add(Flatten()) 91 | model.add(Dense(1)) 92 | model.add(Activation('tanh')) 93 | 94 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 95 | model.compile(loss='MSE', optimizer=sgd, metrics=["accuracy"]) 96 | json_string = model.to_json() 97 | open(SAVE_FOLDER_NAME+"/value_net_model.json", "w").write(json_string) 98 | stop = save_callback() 99 | model.fit(train_x, train_y, batch_size=bsize, nb_epoch=epoch, callbacks=[stop]) 100 | -------------------------------------------------------------------------------- /train/SL_policy_net_h03.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from keras.models import Sequential 4 | from keras.layers.core import Activation, Dense, Flatten 5 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 6 | from keras.optimizers import SGD 7 | from keras.callbacks import Callback 8 | from keras import backend as K 9 | from keras.models import model_from_json 10 | 11 | from utils.base import * 12 | 13 | import numpy as np 14 | from keras.datasets import mnist 15 | 16 | k = 32 17 | bsize = 16 18 | epoch = 500 19 | lrate = 0.003 20 | 21 | SAVE_MODEL_FOLDER = "20160715/h03" 22 | 23 | POSTFIX = "_x.csv" 24 | TRAIN_DATA_FOLDER = "20160715/dataset/" 25 | 26 | 27 | def load_dataset(): 28 | xs, ys = list(), list() 29 | file_names = get_file_names(TRAIN_DATA_FOLDER, POSTFIX) 30 | n_idx = 0 31 | for name in file_names: 32 | datas = list() 33 | cur_rows = list() 34 | csvfile = open(TRAIN_DATA_FOLDER+name) 35 | row_reader = csv.reader(csvfile) 36 | for row in row_reader: 37 | if len(row) == 0: 38 | datas.append(cur_rows) 39 | cur_rows = list() 40 | continue 41 | c = list() 42 | for a in row: 43 | c.append(a) 44 | cur_rows.append(c) 45 | xs.append(datas) 46 | datas = list() 47 | fname = name[:len(name)-5]+"y.csv" 48 | f = open(TRAIN_DATA_FOLDER + fname) 49 | rreader = csv.reader(f) 50 | for row in rreader: 51 | for a in row: 52 | datas.append(float(a)) 53 | ys.append(datas) 54 | n_idx += 1 55 | if n_idx % 10000 == 0: 56 | print n_idx 57 | 58 | xs = np.asarray(xs, dtype=np.float) 59 | ys = np.asarray(ys, dtype=np.float) 60 | print "xs", xs.shape, "ys", ys.shape 61 | return xs, ys 62 | 63 | 64 | class save_callback(Callback): 65 | 66 | def on_epoch_end(self, epoch, logs={}): 67 | self.model.save_weights(SAVE_MODEL_FOLDER+"/policy_net_weights_h03_"+str(epoch)+".h5") 68 | # self.model.optimizer.lr = float(open(SAVE_MODEL_FOLDER+"/lr.txt").read()) 69 | 70 | 71 | if __name__ == "__main__": 72 | 73 | if not os.path.exists(SAVE_MODEL_FOLDER): 74 | os.mkdir(SAVE_MODEL_FOLDER) 75 | 76 | print "load dataset.." 77 | train_x, train_y = load_dataset() 78 | print "..finish" 79 | 80 | print "make model.." 81 | model = Sequential() 82 | 83 | model.add(ZeroPadding2D(padding=(1, 1), input_shape=(38, 9, 9))) 84 | model.add(Convolution2D(k, 3, 3)) 85 | model.add(Activation('relu')) 86 | 87 | for i in range(0, 3): 88 | model.add(ZeroPadding2D(padding=(1, 1))) 89 | model.add(Convolution2D(k, 1, 1)) 90 | model.add(Activation('relu')) 91 | 92 | model.add(Flatten()) 93 | model.add(Dense(81)) 94 | model.add(Activation('softmax')) 95 | 96 | print "..finish" 97 | 98 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 99 | model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=["accuracy"]) 100 | json_string = model.to_json() 101 | open(SAVE_MODEL_FOLDER+"/policy_net_model_h03.json", "w").write(json_string) 102 | stop = save_callback() 103 | model.fit(train_x, train_y, batch_size=bsize, nb_epoch=epoch, callbacks=[stop], verbose=1) 104 | 105 | 106 | 107 | -------------------------------------------------------------------------------- /train/rollout_policy.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from utils.base import * 4 | 5 | from keras.models import Sequential 6 | from keras.layers.core import Activation, Dense, Flatten 7 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 8 | from keras.optimizers import SGD 9 | from keras.callbacks import Callback 10 | from keras import backend as K 11 | 12 | k = 32 13 | bsize = 16 14 | epoch = 350 15 | lrate = 0.003 16 | 17 | SAVE_FOLDER_NAME = "20160511_rollout" 18 | 19 | POSTFIX = ".csv" 20 | OTHER_TRAIN_FOLDER = "train_other_rollout/" 21 | OTHER_TEST_FOLDER = "test_other_rollout/" 22 | 23 | def hidden_layers(m, k): 24 | m.add(ZeroPadding2D(padding=(2, 2))) 25 | m.add(Convolution2D(k, 3, 3)) 26 | m.add(Activation('relu')) 27 | 28 | return m 29 | 30 | def load_dataset(): 31 | FOLDER = list() 32 | FOLDER.append(OTHER_TRAIN_FOLDER) 33 | xs, ys = list(), list() 34 | for folder in FOLDER: 35 | file_names = get_file_names(folder, POSTFIX) 36 | for name in file_names: 37 | datas = list() 38 | cur_rows = list() 39 | csvfile = open(folder+name) 40 | row_reader = csv.reader(csvfile) 41 | if name[-5] == 'x': 42 | for row in row_reader: 43 | if len(row) == 0: 44 | datas.append(cur_rows) 45 | cur_rows = list() 46 | continue 47 | c = list() 48 | for a in row: 49 | c.append(a) 50 | cur_rows.append(c) 51 | xs.append(datas) 52 | datas = list() 53 | fname = name[:len(name)-5]+"y.csv" 54 | f = open(folder + fname) 55 | rreader = csv.reader(f) 56 | for row in rreader: 57 | for a in row: 58 | datas.append(a) 59 | ys.append(datas) 60 | 61 | 62 | xs = np.asarray(xs, dtype=np.float) 63 | ys = np.asarray(ys, dtype=np.float) 64 | print "xs", xs.shape, "ys", ys.shape 65 | return xs, ys 66 | 67 | 68 | class save_callback(Callback): 69 | 70 | def on_epoch_end(self, epoch, logs={}): 71 | self.model.save_weights(SAVE_FOLDER_NAME+"/rollout_policy_net_weights_"+str(epoch)+".h5") 72 | 73 | if __name__ == "__main__": 74 | 75 | print "load dataset.." 76 | #train_x, train_y = load_dataset() 77 | print "..finish" 78 | 79 | print "make model.." 80 | model = Sequential() 81 | 82 | model.add(ZeroPadding2D(padding=(1, 1), input_shape=(38, 9, 9))) 83 | model.add(Convolution2D(k, 5, 5)) 84 | model.add(Activation('relu')) 85 | 86 | for i in range(0, 2): 87 | model = hidden_layers(model, k) 88 | 89 | model.add(ZeroPadding2D(padding=(1, 1))) 90 | model.add(Convolution2D(1, 1, 1)) 91 | model.add(Activation('relu')) 92 | 93 | model.add(Flatten()) 94 | model.add(Dense(81)) 95 | model.add(Activation('softmax')) 96 | 97 | print "..finish" 98 | 99 | json_string = model.to_json() 100 | open(SAVE_FOLDER_NAME+"/rollout_policy_net_model.json", "w").write(json_string) 101 | '''sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 102 | model.compile(loss='categorical_crossentropy', optimizer=sgd) 103 | stop = save_callback() 104 | model.fit(train_x, train_y, batch_size=bsize, nb_epoch=epoch, callbacks=[stop], show_accuracy=True, verbose=2)''' 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /train/SL_policy_net_h04.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from keras.models import Sequential 4 | from keras.layers.core import Activation, Dense, Flatten 5 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 6 | from keras.optimizers import SGD 7 | from keras.callbacks import Callback 8 | from keras import backend as K 9 | from keras.models import model_from_json 10 | 11 | from utils.base import * 12 | 13 | import numpy as np 14 | 15 | 16 | k = 32 17 | bsize = 16 18 | epoch = 500 19 | lrate = 0.03 20 | 21 | SAVE_MODEL_FOLDER = "20160608/h04" 22 | 23 | POSTFIX = "_x.csv" 24 | TRAIN_DATA_FOLDER = "20160608/datasets/phase01/" 25 | 26 | 27 | def hidden_layers(m, k): 28 | m.add(ZeroPadding2D(padding=(1, 1))) 29 | m.add(Convolution2D(k, 3, 3)) 30 | m.add(Activation('relu')) 31 | 32 | return m 33 | 34 | def load_dataset(): 35 | xs, ys = list(), list() 36 | file_names = get_file_names(TRAIN_DATA_FOLDER, POSTFIX) 37 | n_idx = 0 38 | for name in file_names: 39 | datas = list() 40 | cur_rows = list() 41 | csvfile = open(TRAIN_DATA_FOLDER+name) 42 | row_reader = csv.reader(csvfile) 43 | for row in row_reader: 44 | if len(row) == 0: 45 | datas.append(cur_rows) 46 | cur_rows = list() 47 | continue 48 | c = list() 49 | for a in row: 50 | c.append(a) 51 | cur_rows.append(c) 52 | xs.append(datas) 53 | datas = list() 54 | fname = name[:len(name)-5]+"y.csv" 55 | f = open(TRAIN_DATA_FOLDER + fname) 56 | rreader = csv.reader(f) 57 | for row in rreader: 58 | for a in row: 59 | datas.append(float(a)) 60 | ys.append(datas) 61 | n_idx += 1 62 | if n_idx % 100 == 0: 63 | print n_idx 64 | 65 | xs = np.asarray(xs, dtype=np.float) 66 | ys = np.asarray(ys, dtype=np.float) 67 | print "xs", xs.shape, "ys", ys.shape 68 | return xs, ys 69 | 70 | 71 | class save_callback(Callback): 72 | 73 | def on_epoch_end(self, epoch, logs={}): 74 | self.model.save_weights(SAVE_MODEL_FOLDER+"/policy_net_weights_h04_"+str(epoch)+".h5") 75 | self.model.optimizer.lr = float(open(SAVE_MODEL_FOLDER+"/lr.txt").read()) 76 | 77 | if __name__ == "__main__": 78 | 79 | if not os.path.exists(SAVE_MODEL_FOLDER): 80 | os.mkdir(SAVE_MODEL_FOLDER) 81 | 82 | print "load dataset.." 83 | train_x, train_y = load_dataset() 84 | print "..finish" 85 | 86 | print "make model.." 87 | model = Sequential() 88 | 89 | model.add(ZeroPadding2D(padding=(1, 1), input_shape=(38, 9, 9))) 90 | model.add(Convolution2D(k, 5, 5)) 91 | model.add(Activation('relu')) 92 | 93 | for i in range(0, 4): 94 | model = hidden_layers(model, k) 95 | 96 | model.add(ZeroPadding2D(padding=(1, 1))) 97 | model.add(Convolution2D(1, 1, 1)) 98 | model.add(Activation('relu')) 99 | 100 | model.add(Flatten()) 101 | model.add(Activation('softmax')) 102 | 103 | print "..finish" 104 | 105 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 106 | model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=["accuracy"]) 107 | json_string = model.to_json() 108 | open(SAVE_MODEL_FOLDER+"/policy_net_model_h04.json", "w").write(json_string) 109 | stop = save_callback() 110 | model.fit(train_x, train_y, batch_size=bsize, nb_epoch=epoch, callbacks=[stop], verbose=1) 111 | 112 | 113 | 114 | -------------------------------------------------------------------------------- /train/SL_policy_net_h05.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from keras.models import Sequential 4 | from keras.layers.core import Activation, Dense, Flatten 5 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 6 | from keras.optimizers import SGD 7 | from keras.callbacks import Callback 8 | from keras import backend as K 9 | from keras.models import model_from_json 10 | 11 | from utils.base import * 12 | 13 | import numpy as np 14 | 15 | 16 | k = 32 17 | bsize = 16 18 | epoch = 500 19 | lrate = 0.03 20 | 21 | SAVE_MODEL_FOLDER = "20160608/h05" 22 | 23 | POSTFIX = "_x.csv" 24 | TRAIN_DATA_FOLDER = "20160608/datasets/phase01/" 25 | 26 | 27 | def hidden_layers(m, k): 28 | m.add(ZeroPadding2D(padding=(1, 1))) 29 | m.add(Convolution2D(k, 3, 3)) 30 | m.add(Activation('relu')) 31 | 32 | return m 33 | 34 | def load_dataset(): 35 | xs, ys = list(), list() 36 | file_names = get_file_names(TRAIN_DATA_FOLDER, POSTFIX) 37 | n_idx = 0 38 | for name in file_names: 39 | datas = list() 40 | cur_rows = list() 41 | csvfile = open(TRAIN_DATA_FOLDER+name) 42 | row_reader = csv.reader(csvfile) 43 | for row in row_reader: 44 | if len(row) == 0: 45 | datas.append(cur_rows) 46 | cur_rows = list() 47 | continue 48 | c = list() 49 | for a in row: 50 | c.append(a) 51 | cur_rows.append(c) 52 | xs.append(datas) 53 | datas = list() 54 | fname = name[:len(name)-5]+"y.csv" 55 | f = open(TRAIN_DATA_FOLDER + fname) 56 | rreader = csv.reader(f) 57 | for row in rreader: 58 | for a in row: 59 | datas.append(float(a)) 60 | ys.append(datas) 61 | n_idx += 1 62 | if n_idx % 100 == 0: 63 | print n_idx 64 | 65 | xs = np.asarray(xs, dtype=np.float) 66 | ys = np.asarray(ys, dtype=np.float) 67 | print "xs", xs.shape, "ys", ys.shape 68 | return xs, ys 69 | 70 | 71 | class save_callback(Callback): 72 | 73 | def on_epoch_end(self, epoch, logs={}): 74 | self.model.save_weights(SAVE_MODEL_FOLDER+"/policy_net_weights_h05_"+str(epoch)+".h5") 75 | self.model.optimizer.lr = float(open(SAVE_MODEL_FOLDER+"/lr.txt").read()) 76 | 77 | if __name__ == "__main__": 78 | 79 | if not os.path.exists(SAVE_MODEL_FOLDER): 80 | os.mkdir(SAVE_MODEL_FOLDER) 81 | 82 | print "load dataset.." 83 | train_x, train_y = load_dataset() 84 | print "..finish" 85 | 86 | print "make model.." 87 | model = Sequential() 88 | 89 | model.add(ZeroPadding2D(padding=(1, 1), input_shape=(38, 9, 9))) 90 | model.add(Convolution2D(k, 5, 5)) 91 | model.add(Activation('relu')) 92 | 93 | for i in range(0, 5): 94 | model = hidden_layers(model, k) 95 | 96 | model.add(ZeroPadding2D(padding=(1, 1))) 97 | model.add(Convolution2D(1, 1, 1)) 98 | model.add(Activation('relu')) 99 | 100 | model.add(Flatten()) 101 | model.add(Activation('softmax')) 102 | 103 | print "..finish" 104 | 105 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 106 | model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=["accuracy"]) 107 | json_string = model.to_json() 108 | open(SAVE_MODEL_FOLDER+"/policy_net_model_h05.json", "w").write(json_string) 109 | stop = save_callback() 110 | model.fit(train_x, train_y, batch_size=bsize, nb_epoch=epoch, callbacks=[stop], verbose=1) 111 | 112 | 113 | 114 | -------------------------------------------------------------------------------- /train/SL_policy_net_h06.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from keras.models import Sequential 4 | from keras.layers.core import Activation, Dense, Flatten 5 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 6 | from keras.optimizers import SGD 7 | from keras.callbacks import Callback 8 | from keras import backend as K 9 | from keras.models import model_from_json 10 | 11 | from utils.base import * 12 | 13 | import numpy as np 14 | 15 | 16 | k = 32 17 | bsize = 16 18 | epoch = 500 19 | lrate = 0.03 20 | 21 | SAVE_MODEL_FOLDER = "20160608/h06" 22 | 23 | POSTFIX = "_x.csv" 24 | TRAIN_DATA_FOLDER = "20160608/datasets/phase01/" 25 | 26 | 27 | def hidden_layers(m, k): 28 | m.add(ZeroPadding2D(padding=(1, 1))) 29 | m.add(Convolution2D(k, 3, 3)) 30 | m.add(Activation('relu')) 31 | 32 | return m 33 | 34 | def load_dataset(): 35 | xs, ys = list(), list() 36 | file_names = get_file_names(TRAIN_DATA_FOLDER, POSTFIX) 37 | n_idx = 0 38 | for name in file_names: 39 | datas = list() 40 | cur_rows = list() 41 | csvfile = open(TRAIN_DATA_FOLDER+name) 42 | row_reader = csv.reader(csvfile) 43 | for row in row_reader: 44 | if len(row) == 0: 45 | datas.append(cur_rows) 46 | cur_rows = list() 47 | continue 48 | c = list() 49 | for a in row: 50 | c.append(a) 51 | cur_rows.append(c) 52 | xs.append(datas) 53 | datas = list() 54 | fname = name[:len(name)-5]+"y.csv" 55 | f = open(TRAIN_DATA_FOLDER + fname) 56 | rreader = csv.reader(f) 57 | for row in rreader: 58 | for a in row: 59 | datas.append(float(a)) 60 | ys.append(datas) 61 | n_idx += 1 62 | if n_idx % 100 == 0: 63 | print n_idx 64 | 65 | xs = np.asarray(xs, dtype=np.float) 66 | ys = np.asarray(ys, dtype=np.float) 67 | print "xs", xs.shape, "ys", ys.shape 68 | return xs, ys 69 | 70 | 71 | class save_callback(Callback): 72 | 73 | def on_epoch_end(self, epoch, logs={}): 74 | self.model.save_weights(SAVE_MODEL_FOLDER+"/policy_net_weights_h06_"+str(epoch)+".h5") 75 | self.model.optimizer.lr = float(open(SAVE_MODEL_FOLDER+"/lr.txt").read()) 76 | 77 | if __name__ == "__main__": 78 | 79 | if not os.path.exists(SAVE_MODEL_FOLDER): 80 | os.mkdir(SAVE_MODEL_FOLDER) 81 | 82 | print "load dataset.." 83 | train_x, train_y = load_dataset() 84 | print "..finish" 85 | 86 | print "make model.." 87 | model = Sequential() 88 | 89 | model.add(ZeroPadding2D(padding=(1, 1), input_shape=(38, 9, 9))) 90 | model.add(Convolution2D(k, 5, 5)) 91 | model.add(Activation('relu')) 92 | 93 | for i in range(0, 6): 94 | model = hidden_layers(model, k) 95 | 96 | model.add(ZeroPadding2D(padding=(1, 1))) 97 | model.add(Convolution2D(1, 1, 1)) 98 | model.add(Activation('relu')) 99 | 100 | model.add(Flatten()) 101 | model.add(Activation('softmax')) 102 | 103 | print "..finish" 104 | 105 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 106 | model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=["accuracy"]) 107 | json_string = model.to_json() 108 | open(SAVE_MODEL_FOLDER+"/policy_net_model_h06.json", "w").write(json_string) 109 | stop = save_callback() 110 | model.fit(train_x, train_y, batch_size=bsize, nb_epoch=epoch, callbacks=[stop], verbose=1) 111 | 112 | 113 | 114 | -------------------------------------------------------------------------------- /train/SL_policy_net_h07.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from keras.models import Sequential 4 | from keras.layers.core import Activation, Dense, Flatten 5 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 6 | from keras.optimizers import SGD 7 | from keras.callbacks import Callback 8 | from keras import backend as K 9 | from keras.models import model_from_json 10 | 11 | from utils.base import * 12 | 13 | import numpy as np 14 | 15 | 16 | k = 32 17 | bsize = 16 18 | epoch = 500 19 | lrate = 0.03 20 | 21 | SAVE_MODEL_FOLDER = "20160608/h07" 22 | 23 | POSTFIX = "_x.csv" 24 | TRAIN_DATA_FOLDER = "20160608/datasets/phase01/" 25 | 26 | 27 | def hidden_layers(m, k): 28 | m.add(ZeroPadding2D(padding=(1, 1))) 29 | m.add(Convolution2D(k, 3, 3)) 30 | m.add(Activation('relu')) 31 | 32 | return m 33 | 34 | def load_dataset(): 35 | xs, ys = list(), list() 36 | file_names = get_file_names(TRAIN_DATA_FOLDER, POSTFIX) 37 | n_idx = 0 38 | for name in file_names: 39 | datas = list() 40 | cur_rows = list() 41 | csvfile = open(TRAIN_DATA_FOLDER+name) 42 | row_reader = csv.reader(csvfile) 43 | for row in row_reader: 44 | if len(row) == 0: 45 | datas.append(cur_rows) 46 | cur_rows = list() 47 | continue 48 | c = list() 49 | for a in row: 50 | c.append(a) 51 | cur_rows.append(c) 52 | xs.append(datas) 53 | datas = list() 54 | fname = name[:len(name)-5]+"y.csv" 55 | f = open(TRAIN_DATA_FOLDER + fname) 56 | rreader = csv.reader(f) 57 | for row in rreader: 58 | for a in row: 59 | datas.append(float(a)) 60 | ys.append(datas) 61 | n_idx += 1 62 | if n_idx % 100 == 0: 63 | print n_idx 64 | 65 | xs = np.asarray(xs, dtype=np.float) 66 | ys = np.asarray(ys, dtype=np.float) 67 | print "xs", xs.shape, "ys", ys.shape 68 | return xs, ys 69 | 70 | 71 | class save_callback(Callback): 72 | 73 | def on_epoch_end(self, epoch, logs={}): 74 | self.model.save_weights(SAVE_MODEL_FOLDER+"/policy_net_weights_h07_"+str(epoch)+".h5") 75 | self.model.optimizer.lr = float(open(SAVE_MODEL_FOLDER+"/lr.txt").read()) 76 | 77 | if __name__ == "__main__": 78 | 79 | if not os.path.exists(SAVE_MODEL_FOLDER): 80 | os.mkdir(SAVE_MODEL_FOLDER) 81 | 82 | print "load dataset.." 83 | train_x, train_y = load_dataset() 84 | print "..finish" 85 | 86 | print "make model.." 87 | model = Sequential() 88 | 89 | model.add(ZeroPadding2D(padding=(1, 1), input_shape=(38, 9, 9))) 90 | model.add(Convolution2D(k, 5, 5)) 91 | model.add(Activation('relu')) 92 | 93 | for i in range(0, 7): 94 | model = hidden_layers(model, k) 95 | 96 | model.add(ZeroPadding2D(padding=(1, 1))) 97 | model.add(Convolution2D(1, 1, 1)) 98 | model.add(Activation('relu')) 99 | 100 | model.add(Flatten()) 101 | model.add(Activation('softmax')) 102 | 103 | print "..finish" 104 | 105 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 106 | model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=["accuracy"]) 107 | json_string = model.to_json() 108 | open(SAVE_MODEL_FOLDER+"/policy_net_model_h07.json", "w").write(json_string) 109 | stop = save_callback() 110 | model.fit(train_x, train_y, batch_size=bsize, nb_epoch=epoch, callbacks=[stop], verbose=1) 111 | 112 | 113 | 114 | -------------------------------------------------------------------------------- /train/SL_policy_net_h12.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from keras.models import Sequential 4 | from keras.layers.core import Activation, Dense, Flatten 5 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 6 | from keras.optimizers import SGD 7 | from keras.callbacks import Callback 8 | from keras import backend as K 9 | from keras.models import model_from_json 10 | 11 | from utils.base import * 12 | 13 | import numpy as np 14 | 15 | 16 | k = 32 17 | bsize = 16 18 | epoch = 500 19 | lrate = 0.03 20 | 21 | SAVE_MODEL_FOLDER = "20160608/h12" 22 | 23 | POSTFIX = "_x.csv" 24 | TRAIN_DATA_FOLDER = "20160608/datasets/phase01/" 25 | 26 | 27 | def hidden_layers(m, k): 28 | m.add(ZeroPadding2D(padding=(1, 1))) 29 | m.add(Convolution2D(k, 3, 3)) 30 | m.add(Activation('relu')) 31 | 32 | return m 33 | 34 | def load_dataset(): 35 | xs, ys = list(), list() 36 | file_names = get_file_names(TRAIN_DATA_FOLDER, POSTFIX) 37 | n_idx = 0 38 | for name in file_names: 39 | datas = list() 40 | cur_rows = list() 41 | csvfile = open(TRAIN_DATA_FOLDER+name) 42 | row_reader = csv.reader(csvfile) 43 | for row in row_reader: 44 | if len(row) == 0: 45 | datas.append(cur_rows) 46 | cur_rows = list() 47 | continue 48 | c = list() 49 | for a in row: 50 | c.append(a) 51 | cur_rows.append(c) 52 | xs.append(datas) 53 | datas = list() 54 | fname = name[:len(name)-5]+"y.csv" 55 | f = open(TRAIN_DATA_FOLDER + fname) 56 | rreader = csv.reader(f) 57 | for row in rreader: 58 | for a in row: 59 | datas.append(float(a)) 60 | ys.append(datas) 61 | n_idx += 1 62 | if n_idx % 100 == 0: 63 | print n_idx 64 | 65 | xs = np.asarray(xs, dtype=np.float) 66 | ys = np.asarray(ys, dtype=np.float) 67 | print "xs", xs.shape, "ys", ys.shape 68 | return xs, ys 69 | 70 | 71 | class save_callback(Callback): 72 | 73 | def on_epoch_end(self, epoch, logs={}): 74 | self.model.save_weights(SAVE_MODEL_FOLDER+"/policy_net_weights_h12_"+str(epoch)+".h5") 75 | self.model.optimizer.lr = float(open(SAVE_MODEL_FOLDER+"/lr.txt").read()) 76 | 77 | if __name__ == "__main__": 78 | 79 | if not os.path.exists(SAVE_MODEL_FOLDER): 80 | os.mkdir(SAVE_MODEL_FOLDER) 81 | 82 | print "load dataset.." 83 | train_x, train_y = load_dataset() 84 | print "..finish" 85 | 86 | print "make model.." 87 | model = Sequential() 88 | 89 | model.add(ZeroPadding2D(padding=(1, 1), input_shape=(38, 9, 9))) 90 | model.add(Convolution2D(k, 5, 5)) 91 | model.add(Activation('relu')) 92 | 93 | for i in range(0, 12): 94 | model = hidden_layers(model, k) 95 | 96 | model.add(ZeroPadding2D(padding=(1, 1))) 97 | model.add(Convolution2D(1, 1, 1)) 98 | model.add(Activation('relu')) 99 | 100 | model.add(Flatten()) 101 | model.add(Activation('softmax')) 102 | 103 | print "..finish" 104 | 105 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 106 | model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=["accuracy"]) 107 | json_string = model.to_json() 108 | open(SAVE_MODEL_FOLDER+"/policy_net_model_h12.json", "w").write(json_string) 109 | stop = save_callback() 110 | model.fit(train_x, train_y, batch_size=bsize, nb_epoch=epoch, callbacks=[stop], verbose=1) 111 | 112 | 113 | 114 | -------------------------------------------------------------------------------- /train/SL_policy_net_h02.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from keras.models import Sequential 4 | from keras.layers.core import Activation, Dense, Flatten 5 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 6 | from keras.optimizers import SGD 7 | from keras.callbacks import Callback 8 | from keras import backend as K 9 | from keras.models import model_from_json 10 | 11 | from utils.base import * 12 | 13 | import numpy as np 14 | 15 | 16 | k = 32 17 | bsize = 16 18 | epoch = 500 19 | lrate = 0.03 20 | 21 | SAVE_MODEL_FOLDER = "20160608/h02" 22 | 23 | POSTFIX = "_x.csv" 24 | TRAIN_DATA_FOLDER = "20160608/datasets/phase01/" 25 | 26 | 27 | def hidden_layers(m, k): 28 | m.add(ZeroPadding2D(padding=(1, 1))) 29 | m.add(Convolution2D(k, 3, 3)) 30 | m.add(Activation('relu')) 31 | 32 | return m 33 | 34 | def load_dataset(): 35 | xs, ys = list(), list() 36 | file_names = get_file_names(TRAIN_DATA_FOLDER, POSTFIX) 37 | n_idx = 0 38 | for name in file_names: 39 | datas = list() 40 | cur_rows = list() 41 | csvfile = open(TRAIN_DATA_FOLDER+name) 42 | row_reader = csv.reader(csvfile) 43 | for row in row_reader: 44 | if len(row) == 0: 45 | datas.append(cur_rows) 46 | cur_rows = list() 47 | continue 48 | c = list() 49 | for a in row: 50 | c.append(a) 51 | cur_rows.append(c) 52 | xs.append(datas) 53 | datas = list() 54 | fname = name[:len(name)-5]+"y.csv" 55 | f = open(TRAIN_DATA_FOLDER + fname) 56 | rreader = csv.reader(f) 57 | for row in rreader: 58 | for a in row: 59 | datas.append(float(a)) 60 | ys.append(datas) 61 | n_idx += 1 62 | if n_idx % 100 == 0: 63 | print n_idx 64 | 65 | xs = np.asarray(xs, dtype=np.float) 66 | ys = np.asarray(ys, dtype=np.float) 67 | print "xs", xs.shape, "ys", ys.shape 68 | return xs, ys 69 | 70 | 71 | class save_callback(Callback): 72 | 73 | def on_epoch_end(self, epoch, logs={}): 74 | self.model.save_weights(SAVE_MODEL_FOLDER+"/policy_net_weights_h02_"+str(epoch)+".h5") 75 | self.model.optimizer.lr = float(open(SAVE_MODEL_FOLDER+"/lr.txt").read()) 76 | 77 | 78 | if __name__ == "__main__": 79 | 80 | 81 | if not os.path.exists(SAVE_MODEL_FOLDER): 82 | os.mkdir(SAVE_MODEL_FOLDER) 83 | 84 | print "load dataset.." 85 | train_x, train_y = load_dataset() 86 | print "..finish" 87 | 88 | print "make model.." 89 | model = Sequential() 90 | 91 | model.add(ZeroPadding2D(padding=(1, 1), input_shape=(38, 9, 9))) 92 | model.add(Convolution2D(k, 5, 5)) 93 | model.add(Activation('relu')) 94 | 95 | for i in range(0, 2): 96 | model = hidden_layers(model, k) 97 | 98 | model.add(ZeroPadding2D(padding=(1, 1))) 99 | model.add(Convolution2D(1, 1, 1)) 100 | model.add(Activation('relu')) 101 | 102 | model.add(Flatten()) 103 | model.add(Activation('softmax')) 104 | 105 | print "..finish" 106 | 107 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 108 | model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=["accuracy"]) 109 | json_string = model.to_json() 110 | open(SAVE_MODEL_FOLDER+"/policy_net_model_h02.json", "w").write(json_string) 111 | stop = save_callback() 112 | model.fit(train_x, train_y, batch_size=bsize, nb_epoch=epoch, callbacks=[stop], verbose=1) 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /play_DCL/gomill/sgf_moves.py: -------------------------------------------------------------------------------- 1 | """Higher-level processing of moves and positions from SGF games.""" 2 | 3 | from gomill import boards 4 | from gomill import sgf_properties 5 | 6 | 7 | def get_setup_and_moves(sgf_game, board=None): 8 | """Return the initial setup and the following moves from an Sgf_game. 9 | 10 | Returns a pair (board, plays) 11 | 12 | board -- boards.Board 13 | plays -- list of pairs (colour, move) 14 | moves are (row, col), or None for a pass. 15 | 16 | The board represents the position described by AB and/or AW properties 17 | in the root node. 18 | 19 | The moves are from the game's 'leftmost' variation. 20 | 21 | Raises ValueError if this position isn't legal. 22 | 23 | Raises ValueError if there are any AB/AW/AE properties after the root 24 | node. 25 | 26 | Doesn't check whether the moves are legal. 27 | 28 | If the optional 'board' parameter is provided, it must be an empty board of 29 | the right size; the same object will be returned. 30 | 31 | """ 32 | size = sgf_game.get_size() 33 | if board is None: 34 | board = boards.Board(size) 35 | else: 36 | if board.side != size: 37 | raise ValueError("wrong board size, must be %d" % size) 38 | if not board.is_empty(): 39 | raise ValueError("board not empty") 40 | root = sgf_game.get_root() 41 | nodes = sgf_game.main_sequence_iter() 42 | ab, aw, ae = root.get_setup_stones() 43 | if ab or aw: 44 | is_legal = board.apply_setup(ab, aw, ae) 45 | if not is_legal: 46 | raise ValueError("setup position not legal") 47 | colour, raw = root.get_raw_move() 48 | if colour is not None: 49 | raise ValueError("mixed setup and moves in root node") 50 | nodes.next() 51 | moves = [] 52 | for node in nodes: 53 | if node.has_setup_stones(): 54 | raise ValueError("setup properties after the root node") 55 | colour, raw = node.get_raw_move() 56 | if colour is not None: 57 | moves.append((colour, sgf_properties.interpret_go_point(raw, size))) 58 | return board, moves 59 | 60 | def set_initial_position(sgf_game, board): 61 | """Add setup stones to an Sgf_game reflecting a board position. 62 | 63 | sgf_game -- Sgf_game 64 | board -- boards.Board 65 | 66 | Replaces any existing setup stones in the Sgf_game's root node. 67 | 68 | """ 69 | stones = {'b' : set(), 'w' : set()} 70 | for (colour, point) in board.list_occupied_points(): 71 | stones[colour].add(point) 72 | sgf_game.get_root().set_setup_stones(stones['b'], stones['w']) 73 | 74 | def indicate_first_player(sgf_game): 75 | """Add a PL property to the root node if appropriate. 76 | 77 | Looks at the first child of the root to see who the first player is, and 78 | sets PL it isn't the expected player (ie, black normally, but white if 79 | there is a handicap), or if there are non-handicap setup stones. 80 | 81 | """ 82 | root = sgf_game.get_root() 83 | first_player, move = root[0].get_move() 84 | if first_player is None: 85 | return 86 | has_handicap = root.has_property("HA") 87 | if root.has_property("AW"): 88 | specify_pl = True 89 | elif root.has_property("AB") and not has_handicap: 90 | specify_pl = True 91 | elif not has_handicap and first_player == 'w': 92 | specify_pl = True 93 | elif has_handicap and first_player == 'b': 94 | specify_pl = True 95 | else: 96 | specify_pl = False 97 | if specify_pl: 98 | root.set('PL', first_player) 99 | 100 | -------------------------------------------------------------------------------- /play_DCL/gomill/compact_tracebacks.py: -------------------------------------------------------------------------------- 1 | """Compact formatting of tracebacks.""" 2 | 3 | import sys 4 | import traceback 5 | 6 | def log_traceback_from_info(exception_type, value, tb, dst=sys.stderr, skip=0): 7 | """Log a given exception nicely to 'dst', showing a traceback. 8 | 9 | dst -- writeable file-like object 10 | skip -- number of traceback entries to omit from the top of the list 11 | 12 | """ 13 | for line in traceback.format_exception_only(exception_type, value): 14 | dst.write(line) 15 | if (not isinstance(exception_type, str) and 16 | issubclass(exception_type, SyntaxError)): 17 | return 18 | print >>dst, 'traceback (most recent call last):' 19 | text = None 20 | for filename, lineno, fnname, text in traceback.extract_tb(tb)[skip:]: 21 | if fnname == "?": 22 | fn_s = "" 23 | else: 24 | fn_s = "(%s)" % fnname 25 | print >>dst, " %s:%s %s" % (filename, lineno, fn_s) 26 | if text is not None: 27 | print >>dst, "failing line:" 28 | print >>dst, text 29 | 30 | def format_traceback_from_info(exception_type, value, tb, skip=0): 31 | """Return a description of a given exception as a string. 32 | 33 | skip -- number of traceback entries to omit from the top of the list 34 | 35 | """ 36 | from cStringIO import StringIO 37 | log = StringIO() 38 | log_traceback_from_info(exception_type, value, tb, log, skip) 39 | return log.getvalue() 40 | 41 | def log_traceback(dst=sys.stderr, skip=0): 42 | """Log the current exception nicely to 'dst'. 43 | 44 | dst -- writeable file-like object 45 | skip -- number of traceback entries to omit from the top of the list 46 | 47 | """ 48 | exception_type, value, tb = sys.exc_info() 49 | log_traceback_from_info(exception_type, value, tb, dst, skip) 50 | 51 | def format_traceback(skip=0): 52 | """Return a description of the current exception as a string. 53 | 54 | skip -- number of traceback entries to omit from the top of the list 55 | 56 | """ 57 | exception_type, value, tb = sys.exc_info() 58 | return format_traceback_from_info(exception_type, value, tb, skip) 59 | 60 | 61 | def log_error_and_line_from_info(exception_type, value, tb, dst=sys.stderr): 62 | """Log a given exception briefly to 'dst', showing line number.""" 63 | if (not isinstance(exception_type, str) and 64 | issubclass(exception_type, SyntaxError)): 65 | for line in traceback.format_exception_only(exception_type, value): 66 | dst.write(line) 67 | else: 68 | try: 69 | filename, lineno, fnname, text = traceback.extract_tb(tb)[-1] 70 | except IndexError: 71 | pass 72 | else: 73 | print >>dst, "at line %s:" % lineno 74 | for line in traceback.format_exception_only(exception_type, value): 75 | dst.write(line) 76 | 77 | def format_error_and_line_from_info(exception_type, value, tb): 78 | """Return a brief description of a given exception as a string.""" 79 | from cStringIO import StringIO 80 | log = StringIO() 81 | log_error_and_line_from_info(exception_type, value, tb, log) 82 | return log.getvalue() 83 | 84 | def log_error_and_line(dst=sys.stderr): 85 | """Log the current exception briefly to 'dst'. 86 | 87 | dst -- writeable file-like object 88 | 89 | """ 90 | exception_type, value, tb = sys.exc_info() 91 | log_error_and_line_from_info(exception_type, value, tb, dst) 92 | 93 | def format_error_and_line(): 94 | """Return a brief description of the current exception as a string.""" 95 | exception_type, value, tb = sys.exc_info() 96 | return format_error_and_line_from_info(exception_type, value, tb) 97 | 98 | -------------------------------------------------------------------------------- /train/SL_policy_net_h08.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from keras.models import Sequential 4 | from keras.layers.core import Activation, Dense, Flatten 5 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 6 | from keras.optimizers import SGD 7 | from keras.callbacks import Callback 8 | from keras import backend as K 9 | from keras.models import model_from_json 10 | 11 | from utils.base import * 12 | 13 | import numpy as np 14 | 15 | 16 | k = 32 17 | bsize = 16 18 | epoch = 500 19 | lrate = 0.03 20 | 21 | SAVE_MODEL_FOLDER = "20160608/h08" 22 | 23 | POSTFIX = "_x.csv" 24 | TRAIN_DATA_FOLDER = "20160608/datasets/phase01/" 25 | 26 | 27 | def hidden_layers(m, k): 28 | m.add(ZeroPadding2D(padding=(1, 1))) 29 | m.add(Convolution2D(k, 3, 3)) 30 | m.add(Activation('relu')) 31 | 32 | return m 33 | 34 | def load_dataset(): 35 | xs, ys = list(), list() 36 | file_names = get_file_names(TRAIN_DATA_FOLDER, POSTFIX) 37 | n_idx = 0 38 | for name in file_names: 39 | datas = list() 40 | cur_rows = list() 41 | csvfile = open(TRAIN_DATA_FOLDER+name) 42 | row_reader = csv.reader(csvfile) 43 | for row in row_reader: 44 | if len(row) == 0: 45 | datas.append(cur_rows) 46 | cur_rows = list() 47 | continue 48 | c = list() 49 | for a in row: 50 | c.append(a) 51 | cur_rows.append(c) 52 | xs.append(datas) 53 | datas = list() 54 | fname = name[:len(name)-5]+"y.csv" 55 | f = open(TRAIN_DATA_FOLDER + fname) 56 | rreader = csv.reader(f) 57 | for row in rreader: 58 | for a in row: 59 | datas.append(float(a)) 60 | ys.append(datas) 61 | n_idx += 1 62 | if n_idx % 100 == 0: 63 | print n_idx 64 | 65 | xs = np.asarray(xs, dtype=np.float) 66 | ys = np.asarray(ys, dtype=np.float) 67 | print "xs", xs.shape, "ys", ys.shape 68 | return xs, ys 69 | 70 | 71 | class save_callback(Callback): 72 | 73 | def on_epoch_end(self, epoch, logs={}): 74 | self.model.save_weights(SAVE_MODEL_FOLDER+"/policy_net_weights_h08_"+str(epoch)+".h5") 75 | if epoch == 50: 76 | self.model.optimizer.lr *= 0.5 77 | elif epoch == 75: 78 | self.model.optimizer.lr *= 0.5 79 | 80 | if __name__ == "__main__": 81 | 82 | if not os.path.exists(SAVE_MODEL_FOLDER): 83 | os.mkdir(SAVE_MODEL_FOLDER) 84 | 85 | print "load dataset.." 86 | train_x, train_y = load_dataset() 87 | print "..finish" 88 | 89 | print "make model.." 90 | model = Sequential() 91 | 92 | model.add(ZeroPadding2D(padding=(1, 1), input_shape=(38, 9, 9))) 93 | model.add(Convolution2D(k, 5, 5)) 94 | model.add(Activation('relu')) 95 | 96 | for i in range(0, 8): 97 | model = hidden_layers(model, k) 98 | 99 | model.add(ZeroPadding2D(padding=(1, 1))) 100 | model.add(Convolution2D(1, 1, 1)) 101 | model.add(Activation('relu')) 102 | 103 | model.add(Flatten()) 104 | model.add(Activation('softmax')) 105 | 106 | print "..finish" 107 | 108 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 109 | model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=["accuracy"]) 110 | json_string = model.to_json() 111 | open(SAVE_MODEL_FOLDER+"/policy_net_model_h08.json", "w").write(json_string) 112 | stop = save_callback() 113 | model.fit(train_x, train_y, batch_size=bsize, nb_epoch=epoch, callbacks=[stop], verbose=1) 114 | 115 | 116 | 117 | -------------------------------------------------------------------------------- /train/SL_policy_net_h09.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from keras.models import Sequential 4 | from keras.layers.core import Activation, Dense, Flatten 5 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 6 | from keras.optimizers import SGD 7 | from keras.callbacks import Callback 8 | from keras import backend as K 9 | from keras.models import model_from_json 10 | 11 | from utils.base import * 12 | 13 | import numpy as np 14 | 15 | 16 | k = 32 17 | bsize = 16 18 | epoch = 500 19 | lrate = 0.03 20 | 21 | SAVE_MODEL_FOLDER = "20160608/h09" 22 | 23 | POSTFIX = "_x.csv" 24 | TRAIN_DATA_FOLDER = "20160608/datasets/phase01/" 25 | 26 | 27 | def hidden_layers(m, k): 28 | m.add(ZeroPadding2D(padding=(1, 1))) 29 | m.add(Convolution2D(k, 3, 3)) 30 | m.add(Activation('relu')) 31 | 32 | return m 33 | 34 | def load_dataset(): 35 | xs, ys = list(), list() 36 | file_names = get_file_names(TRAIN_DATA_FOLDER, POSTFIX) 37 | n_idx = 0 38 | for name in file_names: 39 | datas = list() 40 | cur_rows = list() 41 | csvfile = open(TRAIN_DATA_FOLDER+name) 42 | row_reader = csv.reader(csvfile) 43 | for row in row_reader: 44 | if len(row) == 0: 45 | datas.append(cur_rows) 46 | cur_rows = list() 47 | continue 48 | c = list() 49 | for a in row: 50 | c.append(a) 51 | cur_rows.append(c) 52 | xs.append(datas) 53 | datas = list() 54 | fname = name[:len(name)-5]+"y.csv" 55 | f = open(TRAIN_DATA_FOLDER + fname) 56 | rreader = csv.reader(f) 57 | for row in rreader: 58 | for a in row: 59 | datas.append(float(a)) 60 | ys.append(datas) 61 | n_idx += 1 62 | if n_idx % 100 == 0: 63 | print n_idx 64 | 65 | xs = np.asarray(xs, dtype=np.float) 66 | ys = np.asarray(ys, dtype=np.float) 67 | print "xs", xs.shape, "ys", ys.shape 68 | return xs, ys 69 | 70 | 71 | class save_callback(Callback): 72 | 73 | def on_epoch_end(self, epoch, logs={}): 74 | self.model.save_weights(SAVE_MODEL_FOLDER+"/policy_net_weights_h09_"+str(epoch)+".h5") 75 | if epoch == 50: 76 | self.model.optimizer.lr *= 0.5 77 | elif epoch == 75: 78 | self.model.optimizer.lr *= 0.5 79 | 80 | if __name__ == "__main__": 81 | 82 | if not os.path.exists(SAVE_MODEL_FOLDER): 83 | os.mkdir(SAVE_MODEL_FOLDER) 84 | 85 | print "load dataset.." 86 | train_x, train_y = load_dataset() 87 | print "..finish" 88 | 89 | print "make model.." 90 | model = Sequential() 91 | 92 | model.add(ZeroPadding2D(padding=(1, 1), input_shape=(38, 9, 9))) 93 | model.add(Convolution2D(k, 5, 5)) 94 | model.add(Activation('relu')) 95 | 96 | for i in range(0, 9): 97 | model = hidden_layers(model, k) 98 | 99 | model.add(ZeroPadding2D(padding=(1, 1))) 100 | model.add(Convolution2D(1, 1, 1)) 101 | model.add(Activation('relu')) 102 | 103 | model.add(Flatten()) 104 | model.add(Activation('softmax')) 105 | 106 | print "..finish" 107 | 108 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 109 | model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=["accuracy"]) 110 | json_string = model.to_json() 111 | open(SAVE_MODEL_FOLDER+"/policy_net_model_h09.json", "w").write(json_string) 112 | stop = save_callback() 113 | model.fit(train_x, train_y, batch_size=bsize, nb_epoch=epoch, callbacks=[stop], verbose=1) 114 | 115 | 116 | 117 | -------------------------------------------------------------------------------- /train/SL_policy_net_h10.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from keras.models import Sequential 4 | from keras.layers.core import Activation, Dense, Flatten 5 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 6 | from keras.optimizers import SGD 7 | from keras.callbacks import Callback 8 | from keras import backend as K 9 | from keras.models import model_from_json 10 | 11 | from utils.base import * 12 | 13 | import numpy as np 14 | 15 | 16 | k = 32 17 | bsize = 16 18 | epoch = 500 19 | lrate = 0.03 20 | 21 | SAVE_MODEL_FOLDER = "20160608/h10" 22 | 23 | POSTFIX = "_x.csv" 24 | TRAIN_DATA_FOLDER = "20160608/datasets/phase01/" 25 | 26 | 27 | def hidden_layers(m, k): 28 | m.add(ZeroPadding2D(padding=(1, 1))) 29 | m.add(Convolution2D(k, 3, 3)) 30 | m.add(Activation('relu')) 31 | 32 | return m 33 | 34 | def load_dataset(): 35 | xs, ys = list(), list() 36 | file_names = get_file_names(TRAIN_DATA_FOLDER, POSTFIX) 37 | n_idx = 0 38 | for name in file_names: 39 | datas = list() 40 | cur_rows = list() 41 | csvfile = open(TRAIN_DATA_FOLDER+name) 42 | row_reader = csv.reader(csvfile) 43 | for row in row_reader: 44 | if len(row) == 0: 45 | datas.append(cur_rows) 46 | cur_rows = list() 47 | continue 48 | c = list() 49 | for a in row: 50 | c.append(a) 51 | cur_rows.append(c) 52 | xs.append(datas) 53 | datas = list() 54 | fname = name[:len(name)-5]+"y.csv" 55 | f = open(TRAIN_DATA_FOLDER + fname) 56 | rreader = csv.reader(f) 57 | for row in rreader: 58 | for a in row: 59 | datas.append(float(a)) 60 | ys.append(datas) 61 | n_idx += 1 62 | if n_idx % 100 == 0: 63 | print n_idx 64 | 65 | xs = np.asarray(xs, dtype=np.float) 66 | ys = np.asarray(ys, dtype=np.float) 67 | print "xs", xs.shape, "ys", ys.shape 68 | return xs, ys 69 | 70 | 71 | class save_callback(Callback): 72 | 73 | def on_epoch_end(self, epoch, logs={}): 74 | self.model.save_weights(SAVE_MODEL_FOLDER+"/policy_net_weights_h10_"+str(epoch)+".h5") 75 | if epoch == 50: 76 | self.model.optimizer.lr *= 0.5 77 | elif epoch == 75: 78 | self.model.optimizer.lr *= 0.5 79 | 80 | if __name__ == "__main__": 81 | 82 | if not os.path.exists(SAVE_MODEL_FOLDER): 83 | os.mkdir(SAVE_MODEL_FOLDER) 84 | 85 | print "load dataset.." 86 | train_x, train_y = load_dataset() 87 | print "..finish" 88 | 89 | print "make model.." 90 | model = Sequential() 91 | 92 | model.add(ZeroPadding2D(padding=(1, 1), input_shape=(38, 9, 9))) 93 | model.add(Convolution2D(k, 5, 5)) 94 | model.add(Activation('relu')) 95 | 96 | for i in range(0, 10): 97 | model = hidden_layers(model, k) 98 | 99 | model.add(ZeroPadding2D(padding=(1, 1))) 100 | model.add(Convolution2D(1, 1, 1)) 101 | model.add(Activation('relu')) 102 | 103 | model.add(Flatten()) 104 | model.add(Activation('softmax')) 105 | 106 | print "..finish" 107 | 108 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 109 | model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=["accuracy"]) 110 | json_string = model.to_json() 111 | open(SAVE_MODEL_FOLDER+"/policy_net_model_h10.json", "w").write(json_string) 112 | stop = save_callback() 113 | model.fit(train_x, train_y, batch_size=bsize, nb_epoch=epoch, callbacks=[stop], verbose=1) 114 | 115 | 116 | 117 | -------------------------------------------------------------------------------- /train/SL_policy_net_h11.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from keras.models import Sequential 4 | from keras.layers.core import Activation, Dense, Flatten 5 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 6 | from keras.optimizers import SGD 7 | from keras.callbacks import Callback 8 | from keras import backend as K 9 | from keras.models import model_from_json 10 | 11 | from utils.base import * 12 | 13 | import numpy as np 14 | 15 | 16 | k = 32 17 | bsize = 16 18 | epoch = 500 19 | lrate = 0.03 20 | 21 | SAVE_MODEL_FOLDER = "20160608/h11" 22 | 23 | POSTFIX = "_x.csv" 24 | TRAIN_DATA_FOLDER = "20160608/datasets/phase01/" 25 | 26 | 27 | def hidden_layers(m, k): 28 | m.add(ZeroPadding2D(padding=(1, 1))) 29 | m.add(Convolution2D(k, 3, 3)) 30 | m.add(Activation('relu')) 31 | 32 | return m 33 | 34 | def load_dataset(): 35 | xs, ys = list(), list() 36 | file_names = get_file_names(TRAIN_DATA_FOLDER, POSTFIX) 37 | n_idx = 0 38 | for name in file_names: 39 | datas = list() 40 | cur_rows = list() 41 | csvfile = open(TRAIN_DATA_FOLDER+name) 42 | row_reader = csv.reader(csvfile) 43 | for row in row_reader: 44 | if len(row) == 0: 45 | datas.append(cur_rows) 46 | cur_rows = list() 47 | continue 48 | c = list() 49 | for a in row: 50 | c.append(a) 51 | cur_rows.append(c) 52 | xs.append(datas) 53 | datas = list() 54 | fname = name[:len(name)-5]+"y.csv" 55 | f = open(TRAIN_DATA_FOLDER + fname) 56 | rreader = csv.reader(f) 57 | for row in rreader: 58 | for a in row: 59 | datas.append(float(a)) 60 | ys.append(datas) 61 | n_idx += 1 62 | if n_idx % 100 == 0: 63 | print n_idx 64 | 65 | xs = np.asarray(xs, dtype=np.float) 66 | ys = np.asarray(ys, dtype=np.float) 67 | print "xs", xs.shape, "ys", ys.shape 68 | return xs, ys 69 | 70 | 71 | class save_callback(Callback): 72 | 73 | def on_epoch_end(self, epoch, logs={}): 74 | self.model.save_weights(SAVE_MODEL_FOLDER+"/policy_net_weights_h11_"+str(epoch)+".h5") 75 | if epoch == 50: 76 | self.model.optimizer.lr *= 0.5 77 | elif epoch == 75: 78 | self.model.optimizer.lr *= 0.5 79 | 80 | if __name__ == "__main__": 81 | 82 | if not os.path.exists(SAVE_MODEL_FOLDER): 83 | os.mkdir(SAVE_MODEL_FOLDER) 84 | 85 | print "load dataset.." 86 | train_x, train_y = load_dataset() 87 | print "..finish" 88 | 89 | print "make model.." 90 | model = Sequential() 91 | 92 | model.add(ZeroPadding2D(padding=(1, 1), input_shape=(38, 9, 9))) 93 | model.add(Convolution2D(k, 5, 5)) 94 | model.add(Activation('relu')) 95 | 96 | for i in range(0, 11): 97 | model = hidden_layers(model, k) 98 | 99 | model.add(ZeroPadding2D(padding=(1, 1))) 100 | model.add(Convolution2D(1, 1, 1)) 101 | model.add(Activation('relu')) 102 | 103 | model.add(Flatten()) 104 | model.add(Activation('softmax')) 105 | 106 | print "..finish" 107 | 108 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 109 | model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=["accuracy"]) 110 | json_string = model.to_json() 111 | open(SAVE_MODEL_FOLDER+"/policy_net_model_h11.json", "w").write(json_string) 112 | stop = save_callback() 113 | model.fit(train_x, train_y, batch_size=bsize, nb_epoch=epoch, callbacks=[stop], verbose=1) 114 | 115 | 116 | 117 | -------------------------------------------------------------------------------- /train/SL_policy_net_h13.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from keras.models import Sequential 4 | from keras.layers.core import Activation, Dense, Flatten 5 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 6 | from keras.optimizers import SGD 7 | from keras.callbacks import Callback 8 | from keras import backend as K 9 | from keras.models import model_from_json 10 | 11 | from utils.base import * 12 | import numpy as np 13 | 14 | k = 32 15 | bsize = 16 16 | epoch = 100 17 | lrate = 0.03 18 | 19 | SAVE_MODEL_FOLDER = "20160608/h13" 20 | 21 | POSTFIX = "_x.csv" 22 | TRAIN_DATA_FOLDER = "20160608/datasets/phase01/" 23 | 24 | 25 | def hidden_layers(m, k): 26 | m.add(ZeroPadding2D(padding=(1, 1))) 27 | m.add(Convolution2D(k, 3, 3)) 28 | m.add(Activation('relu')) 29 | 30 | return m 31 | 32 | def load_dataset(): 33 | xs, ys = list(), list() 34 | file_names = get_file_names(TRAIN_DATA_FOLDER, POSTFIX) 35 | n_idx = 0 36 | for name in file_names: 37 | datas = list() 38 | cur_rows = list() 39 | csvfile = open(TRAIN_DATA_FOLDER+name) 40 | row_reader = csv.reader(csvfile) 41 | for row in row_reader: 42 | if len(row) == 0: 43 | datas.append(cur_rows) 44 | cur_rows = list() 45 | continue 46 | c = list() 47 | for a in row: 48 | c.append(a) 49 | cur_rows.append(c) 50 | xs.append(datas) 51 | datas = list() 52 | fname = name[:len(name)-5]+"y.csv" 53 | f = open(TRAIN_DATA_FOLDER + fname) 54 | rreader = csv.reader(f) 55 | for row in rreader: 56 | for a in row: 57 | datas.append(float(a)) 58 | ys.append(datas) 59 | n_idx += 1 60 | if n_idx % 100 == 0: 61 | print n_idx 62 | 63 | xs = np.asarray(xs, dtype=np.float) 64 | ys = np.asarray(ys, dtype=np.float) 65 | print "xs", xs.shape, "ys", ys.shape 66 | return xs, ys 67 | 68 | 69 | class save_callback(Callback): 70 | 71 | def on_epoch_end(self, epoch, logs={}): 72 | self.model.save_weights(SAVE_MODEL_FOLDER+"/policy_net_weights_h13_"+str(epoch)+".h5") 73 | # if epoch == 50: 74 | # self.model.optimizer.lr *= 2.0 75 | # elif epoch == 75: 76 | # self.model.optimizer.lr *= 2.0 77 | self.model.optimizer.lr = float(open(SAVE_MODEL_FOLDER+"/lr.txt").read()) 78 | 79 | if __name__ == "__main__": 80 | 81 | if not os.path.exists(SAVE_MODEL_FOLDER): 82 | os.mkdir(SAVE_MODEL_FOLDER) 83 | 84 | print "load dataset.." 85 | train_x, train_y = load_dataset() 86 | print "..finish" 87 | 88 | print "make model.." 89 | model = Sequential() 90 | 91 | model.add(ZeroPadding2D(padding=(1, 1), input_shape=(38, 9, 9))) 92 | model.add(Convolution2D(k, 5, 5)) 93 | model.add(Activation('relu')) 94 | 95 | for i in range(0, 13): 96 | model = hidden_layers(model, k) 97 | 98 | model.add(ZeroPadding2D(padding=(1, 1))) 99 | model.add(Convolution2D(1, 1, 1)) 100 | model.add(Activation('relu')) 101 | 102 | model.add(Flatten()) 103 | model.add(Activation('softmax')) 104 | 105 | print "..finish" 106 | 107 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 108 | model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=["accuracy"]) 109 | json_string = model.to_json() 110 | open(SAVE_MODEL_FOLDER+"/policy_net_model_h13.json", "w").write(json_string) 111 | stop = save_callback() 112 | model.fit(train_x, train_y, batch_size=bsize, nb_epoch=epoch, callbacks=[stop], verbose=1) 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /play_DCL/pass_live.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import simple_go 3 | from utils import * 4 | 5 | start_no = long(sys.argv[1]) 6 | end_no = long(sys.argv[2]) 7 | 8 | fp = open("pass_live.log", "w") 9 | 10 | whole_board_flag = False 11 | 12 | if whole_board_flag: 13 | #size = 3 14 | #limit = 3 15 | 16 | size = 6 17 | limit = 12 18 | else: 19 | #size = 2 20 | #limit = 2 21 | 22 | #size = 3 23 | #limit = 3 24 | 25 | #size = 4 26 | #limit = 5 27 | 28 | #size = 5 29 | #limit = 7 30 | 31 | size = 6 32 | limit = 11 33 | 34 | bits = size**2 35 | 36 | if whole_board_flag: 37 | win_condition = size**2 #whole board win 38 | else: 39 | win_condition = 1 40 | 41 | if not end_no: 42 | end_no = 2**bits 43 | 44 | def write(s): 45 | sys.stdout.write(s) 46 | sys.stdout.flush() 47 | fp.write(s) 48 | fp.flush() 49 | 50 | write("Running with size %i with stone limit at %i\n" % (size, limit)) 51 | write("from position %s to position %s\n" % (start_no, end_no)) 52 | write("Result to screen and to pass_live.log\n") 53 | write("Win condition: %i points\n\n" % win_condition) 54 | 55 | best_bit_win = {} 56 | for i in range(limit+1): 57 | best_bit_win[i] = 1 58 | 59 | def create_board(size): 60 | simple_board = [] 61 | for y in range(size): 62 | simple_board.append([0]*size) 63 | return simple_board 64 | 65 | def ref1(b2, b1, x, y, size=size): 66 | b2[x][y] = b1[size-1-x][y] 67 | 68 | def ref2(b2, b1, x, y, size=size): 69 | b2[x][y] = b1[x][size-1-y] 70 | 71 | def ref3(b2, b1, x, y, size=size): 72 | b2[x][y] = b1[size-1-x][size-1-y] 73 | 74 | def ref4(b2, b1, x, y, size=size): 75 | b2[x][y] = b1[y][x] 76 | 77 | def ref5(b2, b1, x, y, size=size): 78 | b2[x][y] = b1[y][size-1-x] 79 | 80 | def ref6(b2, b1, x, y, size=size): 81 | b2[x][y] = b1[size-1-y][x] 82 | 83 | def ref7(b2, b1, x, y, size=size): 84 | b2[x][y] = b1[size-1-y][size-1-x] 85 | 86 | def iterate_bit_goban(no, bits=bits): 87 | x = 1 88 | y = 1 89 | for i in xrange(bits): 90 | if (1L<size: 94 | x = 1 95 | y += 1 96 | 97 | def binary_str(no): 98 | s = "" 99 | while no: 100 | s = str(no%2) + s 101 | no = no / 2 102 | return s 103 | 104 | simple_board2 = create_board(size) 105 | no = start_no 106 | while no<=end_no and no<2**bits: 107 | bit_count = 0 108 | for i in xrange(bits): 109 | if (1L< limit: 115 | i = 0 116 | while not ((1L< 0: 119 | #print "skip by", (2**i - 1) 120 | no += (2**i - 1) 121 | #print "!", binary_str(no), bit_count 122 | if bit_count<=limit: 123 | simple_board = create_board(size) 124 | for x,y in iterate_bit_goban(no): 125 | simple_board[x-1][y-1] = 1 126 | for reflection_function in ref1, ref2, ref3, ref4, ref5, ref6, ref7: 127 | for x in range(size): 128 | for y in range(size): 129 | reflection_function(simple_board2, simple_board, x, y) 130 | if simple_board2 < simple_board: 131 | break 132 | else: 133 | b = simple_go.Board(size) 134 | for x,y in iterate_bit_goban(no): 135 | b.add_stone(simple_go.BLACK, (x, y)) 136 | score = b.unconditional_score(simple_go.BLACK) 137 | if score>=best_bit_win[bit_count]: 138 | write("no: %i, bits: %i, score: %i\n%s\n" % (no, bit_count, score, b)) 139 | best_bit_win[bit_count] = score 140 | no = no + 1 141 | 142 | fp.close() 143 | -------------------------------------------------------------------------------- /play_DCL/network/slpolicy_model_0.json: -------------------------------------------------------------------------------- 1 | {"class_name": "Sequential", "config": [{"class_name": "ZeroPadding2D", "config": {"padding": [1, 1], "batch_input_shape": [null, 38, 9, 9], "trainable": true, "name": "zeropadding2d_1", "input_dtype": "float32"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_1", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 5, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 32, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 5}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_1"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [2, 2], "trainable": true, "name": "zeropadding2d_2"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_2", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 3, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 32, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 3}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_2"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [2, 2], "trainable": true, "name": "zeropadding2d_3"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_3", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 3, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 32, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 3}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_3"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [2, 2], "trainable": true, "name": "zeropadding2d_4"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_4", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 3, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 32, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 3}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_4"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [2, 2], "trainable": true, "name": "zeropadding2d_5"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_5", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 3, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 32, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 3}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_5"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [1, 1], "trainable": true, "name": "zeropadding2d_6"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_6", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 1, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 1, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 1}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_6"}}, {"class_name": "Flatten", "config": {"trainable": true, "name": "flatten_1"}}, {"class_name": "Dense", "config": {"W_constraint": null, "b_constraint": null, "name": "dense_1", "activity_regularizer": null, "trainable": true, "init": "glorot_uniform", "bias": true, "input_dim": null, "b_regularizer": null, "W_regularizer": null, "activation": "linear", "output_dim": 81}}, {"class_name": "Activation", "config": {"activation": "softmax", "trainable": true, "name": "activation_7"}}]} -------------------------------------------------------------------------------- /play_DCL/network/value_net_model.json: -------------------------------------------------------------------------------- 1 | {"class_name": "Sequential", "config": [{"class_name": "ZeroPadding2D", "config": {"padding": [1, 1], "batch_input_shape": [null, 39, 9, 9], "trainable": true, "name": "zeropadding2d_1", "input_dtype": "float32"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_1", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 5, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 192, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 5}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_1"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [2, 2], "trainable": true, "name": "zeropadding2d_2"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_2", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 3, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 192, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 3}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_2"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [2, 2], "trainable": true, "name": "zeropadding2d_3"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_3", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 3, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 192, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 3}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_3"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [2, 2], "trainable": true, "name": "zeropadding2d_4"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_4", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 3, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 192, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 3}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_4"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [2, 2], "trainable": true, "name": "zeropadding2d_5"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_5", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 3, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 192, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 3}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_5"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [1, 1], "trainable": true, "name": "zeropadding2d_6"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_6", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 1, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 1, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 1}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_6"}}, {"class_name": "Flatten", "config": {"trainable": true, "name": "flatten_1"}}, {"class_name": "Dense", "config": {"W_constraint": null, "b_constraint": null, "name": "dense_1", "activity_regularizer": null, "trainable": true, "init": "glorot_uniform", "bias": true, "input_dim": null, "b_regularizer": null, "W_regularizer": null, "activation": "linear", "output_dim": 1}}, {"class_name": "Activation", "config": {"activation": "tanh", "trainable": true, "name": "activation_7"}}]} -------------------------------------------------------------------------------- /play_DCL/network/2nd_rlpolicy_model_21.json: -------------------------------------------------------------------------------- 1 | {"class_name": "Sequential", "config": [{"class_name": "ZeroPadding2D", "config": {"padding": [1, 1], "batch_input_shape": [null, 38, 9, 9], "trainable": true, "name": "zeropadding2d_1", "input_dtype": "float32"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_1", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 5, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 32, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 5}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_1"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [2, 2], "trainable": true, "name": "zeropadding2d_2"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_2", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 3, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 32, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 3}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_2"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [2, 2], "trainable": true, "name": "zeropadding2d_3"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_3", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 3, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 32, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 3}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_3"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [2, 2], "trainable": true, "name": "zeropadding2d_4"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_4", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 3, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 32, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 3}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_4"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [2, 2], "trainable": true, "name": "zeropadding2d_5"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_5", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 3, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 32, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 3}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_5"}}, {"class_name": "ZeroPadding2D", "config": {"padding": [1, 1], "trainable": true, "name": "zeropadding2d_6"}}, {"class_name": "Convolution2D", "config": {"W_constraint": null, "b_constraint": null, "name": "convolution2d_6", "activity_regularizer": null, "trainable": true, "dim_ordering": "th", "nb_col": 1, "subsample": [1, 1], "init": "glorot_uniform", "nb_filter": 1, "border_mode": "valid", "b_regularizer": null, "W_regularizer": null, "activation": "linear", "nb_row": 1}}, {"class_name": "Activation", "config": {"activation": "relu", "trainable": true, "name": "activation_6"}}, {"class_name": "Flatten", "config": {"trainable": true, "name": "flatten_1"}}, {"class_name": "Dense", "config": {"W_constraint": null, "b_constraint": null, "name": "dense_1", "activity_regularizer": null, "trainable": true, "init": "glorot_uniform", "bias": true, "input_dim": null, "b_regularizer": null, "W_regularizer": null, "activation": "linear", "output_dim": 81}}, {"class_name": "Activation", "config": {"activation": "softmax", "trainable": true, "name": "activation_7"}}]} -------------------------------------------------------------------------------- /train/SL_policy_net_h01.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from keras.models import Sequential 4 | from keras.layers.core import Activation, Dense, Flatten 5 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 6 | from keras.optimizers import Adadelta 7 | from keras.callbacks import Callback 8 | from keras import backend as K 9 | from keras.models import model_from_json 10 | 11 | from utils.base import * 12 | 13 | import numpy as np 14 | from keras.datasets import mnist 15 | 16 | k = 32 17 | bsize = 16 18 | epoch = 500 19 | lrate = 0.003 20 | 21 | SAVE_MODEL_FOLDER = "20160715/h01" 22 | 23 | POSTFIX = "_x.csv" 24 | TRAIN_DATA_FOLDER = "20160715/same_dataset/" 25 | 26 | 27 | def hidden_layers(m, k): 28 | m.add(ZeroPadding2D(padding=(1, 1))) 29 | m.add(Convolution2D(k, 3, 3)) 30 | m.add(Activation('relu')) 31 | 32 | return m 33 | 34 | def load_dataset(): 35 | xs, ys = list(), list() 36 | file_names = get_file_names(TRAIN_DATA_FOLDER, POSTFIX) 37 | n_idx = 0 38 | for name in file_names: 39 | datas = list() 40 | cur_rows = list() 41 | csvfile = open(TRAIN_DATA_FOLDER+name) 42 | row_reader = csv.reader(csvfile) 43 | for row in row_reader: 44 | if len(row) == 0: 45 | datas.append(cur_rows) 46 | cur_rows = list() 47 | continue 48 | c = list() 49 | for a in row: 50 | c.append(a) 51 | cur_rows.append(c) 52 | xs.append(datas) 53 | datas = list() 54 | fname = name[:len(name)-5]+"y.csv" 55 | f = open(TRAIN_DATA_FOLDER + fname) 56 | rreader = csv.reader(f) 57 | for row in rreader: 58 | for a in row: 59 | datas.append(float(a)) 60 | ys.append(datas) 61 | n_idx += 1 62 | if n_idx % 100 == 0: 63 | print n_idx 64 | 65 | xs = np.asarray(xs, dtype=np.float) 66 | ys = np.asarray(ys, dtype=np.float) 67 | print "xs", xs.shape, "ys", ys.shape 68 | return xs, ys 69 | 70 | 71 | class save_callback(Callback): 72 | 73 | def on_epoch_end(self, epoch, logs={}): 74 | self.model.save_weights(SAVE_MODEL_FOLDER+"/policy_net_weights_h01_"+str(epoch)+".h5") 75 | # self.model.optimizer.lr = float(open(SAVE_MODEL_FOLDER+"/lr.txt").read()) 76 | 77 | 78 | if __name__ == "__main__": 79 | 80 | if not os.path.exists(SAVE_MODEL_FOLDER): 81 | os.mkdir(SAVE_MODEL_FOLDER) 82 | 83 | print "load dataset.." 84 | train_x, train_y = load_dataset() 85 | # (X_train, y_train), (X_test, y_test) = mnist.load_data() 86 | # X_train = X_train.reshape(X_train.shape[0], 1, 28, 28) 87 | # X_test = X_test.reshape(X_test.shape[0], 1, 28, 28) 88 | # X_train = X_train.astype('float32') 89 | # X_test = X_test.astype('float32') 90 | # X_train /= 255 91 | # X_test /= 255 92 | # Y_train = np_utils.to_categorical(y_train, 10); 93 | # Y_test = np_utils.to_categorical(y_test, 10); 94 | # print(X_train.shape); 95 | # print(Y_train.shape); 96 | # print(X_test.shape); 97 | # print(Y_test.shape); 98 | print "..finish" 99 | 100 | print "make model.." 101 | model = Sequential() 102 | 103 | model.add(ZeroPadding2D(padding=(1, 1), input_shape=(38, 9, 9))) 104 | model.add(Convolution2D(k, 5, 5)) 105 | model.add(Activation('relu')) 106 | 107 | for i in range(0, 1): 108 | model = hidden_layers(model, k) 109 | 110 | model.add(ZeroPadding2D(padding=(1, 1))) 111 | model.add(Convolution2D(1, 1, 1)) 112 | model.add(Activation('relu')) 113 | 114 | model.add(Flatten()) 115 | model.add(Activation('softmax')) 116 | 117 | print "..finish" 118 | 119 | sgd = Adadelta(lr=lrate, rho=0.95, epsilon=1e-08) 120 | model.compile(loss='categorical_crossentropy', optimizer="Adadelta", metrics=["accuracy"]) 121 | json_string = model.to_json() 122 | open(SAVE_MODEL_FOLDER+"/policy_net_model_h01.json", "w").write(json_string) 123 | stop = save_callback() 124 | model.fit(train_x, train_y, batch_size=bsize, nb_epoch=epoch, callbacks=[stop], verbose=1) 125 | 126 | 127 | 128 | -------------------------------------------------------------------------------- /play_DCL/load_sgf.py: -------------------------------------------------------------------------------- 1 | import string, simple_go, re, sys, time 2 | 3 | def sgf_to_coords(size, sgf_coords): 4 | #print sgf_coords 5 | if len(sgf_coords)==0: return "PASS" 6 | letter = string.upper(sgf_coords[0]) 7 | if letter>="I": 8 | letter = chr(ord(letter)+1) 9 | digit = str(ord('a') + int(size) - ord(sgf_coords[1])) 10 | return letter+digit 11 | 12 | #def sgf2tuple(size, move): 13 | # coords = sgf_to_coords(size, move) 14 | # return simple_go.string_as_move(coords, size) 15 | 16 | def sgf2number(sgf): 17 | no = 0 18 | #print "-"*60 19 | #print sgf 20 | while len(sgf)>0: 21 | ch = sgf[0] 22 | sgf = sgf[1:] 23 | if 'a'<=ch<='z': 24 | no = 52 * no + ord(ch)-ord('a') 25 | else: 26 | no = 52 * no + ord(ch)-ord('A') + 26 27 | #print ch,sgf,no 28 | return no 29 | 30 | def sgf2tuple(size, move): 31 | if len(move)==0: 32 | return simple_go.PASS_MOVE 33 | coordsize = len(move)/2 34 | no1 = sgf2number(move[:coordsize]) 35 | no2 = sgf2number(move[coordsize:]) 36 | return no1+1, size-no2 37 | 38 | def load_file(s, game_class = simple_go.Game): 39 | #s = open(name).read() 40 | parts = string.split(s, ";") 41 | header = parts[1] 42 | moves = parts[2:] 43 | sz = re.match(r".*SZ\[(\d+)\].*", header, re.DOTALL) 44 | if sz: 45 | g = game_class(int(sz.group(1))) 46 | else: 47 | raise ValueError, "no size tag" 48 | komi = re.match(r".*KM\[([\d\.]+)\].*", header, re.DOTALL) 49 | if komi: 50 | komi = float(komi.group(1)) 51 | g.set_komi(komi) 52 | ha = re.match(r".*AB(.*?)P.*", header, re.DOTALL) 53 | if ha: 54 | for move in string.split(ha.group(1)[1:-1], "]["): 55 | if g.current_board.side==simple_go.WHITE: 56 | g.make_move(simple_go.PASS_MOVE) 57 | if not g.make_move(sgf2tuple(g.size, move)): 58 | raise ValueError, move 59 | for move_str in moves: 60 | m = re.match(r".*?(\w+)\[(.*?)\](.*)", move_str, re.DOTALL) 61 | if m: 62 | color = m.group(1) 63 | move = m.group(2) 64 | rest = m.group(3) 65 | if color in ("B", "W"): 66 | if (g.current_board.side==simple_go.BLACK) != (string.upper(color[0])=="B"): 67 | g.make_move(simple_go.PASS_MOVE) 68 | if not g.make_move(sgf2tuple(g.size, move)): 69 | raise ValueError, move 70 | elif color in ("AW", "AB"): 71 | while move and color in ("AW", "AB"): 72 | if (g.current_board.side==simple_go.BLACK) != (string.upper(color[1])=="B"): 73 | g.make_move(simple_go.PASS_MOVE) 74 | if not g.make_move(sgf2tuple(g.size, move)): 75 | raise ValueError, move 76 | m = re.match(r"\[(.*?)\](.*)", rest, re.DOTALL) 77 | if m: 78 | move = m.group(1) 79 | rest = m.group(2) 80 | else: 81 | m = re.match(r"(\w+)\[(.*?)\](.*)", rest, re.DOTALL) 82 | if m: 83 | color = m.group(1) 84 | move = m.group(2) 85 | rest = m.group(3) 86 | else: 87 | move = None 88 | return g 89 | 90 | def test_time_usage(file_name, color): 91 | g = load_file(file_name) 92 | moves = g.move_history[:] 93 | while g.undo_move(): 94 | pass 95 | t0 = time.time() 96 | for move in moves: 97 | move_no = len(g.move_history) + 1 98 | if g.current_board.side==simple_go.BLACK: 99 | print "Black to move" 100 | else: 101 | print "White to move" 102 | print "Current move:", move_no, simple_go.move_as_string(move) 103 | if g.current_board.side==color: 104 | print "Generated move:", move_no, simple_go.move_as_string(g.generate_move()) 105 | g.make_move(move) 106 | print 107 | print "="*60 108 | print 109 | t1 = time.time() 110 | print "Total time usage:", t1-t0 111 | 112 | if __name__=="__main__": 113 | g = load_file(sys.argv[1]) 114 | -------------------------------------------------------------------------------- /play_DCL/gomill/ringmaster_command_line.py: -------------------------------------------------------------------------------- 1 | """Command-line interface to the ringmaster.""" 2 | 3 | import os 4 | import sys 5 | from optparse import OptionParser 6 | 7 | from gomill import compact_tracebacks 8 | from gomill.ringmasters import ( 9 | Ringmaster, RingmasterError, RingmasterInternalError) 10 | 11 | 12 | # Action functions return the desired exit status; implicit return is fine to 13 | # indicate a successful exit. 14 | 15 | def do_run(ringmaster, options): 16 | if not options.quiet: 17 | print "running startup checks on all players" 18 | if not ringmaster.check_players(discard_stderr=True): 19 | print "(use the 'check' command to see stderr output)" 20 | return 1 21 | if options.log_gtp: 22 | ringmaster.enable_gtp_logging() 23 | if options.quiet: 24 | ringmaster.set_display_mode('quiet') 25 | if ringmaster.status_file_exists(): 26 | ringmaster.load_status() 27 | else: 28 | ringmaster.set_clean_status() 29 | if options.parallel is not None: 30 | ringmaster.set_parallel_worker_count(options.parallel) 31 | ringmaster.run(options.max_games) 32 | ringmaster.report() 33 | 34 | def do_stop(ringmaster, options): 35 | ringmaster.write_command("stop") 36 | 37 | def do_show(ringmaster, options): 38 | if not ringmaster.status_file_exists(): 39 | raise RingmasterError("no status file") 40 | ringmaster.load_status() 41 | ringmaster.print_status_report() 42 | 43 | def do_report(ringmaster, options): 44 | if not ringmaster.status_file_exists(): 45 | raise RingmasterError("no status file") 46 | ringmaster.load_status() 47 | ringmaster.report() 48 | 49 | def do_reset(ringmaster, options): 50 | ringmaster.delete_state_and_output() 51 | 52 | def do_check(ringmaster, options): 53 | if not ringmaster.check_players(discard_stderr=False): 54 | return 1 55 | 56 | def do_debugstatus(ringmaster, options): 57 | ringmaster.print_status() 58 | 59 | _actions = { 60 | "run" : do_run, 61 | "stop" : do_stop, 62 | "show" : do_show, 63 | "report" : do_report, 64 | "reset" : do_reset, 65 | "check" : do_check, 66 | "debugstatus" : do_debugstatus, 67 | } 68 | 69 | 70 | def run(argv, ringmaster_class): 71 | usage = ("%prog [options] [command]\n\n" 72 | "commands: run (default), stop, show, report, reset, check") 73 | parser = OptionParser(usage=usage, prog="ringmaster", 74 | version=ringmaster_class.public_version) 75 | parser.add_option("--max-games", "-g", type="int", 76 | help="maximum number of games to play in this run") 77 | parser.add_option("--parallel", "-j", type="int", 78 | help="number of worker processes") 79 | parser.add_option("--quiet", "-q", action="store_true", 80 | help="be silent except for warnings and errors") 81 | parser.add_option("--log-gtp", action="store_true", 82 | help="write GTP logs") 83 | (options, args) = parser.parse_args(argv) 84 | if len(args) == 0: 85 | parser.error("no control file specified") 86 | if len(args) > 2: 87 | parser.error("too many arguments") 88 | if len(args) == 1: 89 | command = "run" 90 | else: 91 | command = args[1] 92 | try: 93 | action = _actions[command] 94 | except KeyError: 95 | parser.error("no such command: %s" % command) 96 | ctl_pathname = args[0] 97 | try: 98 | if not os.path.exists(ctl_pathname): 99 | raise RingmasterError("control file %s not found" % ctl_pathname) 100 | ringmaster = ringmaster_class(ctl_pathname) 101 | exit_status = action(ringmaster, options) 102 | except RingmasterError, e: 103 | print >>sys.stderr, "ringmaster:", e 104 | exit_status = 1 105 | except KeyboardInterrupt: 106 | exit_status = 3 107 | except RingmasterInternalError, e: 108 | print >>sys.stderr, "ringmaster: internal error" 109 | print >>sys.stderr, e 110 | exit_status = 4 111 | except: 112 | print >>sys.stderr, "ringmaster: internal error" 113 | compact_tracebacks.log_traceback() 114 | exit_status = 4 115 | sys.exit(exit_status) 116 | 117 | def main(): 118 | run(sys.argv[1:], Ringmaster) 119 | 120 | if __name__ == "__main__": 121 | main() 122 | 123 | -------------------------------------------------------------------------------- /train/SL_policy_net.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | from keras.models import Sequential 4 | from keras.layers.core import Activation, Dense, Flatten 5 | from keras.layers.convolutional import Convolution2D, ZeroPadding2D, MaxPooling2D 6 | from keras.optimizers import SGD 7 | from keras.callbacks import Callback 8 | from keras import backend as K 9 | 10 | from utils.base import * 11 | 12 | k = 32 13 | bsize = 16 14 | epoch = 500 15 | lrate = 0.003 16 | 17 | SAVE_FOLDER_NAME = "20160503" 18 | 19 | POSTFIX = ".csv" 20 | PRO_TRAIN_FOLDER = "train_pro/" 21 | PRO_TEST_FOLDER = "test_pro/" 22 | OTHER_TRAIN_FOLDER = "train_other/" 23 | OTHER_TEST_FOLDER = "test_other/" 24 | 25 | 26 | def hidden_layers(m, k): 27 | m.add(ZeroPadding2D(padding=(2, 2))) 28 | m.add(Convolution2D(k, 3, 3)) 29 | m.add(Activation('relu')) 30 | 31 | return m 32 | 33 | def load_dataset(): 34 | FOLDER = list() 35 | FOLDER.append(PRO_TRAIN_FOLDER) 36 | FOLDER.append(OTHER_TRAIN_FOLDER) 37 | xs, ys = list(), list() 38 | file_names = get_file_names(PRO_TRAIN_FOLDER, POSTFIX) 39 | for name in file_names: 40 | datas = list() 41 | cur_rows = list() 42 | csvfile = open(PRO_TRAIN_FOLDER+name) 43 | row_reader = csv.reader(csvfile) 44 | if name[-5] == 'x': 45 | for row in row_reader: 46 | if len(row) == 0: 47 | datas.append(cur_rows) 48 | cur_rows = list() 49 | continue 50 | c = list() 51 | for a in row: 52 | c.append(a) 53 | cur_rows.append(c) 54 | xs.append(datas) 55 | datas = list() 56 | fname = name[:len(name)-5]+"y.csv" 57 | f = open(PRO_TRAIN_FOLDER + fname) 58 | rreader = csv.reader(f) 59 | for row in rreader: 60 | for a in row: 61 | datas.append(a) 62 | ys.append(datas) 63 | 64 | file_names = get_file_names(OTHER_TRAIN_FOLDER, POSTFIX) 65 | for name in file_names: 66 | datas = list() 67 | cur_rows = list() 68 | csvfile = open(OTHER_TRAIN_FOLDER+name) 69 | row_reader = csv.reader(csvfile) 70 | if name[-6] == 'x': 71 | for row in row_reader: 72 | if len(row) == 0: 73 | datas.append(cur_rows) 74 | cur_rows = list() 75 | continue 76 | c = list() 77 | for a in row: 78 | c.append(a) 79 | cur_rows.append(c) 80 | xs.append(datas) 81 | datas = list() 82 | fname = name[:len(name)-6]+"y_.csv" 83 | f = open(OTHER_TRAIN_FOLDER + fname) 84 | rreader = csv.reader(f) 85 | for row in rreader: 86 | for a in row: 87 | datas.append(a) 88 | ys.append(datas) 89 | xs = np.asarray(xs, dtype=np.float) 90 | ys = np.asarray(ys, dtype=np.float) 91 | print "xs", xs.shape, "ys", ys.shape 92 | return xs, ys 93 | 94 | 95 | class save_callback(Callback): 96 | 97 | def on_epoch_end(self, epoch, logs={}): 98 | json_string = model.to_json() 99 | open(SAVE_FOLDER_NAME+"/policy_net_model_"+str(epoch)+".json", "w").write(json_string) 100 | self.model.save_weights(SAVE_FOLDER_NAME+"/policy_net_weights_"+str(epoch)+".h5") 101 | 102 | if __name__ == "__main__": 103 | 104 | print "load dataset.." 105 | train_x, train_y = load_dataset() 106 | print "..finish" 107 | 108 | print "make model.." 109 | model = Sequential() 110 | 111 | model.add(ZeroPadding2D(padding=(1, 1), input_shape=(38, 9, 9))) 112 | model.add(Convolution2D(k, 5, 5)) 113 | model.add(Activation('relu')) 114 | 115 | for i in range(0, 4): 116 | model = hidden_layers(model, k) 117 | 118 | model.add(ZeroPadding2D(padding=(1, 1))) 119 | model.add(Convolution2D(1, 1, 1)) 120 | model.add(Activation('relu')) 121 | 122 | model.add(Flatten()) 123 | model.add(Dense(81)) 124 | model.add(Activation('softmax')) 125 | 126 | print "..finish" 127 | 128 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 129 | model.compile(loss='categorical_crossentropy', optimizer=sgd) 130 | stop = save_callback() 131 | model.fit(train_x, train_y, batch_size=bsize, nb_epoch=epoch, callbacks=[stop], show_accuracy=True, verbose=1) 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /play_DCL/mcts/states/toy_world_state.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | from __future__ import print_function 3 | 4 | import numpy as np 5 | from scipy.stats import rv_discrete, entropy 6 | from copy import copy 7 | 8 | 9 | class ToyWorldAction(object): 10 | def __init__(self, action): 11 | self.action = action 12 | self._hash = 10*(action[0]+2) + action[1]+2 13 | 14 | def __hash__(self): 15 | return int(self._hash) 16 | 17 | def __eq__(self, other): 18 | return (self.action == other.action).all() 19 | 20 | def __str__(self): 21 | return str(self.action) 22 | 23 | def __repr__(self): 24 | return str(self.action) 25 | 26 | 27 | class ToyWorld(object): 28 | def __init__(self, size, information_gain, goal, manual): 29 | self.size = np.asarray(size) 30 | self.information_gain = information_gain 31 | self.goal = np.asarray(goal) 32 | self.manual = manual 33 | 34 | 35 | class ToyWorldState(object): 36 | def __init__(self, pos, world, belief=None): 37 | self.pos = pos 38 | self.world = world 39 | self.actions = [ToyWorldAction(np.array([0, 1])), 40 | ToyWorldAction(np.array([0, -1])), 41 | ToyWorldAction(np.array([1, 0])), 42 | ToyWorldAction(np.array([-1, 0]))] 43 | if belief: 44 | self.belief = belief 45 | else: 46 | self.belief = dict((a, np.array([1] * 4)) for a in self.actions) 47 | 48 | def _correct_position(self, pos): 49 | upper = np.min(np.vstack((pos, self.world.size)), 0) 50 | return np.max(np.vstack((upper, np.array([0, 0]))), 0) 51 | 52 | def perform(self, action): 53 | # get distribution about outcomes 54 | probabilities = self.belief[action] / np.sum(self.belief[action]) 55 | distrib = rv_discrete(values=(range(len(probabilities)), 56 | probabilities)) 57 | 58 | # draw sample 59 | sample = distrib.rvs() 60 | 61 | # update belief accordingly 62 | belief = copy(self.belief) 63 | belief[action][sample] += 1 64 | 65 | # manual found 66 | if (self.pos == self.world.manual).all(): 67 | print("m", end="") 68 | belief = {ToyWorldAction(np.array([0, 1])): [50, 1, 1, 1], 69 | ToyWorldAction(np.array([0, -1])): [1, 50, 1, 1], 70 | ToyWorldAction(np.array([1, 0])): [1, 1, 50, 1], 71 | ToyWorldAction(np.array([-1, 0])): [1, 1, 1, 50]} 72 | 73 | # build next state 74 | pos = self._correct_position(self.pos + self.actions[sample].action) 75 | 76 | return ToyWorldState(pos, self.world, belief) 77 | 78 | def real_world_perform(self, action): 79 | # update belief accordingly 80 | belief = copy(self.belief) 81 | if (action.action == np.array([0, 1])).all(): 82 | real_action = 0 83 | elif (action.action == np.array([0, -1])).all(): 84 | real_action = 1 85 | elif (action.action == np.array([1, 0])).all(): 86 | real_action = 2 87 | elif (action.action == np.array([-1, 0])).all(): 88 | real_action = 3 89 | belief[action][real_action] += 1 90 | 91 | # manual found 92 | if (self.pos == self.world.manual).all(): 93 | print("M", end="") 94 | belief = {ToyWorldAction(np.array([0, 1])): [50, 1, 1, 1], 95 | ToyWorldAction(np.array([0, -1])): [1, 50, 1, 1], 96 | ToyWorldAction(np.array([1, 0])): [1, 1, 50, 1], 97 | ToyWorldAction(np.array([-1, 0])): [1, 1, 1, 50]} 98 | 99 | pos = self._correct_position(self.pos + action.action) 100 | return ToyWorldState(pos, self.world, belief) 101 | 102 | def is_terminal(self): 103 | return False 104 | 105 | def __eq__(self, other): 106 | return (self.pos == other.pos).all() 107 | 108 | def __hash__(self): 109 | return int(self.pos[0]*100 + self.pos[1]) 110 | 111 | def __str__(self): 112 | return str(self.pos) 113 | 114 | def __repr__(self): 115 | return str(self.pos) 116 | 117 | def reward(self, parent, action): 118 | if (self.pos == self.world.goal).all(): 119 | print("g", end="") 120 | return 100 121 | else: 122 | reward = -1 123 | if self.world.information_gain: 124 | for a in self.actions: 125 | reward += entropy(parent.belief[a], self.belief[a]) 126 | return reward 127 | -------------------------------------------------------------------------------- /tests/test_prob.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | 3 | import numpy as np 4 | from keras.models import model_from_json 5 | from keras.optimizers import SGD 6 | import time, csv, os, sys 7 | from datetime import datetime 8 | 9 | POSTFIX = ".csv" 10 | lrate = 0.003 11 | bsize = 16 12 | Y_names = list() 13 | def get_file_names(path, postfix): 14 | res = [] 15 | 16 | for root, dirs, files in os.walk(path): 17 | 18 | for file in files: 19 | if file[-len(postfix):] == postfix: 20 | res.append(file) 21 | 22 | return res 23 | 24 | def load_dataset(): 25 | xs, ys = list(), list() 26 | file_names = get_file_names(DATA_FOLDER, POSTFIX) 27 | for name in file_names: 28 | datas = list() 29 | cur_rows = list() 30 | csvfile = open(DATA_FOLDER+name) 31 | row_reader = csv.reader(csvfile) 32 | if name[-5] == 'x': 33 | for row in row_reader: 34 | if len(row) == 0: 35 | datas.append(cur_rows) 36 | cur_rows = list() 37 | continue 38 | c = list() 39 | for a in row: 40 | c.append(a) 41 | cur_rows.append(c) 42 | xs.append(datas) 43 | datas = list() 44 | fname = name[:len(name)-5]+"y.csv" 45 | Y_names.append(fname) 46 | f = open(DATA_FOLDER + fname) 47 | rreader = csv.reader(f) 48 | for row in rreader: 49 | for a in row: 50 | datas.append(a) 51 | ys.append(datas) 52 | xs = np.asarray(xs, dtype=np.float) 53 | ys = np.asarray(ys, dtype=np.float) 54 | print "xs", xs.shape, "ys", ys.shape 55 | return xs, ys 56 | 57 | 58 | if __name__ == "__main__": 59 | 60 | MODEL_JSON = sys.argv[1] 61 | MODEL_H5 = sys.argv[2] 62 | DATA_FOLDER = sys.argv[3] 63 | SAVE_FOLDER = sys.argv[4] 64 | 65 | CUR_DATETIME = datetime.now().strftime("%y%m%d_%H%M%S") 66 | cor, uncor = 0, 0 67 | 68 | X, Y = load_dataset() 69 | model = model_from_json(open(MODEL_JSON).read()) 70 | model.load_weights(MODEL_H5) 71 | sgd = SGD(lr=lrate, decay=0.0, momentum=0.0, nesterov=False) 72 | model.compile(loss="categorical_crossentropy", optimizer=sgd) 73 | before = time.time() 74 | pResult = model.predict(X) 75 | after = time.time() 76 | print "걸린 시간", after-before 77 | for i in range(0, len(pResult)): 78 | pred = pResult[i].argmax() 79 | real = Y[i].argmax() 80 | if pred == real: 81 | cor += 1 82 | else: 83 | uncor += 1 84 | ACC = 100.0 * cor / len(Y) 85 | print "맞은 개수:", cor 86 | print "틀린 개수:", uncor 87 | print "정확도: %.5f" % ACC 88 | 89 | print "피쳐 저장 중.." 90 | txt = "" 91 | lY = Y.tolist() 92 | n_y = 0 93 | for y in lY: 94 | txt += Y_names[n_y] +"\n" 95 | convert_y = np.zeros((9,9)) 96 | for n_row in range(0, len(y)): 97 | cur_prob = y[n_row] 98 | convert_row = int(n_row / 9) 99 | convert_col = n_row - convert_row * 9 100 | convert_row += 1 101 | convert_col += 1 102 | row = 10 - convert_row -1 103 | col = convert_col -1 104 | convert_y[row][col] = cur_prob 105 | convert_y = convert_y.tolist() 106 | for n_row in range(0, len(convert_y)): 107 | for n_comp in range(0, len(convert_y[n_row])): 108 | txt += "%.5f" % convert_y[n_row][n_comp] 109 | if n_comp < len(convert_y[n_row])-1: 110 | txt += "," 111 | txt += "\n" 112 | txt += "\n" 113 | n_y += 1 114 | fname = CUR_DATETIME +"_features.csv" 115 | open(SAVE_FOLDER+fname, 'w').write(txt) 116 | print "..피쳐 저장 끝, 파일 이름:", fname 117 | 118 | print "확률 저장 중.." 119 | txt = "" 120 | lResult = pResult.tolist() 121 | for result in lResult: 122 | convert_result = np.zeros((9,9)) 123 | for n_row in range(0, len(result)): 124 | cur_prob = result[n_row] 125 | convert_row = int(n_row / 9) 126 | convert_col = n_row - convert_row * 9 127 | convert_row += 1 128 | convert_col += 1 129 | row = 10 - convert_row -1 130 | col = convert_col -1 131 | convert_result[row][col] = cur_prob 132 | convert_result = convert_result.tolist() 133 | for n_row in range(0, len(convert_result)): 134 | for n_comp in range(0, len(convert_result[n_row])): 135 | txt += "%.5f" % convert_result[n_row][n_comp] 136 | if n_comp < len(convert_result[n_row])-1: 137 | txt += "," 138 | txt += "\n" 139 | txt += "\n" 140 | fname = CUR_DATETIME+"_result.csv" 141 | open(SAVE_FOLDER+fname, 'w').write(txt) 142 | 143 | print "..확률 저장 끝, 파일 이름:", fname 144 | -------------------------------------------------------------------------------- /play_DCL/gomill/ascii_tables.py: -------------------------------------------------------------------------------- 1 | """Render tabular output. 2 | 3 | This is designed for screen or text-file output, using a fixed-width font. 4 | 5 | """ 6 | 7 | from collections import defaultdict 8 | 9 | class Column_spec(object): 10 | """Details of a table column. 11 | 12 | Public attributes: 13 | align -- 'left' or 'right' 14 | right_padding -- int 15 | 16 | """ 17 | def __init__(self, align='left', right_padding=1): 18 | self.align = align 19 | self.right_padding = right_padding 20 | 21 | def render(self, s, width): 22 | if self.align == 'left': 23 | s = s.ljust(width) 24 | elif self.align == 'right': 25 | s = s.rjust(width) 26 | return s + " " * self.right_padding 27 | 28 | class Table(object): 29 | """Render tabular output. 30 | 31 | Normal use: 32 | 33 | tbl = Table(row_count=3) 34 | tbl.add_heading('foo') 35 | i = tbl.add_column(align='left', right_padding=3) 36 | tbl.set_column_values(i, ['a', 'b']) 37 | [...] 38 | print '\n'.join(tbl.render()) 39 | 40 | """ 41 | def __init__(self, row_count=None): 42 | self.col_count = 0 43 | self.row_count = row_count 44 | self.headings = [] 45 | self.columns = [] 46 | self.cells = defaultdict(str) 47 | 48 | def set_row_count(self, row_count): 49 | """Change the table's row count.""" 50 | self.row_count = row_count 51 | 52 | def add_heading(self, heading, span=1): 53 | """Specify a column or column group heading. 54 | 55 | To leave a column with no heading, pass the empty string. 56 | 57 | To allow a heading to cover multiple columns, pass the 'span' parameter 58 | and don't add headings for the rest of the covered columns. 59 | 60 | """ 61 | self.headings.append((heading, span)) 62 | 63 | def add_column(self, **kwargs): 64 | """Add a column to the table. 65 | 66 | align -- 'left' (default) or 'right' 67 | right_padding -- int (default 1) 68 | 69 | Returns the column id 70 | 71 | Right padding is the number of spaces to leave between this column and 72 | the next. 73 | 74 | (The last column should have right padding 1, so that the heading can 75 | use the full width if necessary.) 76 | 77 | """ 78 | column = Column_spec(**kwargs) 79 | self.columns.append(column) 80 | column_id = self.col_count 81 | self.col_count += 1 82 | return column_id 83 | 84 | def get_column(self, column_id): 85 | """Retrieve a column object given its id. 86 | 87 | You can use this to change the column's attributes after adding it. 88 | 89 | """ 90 | return self.columns[column_id] 91 | 92 | def set_column_values(self, column_id, values): 93 | """Specify the values for a column. 94 | 95 | column_id -- as returned by add_column() 96 | values -- iterable 97 | 98 | str() is called on the values. 99 | 100 | If values are not supplied for all rows, the remaining rows are left 101 | blank. If too many values are supplied, the excess values are ignored. 102 | 103 | """ 104 | for row, value in enumerate(values): 105 | self.cells[row, column_id] = str(value) 106 | 107 | def render(self): 108 | """Render the table. 109 | 110 | Returns a list of strings. 111 | 112 | Each line has no trailing whitespace. 113 | 114 | Lines which would be wholly blank are omitted. 115 | 116 | """ 117 | def column_values(col): 118 | return [self.cells[row, col] for row in xrange(self.row_count)] 119 | 120 | result = [] 121 | 122 | cells = self.cells 123 | widths = [max(map(len, column_values(i))) 124 | for i in xrange(self.col_count)] 125 | col = 0 126 | heading_line = [] 127 | for heading, span in self.headings: 128 | # width available for the heading 129 | width = (sum(widths[col:col+span]) + 130 | sum(self.columns[i].right_padding 131 | for i in range(col, col+span)) - 1) 132 | shortfall = len(heading) - width 133 | if shortfall > 0: 134 | width += shortfall 135 | # Make the leftmost column in the span wider to fit the heading 136 | widths[col] += shortfall 137 | heading_line.append(heading.ljust(width)) 138 | col += span 139 | result.append(" ".join(heading_line).rstrip()) 140 | 141 | for row in xrange(self.row_count): 142 | l = [] 143 | for col, (column, width) in enumerate(zip(self.columns, widths)): 144 | l.append(column.render(cells[row, col], width)) 145 | line = "".join(l).rstrip() 146 | if line: 147 | result.append(line) 148 | return result 149 | 150 | -------------------------------------------------------------------------------- /play_DCL/gomill/ringmaster_presenters.py: -------------------------------------------------------------------------------- 1 | """Live display for ringmasters.""" 2 | 3 | import os 4 | import subprocess 5 | import sys 6 | from cStringIO import StringIO 7 | 8 | 9 | class Presenter(object): 10 | """Abstract base class for presenters. 11 | 12 | This accepts messages on four _channels_, with codes 13 | warnings 14 | status 15 | screen_report 16 | results 17 | 18 | Warnings are always displayed immediately. 19 | 20 | Some presenters will delay display of other channels until refresh() is 21 | called; some will display them immediately. 22 | 23 | """ 24 | 25 | # If this is true, ringmaster needn't bother doing the work to prepare most 26 | # of the display. 27 | shows_warnings_only = False 28 | 29 | def clear(self, channel): 30 | """Clear the contents of the specified channel.""" 31 | raise NotImplementedError 32 | 33 | def say(self, channel, s): 34 | """Add a message to the specified channel. 35 | 36 | channel -- channel code 37 | s -- string to display (no trailing newline) 38 | 39 | """ 40 | raise NotImplementedError 41 | 42 | def refresh(self): 43 | """Re-render the current screen. 44 | 45 | This typically displays the full status and screen_report, and the most 46 | recent warnings and results. 47 | 48 | """ 49 | raise NotImplementedError 50 | 51 | def get_stream(self, channel): 52 | """Return a file-like object wired up to the specified channel. 53 | 54 | When the object is closed, the text written it is sent to the channel 55 | (except that any trailing newline is removed). 56 | 57 | """ 58 | return _Channel_writer(self, channel) 59 | 60 | class _Channel_writer(object): 61 | """Support for get_stream() implementation.""" 62 | def __init__(self, parent, channel): 63 | self.parent = parent 64 | self.channel = channel 65 | self.stringio = StringIO() 66 | 67 | def write(self, s): 68 | self.stringio.write(s) 69 | 70 | def close(self): 71 | s = self.stringio.getvalue() 72 | if s.endswith("\n"): 73 | s = s[:-1] 74 | self.parent.say(self.channel, s) 75 | self.stringio.close() 76 | 77 | 78 | class Quiet_presenter(Presenter): 79 | """Presenter which shows only warnings. 80 | 81 | Warnings go to stderr. 82 | 83 | """ 84 | shows_warnings_only = True 85 | 86 | def clear(self, channel): 87 | pass 88 | 89 | def say(self, channel, s): 90 | if channel == 'warnings': 91 | print >>sys.stderr, s 92 | 93 | def refresh(self): 94 | pass 95 | 96 | 97 | class Box(object): 98 | """Description of screen layout for the clearing presenter.""" 99 | def __init__(self, name, heading, limit): 100 | self.name = name 101 | self.heading = heading 102 | self.limit = limit 103 | self.contents = [] 104 | 105 | def layout(self): 106 | return "\n".join(self.contents[-self.limit:]) 107 | 108 | class Clearing_presenter(Presenter): 109 | """Low-tech full-screen presenter. 110 | 111 | This shows all channels. 112 | 113 | """ 114 | shows_warnings_only = False 115 | 116 | # warnings has to be last, so we can add to it immediately 117 | box_specs = ( 118 | ('status', None, 999), 119 | ('screen_report', None, 999), 120 | ('results', "Results", 6), 121 | ('warnings', "Warnings", 4), 122 | ) 123 | 124 | def __init__(self): 125 | self.boxes = {} 126 | self.box_list = [] 127 | for t in self.box_specs: 128 | box = Box(*t) 129 | self.boxes[box.name] = box 130 | self.box_list.append(box) 131 | self.clear_method = None 132 | 133 | def clear(self, channel): 134 | self.boxes[channel].contents = [] 135 | 136 | def say(self, channel, s): 137 | self.boxes[channel].contents.append(s) 138 | # 'warnings' box heading might be missing, but never mind. 139 | if channel == 'warnings': 140 | print s 141 | 142 | def refresh(self): 143 | self.clear_screen() 144 | for box in self.box_list: 145 | if not box.contents: 146 | continue 147 | if box.heading: 148 | print "= %s = " % box.heading 149 | print box.layout() 150 | if box.name != 'warnings': 151 | print 152 | 153 | def screen_height(self): 154 | """Return the current terminal height, or best guess.""" 155 | return os.environ.get("LINES", 80) 156 | 157 | def clear_screen(self): 158 | """Try to clear the terminal screen (if stdout is a terminal).""" 159 | if self.clear_method is None: 160 | try: 161 | isatty = os.isatty(sys.stdout.fileno()) 162 | except Exception: 163 | isatty = False 164 | if isatty: 165 | self.clear_method = "clear" 166 | else: 167 | self.clear_method = "delimiter" 168 | 169 | if self.clear_method == "clear": 170 | try: 171 | retcode = subprocess.call("clear") 172 | except Exception: 173 | retcode = 1 174 | if retcode != 0: 175 | self.clear_method = "newlines" 176 | if self.clear_method == "newlines": 177 | print "\n" * (self.screen_height()+1) 178 | elif self.clear_method == "delimiter": 179 | print 78 * "-" 180 | 181 | -------------------------------------------------------------------------------- /play_DCL/config.py: -------------------------------------------------------------------------------- 1 | import sys 2 | debug_flag = False 3 | debug_output = sys.stderr 4 | 5 | komi = 7.5 6 | repeat_check = True 7 | use_threads_scoring_system = False 8 | use_nth_order_liberties = 3 #10 9 | use_nth_order_liberties = 0 10 | use_oxygen = False 11 | 12 | use_lambda = True 13 | debug_tactics = False 14 | sgf_trace_tactics = False 15 | use_shadow_cache = True 16 | #slower settings 17 | #lambda_node_limit = 100 18 | #lambda_slow_node_limit = 3 19 | #lambda_connection_node_limit = 30 20 | #faster setting 21 | 22 | # capture nodes: 23 | # max (min_capture_lambda1_nodes, lambda_node_limit * capture_lambda1_factor_limit * 8(danger sense)) 24 | # life&death: 25 | # lambda_slow_node_limit*other_lambda1_factor_limit*8 (danger sense) *5 (big_block_life_death_increase) 26 | # connection: 27 | # lambda_connection_node_limit*other_lambda1_factor_limit*8 (danger sense) 28 | 29 | lambda_node_limit = 400 #capture tactics 30 | capture_lambda1_factor_limit = 10 #multiplied with above gives 1000 31 | min_capture_lambda1_nodes = 2000 #needed, sometimes 1000 nodes is not enough and this is fatal in ladders (==ladder limit == 2-1 liberty tactics) 32 | lambda_slow_node_limit = 20 #life/death tactics 33 | lambda_connection_node_limit = 100 #connection tactics 34 | other_lambda1_factor_limit = 2 #used for life, death and connection tactics 35 | use_danger_increase = True #if danger is detected, will increase node limits up to 8x 36 | use_big_block_life_death_increase = True #will do up to 5x more life&death nodes for bigger blocks 37 | 38 | # When this is on and there are no tactical moves it tries to find 39 | # biggest unconditional move. This shortens yose significantly. Without 40 | # this it will play in its territory until it accidentally makes it all 41 | # unconditionally alive. Maybe first flag to turn off. First though 42 | # reduce node limits significantly. 43 | use_unconditional_search = True 44 | 45 | # Expensive, allows for better shape defences: This tries other nearby 46 | # moves in hope of finding move that defends same blocks but has better 47 | # shape score. Sometimes expensive when it finds better move and again 48 | # tries better neighbors, etc... 49 | # More important flag than use_danger_increase -flag 50 | try_to_find_better_defense = True 51 | 52 | # Turns on life&death and heuristical death reading. This is expensive even with low node limits. 53 | # Turn this off before try_to_find_alive_move is turned off. 54 | # Should turn this off before ladder limit is turned too low 55 | use_life_and_death = True 56 | 57 | # Crucial and when losing a lot also quite expensive. If this is false it 58 | # will not do tactical reading for selected move. This flag is more 59 | # important than try_to_find_better_defense. Capture node limits should 60 | # probably be < 100 before this is used. However maybe using this before 61 | # reducing ladder limits is reduced dangerously low. 62 | # This disables try_to_find_better_defense also, but it should have been disabled before this anyway. 63 | try_to_find_alive_move = True 64 | 65 | # Crucial! All flags are off and all node limits are at 1 and still not 66 | # enough time? Using this will make engine weaker than WeakBot50k. Maybe 67 | # less than 1s/move. 68 | use_tactical_reading = False 69 | 70 | # *Fast* last resort: needs initialization when turned on (maybe make it 71 | # detect this automatically): This is a bit like IdiotBot, but a bit: 72 | # weaker: It will play into unconditional territories but not into eyes 73 | # and gives priority for atari related moves. See big games project 74 | # images. 1s of available time + time used on netlag should be enough to 75 | # finish game. Initialization + move generation using this faster than 76 | # use_tactical_reading and subsequent moves are generated in less than 1/100s. 77 | play_randomly = False 78 | 79 | 80 | #Plays out using local scoring each block from attack and defense viewpoint 81 | #Use that as status if tactical reading didn't give any result 82 | #replaces most capture tactics and life and death reading 83 | use_playout_reading = True 84 | if use_playout_reading: 85 | ## lambda_node_limit = 30 86 | ## #lambda_slow_node_limit = 1 87 | ## #use_big_block_life_death_increase = False 88 | ## use_life_and_death = False 89 | eye_heuristic_move_limit = 10 90 | ## use_danger_increase = False 91 | 92 | fast_playout = True 93 | if fast_playout: 94 | liberty_range = 2 95 | eye_range = 2 96 | connection_range = 4 97 | else: 98 | liberty_range = 4 99 | eye_range = 3 100 | connection_range = 6 101 | 102 | playout_alpha_beta = False 103 | playout_alpha_beta_games = 13 104 | playout_alpha_beta_depth = 4 105 | 106 | time_usage_ratio = 1.0 #1.0: use all time 107 | 108 | try: 109 | import c_board 110 | use_c = True 111 | monte_carlo_games = 1000 112 | ## pattern_file = "patterns.dat" 113 | ## ok = c_board.load_pattern_result_table(pattern_file) 114 | ## if not ok: 115 | ## sys.stderr.write("Warning: patterns file not loaded!\n") 116 | except: 117 | use_c = False 118 | monte_carlo_games = 0 119 | monte_carlo_games = 0 120 | 121 | use_uct = use_c 122 | uct_count = 100 123 | uct_result_sure = 0.95 124 | uct_enable_nice_endgame = False 125 | 126 | use_uct_tactics = use_uct and False 127 | 128 | use_opening_library = False 129 | 130 | # time management settings 131 | time_per_move_limit = 25 132 | games_per_move_limit = 2**63 #let time to limit usually... 133 | games_per_second_estimate = 2000 134 | manage_time = False 135 | statistics_limit = 20 136 | lambda_node_limit_baseline = 0 137 | node_divisor = 2 138 | factor_divisor = 2 139 | 140 | lambda_limit = 10 141 | use_pn_search = True #Proof number search 142 | lambda_depth_limit = 10001 143 | danger_move_limit = 5 144 | 145 | use_chains = False 146 | 147 | purely_random_no_eye_fill = True 148 | always_remove_opponent_dead = False 149 | --------------------------------------------------------------------------------