Example #1
0
    def evaluate(self, hive_nectar, majority, guidance):
        if hive_nectar > 10:
            adjusted_nectar = 1
        elif hive_nectar < 0:
            adjusted_nectar = 0
        else:
            adjusted_nectar = hive_nectar / 10

        brain = nn.create_phenotype(self.chromo)
        
        # implementing the ability for bees to be 'guided'
        if guidance:
               found_nectar = guidance
        else:
               found_nectar = random.random()
        #################################################
        
        ins = (found_nectar, 
               self.outputs[-1][0], 
               self.fitnesses[-1], 
               hive_nectar,
               majority)
               #self.inputs[-1][0])
        brain.flush()
        outs = brain.pactivate(ins)
        self.inputs.append(ins)     
        self.outputs.append(outs)
Example #2
0
def eval_individual(chromo):
    brain = nn.create_phenotype(chromo)
    found_nectar = random.random()

    brain.flush()
    output = brain.sactivate([found_nectar])

    return (found_nectar, output[0])
Example #3
0
def eval_individual(chromo, last_choice, last_fitness, hive_nectar):
    brain = nn.create_phenotype(chromo)
    found_nectar = random.random()

    brain.flush()
    output = brain.sactivate([found_nectar, last_choice, last_fitness, hive_nectar])
    exp.write(str(output[0])+'\n')
    return (found_nectar, output[0])
