Compare commits

...
This repository has been archived on 2023-12-31. You can view files and clone it, but cannot push or open issues or pull requests.

2 Commits

Author SHA1 Message Date
linhara
b80df9cd4f PID logic completed but not implemented, constants need tuning 2020-10-07 00:46:35 +02:00
linhara
29e4a43f63 Auto stash before merge of "linus" and "origin/felix-lek" 2020-10-04 15:43:04 +02:00
2 changed files with 187 additions and 17 deletions

View File

@ -1,7 +0,0 @@
import main
number_of_launches = 6
result_list = []
for i in range(number_of_launches):
result_list.append(main.main())
for result in result_list:
print("Game " + result[0] + " had a score of: " + str(result[1]))

197
main.py
View File

@ -14,6 +14,10 @@ game_layer = GameLayer(api_key)
time_until_run_ends = 70
utilities = 3
money_reserve_multiplier = 1.5
desiredTemperature = 21
#logresidence[i][x] = temperatur nr X i byggnad med index i (andra byggnaden), samma i som state.residences
logResidenceInfo = []
PID_Ivalues = []
def main():
@ -25,6 +29,7 @@ def main():
available_tiles = []
queue_timeout = 1
game_layer.new_game(map_name)
print("Starting game: " + game_layer.game_state.game_id)
game_layer.start_game()
@ -32,6 +37,7 @@ def main():
start_time = time.time()
state = game_layer.game_state
chart_map()
while state.turn < state.max_turns:
state = game_layer.game_state
try:
@ -40,6 +46,7 @@ def main():
ema_k_value = (2/(rounds_between_energy+1))
EMA_temp = state.current_temp * ema_k_value + EMA_temp*(1-ema_k_value)
take_turn()
recordTempHistories(state.residences)
except Exception:
print(traceback.format_exc())
game_layer.end_game()
@ -71,17 +78,90 @@ def take_turn():
for error in state.errors:
print("Error: " + error)
# pre-made test strategy which came with starter kit
#if (i == 0 or i%5 == 0)and i<26:
# game_layer.place_foundation(freeSpace[(i//5)+2], game_layer.game_state.available_residence_buildings[i//5].building_name)
'''
state = game_layer.game_state
if len(state.residences) < 1:
for i in range(len(state.map)):
for j in range(len(state.map)):
if state.map[i][j] == 0:
x = i
y = j
break
game_layer.place_foundation((x, y), game_layer.game_state.available_residence_buildings[0].building_name)
if (game_layer.game_state.turn == 0):
game_layer.place_foundation(freeSpace[2], game_layer.game_state.available_residence_buildings[0].building_name)
the_first_residence = state.residences[0]
if the_first_residence.build_progress < 100:
game_layer.build(freeSpace[2])
if len(state.residences) == 1:
game_layer.place_foundation(freeSpace[3], game_layer.game_state.available_residence_buildings[5].building_name)
the_second_residence = state.residences[1]
if the_second_residence.build_progress < 100:
game_layer.build(freeSpace[3])
if len(state.residences) == 2:
game_layer.place_foundation(freeSpace[5], game_layer.game_state.available_residence_buildings[1].building_name)
the_third_residence = state.residences[2]
if the_third_residence.build_progress < 100:
game_layer.build(freeSpace[5])
if len(state.residences) == 3:
game_layer.place_foundation((4,4), game_layer.game_state.available_residence_buildings[4].building_name)
the_fourth_residence = state.residences[3]
if the_fourth_residence.build_progress < 100:
game_layer.build((4,4))
if len(state.residences) == 4:
game_layer.place_foundation((4,5), game_layer.game_state.available_residence_buildings[3].building_name)
the_fifth_residence = state.residences[4]
if the_fifth_residence.build_progress < 100:
game_layer.build((4,5))
if len(state.residences) == 5:
game_layer.place_foundation((4,6), game_layer.game_state.available_residence_buildings[4].building_name)
the_sixth_residence = state.residences[5]
if (the_sixth_residence.build_progress < 100) and game_layer.game_state.funds > 4000:
game_layer.build((4,6))
for i in range(len(state.residences)):
if state.residences[i].health < 45:
game_layer.maintenance(state.residences[i].X, state.residences[i].Y)
for i in range(len(state.residences)):
if game_layer.game_state.turn % ROUNDVARIABLE == i:
adjustEnergy(the_first_residence)
elif the_first_residence.health < :
game_layer.maintenance(freeSpace[2])
elif the_second_residence.health < 70:
game_layer.maintenance(freeSpace[3])
elif the_third_residence.health < 70:
game_layer.maintenance(freeSpace[5])
elif the_fourth_residence.health < 70:
game_layer.maintenance((4,4))
elif the_fifth_residence.health < 70:
game_layer.maintenance((4,5))
elif the_sixth_residence.health < 70:
game_layer.maintenance((4,6))
elif (the_second_residence.health > 70) and not len(state.utilities) > 0:
game_layer.place_foundation(freeSpace[4], game_layer.game_state.available_utility_buildings[2].building_name)
elif (state.utilities[0].build_progress < 100):
game_layer.build(freeSpace[4])
#elif (game_layer.game_state.turn > 35) and not len(state.utilities) > 1:
# game_layer.place_foundation((4,6), game_layer.game_state.available_utility_buildings[1].building_name)
#elif (state.utilities[1].build_progress < 100):
# game_layer.build((4,6))
elif (game_layer.game_state.turn % rounds_between_energy == 0):
adjustEnergy(the_first_residence)
elif (game_layer.game_state.turn % rounds_between_energy == 1):
adjustEnergy(the_second_residence)
elif (game_layer.game_state.turn % rounds_between_energy == 2):
adjustEnergy(the_third_residence)
elif (game_layer.game_state.turn % rounds_between_energy == 3):
adjustEnergy(the_fourth_residence)
elif (game_layer.game_state.turn % rounds_between_energy == 4):
adjustEnergy(the_fifth_residence)
elif (game_layer.game_state.turn % rounds_between_energy == 5):
adjustEnergy(the_sixth_residence)
else:
the_only_residence = state.residences[0]
if the_only_residence.build_progress < 100:
@ -304,6 +384,18 @@ def chart_map():
optimize_available_tiles()
def evaluateTile(tile):
# score -1 för att ta bort själva tilen man checkar
score = -1
x = tile[0]
y = tile[1]
for i in range(5):
for j in range(5):
if state.map[x - 2 + i][y - 2 + i] and abs(i - 2) + abs(j - 2) <= 2:
score += 1
def optimize_available_tiles():
global average_x, average_y, score_list
average_x = 0
@ -381,5 +473,90 @@ def build(structure):
return False
def check_energies(buildings):
for building in enumerate(buildings):
if not 19 < building[1].temperature < 23:
adjust_energy_PID(building[0], building[1])
return False
def adjust_energy_PID(index, current_building):
newEnergy = 0
blueprint = game_layer.get_residence_blueprint(current_building.building_name)
base_energy = blueprint.base_energy_need
global state, desiredTemperature, PID_Ivalues
KP, KI, KD = getBuildingConstants(current_building.building_name)
P = (desiredTemperature - current_building.temperature) * KP
I = current_building.I + (
desiredTemperature - current_building.temperature) * KI # TODO fixa current_bulding.I PID_Ivalues listan
D = calcCurrentD(logResidenceInfo[index]) * KD # jag är genius
newEnergy = P + I + D
if newEnergy + base_energy < base_energy:
game_layer.adjust_energy_level((current_building.X, current_building.Y), base_energy + 0.01)
return True
elif newEnergy + base_energy > base_energy:
game_layer.adjust_energy_level((current_building.X, current_building.Y), newEnergy + base_energy)
return True
else:
return False
def calcCurrentD(tmp_history):
# måste hitta necessaryDenominator för nytt nrDerivativeDots
ans = 0
consts = [-2, -1, 0, 1, 2]
nrDerivativeDots = 5 # endast udda antal
necessaryDenominator = 10
# for currDerivativeConstant in (range(-1*(nrDerivativeDots//2), (nrDerivativeDots//2)+1)): #+1 pga non-inclusive
for i in range(5):
ans += tmp_history[i] * consts[i]
return ans / necessaryDenominator
def recordTempHistories(buildings):
global logResidenceInfo, PID_Ivalues
while len(logResidenceInfo) < len(buildings):
logResidenceInfo.append([])
while len(PID_Ivalues) < len(buildings):
PID_Ivalues.append(3) # nu blir 3 I värdets start value på alla byggnader
for building in enumerate(buildings):
logResidenceInfo[building[0]].append(building[1].temperature)
# testHouse = buildings[0]
# testHouse.a = 1
# logResidenceInfo[0].append(testHouse.temperature)
# for building in buildings:
# building.tmp_History.append(building.temperature)
# f = open("tempLog.txt", "a+")
# f.write(str(game_layer.game_state.turn))
# f.write("; ")
# f.write(str(logResidenceInfo[0][-1]))
# f.write("; ")
# f.write(str(game_layer.game_state.current_temp))
# f.write("; ")
# if game_layer.game_state.turn > 5:
# d = calcCurrentD(logResidenceInfo[0][-5:])
# f.write(str(d))
# f.write("\r")
# f.close()
# if state.turn == 30:
# print(logResidenceInfo[0])
# for building in buildings:
# building.tmp_History.append(building.temperature)
def getBuildingConstants(building_name):
valuesDict = {"Apartments": (0.1, 0.3, 0.3), "ModernApartments": (0.1, 0.3, 0.3), "Cabin": (0.1, 0.3, 0.3),
"EnvironmentalHouse": (0.1, 0.3, 0.3), "HighRise": (0.1, 0.3, 0.3),
"LuxuryResidence": (0.1, 0.3, 0.3)}
return valuesDict.get(building_name)
if __name__ == "__main__":
main()