Ejemplo n.º 1
0
    def __init__(self, des, A, E):
        '''Initializer'''

        # Initialize the evolution API
        self.e = evolution.evolution()

        # Calculate estimated alpha using ratio of H to E for each row
        ## Solve for alpha as follows
        ## Use: r = H/E = alpha/(1-alpha)
        ## (based on alpha = probability of H, (1-alpha) = probability of E)
        ## Then: solve for alpha --> alpha = H/E / (1+H/E)
        H = np.sum(A, axis=0)
        with np.errstate(divide='ignore', invalid='ignore'):
            r = H.sum(axis=1) / E.sum(axis=1)
        r = np.nan_to_num(r)  # Remove NaN Just in case
        self.alpha = r / (1 + r)

        # Set the row normalized set of A matrices for each action
        self.A = np.copy(A)  # copy to avoid rewriting A due to shallow copy
        for i in range(0, A.shape[0]):
            self.A[i] = matop.normalize_rows(self.A[i])

        # Set row normalized E matrix
        self.E = matop.normalize_rows(E)

        # Set desired states
        self.des = des
Ejemplo n.º 2
0
def main(args):
    parser = argparse.ArgumentParser(
        description='Simulate a task to gather the data for optimization')
    parser.add_argument('folder', type=str, help="(str) folder", default=None)
    parser.add_argument('-format', type=str, default="pdf")
    args = parser.parse_args(args)

    # Load evolution API
    e = evolution.evolution()

    # Initalize plot
    plt = pp.setup()

    # Initialize baseline limit in x axis
    lim = 0

    # Plot all evolution files found in the given folder together
    ## Extract all evolution files in the indicated folder
    filelist = sorted([
        f for f in os.listdir(args.folder)
        if f.startswith('evolution_') and f.endswith('pkl')
    ])

    # filelist = [f for f in os.listdir(args.folder) if
    # 				f.startswith('evolution_') and f.endswith('pkl')]
    li = []
    ## For each file in the list, add the evolution to the plot
    for file in filelist:
        ## Load the file
        e.load(args.folder + file)

        ## Plot it
        print(file)
        plt, l = plot_evolution(plt, e, label=str(file))
        li += l
        ## Fix lim according to the highest number of generations
        if len(e.stats) > lim:
            lim = len(e.stats)

    # Axis labels and dimensions
    plt.xlim(0, lim)
    plt.xlabel('Generation')
    plt.ylabel('Fitness')
    plt.legend([li[0], li[-1]], ['Model-based runs', 'Standard runs'])
    plt = pp.adjust(plt)

    # Save figure
    fname = os.path.dirname(args.folder)
    folder = "figures/evolution/"
    if not os.path.exists(os.path.dirname(folder)):
        os.makedirs(os.path.dirname(folder))
    filename_raw = os.path.splitext(os.path.basename(fname))[0]
    plt.savefig(folder + "evolution_%s.%s" % (filename_raw, args.format))
    plt.close()
Ejemplo n.º 3
0
    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!)


########################
#  Load evolution API  #
########################
e = evolution.evolution()

# Specify network topology
shape_file = "../../conf/policies/gas_shape.txt"
policy_shape = [6, 20, 20, 3]
num_params = 0
bias_add = True
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)

