start_time = time()  # just a timer

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))
Example #2
0
    y = i[1]

    g = gene_functions.Gaps3(x, y)

    velocity = [0, 0]
    dna = [0.29087740586878064]

    for j in range(2):
        velocity[j] = g.input0()[j] * dna[0]
                      # g.input1()[j] * dna[1] + \
                      # g.input4()[j] * dna[2]
    print(velocity)

            # print('velocity', velocity)

    e = environments.Environment(solids=[pe.Circle(static=True),
                            pe.Circle(radius=1, pos=i, velocity=velocity)],
                    g_type='nonuniform',
                    g_strength=10)


    # run the physics engine until either the termination condition is reached (termination function has to be put into the physics_engine.py
    runtime = pe.run_physics_engine(.2, e, 100)
    # print('runtime', runtime)

    # if the simulation exceeds the time limit before the termination condition is reached, or way too quickly, the organism's fitness will be virtually 0
    if runtime >= 100 or runtime <= .2 * 10:
        print('fitness', 0)
    else:
        # fitness function, tries to minimize velocity while maximizing time spent before crashing back down to planet, uses normalized quantities
        velocity_magnitude = pe.distance(velocity, [0, 0])
import physics_engine as pe
from environments import Environment
import gene_functions

g = gene_functions.Gatd1()

velocity = [0, 0]
dna = [1.0493179215779653, 0.26432598219944303]

for j in range(2):
    velocity[j] = g.input0()[j] * dna[0] + \
                  g.input1()[j] * dna[1]


