def _init_status(self, env): home_pos = env.unwrapped.owner_base() enemy_pos = env.unwrapped.enemy_base() x_range = self._x_per_unit * self._range_width y_range = self._y_per_unit * self._range_width self._status = RoundTripCourse(home_pos, enemy_pos, (x_range, y_range), self._explore_step) self._status.reset()
def _init_status(self): map_size = self.env.unwrapped.map_size() x_per_unit = map_size[0] / self._compress_width y_per_unit = map_size[1] / self._compress_width home_pos = self.env.unwrapped.owner_base() enemy_pos = self.env.unwrapped.enemy_base() x_range = x_per_unit * self._range_width y_range = y_per_unit * self._range_width self._status = RoundTripCourse(home_pos, enemy_pos, (x_range, y_range), self._explore_step) self._status.reset()
def _reset(self): obs = self.env._reset() map_size = self.env.unwrapped.map_size() x_per_unit = map_size[0] / self._compress_width y_per_unit = map_size[1] / self._compress_width home_pos = self.env.unwrapped.owner_base() enemy_pos = self.env.unwrapped.enemy_base() x_range = x_per_unit * self._range_width y_range = y_per_unit * self._range_width self._status = RoundTripCourse(home_pos, enemy_pos, (x_range, y_range), self._explore_step) self._status.reset() self._home_base = DestRange(self.env.unwrapped.owner_base(), dest_range=14) return obs
class TargetTerminalWrapperV2(gym.Wrapper): def __init__(self, env, compress_width, range_width, explore_step): super(TargetTerminalWrapperV2, self).__init__(env) self._status = None self._compress_width = compress_width self._range_width = range_width self._explore_step = explore_step def _reset(self): obs = self.env._reset() map_size = self.env.unwrapped.map_size() x_per_unit = map_size[0] / self._compress_width y_per_unit = map_size[1] / self._compress_width home_pos = self.env.unwrapped.owner_base() enemy_pos = self.env.unwrapped.enemy_base() x_range = x_per_unit * self._range_width y_range = y_per_unit * self._range_width self._status = RoundTripCourse(home_pos, enemy_pos, (x_range, y_range), self._explore_step) self._status.reset() self._home_base = DestRange(self.env.unwrapped.owner_base()) return obs def _step(self, action): obs, rwd, done, info = self.env._step(action) return obs, rwd, self._check_terminal(obs, done), info def _check_terminal(self, obs, done): if done: return done survive = self.env.unwrapped.scout_survive() if not survive: return True scout = self.env.unwrapped.scout() status = self._status.check_status( (scout.float_attr.pos_x, scout.float_attr.pos_y)) if status == RoundTripStatus.TERMINAL: return True elif status == RoundTripStatus.BACKWORD: if self._home_base.in_range( (scout.float_attr.pos_x, scout.float_attr.pos_y)): print("backward return_HOME,", status) return True return done
def _init_range_and_status(self, env): home = env.unwrapped.owner_base() target = env.unwrapped.enemy_base() map_size = env.unwrapped.map_size() x_per_unit = map_size[0] / self._compress_width y_per_unit = map_size[1] / self._compress_width x_range = x_per_unit * self._range_width y_range = y_per_unit * self._range_width x_radius = x_range / 2 y_radius = y_range / 2 self._x_low = target[0] - x_radius self._x_high = target[0] + x_radius self._y_low = target[1] - y_radius self._y_high = target[1] + y_radius print( 'Evade per_unit=({},{}), radius=({},{}), x_range=({},{}), y_range=({},{}), target={}' .format(x_per_unit, y_per_unit, x_radius, y_radius, self._x_low, self._x_high, self._y_low, self._y_high, target)) self._status = RoundTripCourse(home, target, (x_range, y_range), self._explore_step) self._status.reset()
class ScoutGlobalImgFeatureV3(ImgFeatExtractor): def __init__(self, compress_width, range_width, explore_step, reverse): super(ScoutGlobalImgFeatureV3, self).__init__(compress_width, reverse) self._channel_num = GLOBAL_CHANNEL self._range_width = range_width self._explore_step = explore_step self._status = None def reset(self, env): super(ScoutGlobalImgFeatureV3, self).reset(env) self._init_status(env) print('GlobalImgFeatureV1 radius=({},{}), per_unit=({},{})'.format( self._x_radius, self._y_radius, self._x_per_unit, self._y_per_unit)) def _init_status(self, env): home_pos = env.unwrapped.owner_base() enemy_pos = env.unwrapped.enemy_base() x_range = self._x_per_unit * self._range_width y_range = self._y_per_unit * self._range_width self._status = RoundTripCourse(home_pos, enemy_pos, (x_range, y_range), self._explore_step) self._status.reset() def extract(self, env, obs): owners, neutrals, enemys = self.unit_dispatch(obs) image = np.zeros( [self._compress_width, self._compress_width, self._channel_num]) channel_base = self.target_pos_channel(env, image, 0) channel_base = self.scout_attr_channel(env, image, channel_base) channel_base = self.nertral_attr_channel(neutrals, image, channel_base) channel_base = self.owner_attr_channel(owners, image, channel_base) channel_base = self.enemy_attr_channel(enemys, image, channel_base) channel_base = self.scout_status_channel(env, image, channel_base) #print('channel total number=', channel_base) #print('image=', image) return image def obs_space(self): low = np.zeros( [self._compress_width, self._compress_width, self._channel_num]) high = np.ones( [self._compress_width, self._compress_width, self._channel_num]) return Box(low, high) def home_pos_channel(self, env, image, channel_num): home = env.unwrapped.owner_base() i, j = self.pos_2_2d(home[0], home[1]) #print("home coordinate({}, {}), home={}".format(i, j, home)) image[i, j, channel_num] += 1 return channel_num + 1 def target_pos_channel(self, env, image, channel_num): target = env.unwrapped.enemy_base() i, j = self.pos_2_2d(target[0], target[1]) #print("target coordinate({}, {}), target={}".format(i, j, target)) image[i, j, channel_num] += 1 return channel_num + 1 def scout_attr_channel(self, env, image, channel_base): scout = env.unwrapped.scout() i, j = self.pos_2_2d(scout.float_attr.pos_x, scout.float_attr.pos_y) image[i, j, channel_base + 0] = 1 # scout in this position #print("scout coordinate({}, {}), scout=({},{})".format(i, j, # scout.float_attr.pos_x, scout.float_attr.pos_y)) if scout.bool_attr.is_flying: image[i, j, channel_base + 1] = 1 # flying else: image[i, j, channel_base + 1] = 0 # flying image[i, j, channel_base + 2] = scout.float_attr.facing image[i, j, channel_base + 3] = scout.float_attr.radius image[i, j, channel_base + 4] = scout.float_attr.health / scout.float_attr.health_max #print("scout image attr:", image[i,j,:]) #print("scout image attr: facing={},radius={},health={}".format( # scout.float_attr.facing, scout.float_attr.radius, scout.float_attr.health)) return channel_base + 5 def scout_status_channel(self, env, image, channel_base): scout = env.unwrapped.scout() self._status.check_status( (scout.float_attr.pos_x, scout.float_attr.pos_y)) ones = np.ones([self._compress_width, self._compress_width]) zeros = np.zeros([self._compress_width, self._compress_width]) if self._status.status() == RoundTripStatus.EXPLORE: image[:, :, channel_base] = ones #print('explore feature') else: image[:, :, channel_base] = zeros #print('backward feature') return channel_base + 1 def nertral_attr_channel(self, neutrals, image, channel_base): nertral_count = 0 resource_count = 0 for u in neutrals: i, j = self.pos_2_2d(u.float_attr.pos_x, u.float_attr.pos_y) if u.unit_type in sm.MINERAL_UNITS or u.unit_type in sm.VESPENE_UNITS: resource_count += 1 image[i, j, channel_base + 0] += 1 #print('**resrouce unit=', u.tag, '; resource type=', u.unit_type) else: nertral_count += 1 image[i, j, channel_base + 1] += 1 #print('--neutral unit=', u.tag, '; enutral type=', u.unit_type) #print("---nertral_count---,", nertral_count) #print("***nertral_count***,", resource_count) return channel_base + 2 def owner_attr_channel(self, owners, image, channel_base): for u in owners: i, j = self.pos_2_2d(u.float_attr.pos_x, u.float_attr.pos_y) if u.unit_type in sm.BASE_UNITS: image[i, j, channel_base + 0] += 1 elif u.unit_type in sm.BUILDING_UNITS: image[i, j, channel_base + 1] += 1 elif u.unit_type in sm.COMBAT_AIR_UNITS: image[i, j, channel_base + 2] += 1 elif u.unit_type in sm.COMBAT_UNITS: image[i, j, channel_base + 3] += 1 else: image[i, j, channel_base + 4] += 1 return channel_base + 5 def enemy_attr_channel(self, enemys, image, channel_base): for u in enemys: i, j = self.pos_2_2d(u.float_attr.pos_x, u.float_attr.pos_y) #print('enemy coordinate={},{}'.format(i, j)) if u.unit_type in sm.BASE_UNITS: image[i, j, channel_base + 0] += 1 elif u.unit_type in sm.BUILDING_UNITS: image[i, j, channel_base + 1] += 1 elif u.unit_type in sm.COMBAT_AIR_UNITS: image[i, j, channel_base + 2] += 1 elif u.unit_type in sm.COMBAT_UNITS: image[i, j, channel_base + 3] += 1 else: image[i, j, channel_base + 4] += 1 return channel_base + 5 def unit_dispatch(self, obs): units = obs.observation['units'] owners = [] neutrals = [] enemys = [] for u in units: if u.int_attr.alliance == sm.AllianceType.SELF.value: owners.append(u) elif u.int_attr.alliance == sm.AllianceType.NEUTRAL.value: neutrals.append(u) elif u.int_attr.alliance == sm.AllianceType.ENEMY.value: enemys.append(u) else: continue return owners, neutrals, enemys
class TargetRoundTripRwd(gym.Wrapper): def __init__(self, env, compress_width, range_width, explore_step): super(TargetRoundTripRwd, self).__init__(env) self._explore_rewards = None self._backward_rewards = None self._final_rewards = None self._status = None self._compress_width = compress_width self._range_width = range_width self._explore_step = explore_step def _assemble_reward(self): raise NotImplementedError def _reset(self): self._assemble_reward() obs = self.env._reset() for fr in self._explore_rewards: fr.reset(obs, self.env.unwrapped) for br in self._backward_rewards: br.reset(obs, self.env.unwrapped) for r in self._final_rewards: r.reset(obs, self.env.unwrapped) self._init_status() return obs def _init_status(self): map_size = self.env.unwrapped.map_size() x_per_unit = map_size[0] / self._compress_width y_per_unit = map_size[1] / self._compress_width home_pos = self.env.unwrapped.owner_base() enemy_pos = self.env.unwrapped.enemy_base() x_range = x_per_unit * self._range_width y_range = y_per_unit * self._range_width self._status = RoundTripCourse(home_pos, enemy_pos, (x_range, y_range), self._explore_step) self._status.reset() def _step(self, action): obs, rwd, done, other = self.env._step(action) scout = self.env.unwrapped.scout() status = self._status.check_status( (scout.float_attr.pos_x, scout.float_attr.pos_y)) new_rwd = 0 if status.value == RoundTripStatus.EXPLORE.value: for r in self._explore_rewards: r.compute_rwd(obs, rwd, done, self.env.unwrapped) new_rwd += r.rwd for r in self._final_rewards: r.compute_rwd(obs, rwd, done, self.env.unwrapped) new_rwd += r.rwd return obs, new_rwd, done, other else: for r in self._backward_rewards: r.compute_rwd(obs, rwd, done, self.env.unwrapped) new_rwd += r.rwd for r in self._final_rewards: r.compute_rwd(obs, rwd, done, self.env.unwrapped) new_rwd += r.rwd return obs, new_rwd, done, other
class ScoutVecFeatureV3(VecFeature): def __init__(self, compress_width, range_width, explore_step): super(ScoutVecFeatureV3, self).__init__() self._compress_width = compress_width self._range_width = range_width self._explore_step = explore_step def reset(self, env): super(ScoutVecFeatureV3, self).reset(env) self._init_range_and_status(env) def obs_space(self): low = np.zeros(12) high = np.ones(12) return Box(low, high) def extract(self, env, obs): scout = env.unwrapped.scout() scout_raw_pos = (scout.float_attr.pos_x, scout.float_attr.pos_y) home_pos = env.unwrapped.owner_base() enemy_pos = env.unwrapped.enemy_base() scout_pos = self._pos_transfer(scout_raw_pos[0], scout_raw_pos[1]) home_pos = self._pos_transfer(home_pos[0], home_pos[1]) enemy_pos = self._pos_transfer(enemy_pos[0], enemy_pos[1]) features = [] features.append(float(scout_pos[0]) / self._map_size[0]) features.append(float(scout_pos[1]) / self._map_size[1]) features.append( float(abs(home_pos[0] - scout_pos[0])) / self._map_size[0]) features.append( float(abs(home_pos[1] - scout_pos[1])) / self._map_size[1]) features.append( float(abs(enemy_pos[0] - scout_pos[0])) / self._map_size[0]) features.append( float(abs(enemy_pos[1] - scout_pos[1])) / self._map_size[1]) features.append(scout.float_attr.health / scout.float_attr.health_max) if self._have_enemies(obs): features.append(float(1)) else: features.append(float(0)) if self._check_in_target_range(scout): features.append(float(1)) else: features.append(float(0)) self._status.check_status(scout_raw_pos) if self._status.status() == RoundTripStatus.EXPLORE: #print('vec_feature, explore, pr=', self._status.progress_bar()) features.append(float(1)) features.append(float(0)) else: #print('vec_feature, backword, pr=', self._status.progress_bar()) features.append(float(0)) features.append(float(1)) features.append(self._status.progress_bar()) #print('vec_feature:', features) return np.array(features) def _have_enemies(self, obs): enemy_count = 0 units = obs.observation['units'] for u in units: if (u.int_attr.alliance == sm.AllianceType.ENEMY.value): if (u.unit_type in sm.COMBAT_UNITS or u.unit_type in sm.COMBAT_AIR_UNITS): enemy_count += 1 if enemy_count > 0: return True else: return False def _init_range_and_status(self, env): home = env.unwrapped.owner_base() target = env.unwrapped.enemy_base() map_size = env.unwrapped.map_size() x_per_unit = map_size[0] / self._compress_width y_per_unit = map_size[1] / self._compress_width x_range = x_per_unit * self._range_width y_range = y_per_unit * self._range_width x_radius = x_range / 2 y_radius = y_range / 2 self._x_low = target[0] - x_radius self._x_high = target[0] + x_radius self._y_low = target[1] - y_radius self._y_high = target[1] + y_radius print( 'Evade per_unit=({},{}), radius=({},{}), x_range=({},{}), y_range=({},{}), target={}' .format(x_per_unit, y_per_unit, x_radius, y_radius, self._x_low, self._x_high, self._y_low, self._y_high, target)) self._status = RoundTripCourse(home, target, (x_range, y_range), self._explore_step) self._status.reset() def _check_in_target_range(self, scout): if scout.float_attr.pos_x > self._x_high: return False elif scout.float_attr.pos_x < self._x_low: return False elif scout.float_attr.pos_y > self._y_high: return False elif scout.float_attr.pos_y < self._y_low: return False else: return True