def fitness(individual): ### Set the policy file that swarmulator reads policy_file = "conf/policies/gas_params.txt" fh.save_to_txt(individual, sim.path + policy_file) sim.runtime_setting("policy", policy_file) # Use random policy ### Run swarmulator in batches # Indicate the minimum number and the maximum number of agents to run with. # In this example, a run will feature anything between 10 and 20 robots. f = sim.batch_run( num_agents, args.batchsize ) # Run with 10-20 robots, 5 times (default args.batchsize=5) return f.mean( ), # Fitness = average (note trailing comma to cast to tuple!)
def save_policy(self, policy, pr_actions=None, name="temp"): '''Save the policy in the correct format for use in Swarmulator''' # Resize policy to correct dimensions and normalize, # else assume it's already correct. if pr_actions is not None: policy = np.reshape(policy,(policy.size//pr_actions,pr_actions)) # Normalize rows if needed if pr_actions > 1: policy = matop.normalize_rows(policy) # Save the policy so it can be used by the simulator policy_filename = "conf/policies/%s.txt"%name policy_file = self.sim.path + "/" + policy_filename # Write in the correct format for reading if policy.shape[1] == 1: fh.save_to_txt(policy.T, policy_file) else: fh.save_to_txt(policy, policy_file) # Return the filename return policy_filename
params_file = "conf/policies/gas_params.txt" shape_file = "conf/policies/gas_shape.txt" policy_shape = [6, 20, 20, 3] num_params = 0 bias_add = True # weights_1 = np.array([0.080456, -0.303504, 0.395113, 0.228546, 0.016407, -0.887294, 0.329325, 0.433275, -0.089334, 0.300071, 0.229681, 0.841845, 1.252784, 0.235261, 0.228250, -0.082363, 0.725747, 0.032496, 0.695830, 0.060089, -1.958798, -0.259668, 0.470762, -0.732533, 0.860869, -0.285537, 0.054779, 1.003520, 0.188236, 0.815713, 1.487587, 0.619835, 1.473132, -1.313264, -0.384514, 0.101362, 1.316263, -0.116691, 0.421127, 0.417190, -0.095832, -0.210092, 0.246901, 0.081224, 0.549971, 0.723390, 0.153403, -0.104267, -0.025591, 0.194423, 0.158074, 1.028376, 0.690195, 0.260484, 0.318477, -0.158902, 1.247259, -0.066438, 0.614854, 0.209926, 0.947020, -0.292501, -0.459144, 0.526093, -0.010345, 0.069361, 0.391364, 0.754767, 0.218405, -0.920412, -0.761214, 0.415802, 1.127793, 0.425206, 0.481448, -0.046494, 0.592636, -0.033065, -0.144687, 0.481133, 0.048706, 0.065242, -0.566766, 2.470658, 1.996322, -0.623354, 2.299715, -0.145915, 1.836268, 0.534072, -0.609410, 0.968952, 0.091342, -1.360226, -2.740522, -0.965921, -0.266939, -1.665353, -3.364483, -2.969792, -2.223998, 0.284742, -2.939789, 0.306314, 0.302290, -2.612224, 0.135209, 2.060179, 2.890947, 1.614845, 0.349323, -2.027353, -0.132856, -1.806930, -0.489761, -3.304501, 1.092415, -3.015353, 0.486736, 0.800713]) # bias_1 = np.array([0.187781, 0.000000, 0.490506, 0.470928, 0.430286, -0.484403, 0.687263, 0.992733, 0.022498, -0.093953, -0.983269, 0.794059, 2.302896, 0.498422, 0.467022, -0.193897, 1.667498, 0.190847, 0.568896, 0.383632]) # weights_2 = np.array([0.168050, 2.040384, -2.064979, -2.828241, -4.767238, -2.076162, 0.305570, -0.240777, 0.624126, -4.686874, -0.062618, 0.594675, -4.361878, -0.665496, 0.509848, 0.279178, 0.342913, -0.196771, 0.452966, 1.816207, -0.086592, -0.195278, 0.175914, 0.339668, -0.348583, -0.318320, -0.348183, 0.144696, 0.385930, -0.161795, -0.307786, 0.300943, -0.205556, 0.259846, 0.296160, -0.131186, 0.090441, -0.256097, 0.382138, -0.003487, 1.035805, 0.140748, -1.548141, -0.228025, 0.900092, 0.252229, -0.816036, -0.288833, -0.188033, -1.152571, -0.233309, -0.260314, -2.274416, -0.244243, -0.309734, -0.496004, 1.579350, -0.451627, 1.109074, -0.438621, 1.782142, 1.791552, 2.012299, 0.378595, 1.210860, 1.211952, 0.007271, -0.650013, -0.671363, 0.470484, -1.171576, -0.588528, 0.911642, -0.870711, -0.542567, -0.374684, 1.547122, -0.931147, 0.859845, 1.174022, 1.436142, 0.586575, 0.968079, 1.709190, 0.251601, 1.077071, -0.243018, -0.150358, 0.100469, 0.999609, 0.606790, -0.059514, 1.417337, -0.212800, -0.018555, -0.169697, 1.203942, 0.431439, 1.638386, 0.628225, 1.907978, -1.458011, -2.756999, -1.076679, 0.447772, -0.708924, -0.800847, -0.196590, -0.556003, 0.126425, 1.003520, -1.142756, -2.053583, 0.176839, -0.118457, -0.068349, 2.032997, -0.581575, -1.347530, -1.150051, 1.190609, 1.351558, 1.809976, 0.981795, 0.306670, 0.760385, -0.608100, -0.866198, -0.398588, 1.308119, -0.632415, -0.216025, 1.181097, -0.433247, 0.057291, -0.220330, 1.523498, -0.132617, 0.398260, 0.553353, 0.144620, -1.198702, 0.316820, 0.327191, 0.091322, 0.007457, 0.403278, 0.910350, 0.098573, 1.327490, 0.680395, 0.587790, 1.303851, 0.708418, 0.473462, 0.561879, 0.367688, 0.600028, 0.699535, -0.496516, 2.882540, 0.338853, 3.593900, 2.915204, 1.544322, 3.218222, -2.338711, -2.315939, -2.207203, 2.517088, -2.763965, -1.942528, 3.135617, -2.080489, -2.127337, -2.804512, 3.057767, -1.375818, 3.215534, 0.413034, -0.200762, 1.576871, 0.131965, -1.875759, -1.726544, 0.326938, 0.033159, -0.426027, 0.465382, -3.827561, 0.136851, -0.352107, -4.424959, -0.671889, 0.302214, 0.282938, -0.676272, 0.477651, -0.817575, 0.117896, -0.363701, 1.222605, -1.335815, -1.116346, -0.345092, 0.131038, 0.123748, 0.126519, -0.036687, -3.829664, 0.295692, 0.137014, -5.108375, 0.573952, 0.520383, 0.381148, 0.176746, -0.196227, -0.353303, 0.099086, 1.440653, 0.255907, 0.239402, 0.634858, 0.295164, 0.045068, 0.261245, -0.185554, 0.220197, 0.445250, -0.076673, -0.270609, -0.209968, -0.189423, 0.184081, -0.071681, 0.915493, -0.106573, 0.911570, 0.491155, 1.185940, -0.194083, 0.287890, -0.166589, 0.251456, 0.146068, 0.441890, 0.460928, 0.236590, 0.386487, -0.025955, 0.195227, 0.911161, 0.338874, 0.051224, 0.238500, 1.040561, -0.085525, 0.758794, -0.444311, 0.726583, 1.432099, -2.267616, -2.660228, -3.595770, -1.415018, -0.115711, -0.100519, 0.642899, -4.834349, -0.045481, 0.481859, -4.322209, -0.633157, 0.553555, 0.275532, 0.312792, -0.392147, -0.148839, 0.899511, 0.375180, 0.101979, -1.740720, -2.028675, -1.204086, -1.630775, 0.626285, 0.304780, 0.211987, -1.971024, 0.786974, 0.285175, -1.618286, 0.261753, 0.724894, 0.666280, 0.595695, 0.215269, -0.209404, -0.249569, 3.107862, -0.967245, -1.745504, 0.732477, 2.573652, 1.911714, -2.976255, -2.163399, -1.943419, -2.135745, -1.303227, -2.731088, -3.242814, -2.293726, -3.009649, -2.525989, 3.491734, -1.780691, 2.835977, -0.784161, 0.818952, -0.688410, 0.751935, 0.484386, 0.028855, 0.318381, 0.134204, 0.467463, 0.396727, 0.523939, 0.676164, 0.610283, 0.539500, 0.587218, 0.181029, 0.453235, 0.535466, 0.504869, 0.647505, -0.437345, 2.609795, -1.603602, -0.661686, 1.445924, -0.114868, 1.110839, -1.709649, -1.799764, -2.078711, -1.972401, -2.180535, -2.101886, -2.660636, -2.100333, -2.442467, -2.589785, 2.994494, -0.837452, 2.771813, -0.962168, 0.224056, -1.311027, -1.481087, -1.081871, -0.587762, -0.847950, 0.836055, 0.479678, -0.107777, -1.917307, 0.191085, 0.954773, -1.957051, 0.869845, 0.593439, 0.707358, 0.392914, 0.688720, 0.009701, -1.256007, -0.230035, -0.884554, -1.947885, -1.875595, -1.461321, -1.591874, 1.067095, 0.499432, 0.758779, -2.065275, 0.663975, 0.720824, -1.295973, 1.017036, 1.165535, 0.851763, -0.026146, 0.952122, -0.637873, -0.716149]) # bias_2 = np.array([1.034986, 0.085298, 0.743393, 0.250663, 0.620471, 0.339378, 0.182997, 0.187399, -0.457824, 0.881658, 0.129379, 0.061748, 1.153635, 0.300037, 0.155361, -0.157184, 0.935633, 0.142752, 0.926348, -0.040259]) # weights_3 = np.array([1.612773, 1.690614, 1.804894, -4.791912, -0.280219, -0.449845, 1.838871, 3.255228, 4.029651, 3.246441, 3.789186, 1.069200, 4.325471, 0.084933, 1.402819, 2.008231, 2.209121, 2.405088, -0.800419, -0.982383, -1.404540, -0.671861, -1.129601, -1.151478, -1.820853, -0.760996, -0.744890, 4.980825, 2.245547, 2.471924, -1.262146, -2.756188, -0.265810, -1.008244, -0.561902, -1.273547, 5.530477, 3.860540, 4.689218, -0.905146, -1.615405, -1.302895, -2.032653, -1.539001, -1.368316, -1.496933, -1.206579, -0.832636, 1.753875, 1.527737, 2.070429, -1.122385, -0.952002, -1.657952, 2.254431, 2.799551, 1.539449, -2.764688, 0.060556, -0.739542]) # bias_3 = np.array([0.593684, 0.231821, 0.262835]) # params = np.concatenate([weights_1,bias_1,weights_2,bias_2,weights_3,bias_3]) # print(params) # print(np.shape(params)) # computing number of necessary parameters to describe network num_params += np.sum([ policy_shape[i] * policy_shape[i + 1] for i in range(len(policy_shape) - 1) ]) if (bias_add): num_params += np.sum(policy_shape[1:]) params = np.random.uniform(-5, 5, num_params) fh.save_to_txt(params, params_file) fh.save_to_txt(np.array(policy_shape), shape_file)
def fitness(self, x): fh.save_to_txt(x, sim.path + policy_file) f = sim.batch_run_envs( environments ) # Run with 10-20 robots, 5 times (default args.batchsize=5) return [f.mean()]
environments = [ 'rand_env_1', 'rand_env_2', 'rand_env_3', 'rand_env_4', 'rand_env_5' ] policy_shape = [6, 20, 20, 3] num_params = 0 bias_add = True num_agents = 10 sim.set_n_agents(num_agents) num_params += np.sum([ policy_shape[i] * policy_shape[i + 1] for i in range(len(policy_shape) - 1) ]) if (bias_add): num_params += np.sum(policy_shape[1:]) fh.save_to_txt(np.array(policy_shape), shape_file) class prob_bart: def __init__(self): self.dim = num_params def fitness(self, x): fh.save_to_txt(x, sim.path + policy_file) f = sim.batch_run_envs( environments ) # Run with 10-20 robots, 5 times (default args.batchsize=5) return [f.mean()] def get_bounds(self): return ([-20] * num_params, [20] * num_params)
def write_best(self): self.winner = tools.selBest(self.pop, 1)[0] fh.save_to_txt(self.winner, args.output_file)