def get_fitness(dna):
    # reset position
    e.solids[1].pos = [x, y]

    g = gene_functions.Gaps2(x, y)

    # set velocity based on input functions and weights from DNA
    for i in range(2):
        e.solids[1].velocity[i] = g.input0()[i] * dna[0] + \
                                  g.input3()[i] * dna[1] + \
                                  g.input4()[i] * dna[2]

    initial_velocity = e.solids[1].velocity

    # 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(tick_length, e, time_limit)

    # 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 >= time_limit or runtime <= tick_length * 10:
        return .00001 # can't be 0 bc that would cause division by 0 later on

    # fitness function, tries to minimize velocity while maximizing time spent before crashing back down to planet, uses normalized quantities
    velocity_magnitude = pe.distance(initial_velocity, [0, 0])

    norm_runtime = (runtime - tick_length * 10) / (time_limit - tick_length * 10)
    norm_velocity = velocity_magnitude / 5

    return (norm_runtime - norm_velocity) ** 2
def get_fitness(dna):
    # create unique object of gene function class for each iteration
    g = gene_functions.Gatd1()

    # set velocity based on input functions and weights from DNA
    for i in range(2):
        e.solids[0].velocity[i] = g.input0()[i] * dna[0] + \
                                  g.input1()[i] * dna[1]

    end_pos = pe.run_physics_engine(tick_length, e, time_limit)
    return 1 / (pe.distance([0, 0], end_pos)**.5)
def get_fitness(organism):
    # reset position, set velocity to what its DNA dictates
    e.solids[1].pos = [0, 11.001]
    e.solids[1].velocity = [0] + organism.dna

    # 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(tick_length, e, time_limit)

    # 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 >= time_limit or runtime <= tick_length * 10:
        return .001

    # fitness function, tries to minimize velocity while maximizing time spent before crashing back down to planet, uses normalized quantities
    velocity_magnitude = pe.distance([0] + organism.dna, [0, 0])

    norm_runtime = runtime / time_limit
    norm_velocity = velocity_magnitude / 5

    return (norm_runtime - norm_velocity)**2
Ejemplo n.º 4
0
def get_fitness(dna, destination):
    # create unique object of gene function class for each iteration
    g = gene_functions.Gasv1(e.g_strength[1], destination)

    # make sure to reset pos before starting again!!!!!!!!!!
    e.solids[0].pos = [-100, .001]

    # set velocity based on input functions and weights from DNA
    for i in range(2):
        e.solids[0].velocity[i] = g.input0()[i] * dna[0] + \
                                  g.input1()[i] * dna[1] + \
                                  g.input2()[i] * dna[2] + \
                                  g.input3()[i] * dna[3]

        if abs(e.solids[0].velocity[i]) >= 100:
            return 10 ** -10

    end_pos = pe.run_physics_engine(tick_length, e, time_limit)

    return 1 / (pe.distance(destination, end_pos) ** 2)
Ejemplo n.º 5
0
def get_fitness(start_x, dna):
    start_pos = get_start_pos(start_x, top)
    e.solids[1].pos = [start_pos[0], start_pos[1]]

    # create unique object of gene function class for each iteration
    g = gene_functions.Gaps3(e.solids[1].pos[0], e.solids[1].pos[1])

    # set velocity based on input functions and weights from DNA
    for i in range(2):
        e.solids[1].velocity[i] = g.input0()[i] * dna[0]
        # g.input1()[i] * dna[1] + \
        # g.input4()[i] * dna[2]

    # 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(tick_length, e, time_limit)

    # 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 >= time_limit or runtime <= tick_length * 10:
        return .00001  # can't be 0 bc that could cause division by 0 later on

    return runtime**2
Ejemplo n.º 6
0
    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])

        norm_runtime = (runtime - .2 * 10) / (100 - .2 * 10)
        norm_velocity = velocity_magnitude / 5

        print('fitness', (norm_runtime - norm_velocity) ** 2)

    print()
# 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
    n.feedforward()

    # setup for running physics engine
    e.solids[0].velocity = [n.l2[0][0] * 100, n.l2[0][1] * 100]
    e.solids[0].pos = [-100, .001]

    # run physics engine and use it in the cost function to determine error for later use in backpropagation
    end_pos = pe.run_physics_engine(tick_length=.2,
                                    environ=e,
                                    time_limit=10**3)

    # modify data for optimal use by backpropagation algorithm
    difference = [end_pos[0] - destination[0], end_pos[1] - destination[1]]
    share_proportion = .5 - (nn.nonlin(np.random.normal()) / 2)
    complement = 1 - share_proportion
    shared_difference = [
        (difference[0] * complement) + (difference[1] * share_proportion),
        (difference[1] * complement) + (difference[0] * share_proportion)
    ]

    l2_error = np.array(
        [-tanh(shared_difference[0] / 100), -tanh(shared_difference[1] / 100)])

    n.backpropagation(l2_error)
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))
    # # switch variables every 5 iterations
    # if i % 20 == 0:
    #     destination = destinations[int(i / 20) % 5]
    #     e.g_strength[1] = gravities[int(i / 20) % 5]

    # turn the inputs into outputs using existing weights
    n.feedforward()

    # setup for running physics engine
    initial_velocity = [n.l2[0][0] * 10, n.l1[0][1] * 10]
    e.solids[1].velocity = initial_velocity
    e.solids[1].pos = [0, 11.001]

    # run physics engine and use it in the cost function to determine error for later use in backpropagation
    final_velocity = pe.run_physics_engine(tick_length=.2,
                                           environ=e,
                                           time_limit=50)

    # modify data for optimal use by backpropagation algorithm
    difference = [
        initial_velocity[0] - final_velocity[0],
        initial_velocity[1] - final_velocity[1]
    ]
    share_proportion = .5 - (nn.nonlin(np.random.normal()) / 2)
    complement = 1 - share_proportion
    shared_difference = [
        (difference[0] * complement) + (difference[1] * share_proportion),
        (difference[1] * complement) + (difference[0] * share_proportion)
    ]

    l2_error = np.array(
Ejemplo n.º 10
0
                        temp = tweak(time_limit)
                        if temp > 0:
                            time_limit = temp
                            n.inputs[0][3] = temp

                # turn the inputs into outputs using existing weights
                n.feedforward()

                # setup for running physics engine
                initial_velocity = [n.l2[0][0] * 10, n.l2[0][1] * 10]
                e.solids[1].velocity = initial_velocity
                e.solids[1].pos = [n.inputs[0][0], n.inputs[0][1]]

                # run physics engine and use it in the cost function to determine error for later use in backpropagation
                runtime = pe.run_physics_engine(tick_length=.2,
                                                environ=e,
                                                time_limit=time_limit,
                                                e_type='PS_1')

                # modify data for optimal use by backpropagation algorithm
                difference = [runtime, runtime]

                if i == (10**magnitude) - 1:
                    print('PS_1', difference)

                n.backpropagation(get_l2_error(difference))

            # start things off again
            e = PS_2
            time_limit = 50

            n.inputs = np.array(