Example #4
0
	def evalFitness(self,genomes):
		print("evalFitness")
		for g in genomes:
			net = nn.create_phenotype(g)
			#net = nn.create_fast_feedforward_phenotype(g)
			g.fitness = self.run(net)
			print(g.fitness)
			
			"""
Example #5
0
def evaluate_population(population):

    twelve_degrees = 0.2094384  #radians
    num_steps = 10**5

    for chromo in population:

        net = nn.create_phenotype(chromo)

        # initial conditions (as used by Stanley)
        x = random.randint(0, 4799) / 1000.0 - 2.4
        x_dot = random.randint(0, 1999) / 1000.0 - 1.0
        theta = random.randint(0, 399) / 1000.0 - 0.2
        theta_dot = random.randint(0, 2999) / 1000.0 - 1.5
        #x = 0.0
        #x_dot = 0.0
        #theta = 0.0
        #theta_dot = 0.0

        fitness = 0

        for trials in xrange(num_steps):

            # maps into [0,1]
            inputs = [(x + 2.4) / 4.8, (x_dot + 0.75) / 1.5,
                      (theta + twelve_degrees) / 0.41, (theta_dot + 1.0) / 2.0]

            # a normalizacao so acontece para estas condicoes iniciais
            # nada garante que a evolucao do sistema leve a outros
            # valores de x, x_dot e etc...

            action = net.pactivate(inputs)
            #print inputs,action
            # [0.6731124662078692, 0.4177235725492288, 0.34882554840901664, 0.41364238610962345] [0.344597476333986]

            # Apply action to the simulated cart-pole
            x, x_dot, theta, theta_dot = cart_pole(action[0], x, x_dot, theta,
                                                   theta_dot)

            # Check for failure.  If so, return steps
            # the number of steps indicates the fitness: higher = better
            fitness += 1
            if (abs(x) >= 2.4 or abs(theta) >= twelve_degrees):
                #if abs(theta) > twelve_degrees: # Igel (p. 5) uses theta criteria only
                # the cart/pole has run/inclined out of the limits
                break

        chromo.fitness = fitness
Example #6
0
    def evaluate(self, hive_nectar):
        if hive_nectar > 10:
            adjusted_nectar = 1
        elif hive_nectar < 0:
            adjusted_nectar = 0
        else:
            adjusted_nectar = hive_nectar / 10
        # adjusted_nectar =

        brain = nn.create_phenotype(self.chromo)
        found_nectar = random.random()
        ins = (found_nectar, 
               self.outputs[-1][0], 
               self.fitnesses[-1], 
               hive_nectar)
        brain.flush()
        outs = brain.pactivate(ins)
        self.inputs.append(ins)     
        self.outputs.append(outs)
def evaluate_population(chromosomes):
    num_steps = 10 ** 5

    for chromo in chromosomes:

        net = nn.create_phenotype(chromo)

        # initial conditions (as used by Stanley)        
        x = random.randint(0, 4799) / 1000.0 - 2.4
        x_dot = random.randint(0, 1999) / 1000.0 - 1.0
        theta = random.randint(0, 399) / 1000.0 - 0.2
        theta_dot = random.randint(0, 2999) / 1000.0 - 1.5

        fitness = 0

        for trials in xrange(num_steps):
            # maps into [0,1]
            inputs = [(x + 2.4) / 4.8,
                      (x_dot + 0.75) / 1.5,
                      (theta + angle_limit) / 0.41,
                      (theta_dot + 1.0) / 2.0]

            action = net.pactivate(inputs)
            action[0] += 0.4 * (random.random() - 0.5)

            # Apply action to the simulated cart-pole
            x, x_dot, theta, theta_dot = cart_pole(action[0], x, x_dot, theta, theta_dot)

            # Check for failure.  If so, return steps
            # the number of steps indicates the fitness: higher = better
            fitness += 1
            if abs(x) >= 2.4 or abs(theta) >= angle_limit:
                # if abs(theta) > angle_limit: # Igel (p. 5) uses theta criteria only
                # the cart/pole has run/inclined out of the limits
                break

        chromo.fitness = fitness
Example #8
0
cart = gz.rectangle(SCALE * 0.5, SCALE * 0.25, xy=(150, 80), stroke_width=1, fill=(0, 1, 0))
pole = gz.rectangle(SCALE * 0.1, SCALE * 1.0, xy=(150, 55), stroke_width=1, fill=(1, 1, 0))

# random.seed(0)

# load the winner
with open('winner_chromosome') as f:
    c = cPickle.load(f)

print 'Loaded chromosome:'
print c

local_dir = os.path.dirname(__file__)
config = Config(os.path.join(local_dir, 'spole_config'))
net = nn.create_phenotype(c)

# initial conditions (as used by Stanley)
x = randint(0, 4800) / 1000.0 - 2.4
x_dot = randint(0, 4000) / 1000.0 - 2.0
theta = randint(0, 400) / 1000.0 - 0.2
theta_dot = randint(0, 3000) / 1000.0 - 1.5

last_t = 0


def make_frame(t):
    global x, x_dot, theta, theta_dot, last_t

    # maps into [0,1]
    inputs = [(x + 2.4) / 4.8,
Example #9
0
    def run(self, testing=False):
        """ Runs the cart-pole experiment and evaluates the population. """

        if self.__markov:
            # markov experiment: full system's information is provided to the network
            for chromo in self.__population:
                # chromosome to phenotype
                assert chromo.num_inputs == 6, "There must be 6 inputs to the network"
                net = nn.create_phenotype(chromo)

                self.__initial_state()

                if testing:
                    # cart's position, first pole's angle, second pole's angle
                    # print "\nInitial conditions:"
                    print "{0:f} \t {1:f} \t {2:f}".format(self.__state[0], self.__state[2], self.__state[4])
                    pass

                steps = 0

                while steps < 100000:
                    inputs = [self.__state[0] / 4.80,  # cart's initial position
                              self.__state[1] / 2.00,  # cart's initial speed
                              self.__state[2] / 0.52,  # pole_1 initial angle
                              self.__state[3] / 2.00,  # pole_1 initial angular velocity
                              self.__state[4] / 0.52,  # pole_2 initial angle
                              self.__state[5] / 2.00]  # pole_2 initial angular velocity

                    # activate the neural network
                    output = net.pactivate(inputs)
                    # maps [-1,1] onto [0,1]
                    action = 0.5 * (output[0] + 1.0)
                    # advances one time step
                    self.__state = integrate(action, self.__state, 1)

                    if self.__outside_bounds():
                        # network failed to solve the task
                        if testing:
                            print "Failed at step {0:d} \t {1:+1.2f} \t {2:+1.2f} \t {3:+1.2f}".format(steps, self.__state[0], self.__state[2], self.__state[4])
                            sys.exit(0)
                        else:
                            break
                    steps += 1

                chromo.fitness = float(steps)  # the higher the better
                # if self.print_status:
                #    print "Chromosome %3d evaluated with score: %d " %(chromo.id, chromo.fitness)

        else:
            # non-markovian: no velocity information is provided (only 3 inputs)
            for chromo in self.__population:
                assert chromo.num_inputs == 3, "There must be 3 inputs to the network"
                net = nn.create_phenotype(chromo)
                self.__initial_state()

                chromo.fitness, score = self.__non_markov(net, 1000, testing)

                # print "Chromosome %3d %s evaluated with fitness %2.5f and score: %s" %(chromo.id, chromo.size(), chromo.fitness, score)

            # we need to make sure that the found solution is robust enough and good at
            # generalizing for several different initial conditions, so the champion
            # from each generation (i.e., the one with the highest F) passes for a
            # generalization test (the criteria here was defined by Gruau)

            best = max(self.__population)  # selects the best network
            if self.print_status:
                print "\t\nBest chromosome of generation: {0:d}".format(best.ID)

            # ** *******************#
            #  GENERALIZATION TEST  #
            # **********************#

            # first: can it balance for at least 100k steps?
            best_net = nn.create_phenotype(best)
            best_net.flush()
            self.__initial_state()  # reset initial state
            # long non-markovian test
            if self.print_status:
                print "Starting the 100k test..."
            score = self.__non_markov(best_net, 100000, testing)[1]

            if score > 99999:
                if self.print_status:
                    print "\tWinner passed the 100k test! Starting the generalization test..."
                # second: now let's try 625 different initial conditions
                balanced = self.__generalization_test(best_net, testing)

                if balanced > 200:
                    if self.print_status:
                        print "\tWinner passed the generalization test with score: {0:d}\n".format(balanced)
                    # set chromosome's fitness to 100k (and ceases the simulation)
                    best.fitness = 100000
                    best.score = balanced
                else:
                    if self.print_status:
                        print "\tWinner failed the generalization test with score: {0:d}\n".format(balanced)

            else:
                if self.print_status:
                    print "\tWinner failed at the 100k test with score {0:d}\n ".format(score)
Example #10
0
    def run(self, testing=False):
        """ Runs the cart-pole experiment and evaluates the population. """

        if self.__markov:
            # markov experiment: full system's information is provided to the network
            for chromo in self.__population:
                # chromosome to phenotype
                assert chromo.num_inputs == 6, "There must be 6 inputs to the network"
                net = nn.create_phenotype(chromo)

                self.__initial_state()

                if testing:
                    # cart's position, first pole's angle, second pole's angle
                    # print "\nInitial conditions:"
                    print "{0:f} \t {1:f} \t {2:f}".format(
                        self.__state[0], self.__state[2], self.__state[4])
                    pass

                steps = 0

                while steps < 100000:
                    inputs = [
                        self.__state[0] / 4.80,  # cart's initial position
                        self.__state[1] / 2.00,  # cart's initial speed
                        self.__state[2] / 0.52,  # pole_1 initial angle
                        self.__state[3] /
                        2.00,  # pole_1 initial angular velocity
                        self.__state[4] / 0.52,  # pole_2 initial angle
                        self.__state[5] / 2.00
                    ]  # pole_2 initial angular velocity

                    # activate the neural network
                    output = net.pactivate(inputs)
                    # maps [-1,1] onto [0,1]
                    action = 0.5 * (output[0] + 1.0)
                    # advances one time step
                    self.__state = integrate(action, self.__state, 1)

                    if self.__outside_bounds():
                        # network failed to solve the task
                        if testing:
                            print "Failed at step {0:d} \t {1:+1.2f} \t {2:+1.2f} \t {3:+1.2f}".format(
                                steps, self.__state[0], self.__state[2],
                                self.__state[4])
                            sys.exit(0)
                        else:
                            break
                    steps += 1

                chromo.fitness = float(steps)  # the higher the better
                # if self.print_status:
                #    print "Chromosome %3d evaluated with score: %d " %(chromo.id, chromo.fitness)

        else:
            # non-markovian: no velocity information is provided (only 3 inputs)
            for chromo in self.__population:
                assert chromo.num_inputs == 3, "There must be 3 inputs to the network"
                net = nn.create_phenotype(chromo)
                self.__initial_state()

                chromo.fitness, score = self.__non_markov(net, 1000, testing)

                # print "Chromosome %3d %s evaluated with fitness %2.5f and score: %s" %(chromo.id, chromo.size(), chromo.fitness, score)

            # we need to make sure that the found solution is robust enough and good at
            # generalizing for several different initial conditions, so the champion
            # from each generation (i.e., the one with the highest F) passes for a
            # generalization test (the criteria here was defined by Gruau)

            best = max(self.__population)  # selects the best network
            if self.print_status:
                print "\t\nBest chromosome of generation: {0:d}".format(
                    best.ID)

            # ** *******************#
            #  GENERALIZATION TEST  #
            # **********************#

            # first: can it balance for at least 100k steps?
            best_net = nn.create_phenotype(best)
            best_net.flush()
            self.__initial_state()  # reset initial state
            # long non-markovian test
            if self.print_status:
                print "Starting the 100k test..."
            score = self.__non_markov(best_net, 100000, testing)[1]

            if score > 99999:
                if self.print_status:
                    print "\tWinner passed the 100k test! Starting the generalization test..."
                # second: now let's try 625 different initial conditions
                balanced = self.__generalization_test(best_net, testing)

                if balanced > 200:
                    if self.print_status:
                        print "\tWinner passed the generalization test with score: {0:d}\n".format(
                            balanced)
                    # set chromosome's fitness to 100k (and ceases the simulation)
                    best.fitness = 100000
                    best.score = balanced
                else:
                    if self.print_status:
                        print "\tWinner failed the generalization test with score: {0:d}\n".format(
                            balanced)

            else:
                if self.print_status:
                    print "\tWinner failed at the 100k test with score {0:d}\n ".format(
                        score)
Example #11
0
from random import randint
import cPickle as pickle
import single_pole

chromosome.node_gene_type = genome2.NodeGene

# load the winner
file = open('winner_chromosome', 'r')
c = pickle.load(file)
file.close()

print 'Loaded chromosome:'
print c

config.load('spole_config')
net = nn.create_phenotype(c)

#x = 0.0
#x_dot = 0.0
#theta = 0.0
#theta_dot = 0.0

# initial conditions (as used by Stanley)
x = randint(0, 4799) / 1000.0 - 2.4
x_dot = randint(0, 1999) / 1000.0 - 1.0
theta = randint(0, 399) / 1000.0 - 0.2
theta_dot = randint(0, 2999) / 1000.0 - 1.5

print "\nInitial conditions:"
print "%2.4f   %2.4f   %2.4f   %2.4f" % (x, x_dot, theta, theta_dot)
for step in xrange(10**5):
Example #12
0
def eval_individual(chromo,logFP=None):
    brain = nn.create_phenotype(chromo)
    sumTime = 0
    sumPeople = 0
    sumDist = 0
    
    for trial in range(3):
        window = GraphWin("CS81 Final Project",1000,1000)
        window.setBackground("blue")
        
        preyList = []
        time = 0
        people = 0
        distance = 0
    
        p1 = Prey(0,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        p2 = Prey(1,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        p3 = Prey(2,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        p4 = Prey(3,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        p5 = Prey(4,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        p6 = Prey(5,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        p7 = Prey(6,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        p8 = Prey(7,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        p9 = Prey(8,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        p10 = Prey(9,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        p11 = Prey(10,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        p12 = Prey(11,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        p13 = Prey(12,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        p14 = Prey(13,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        p15 = Prey(14,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        p16 = Prey(15,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        p17 = Prey(16,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        p18 = Prey(17,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        p19 = Prey(18,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        p20 = Prey(19,(random()*800) + 100,(random()*800) + 100,choice([-1,1])*2*pi*random(),window)
        

        preyList.append(p1)
        preyList.append(p2)
        preyList.append(p3)
        preyList.append(p4)
        preyList.append(p5)
        preyList.append(p6)
        preyList.append(p7)
        preyList.append(p8)
        preyList.append(p9)
        preyList.append(p10)
        preyList.append(p11)
        preyList.append(p12)
        preyList.append(p13)
        preyList.append(p14)
        preyList.append(p15)
        preyList.append(p16)
        preyList.append(p17)
        preyList.append(p18)
        preyList.append(p19)
        preyList.append(p20)
    
        shark = Predator(1000,1000,5*pi/4,window)

        while (len(preyList) > 3) and time < 15000:
            time += 1
            
            for p in preyList:
                if time %50 == 0:
                    distance += p.getTraveled()
                    p.setPastX(p.getX())
                    p.setPastY(p.getY())
                    
                brain.flush()
                inputs = p.calculateInputs(preyList,shark)

                if inputs == "DEAD":
                    preyList.remove(p)

                else:
                    outputs = brain.pactivate(inputs)
                    p.move(outputs[0],outputs[1])

            if time == 4500:
                print "Shark attack!"
            if time > 4500:
                shark.move(preyList)

        window.close()

        people = len(preyList)
        time -= 4500
        time /= float(10500)
        people /= float(20)
        distance /= float(75000)

        sumTime += time
        sumPeople += people
        sumDist += distance
        
    return .4*(sumTime/3) + .4*(sumPeople/3) + .2*(sumDist/3)