e = Environment(solids=[pe.Circle(pos=[-100, -100], velocity=velocity),
                        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)

print(e.solids[0].velocity)

# 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(.2, e, 10 ** 10)

print('fitness', 1 / pe.distance([0, 0], end_pos))
from environments import Environment
import numpy as np
from time import time
import math

start_time = time()  # just a timer


# hyperbolic tangent function, similar to sigmoid but has a range of (-1, 1) as opposed to (0, 1), used for squahsing but with negs
def tanh(x):
    return (math.e**(2 * x) - 1) / (math.e**(2 * x) + 1)


# config
order = 5
e = Environment(solids=[pe.Circle(pos=[-100, .001])],
                g_type='uniform',
                g_strength=[0, -9.81])
destination = np.array([100, 0])
n = nn.NeuralNetwork(inputs=np.array([[
    e.g_strength[1] / 10, (destination[0] - e.solids[0].pos[0]) / 100,
    (destination[1] - e.solids[0].pos[1]) / 100
]]),
                     l1_size=4)

# run neural network
for i in range(10**order):
    if i % ((10**order) / 100) == 0:
        print(i / (10**(order - 2)))

    # turn the inputs into outputs using existing weights
import numpy as np
from time import time
import math

start_time = time()  # just a timer


# hyperbolic tangent function, similar to sigmoid but has a range of (-1, 1) as opposed to (0, 1), used for squahsing but with negs
def tanh(x):
    return (math.e**(2 * x) - 1) / (math.e**(2 * x) + 1)


# config
order = 3
e = Environment(solids=[
    pe.Circle(pos=[-100, 0], mass=100, static=True),
    pe.Circle(pos=[0, 0], velocity=[4, 0], mass=1, radius=1),
    pe.Circle(pos=[50, 0], velocity=[0, 2.582], mass=20)
],
                g_type='nonuniform',
                g_strength=10)
n = nn.NeuralNetwork(inputs=np.array(
    [[e.g_strength / 10, e.solids[0].pos[0] / 10, e.solids[0].pos[1] / 10]]),
                     l1_size=8)

# run neural network
for i in range(10**order):
    # print percent progress
    if i % ((10**order) / 100) == 0:
        print(i / (10**(order - 2)))
import physics_engine as pe
from environments import Environment
import gene_functions

g = gene_functions.Gatd1()

velocity = [0, 0]
dna = [-2.4215988204335663, -1.0889519542788124]

for j in range(2):
    velocity[j] = g.input0()[j] * dna[0] + \
                  g.input1()[j] * dna[1]

e = Environment(solids=[
    pe.Circle(pos=[-100, -100], velocity=velocity),
    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)

print(e.solids[0].velocity)

# 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(.2, e, 10**10)

print('fitness', 1 / pe.distance([0, 0], 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)
    dna = [
        -2.1693324115334147, 2.970598072220141, 2.607791095856965,
        1.5847747047886989, 0.4836883044962237
    ]

    for j in range(2):
        velocity[j] = g.input0()[j] * dna[0] + \
                      g.input1()[j] * dna[1] + \
                      g.input2()[j] * dna[2] + \
                      g.input3()[j] * dna[3] + \
                      g.input4()[j] * dna[4]

        # print('velocity', velocity)

    e = environments.Environment(solids=[
        pe.Circle(static=True),
        pe.Circle(radius=1, pos=i, velocity=velocity)
    ],
                                 g_type='nonuniform',
                                 g_strength=10)

    # run the physics engine until either the termination condition is reached (termination function has to be put into the physics_engine.py
    runtime = pe.run_physics_engine(.2, e, 100)
    # print('runtime', runtime)

    # if the simulation exceeds the time limit before the termination condition is reached, or way too quickly, the organism's fitness will be virtually 0
    if runtime >= 100 or runtime <= .2 * 10:
        print('fitness', 0)
    else:
        # fitness function, tries to minimize velocity while maximizing time spent before crashing back down to planet, uses normalized quantities
        velocity_magnitude = pe.distance(velocity, [0, 0])
import gene_functions
from environments import Environment
import physics_engine as pe

dna = [-0.4782179829546539, 0.07012079426483595, 2.7799442290567735, 0.4543957971535216]

destination = [100, 100]
g = gene_functions.Gasv1(-9.81, destination)
velocity = [0, 0]

for i in range(2):
    velocity[i] = g.input0()[i] * dna[0] + \
                  g.input1()[i] * dna[1] + \
                  g.input2()[i] * dna[2] + \
                  g.input3()[i] * dna[3]

print('velocity', velocity)

e = Environment(solids=[pe.Circle(pos=[-100, .001], velocity=velocity)],
                g_type='uniform',
                g_strength=[0, -9.81])

end_pos = pe.run_physics_engine(.2, e, 10 ** 2)
print('end_pos', end_pos)

print('dist', pe.distance(end_pos, destination))
start_time = time() # just a timer

gen_count = 300 # for how many generations training will last
mutate_chance = .5 # the odds of an organism being mutated on any given generation
full_mutate_chance = .2 # odds of an organism being replaced by a randomized organism instead of just being tweaked according to the normal distribution
standard_deviations = [.1 for i in range(3)] # how much each gene is mutated by, follows normal distribution so
gene_ranges = [(-5, 5) for i in range(3)]
pop_size = 100 # number of organisms in the population

time_limit = 50 # 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

start_pos = [0, 11.001]
x = start_pos[0]
y = start_pos[1]
e = Environment(solids=[pe.Circle(static=True),
                        pe.Circle(radius=1, pos=start_pos)],
                g_type='nonuniform',
                g_strength=10)


# initialize population with random genes
initial_population = []
for i in range(pop_size):
    dna = []
    for gene_range in gene_ranges:
        dna.append(randrange(gene_range[0], gene_range[1]))
    initial_population.append(Organism(dna))

p = Population(initial_population)
Example #11
0
    if r.random() < .5:
        return x - delta
    return x + delta


# all 6 possible orders in which the algorithm will be introduced to the environments
orders = [['PS', 'TD', 'SV'], ['PS', 'SV', 'TD'], ['TD', 'PS', 'SV'],
          ['TD', 'SV', 'PS'], ['SV', 'TD', 'PS'], ['SV', 'PS', 'TD']]
# possible start locations for PS_1's rocket (solids[1])
ps1_starts = [[-11.001, .1], [.1, -11.001], [11.001, .1], [.1, 11.001],
              [7.8, 7.8], [-7.8, 7.8], [-7.8, -7.8], [7.8, -7.8]]

# 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),
top = True  # boolean for whether rocket is moving right along top of planet or left along bottom of planet with each generation, switches halfway through
start_x = -10  # initial x-coordinate of rocket, will be incremented with each generation so that organisms adapted to all positions/launch angles

gen_count = 3000  # for how many generations training will last
mutate_chance = .75  # 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 = [
    .1 for i in range(5)
]  # how much each gene is mutated by, follows normal distribution so
gene_ranges = [(-3, 3) for i in range(5)]
pop_size = 40  # number of organisms in the population

time_limit = 40  # 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(static=True),
                        pe.Circle(radius=1)],
                g_type='nonuniform',
                g_strength=10)

# 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))

p = Population(initial_population)

# iterates through all generations
from genetic_algorithm import *
from random import randrange
import physics_engine as pe
from environments import Environment
from time import time

start_time = time()

e = Environment(
    solids=[pe.Circle(static=True),
            pe.Circle(radius=1, pos=[0, 11.001])],
    g_type='nonuniform',
    g_strength=10)

gen_count = 1000  # for how many generations training will last
mutate_chance = .8  # the odds of an organism being mutated on any given generation
full_mutate_chance = .1  # odds of an organism being replaced by a randomized organism instead of just being tweaked according to the normal distribution
standard_deviations = [
    .15
]  # how much each gene is mutated by, follows normal distribution so
gene_ranges = [(0, 6)]
pop_size = 20  # number of organisms in the population

time_limit = 100  # 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

# initialize population with random genes
initial_population = []
for i in range(pop_size):
    dna = []
    for gene_range in gene_ranges: