Skip to content

Commit

Permalink
Added lots of important files
Browse files Browse the repository at this point in the history
  • Loading branch information
Royz2123 authored and Royz2123 committed May 28, 2019
1 parent f5d387d commit da7f0e7
Show file tree
Hide file tree
Showing 281 changed files with 65,957 additions and 76 deletions.
16 changes: 9 additions & 7 deletions army_genetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@
import util

MAIN_BOARD = generate_base.generate_main_base()
SAVE_FOLDER = "./optimize_viz/"
SAVE_FOLDER = "./optimize_viz/genetics/army_genetics/"

class ArmyGenetics:
MAXIMIZE_FITNESS = True

"""
This class manages all of the functions necessery in order to run GA.
"""
DEFAULT_MODE = DESTORYER

def __init__(self, army=None, buildings=MAIN_BOARD, mode=DEFAULT_MODE):
if army is None:
army, titles = generate_army.generate_random_army()

self._army = army
self._game_board = GameBoard(buildings)
def __init__(self, level=2, mode=DEFAULT_MODE):
self._army, titles = generate_army.generate_army_by_level(townhall_level=level)
self._game_board = GameBoard(generate_base.generate_base_by_level(level))
self._sim = Simulator(self._game_board)
self._fit = 0
self._mode = mode
Expand Down Expand Up @@ -63,6 +62,9 @@ def calc_fitness(self):

print(self._fit)

def minimize_fitness(self):
return False

def get_fitness(self):
return self._fit

Expand Down
2 changes: 1 addition & 1 deletion board.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def add_emptys(self):
for size in range(1,GameBoard.BIGGEST_BUILDING_SIZE+1):
for i in range(BOARD_SIZE-size):
for j in range(BOARD_SIZE-size):
if (board[i:i+size][j:j+size]==BUILDINGS_MAP.get(Empty)).all():
if (board[i:i+size,j:j+size]==BUILDINGS_MAP.get(Empty)).all():
self._buildings.append(Empty(size=size,pos=(i,j)))

def get_defensive_buildings(self):
Expand Down
92 changes: 46 additions & 46 deletions board_genetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,79 @@
import generate_base


SAVE_FOLDER = "./optimize_viz/genetics/board_genetics/"


class BoardGenetics:
MAXIMIZE_FITNESS = False
DEFAULT_MODE = DESTORYER

"""
This class manages all of the functions necessery in order to run GA.
"""

def __init__(self, buildings=None):
if buildings is None:
self.quants = QUANTS
self.levels = LEVELS
buildings = generate_base.generate_random_base(self.quants, self.levels)
else:
self.quants = [0] * len(BUILDINGS_MAP)
self.levels = [0] * len(BUILDINGS_MAP)
for building in buildings:
index = BUILDINGS_MAP[building.__class__]
self.quants[index] += 1
self.levels[index] = building.get_level()

self.game_board = GameBoard(buildings)
self.sim = Simulator(self.game_board)
self._fit=SMALL_NUMBER
def __init__(self, army, level=4, mode=DEFAULT_MODE):
self._army = army
self._game_board = GameBoard(generate_base.generate_random_base_by_level(level=level))
self._sim = Simulator(self._game_board)
self._fit = SMALL_NUMBER
self._mode = mode
self.calc_fitness()

def minimize_fitness(self):
return True

def run(self):
army, titles = generate_random_army()
return self.sim.run(army)
return self._sim.run(self._army)

"""
Our fitness function, currently linear.
"""

def calc_fitness(self):
outcome = self.run()
self._fit = 2 * outcome["percent"] + 0.3 * outcome["stars"] + outcome["gold"] + outcome["elixir"]
try:
outcome = self.run()

# calculate fitness
if self._mode == REGULAR:
self._fit = 2 * outcome["percent"] + 0.3 * outcome["stars"] + outcome["gold"] + outcome["elixir"]
elif self._mode == DESTORYER:
self._fit = 2 * outcome["percent"] + 0.3 * outcome["stars"]
elif self._mode == GOLD_DIGGER:
self._fit = outcome["gold"]
elif self._mode == ELIXIR_LOVER:
self._fit = outcome["elixir"]
elif self._mode == GOLD_DIGGER:
self._fit = outcome["gold"] + outcome["elixir"]

except Exception as e:
print(e)
self._fit = 0

print(self._fit)

def get_fitness(self):
return self._fit

def viz(self, index):
self.game_board.update_viz()
def viz(self, index, viz_mode=True):
if viz_mode:
path = SAVE_FOLDER + "%04d.png" % index
board_viz.viz_board(game_board=self._game_board, army=self._army, viz=False, path=path)
self._game_board.update_viz()

"""
Our mutation function.
"""

def mutation(self,mutation_rate):
def mutation(self):
# getting buildings and buildings that are not empty
buildings = self.game_board.get_buildings()
buildings = self._game_board.get_buildings()
non_emptys = [building for building in buildings if
building.__class__ != Empty]
# running through all the non empty buildings and try to replace them
# with other buildings in the same size (including emptys)
for i in range(len(non_emptys)):
if random.random() < mutation_rate:
if random.random() < MUTATION_RATE:
build1 = buildings[i]
same_size = [build for build in buildings if
build.get_size() == build1.get_size()]
Expand All @@ -70,26 +88,8 @@ def mutation(self,mutation_rate):
buildings[i].set_pos(build2.get_pos())
# avoiding overlap
buildings[buildings.index(same_size[chosen_index])].set_pos(build1.get_pos())
self.game_board.set_buildings(buildings)
self.sim.set_game_board(self.game_board)
self._game_board.set_buildings(buildings)
self._sim.set_game_board(self._game_board)
self.calc_fitness()
self.game_board.add_emptys()

self._game_board.add_emptys()

def lst_1D_to_2D(l, n):
return [l[i:i + n] for i in range(0, len(l), n)]


def lst_2D_2_1D(sqr):
"""
This function takes 2D array and transforms it to 1D array
:param sqr: the 2D array that should be changed
:return: the 1D transformed array
"""
if type(sqr[0]) == int:
return sqr
l = []
for i in range(len(sqr)):
for j in range(len(sqr[i])):
l.append(sqr[i][j])
return l
2 changes: 1 addition & 1 deletion constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
BOARD_SIZE = 28
BOARD_SIZE = 30
MARGIN = 20
QUANTS = [0, 2, 2, 1, 1, 1, 2, 2, 2, 4, 4, 3, 1, 2, 75]
LEVELS = [0, 4, 3, 4, 1, 1, 3, 6, 6, 6, 6, 5, 1, 3, 4]
Expand Down
Loading

0 comments on commit da7f0e7

Please sign in to comment.