Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Cost randomization, smol standard task
  • Loading branch information
cswinter committed May 11, 2020
1 parent fe9ae5b commit 3b90b08
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
11 changes: 10 additions & 1 deletion codecraft.py
Expand Up @@ -27,7 +27,16 @@ def create_game(game_length: int = None,
f'&actionDelay={action_delay}'
f'&scriptedOpponent={scripted_opponent}'
f'&idleOpponent={idle_opponent}'
f'&mothershipDamageMultiplier={rules.mothership_damage_multiplier}',
f'&mothershipDamageMultiplier={rules.mothership_damage_multiplier}'
f'&costModifierSize1={rules.cost_modifier_size[0]}'
f'&costModifierSize2={rules.cost_modifier_size[1]}'
f'&costModifierSize3={rules.cost_modifier_size[2]}'
f'&costModifierSize4={rules.cost_modifier_size[3]}'
f'&costModifierConstructor={rules.cost_modifier_constructor}'
f'&costModifierStorage={rules.cost_modifier_storage}'
f'&costModifierShields={rules.cost_modifier_shields}'
f'&costModifierMissiles={rules.cost_modifier_missiles}'
f'&costModifierEngines={rules.cost_modifier_engines}',
json=custom_map).json()
else:
response = requests.post(f'http://localhost:9000/start-game?actionDelay={action_delay}').json()
Expand Down
73 changes: 71 additions & 2 deletions gym_codecraft/envs/codecraft_vec_env.py
Expand Up @@ -3,6 +3,7 @@

from enum import Enum
from dataclasses import dataclass
from typing import Tuple
import numpy as np

import codecraft
Expand Down Expand Up @@ -87,6 +88,12 @@ def endallenemies(self):
@dataclass(frozen=True)
class Rules:
mothership_damage_multiplier: float = 1.0
cost_modifier_size: Tuple[float, float, float, float] = (1.0, 1.0, 1.0, 1.0)
cost_modifier_missiles: float = 1.0
cost_modifier_shields: float = 1.0
cost_modifier_storage: float = 1.0
cost_modifier_constructor: float = 1.0
cost_modifier_engines: float = 1.0


def drone_dict(x, y,
Expand Down Expand Up @@ -120,6 +127,17 @@ def random_drone():
def random_rules(randomness: float):
return Rules(
mothership_damage_multiplier=2 ** np.random.uniform(0.0, 4.0 * randomness),
cost_modifier_size=(
1.0,
1.0 - np.random.uniform(0.0, 0.15 * randomness),
1.0 - np.random.uniform(0.0, 0.15 * randomness),
1.0 - np.random.uniform(0.0, 0.4 * randomness),
),
cost_modifier_constructor=1.0-np.random.uniform(0.0, 0.4 * randomness),
cost_modifier_missiles=1.0-np.random.uniform(0.0, 0.1 * randomness),
cost_modifier_shields=1.0-np.random.uniform(0.0, 0.1 * randomness),
cost_modifier_storage=1.0-np.random.uniform(0.0, 0.4 * randomness),
cost_modifier_engines=1.0-np.random.uniform(0.0, 0.3 * randomness),
)


Expand Down Expand Up @@ -249,6 +267,40 @@ def map_arena_medium_large_ms(randomize: bool, hardness: int):
}


def map_smol_standard(randomize: bool, hardness: int):
ms = dict(constructors=3,
storage_modules=3,
missile_batteries=3,
shield_generators=1,
resources=10)
ms2 = ms.copy()
if randomize:
imbalance = np.random.randint(-10, 11)
ms['resources'] += imbalance
ms2['resources'] -= imbalance
if randomize:
hardness = np.random.randint(0, hardness+1)
if hardness == 0:
map_width = 2000
map_height = 2000
mineral_count = 8
else:
map_width = 2500
map_height = 2500
mineral_count = 13

angle = 2 * np.pi * np.random.rand()
spawn_x = (map_width // 2 - 100) * np.sin(angle)
spawn_y = (map_height // 2 - 100) * np.cos(angle)
return {
'mapWidth': map_width,
'mapHeight': map_height,
'minerals': mineral_count * [(3, 25)],
'player1Drones': [drone_dict(spawn_x, spawn_y, **ms)],
'player2Drones': [drone_dict(-spawn_x, -spawn_y, **ms2)],
}


def map_arena(randomize: bool, hardness: int):
if randomize:
hardness = np.random.randint(0, hardness+1)
Expand Down Expand Up @@ -597,6 +649,21 @@ def __init__(self,
[0, 1, 0, 0, 1]
]
self.custom_map = map_arena
elif objective == Objective.SMOL_STANDARD:
self.game_length = 5 * 60 * 60
self.builds = [
[1, 0, 1, 0, 0],
[0, 2, 0, 0, 0],
[0, 1, 0, 0, 1],
[0, 3, 0, 0, 1],
[0, 2, 0, 0, 2],
[2, 1, 1, 0, 0],
[2, 0, 2, 0, 0],
[2, 0, 1, 1, 0],
[0, 2, 0, 1, 1],
[1, 0, 0, 0, 0],
]
self.custom_map = map_smol_standard
elif objective == Objective.STANDARD:
self.game_length = 5 * 60 * 60
self.builds = [
Expand Down Expand Up @@ -909,6 +976,7 @@ class Objective(Enum):
ARENA_MEDIUM_LARGE_MS = 'ARENA_MEDIUM_LARGE_MS'
ARENA = 'ARENA'
STANDARD = 'STANDARD'
SMOL_STANDARD = 'SMOL_STANDARD'
MICRO_PRACTICE = 'MICRO_PRACTICE'
SCOUT = 'SCOUT'

Expand All @@ -923,15 +991,16 @@ def vs(self):
self == Objective.ARENA_TINY_2V2 or\
self == Objective.ARENA_MEDIUM or\
self == Objective.ARENA or\
self == Objective.STANDARD or\
self == Objective.STANDARD or \
self == Objective.SMOL_STANDARD or\
self == Objective.MICRO_PRACTICE or\
self == Objective.ARENA_MEDIUM_LARGE_MS:
return True
else:
raise Exception(f'Objective.vs not implemented for {self}')

def naction(self):
if self == Objective.STANDARD:
if self == Objective.STANDARD or self == Objective.SMOL_STANDARD:
return 18
elif self == Objective.ARENA:
return 11
Expand Down
6 changes: 6 additions & 0 deletions main.py
Expand Up @@ -424,6 +424,12 @@ def eval(policy,
}
randomize = True
hardness = 4
elif objective == envs.Objective.SMOL_STANDARD:
opponents = {
'alpha': {'model_file': 'standard/curious-dust-35M.pt'},
}
randomize = True
hardness = 1
elif objective == envs.Objective.MICRO_PRACTICE:
opponents = {
'beta': {'model_file': 'mp/ethereal-bee-40M.pt'},
Expand Down

0 comments on commit 3b90b08

Please sign in to comment.