-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from Galtvam/gcloud-workers
Google Cloud Workers
- Loading branch information
Showing
16 changed files
with
1,434 additions
and
372 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM tensorflow/tensorflow | ||
|
||
COPY requirements.txt requirements.txt | ||
|
||
RUN pip install -r requirements.txt | ||
|
||
ENTRYPOINT [ "python" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
# OthelloZero | ||
[![license](https://img.shields.io/badge/license-GPL%20v3.0-brightgreen.svg?style=flat-square)](https://github.com/Galtvam/OthelloZero/blob/main/LICENSE) | ||
![LastCommit](https://img.shields.io/github/last-commit/Galtvam/OthelloZero?style=flat-square) | ||
![CommitSinceLastversion](https://img.shields.io/github/commits-since/Galtvam/OthelloZero/0.0.1/master?label=Commits%20Since%20Lastest%20Version&style=flat-square) | ||
# OthelloZero | ||
|
||
## How to create SSH private key: | ||
```ssh-keygen -t rsa -f ~/.ssh/othello-zero -C othello-zero``` | ||
|
||
Add private-keys to Computer Engine metadatas |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import random | ||
import logging | ||
import numpy as np | ||
|
||
from Net.NNet import NeuralNets | ||
from Othello import OthelloGame, OthelloPlayer, BoardView | ||
|
||
from othelo_mcts import OthelloMCTS | ||
|
||
|
||
class OthelloAgent: | ||
def __init__(self, game): | ||
self.game = game | ||
|
||
def play(self): | ||
"""Do an action on OthelloGame""" | ||
raise NotImplementedError | ||
|
||
|
||
class RandomOthelloAgent(OthelloAgent): | ||
def play(self): | ||
possible_moves = tuple(self.game.get_valid_actions()) | ||
move = random.choice(possible_moves) | ||
self.game.play(*move) | ||
|
||
|
||
class GreedyOthelloAgent(OthelloAgent): | ||
def play(self): | ||
move_points = {} | ||
possible_moves = tuple(self.game.get_valid_actions()) | ||
points_before = game.get_players_points()[game.current_player] | ||
board = self.game.board(BoardView) | ||
|
||
for move in possible_moves: | ||
state = np.copy(self.game.board(BoardView)) | ||
OthelloGame.flip_board_squares(state, game.current_playe, *move) | ||
points = OthelloGame.get_board_players_points(state)[OthelloPlayer.BLACK] - points_before | ||
move_points[move] = points | ||
|
||
greedy_move = max(move_points, key=move_points.get) | ||
game.play(*greedy_move) | ||
|
||
|
||
class NeuralNetworkOthelloAgent(OthelloAgent): | ||
def __init__(self, game, neural_network, num_simulations, degree_exploration, temperature=0): | ||
self.temperature = 0 | ||
self.neural_network = neural_network | ||
self.num_simulations = num_simulations | ||
self.mcts = OthelloMCTS(game.board_size, neural_network, degree_exploration) | ||
super().__init__(game) | ||
|
||
def play(self): | ||
state = self.game.board(BoardView.TWO_CHANNELS) | ||
for _ in range(self.num_simulations): | ||
self.mcts.simulate(state, self.game.current_player) | ||
|
||
if self.game.current_player == OthelloPlayer.WHITE: | ||
state = OthelloGame.invert_board(state) | ||
|
||
if self.neural_network.network_type is NeuralNets.ONN: | ||
action_probabilities = self.mcts.get_policy_action_probabilities(state, self.temperature) | ||
else: | ||
action_probabilities = self.mcts.get_policy_action_probabilities( | ||
self.game.board(), self.temperature) | ||
|
||
valid_actions = self.game.get_valid_actions() | ||
best_action = max(valid_actions, key=lambda position: action_probabilities[tuple(position)]) | ||
self.game.play(*best_action) | ||
|
||
|
||
def duel_between_agents(game, agent_1, agent_2): | ||
players_agents = { | ||
OthelloPlayer.BLACK: agent_1, | ||
OthelloPlayer.WHITE: agent_2 | ||
} | ||
|
||
logging.info(f'Duel - Started') | ||
while not game.has_finished(): | ||
logging.info(f'Duel - Round: {game.round}') | ||
agent = players_agents[game.current_player] | ||
agent.play() | ||
|
||
winner, points = game.get_winning_player() | ||
return players_agents[winner], points |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#! /bin/bash | ||
|
||
LOGIN_USER=othello-zero | ||
STARTUP_SUCCESS_FILE=/home/$LOGIN_USER/.ran-startup-script | ||
|
||
if test ! -f "$STARTUP_SUCCESS_FILE"; then | ||
echo "$STARTUP_SUCCESS_FILE does not exist. running startup..." | ||
|
||
# add user | ||
sudo useradd -m $LOGIN_USER | ||
|
||
# no more 'sudo docker' after this | ||
sudo groupadd docker | ||
sudo usermod -aG docker $LOGIN_USER | ||
newgrp docker | ||
|
||
# make sure docker-credential-gcloud is in PATH | ||
# https://stackoverflow.com/questions/54494386/gcloud-auth-configure-docker-on-gcp-vm-instance-with-ubuntu-not-setup-properly | ||
sudo ln -s /snap/google-cloud-sdk/current/bin/docker-credential-gcloud /usr/local/bin | ||
|
||
# make gcloud docker's credential helper | ||
sudo -u $LOGIN_USER bash -c 'gcloud auth configure-docker --quiet' | ||
|
||
# host machine requires nvidia drivers. tensorflow image should contain the rest required | ||
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin | ||
sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600 | ||
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub | ||
sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /" | ||
sudo apt-get update && sudo apt-get install -y cuda-drivers | ||
|
||
# install docker | ||
sudo apt-get update && apt-get install -y \ | ||
apt-transport-https \ | ||
ca-certificates \ | ||
curl \ | ||
gnupg-agent \ | ||
software-properties-common | ||
|
||
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - | ||
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | ||
sudo apt-get update && sudo apt-get install -y docker-ce docker-ce-cli containerd.io | ||
|
||
# install nvidia docker support | ||
distribution=$(. /etc/os-release;echo $ID$VERSION_ID) | ||
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add - | ||
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list | ||
sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit | ||
sudo systemctl restart docker | ||
|
||
docker pull igorxp5/othello-zero | ||
|
||
# create file which will be checked on next reboot | ||
touch /home/$LOGIN_USER/.ran-startup-script | ||
else | ||
echo "$STARTUP_SUCCESS_FILE exists. not running startup script!" | ||
fi |
Oops, something went wrong.