# Specify the genome length and the population size
Ejemplo n.º 4
0
def main(args):
	####################################################################		
	# Initialize

	# Argument parser
	parser = argparse.ArgumentParser(
		description='Simulate a task to gather the data for optimization'
		)

	parser.add_argument('controller', type=str, 
		help="Controller to use")
	parser.add_argument('-generations', type=int, default=50,
		help="Max generations after which the evolution quits, default = 50")
	parser.add_argument('-pop', type=int, default=100,
		help="Population size used in the evolution, default = 100")
	parser.add_argument('-t', type=int, default=200,
		help="Time for which each simulation is executed, deafult = 200s")
	parser.add_argument('-nmin', type=int, default=10,
		help="Minimum number of robots simulated, default = 10")
	parser.add_argument('-nmax', type=int, default=20,
		help="Maximum number of robots simulated, default = 20")
	parser.add_argument('-reruns', type=int, default=5,
		help="Number of policy re-evaluations, default = 5")
	parser.add_argument('-plot', type=str, default=None,
		help="Specify the relative path to a pkl \
			evolution file to plot the evolution.")
	parser.add_argument('-environment', type=str, default="square20",
		help=" Environment used in the simulations, \
			default is a square room of size 20 by 20.")
	parser.add_argument('-id', type=int, default=np.random.randint(0,10000),
		help="ID of evolutionary run, default = random integer")
	parser.add_argument('-resume', type=str, default=None,
		help="If specified, it will resume the evolution \
			from a previous pkl checkpoint file")
	parser.add_argument('-evaluate', type=str, default=None,
		help="If specified, it will evaluate the best result \
			in an evolution pkl file")

	args = parser.parse_args(args)

	# Load parameters
	fitness, controller, agent, pr_states, pr_actions = \
								parameters.get(args.controller)

	# Set up path to filename to save the evolution
	folder = "data/%s/evolution/" % (controller)
	directory = os.path.dirname(folder)
	if not os.path.exists(directory):
		os.makedirs(directory)
	filename = folder + "evolution_standard_%s_t%i_%i" % (controller, args.t, args.id)

	# Evolution API setup
	e = evolution.evolution()
	e.setup(fitnessfunction, 
		GENOME_LENGTH=pr_states*pr_actions, 
		POPULATION_SIZE=args.pop)
	####################################################################
	

	####################################################################
	# Plot file from file args.plot if indicated
	####################################################################
	if args.plot is not None:
    	plot(args.plot)
	
	####################################################################
	# Evolve or evaluate
	# Swarmulator API set up
	sim = simulator()
	sim.sim.runtime_setting("time_limit", str(args.t))
	sim.sim.runtime_setting("simulation_realtimefactor", str("300"))
	sim.sim.runtime_setting("environment", args.environment)
	sim.sim.runtime_setting("fitness", fitness)
	sim.sim.runtime_setting("pr_states", str(pr_states))
	sim.sim.runtime_setting("pr_actions", str(pr_actions))

	# if -evaluate <path_to_evolution_savefile>
	# Evaluate the performance of the best individual from the evolution file
	if args.evaluate is not None:
		sim.make(controller=controller, agent=agent, 
			clean=True, animation=False, logger=True, verbose=False)

		# Load the evolution parameters
		e.load(args.evaluate)
		individual = e.get_best()

		# Evaluate and save the log
		import copy
		runs = copy.deepcopy(args.reruns)
		args.reruns = 1
		f = []
		for i in range(runs):
			f.append(fitnessfunction(individual))
			sim.save_log(filename_ext="%s/evolution/evo_log_%i"%(controller,i))

		# Save evaluation data
		fh.save_pkl(f,"data/%s/benchmark_evolution_%s_t%i_r%i_%i_runs%i_%i.pkl"
			%(controller,controller,args.t,args.nmin,args.nmax,runs,args.id))
		
	# if -resume <path_to_evolution_savefile>
	# Resume evolution from file args.resume
	elif args.resume is not None:
		sim.make(controller=controller, agent=agent, 
			clean=True, animation=False, logger=False, verbose=False)

		# Load the evolution from the file
		e.load(args.resume)

		# Evolve starting from there
		p = e.evolve(generations=args.generations, 
			checkpoint=filename, 
			population=e.pop,
			verbose=True)

		# Save the evolution
		e.save(filename)

	# Otherwise, just run normally and start a new evolution from scratch
	else:
		sim.make(controller=controller, agent=agent, 
			clean=True, animation=False, logger=False, verbose=False)

		p = e.evolve(generations=args.generations, 
			checkpoint=filename, 
			verbose=True)

		# Save the evolution
		e.save(filename)
	####################################################################

if __name__=="__main__":
    main(sys.argv)