def step(self, obs): super(MoveToBeacon, self).step(obs) if MoveToBeacon.loaded == False: self.loadK("8Dense-10-epochs-2-batches-3000-dataSetSize-98%") MoveToBeacon.loaded = True input = obs.observation.feature_minimap[5] for x in input: output = "" for i in x: output+=str(i) output+="" print(output) print("\n") #If maring is selected, use DNN #print("Single: " + (str)(len(obs.observation.single_select))) #print("Multi: " + (str)(len(obs.observation.multi_select))) if self.unit_type_is_selected(obs, units.Terran.Marine): if self.can_do(obs, actions.FUNCTIONS.Attack_screen.id): newInput = self.stencil(obs.observation.feature_minimap[3], obs.observation.feature_minimap[5]) #for x in stencil: # output = "" # for i in x: # output+=str(i) # output+="" # print(output) #print("\n") #for x in input: # output = "" # for i in x: # output+=str(i) # output+="" # print(output) #print("\n") #for x in newInput: # output = "" # for i in x: # output+=str(i) # output+=" " # print(output) #print("\n") newInput = numpy.expand_dims(newInput, axis=2) newInput = newInput.reshape([-1,const.InputSize(),const.InputSize(),1]) prediction = self.model.predict(newInput) outputx = prediction[0][0] * const.ScreenSize().x outputy = prediction[0][1] * const.ScreenSize().y #print(('Network Prediction vs Optimum: ({},{}) ({},{})'.format(int(outputx),int(outputy),beacon.x,beacon.y)), end='\r') return actions.FUNCTIONS.Attack_screen("now", (outputx,outputy)) #Select Marine else: marine = self.get_units_by_type(obs, units.Terran.Marine) if len(marine) > 0: return actions.FUNCTIONS.select_point('select_all_type', (marine[0].x, marine[0].y)) #features.MinimapFeatures. return actions.FUNCTIONS.no_op()
def use_ability(x, y, type, available): act = [get_closest_ability(type, available)] if act == 0: return [0] act.append([int(0)]) act.append([x * const.ScreenSize().x, y * const.ScreenSize().x]) return act
def position_translation(position, value): #Action if (position == 0): #Select point if (value in [2]): return [0, 1] #Select rect # elif (value in [3]): # return [1, 1] #Smart screen elif (value in [451, 452]): return [1, 1] #Attack screen elif (value in [13, 17, 12, 14, 16]): return [2, 1] #Hold pos # elif (value in [274,453]): # return [4, 1] #Select army elif (value in [7]): return [3, 1] #Use ability else: return [4, 1] #Stack action if (position == 1): return [5, value[0] / 4] #Coordinates if (position >= 2): return [position + 4, value / const.ScreenSize().x] print("Something went wrong in position_translation") return 0
def step(self, obs): super(Overmind, self).step(obs) if (not self.loaded): self.loaded = True self.homeHatch = self.get_buildings(obs, units.Zerg.Hatchery)[0] self.model = ks.models.load_model( "C:\\Users\\Charlie\Models\\Conv2D-noobs - 1st Gen") return FUNCTIONS.move_camera( [const.MiniMapSize().x / 2, const.MiniMapSize().y / 2]) # If nothing to macro # if obs.observation.player['food_army'] < 10: # function = self.macro(obs) # print(function) # return function T = Translator() tFeatureLayers = T.translate_feature_layers( T.crop_feature_layers(obs.observation.feature_screen[0], obs.observation.feature_screen, const.ScreenSize().x, const.ScreenSize().y)) featureLayers = (np.moveaxis((np.array(tFeatureLayers)), 0, 2)).reshape(-1, const.ScreenSize().x, const.ScreenSize().y, 12) prediction = self.model.predict(featureLayers)[0] action = translate(obs, prediction) print(action[0]) for pred in prediction: print(pred) if (action[0] in obs.observation.available_actions): return actions.FunctionCall(action[0], action[1:]) else: print("No-op, Switching to Macro:") function = self.macro(obs) print(function) return function
def main(unused_argv): #build_knet() #build_transformer() #check_replay() #build_LSTM() # Dynamically grow the memory used on GPU # config = tf.ConfigProto() # config.gpu_options.allow_growth = True # sess = tf.Session(config=config) # set_session(sess) # for i in range(0, 100): train_LSTM() #transform_replay #Agent agent = Overmind() try: while True: with sc2_env.SC2Env( False, map_name='KingsCove', players=[ sc2_env.Agent(sc2_env.Race.zerg), sc2_env.Bot(sc2_env.Race.terran, sc2_env.Difficulty.very_easy) ], agent_interface_format=features.AgentInterfaceFormat( #What resolution the player sees the world at feature_dimensions=features.Dimensions( screen=const.ScreenSize(), minimap=const.MiniMapSize()), #More indepth unit information use_raw_units=True, use_camera_position=True, #Increase camera size to encompass whole map camera_width_world_units=round(const.WorldSize().x)), #Steps default is 8 per frame (168APM) (16 = 1 second) step_mul=16, # 175, #Max steps per game (0 is infinite) game_steps_per_episode=0, #visualize pysc2 input layers visualize=False, #Play-back-time realtime=False, #Fog of War #disable_fog=False, ) as env: run_loop.run_loop([agent], env) except KeyboardInterrupt: pass
def attack_point(x, y): act = [12] act.append([int(0)]) act.append([x * const.ScreenSize().x, y * const.ScreenSize().x]) return act
def smart_screen(x, y): act = [451] act.append([int(0)]) act.append([x * const.ScreenSize().x, y * const.ScreenSize().x]) return act
def select_rect(x1, y1, x2, y2): act = [3] act.append([int(0)]) act.append([x1 * const.ScreenSize().x, y1 * const.ScreenSize().x]) act.append([x2 * const.ScreenSize().x, y2 * const.ScreenSize().x]) return act
def select_point(type, x, y): act = [2] act.append([int(type)]) act.append([x * const.ScreenSize().x, y * const.ScreenSize().x]) return act
def step(self, obs): super(GenerateMoveToBeaconTestData, self).step(obs) # Extract file code # with open ('training_data/0.csv') as csv_file: # reader = csv.reader(csv_file) # count = 0 # for row in reader: # if count == 0: # GenerateMoveToBeaconTestData.packagedInput = row # if count == 2: # GenerateMoveToBeaconTestData.packagedOutput = row # count+=1 #If previous action was successful, record as training data if obs.reward > 0: #counter = 0 #output = "" #for x in GenerateMoveToBeaconTestData.packagedInput: # output+=str(x) # output+=" " # counter+=1 # if (counter == const.InputSize()): # print(output) # output = "" # counter = 0 #print(GenerateMoveToBeaconTestData.packagedOutput) fileName = 'raw_training_data/' + str(self.packageCounter) + '.csv' with open(fileName, mode='w', newline='') as file: writer = csv.writer(file) writer.writerows(self.packagedInput) writer.writerow(self.packagedOutput) self.packageCounter+=1 #if (self.packageCounter == 3000): # self.packageCounter = 4000 newinput = obs.observation.feature_screen[6] #stencil = obs.observation.feature_minimap[3] ##24x24 is refined input data size ##Use camera stencil to grab relevent data #newinput = numpy.zeros((const.InputSize(),const.InputSize()),int) #counterx = 0 #countery = 0 #for numy, y in enumerate(stencil): # for numx, x in enumerate(y): # if (x == 1): # newinput[countery][counterx] = input[numy][numx] # counterx+=1 # if (counterx == const.InputSize()): # countery+=1 # counterx=0 for unit in obs.observation.feature_units: if(unit.unit_type == 317): beacon = unit #for x in newinput: # output = "" # for i in x: # output+=str(i) # output+="" # print(output) #print("\n") #Screen is not 84x84 but ~84x60 but 84x84 for simplicity outputx = beacon.x #random.randint(0,const.ScreenSize()) outputy = beacon.y #random.randint(0,const.ScreenSize()) self.packagedInput = newinput #/84 to get a number between 0 and 1 as outputs for DNN self.packagedOutput = [float(round(Decimal(outputx/const.ScreenSize().x),2)), float(round(Decimal(outputy/const.ScreenSize().y),2))] if self.unit_type_is_selected(obs, units.Terran.Marine): if self.can_do(obs, actions.FUNCTIONS.Attack_screen.id): return actions.FUNCTIONS.Attack_screen("now", (outputx,outputy)) #Select Marine else: marine = self.get_units_by_type(obs, units.Terran.Marine) if len(marine) > 0: return actions.FUNCTIONS.select_point('select_all_type', (marine[0].x, marine[0].y)) #features.MinimapFeatures. return actions.FUNCTIONS.no_op()
def step(self, obs): super(DefeatEnemies, self).step(obs) if DefeatEnemies.loaded == False: self.loadK("LSTM84") DefeatEnemies.loaded = True if self.unit_type_is_selected(obs, units.Terran.Marine): if self.can_do(obs, actions.FUNCTIONS.Attack_screen.id): #84x84 Detailed #Things to be known: #Height_map(0) visibility(1) creep(2) Player_relative(5) Unit_type(6) selected(7) hit_points(8) unit_density(11) input = obs.observation.feature_screen[6] #features.Dimensions.screen #31x31 Simplified #input = obs.observation.feature_screen[5] #stencil = obs.observation.feature_screen[3] #newInput = numpy.zeros((const.InputSize(),const.InputSize()),int) #counterx = 0 #countery = 0 #for numy, y in enumerate(stencil): # for numx, x in enumerate(y): # if (x == 1): # newInput[countery][counterx] = input[numy][numx] # counterx+=1 # if (counterx == const.InputSize()): # countery+=1 # counterx=0 obs.observation for x in obs.observation.feature_screen[6]: output = "" for i in x: output+=str(i) output+="" print(output) print("\n") newInput = numpy.expand_dims(input, axis=2) newInput = newInput.reshape([-1,const.InputSize(),const.InputSize(),1]) #self.model.fit(TD[0], TD[1], #batch_size=batch_size, #epochs=epochs, #validation_split=0.1, #shuffle=False, verbose=verbose) prediction = self.model.predict(newInput) #for marines in obs.observation.multi_select: # obs.observation.multi_select[0] # prediction = self.model.predict(newInput) outputx = prediction[0][0] * const.ScreenSize().x outputy = prediction[0][1] * const.ScreenSize().y #return actions.FunctionCall(function_id, args) return actions.FUNCTIONS.Attack_screen("now", (outputx,outputy)) else: marine = self.get_units_by_type(obs, units.Terran.Marine) if len(marine) > 0: return actions.FUNCTIONS.select_point('select_all_type', (marine[0].x, marine[0].y))