gen_count = 100 # for how many generations training will last mutate_chance = .8 # the odds of an organism being mutated on any given generation full_mutate_chance = .4 # odds of an organism being replaced by a randomized organism instead of just being tweaked according to the normal distribution standard_deviations = [ .05 for i in range(2) ] # how much each gene is mutated by, follows normal distribution so gene_ranges = [(-3, 3) for i in range(2)] pop_size = 20 # number of organisms in the population time_limit = 10**10 # how long each fitness test will run for before just giving up tick_length = .2 # how often the physics engine will update, smaller values create more precise simulations but take longer e = Environment(solids=[ pe.Circle(pos=[-100, -100]), pe.Rect(static=True, pos=[-155, 0], height=300), pe.Rect(static=True, pos=[155, 0], height=300), pe.Rect(static=True, pos=[0, -155], width=300), pe.Rect(static=True, pos=[0, 155], width=300) ], g_type='downward', g_strength=.2) # initialize population with random genes initial_population = [] for i in range(pop_size): dna = [] for gene_range in gene_ranges: dna.append(np.random.uniform(gene_range[0], gene_range[1])) initial_population.append(Organism(dna))
velocity[i] = g.input0()[i] * dna[0] + \ g.input1()[i] * dna[1] + \ g.input2()[i] * dna[2] + \ g.input3()[i] * dna[3] + \ g.input4()[i] * dna[4] + \ g.input5()[i] * dna[5] # print('destination', destination) # print('barrier_x', barrier_x) # print('velocity', velocity) if stahp: sys.exit() e = Environment(solids=[ pe.Circle(pos=[-100, -100], velocity=velocity), pe.Rect(static=True, width=100, pos=[barrier_x, 0]), pe.Rect(static=True, pos=[-155, 0], height=300), pe.Rect(static=True, pos=[155, 0], height=300), pe.Rect(static=True, pos=[0, -155], width=300), pe.Rect(static=True, pos=[0, 155], width=300) ], g_type='downward', g_strength=.2) # run the physics engine until the termination condition is reached (termination function has to be put into the physics_engine.py and return statement corrected) # time_limit is set to be virtually infinite because we are not worried about this environment running indefinitely, the ball will always eventually slow down to stop end_pos = pe.run_physics_engine(.1, e, 10**10) total_distance += pe.distance(destination, end_pos) # print('end_pos', end_pos) # print('distance', pe.distance(destination, end_pos))
import physics_engine as pe from random import randrange # this file is for storing and saving environments so that run_physics_engine.py isn't overrun with many lines of commented-out code # has an instance variable for everything that can be unique to an environment class Environment: def __init__(self, solids, g_type=None, g_strength=None): self.solids = solids self.g_type = g_type self.g_strength = g_strength # ball through gap in wall e15 = Environment(solids=[pe.Circle(), pe.Rect(static=True, pos=[100, -450], height=1000), pe.Rect(static=True, pos=[100, 600], height=1000)], g_type='uniform', g_strength=[0, -9.81]) # NN_PS_1, but without the moon to slingshot off of e14 = Environment(solids=[pe.Circle(pos=[-100, 0], static=True, mass=100), pe.Circle(pos=[0, 0], velocity=[3.3, 3.78], mass=1, radius=1)], g_type='nonuniform', g_strength=10) # NN_PS_1, maximize velocity from gravity assist e13 = Environment(solids=[pe.Circle(pos=[-100, 0], static=True, mass=100), pe.Circle(pos=[0, 0], velocity=[3.3, 3.78], mass=1, radius=1), pe.Circle(pos=[50, 0], velocity=[0, 2.582], mass=20)], g_type='nonuniform', g_strength=10)
# 6 environments for the LT ML algorithm to use, only initialized with instance variables that will be kept constant PS_1 = Environment( solids=[pe.Circle(static=True), pe.Circle(radius=1, pos=[0, 11.01])], g_type='nonuniform', g_strength=100) PS_2 = Environment(solids=[ pe.Circle(static=True, pos=[-100, 0], mass=100), pe.Circle(radius=1, pos=[-88.99, 0], mass=1), pe.Circle(radius=3, pos=[1, 0], velocity=[0, 3.162]) ], g_type='nonuniform', g_strength=10) TD_1 = Environment(solids=[ pe.Circle(pos=[1, 1]), pe.Rect(static=True, pos=[-155, 0], height=300), pe.Rect(static=True, pos=[155, 0], height=300), pe.Rect(static=True, pos=[0, -155], width=300), pe.Rect(static=True, pos=[0, 155], width=300) ], g_type='downward', g_strength=.2) TD_2 = Environment(solids=[ pe.Circle(pos=[-100, -100]), pe.Rect(static=True, width=150, pos=[10, 0]), pe.Rect(static=True, pos=[-155, 0], height=300), pe.Rect(static=True, pos=[155, 0], height=300), pe.Rect(static=True, pos=[0, -155], width=300), pe.Rect(static=True, pos=[0, 155], width=300) ], g_type='downward',