Skip to content

Commit

Permalink
repackage
Browse files Browse the repository at this point in the history
  • Loading branch information
Sander Land committed May 1, 2020
1 parent e8b0d2a commit 8f8d443
Show file tree
Hide file tree
Showing 17 changed files with 336 additions and 338 deletions.
Empty file removed __init__.py
Empty file.
10 changes: 5 additions & 5 deletions bots/ai2gtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import sys
import time

from ai import ai_move
from common import OUTPUT_DEBUG, OUTPUT_ERROR, OUTPUT_INFO
from core.ai import ai_move
from core.common import OUTPUT_ERROR, OUTPUT_INFO
from bots.settings import bot_strategy_names
from engine import EngineDiedException, KataGoEngine
from game import Game, Move
from sgf_parser import Move
from core.engine import EngineDiedException, KataGoEngine
from core.game import Game
from core.sgf_parser import Move

if len(sys.argv) < 2:
bot = "dev"
Expand Down
4 changes: 2 additions & 2 deletions bots/engine_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import threading
import traceback

from common import OUTPUT_DEBUG, OUTPUT_ERROR, OUTPUT_INFO
from engine import KataGoEngine
from core.common import OUTPUT_INFO
from core.engine import KataGoEngine

PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 8587

Expand Down
10 changes: 5 additions & 5 deletions bots/selfplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from collections import defaultdict
from concurrent.futures.thread import ThreadPoolExecutor

from ai import ai_move
from common import OUTPUT_DEBUG, OUTPUT_ERROR, OUTPUT_INFO
from core.ai import ai_move
from core.common import OUTPUT_ERROR, OUTPUT_INFO
from elote import EloCompetitor
from engine import KataGoEngine
from game import Game
from core.engine import KataGoEngine
from core.game import Game
import json

DB_FILENAME = "bots/ai_performance.pickle"
Expand Down Expand Up @@ -96,7 +96,7 @@ def retrieve_ais(selected_ais):


test_ais = [
# AI("Jigo", {}, {"max_visits": 100}),
# AI("Jigo", {}, {"max_visits": 100}),
AI("Policy", {}),
AI("P:Local", {}),
AI("P:Pick", {}),
Expand Down
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,6 @@
]
},
"debug": {
"level": 1
"level": 0
}
}
4 changes: 4 additions & 0 deletions core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from gui.badukpan import BadukPanControls, BadukPanWidget
from gui.controls import Controls
from gui.kivyutils import *
from gui.popups import LoadSGFPopup
14 changes: 7 additions & 7 deletions ai.py → core/ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import math
import random
import time
from typing import Any, Dict, List, Tuple
from typing import Dict, List, Tuple

import numpy as np

from common import OUTPUT_DEBUG, OUTPUT_ERROR, OUTPUT_INFO, var_to_grid
from engine import EngineDiedException
from game import Game, GameNode, IllegalMoveException, Move
from core.common import OUTPUT_DEBUG, OUTPUT_ERROR, OUTPUT_INFO, var_to_grid
from core.engine import EngineDiedException
from core.game import Game, GameNode, IllegalMoveException, Move


def weighted_selection_without_replacement(items: List[Tuple[float, float, int, int]], pick_n: int) -> List[Tuple[float, float, int, int]]:
Expand Down Expand Up @@ -132,18 +132,18 @@ def ai_move(game: Game, ai_mode: str, ai_settings: Dict) -> Tuple[Move, GameNode
else:
raise ValueError(f"Unknown AI mode {ai_mode}")
elif "balance" in ai_mode and candidate_ai_moves[0]["move"] != "pass": # don't play suicidal to balance score - pass when it's best
sign = cn.player_sign(cn.next_player) # TODO check
sign = cn.player_sign(cn.next_player)
sel_moves = [ # top move, or anything not too bad, or anything that makes you still ahead
move
for i, move in enumerate(candidate_ai_moves)
if i == 0
or move["visits"] >= ai_settings["min_visits"]
and (move["pointsLost"] < ai_settings["random_loss"] or move["pointsLost"] < ai_settings["max_loss"] and sign * move["scoreLead"] > ai_settings["target_score"])
]
aimove = Move.from_gtp(random.choice(sel_moves)["move"], player=cn.next_player) # TODO: could be weighted towards worse
aimove = Move.from_gtp(random.choice(sel_moves)["move"], player=cn.next_player)
ai_thoughts += f"Balance strategy selected moves {sel_moves} based on target score and max points lost, and randomly chose {aimove.gtp()}."
elif "jigo" in ai_mode and candidate_ai_moves[0]["move"] != "pass":
sign = cn.player_sign(cn.next_player) # TODO check
sign = cn.player_sign(cn.next_player)
jigo_move = min(candidate_ai_moves, key=lambda move: abs(sign * move["scoreLead"] - ai_settings["target_score"]))
aimove = Move.from_gtp(jigo_move["move"], player=cn.next_player)
ai_thoughts += f"Jigo strategy found candidate moves {candidate_ai_moves} moves and chose {aimove.gtp()} as closest to 0.5 point win"
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions engine.py → core/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import time
from typing import Callable, Optional

from common import OUTPUT_DEBUG, OUTPUT_ERROR, OUTPUT_EXTRA_DEBUG
from game_node import GameNode
from core.common import OUTPUT_DEBUG, OUTPUT_ERROR, OUTPUT_EXTRA_DEBUG
from core.game_node import GameNode


class EngineDiedException(Exception):
Expand Down
12 changes: 6 additions & 6 deletions game.py → core/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from datetime import datetime
from typing import Dict, List, Union

from common import var_to_grid, OUTPUT_INFO, OUTPUT_ERROR, OUTPUT_DEBUG
from engine import KataGoEngine
from game_node import GameNode
from sgf_parser import SGF, Move
from core.common import var_to_grid, OUTPUT_INFO, OUTPUT_DEBUG
from core.engine import KataGoEngine
from core.game_node import GameNode
from core.sgf_parser import SGF, Move


class IllegalMoveException(Exception):
Expand Down Expand Up @@ -60,7 +60,7 @@ def _calculate_groups(self):
try:
# for m in self.moves:
for node in self.current_node.nodes_from_root:
for m in node.move_with_placements: # TODO: placements are never illegal
for m in node.move_with_placements:
self._validate_move_and_update_chains(m, True) # ignore ko since we didn't know if it was forced
except IllegalMoveException as e:
raise Exception(f"Unexpected illegal move ({str(e)})")
Expand Down Expand Up @@ -109,7 +109,7 @@ def neighbours(moves):
raise IllegalMoveException("Ko")
self.prisoners += self.last_capture

if -1 not in neighbours(self.chains[this_chain]): # TODO: NZ?
if -1 not in neighbours(self.chains[this_chain]): # TODO: NZ rules?
raise IllegalMoveException("Suicide")

# Play a Move from the current position, raise IllegalMoveException if invalid.
Expand Down
5 changes: 2 additions & 3 deletions game_node.py → core/game_node.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import copy
import math
import random
from typing import Dict, List, Optional, Tuple

from common import evaluation_class, var_to_grid
from sgf_parser import Move, SGFNode
from core.common import evaluation_class, var_to_grid
from core.sgf_parser import Move, SGFNode


class GameNode(SGFNode):
Expand Down
Loading

0 comments on commit 8f8d443

Please sign in to comment.