Esempio n. 1
0
   def annBreeding(self, mom, dad):
      child = Ann() 
      momScore = mom.highScore
      dadScore = dad.highScore
      total = momScore + dadScore
      parents = [mom, dad] #parents in array for easier indexing

      #choosing fittest parents, so they pass their genes more often
      if momScore > dadScore: 
         percent = momScore/total
         fittest = 0
         notFit = 1
      else:
         percent = dadScore/total
         fittest = 1
         notFit = 0

      for i in range(0, len(mom.data) - 1): #iterate through layers
         for j in range(0, len(mom.data[i])): #iterate through neurons
               chance = random.uniform(0, 1); #which parent to get genne from
               if chance < percent:
                  child.data[i][j] = parents[fittest].data[i][j]
               else:
                  child.data[i][j] = parents[notFit].data[i][j]

      return child
Esempio n. 2
0
    def annBreeding(self, mom, dad):
        child = Ann()
        momScore = mom.highScore
        dadScore = dad.highScore
        #print "momScore: ", momScore, " and dadScore: ", dadScore
        momScore += 651  #avoid dbz error
        dadScore += 651
        total = momScore + dadScore
        parents = [mom, dad]  #parents in array for easier indexing

        #choosing fittest parents, so they pass their genes more often
        if momScore > dadScore:
            percent = momScore / total
            fittest = 0
            notFit = 1
        else:
            percent = dadScore / total
            fittest = 1
            notFit = 0

        #TODO try swapping weights instead of entire neurons
        for i in range(0, len(mom.data) - 1):  #iterate through layers
            for j in range(0, len(mom.data[i])):  #iterate through neurons
                chance = random.uniform(0, 1)
                #which parent to get genne from
                if chance < percent:
                    child.data[i][j] = parents[fittest].data[i][j]
                else:
                    child.data[i][j] = parents[notFit].data[i][j]

        return child
Esempio n. 3
0
 def run_simulation(self, environments_copy):
     weights = self.prepare_weights_for_ann()
     ann = Ann(weights=weights,
               hidden_layers=self.hidden_layers,
               activation_functions=self.activation_functions)
     for j in range(len(environments_copy)):
         environment = environments_copy[j]
         agent = environments_copy[j].agent
         for k in range(60):
             agent_sensor_output = agent.sense_front_left_right(environment)
             ann_inputs = [
                 1 if agent_sensor_output[0] == CellItem.food else 0,
                 1 if agent_sensor_output[1] == CellItem.food else 0,
                 1 if agent_sensor_output[2] == CellItem.food else 0,
                 1 if agent_sensor_output[0] == CellItem.poison else 0,
                 1 if agent_sensor_output[1] == CellItem.poison else 0,
                 1 if agent_sensor_output[2] == CellItem.poison else 0
             ]
             prediction = ann.predict(inputs=ann_inputs)
             best_index = prediction.argmax()
             if best_index == 1:
                 agent.move_left()
             elif best_index == 2:
                 agent.move_right()
             agent.move_forward(environment)
Esempio n. 4
0
def main():
    test_data_set_path = '../../../Dataset/TestData/'
    test_files = os.listdir(test_data_set_path)
    test_files = random.sample(test_files, 10)

    network = Ann()
    print "=" * 20, "Test Results", "=" * 20
    for each_file in test_files:
        print each_file.ljust(10) + ": " + network.get_emotion(test_data_set_path + each_file)
    print "=" * 54

    print "Now recorded: ", network.get_emotion("/home/venkatesh/Desktop/recorded.wav")
    print "Now recorded: ", network.get_emotion("/home/venkatesh/Desktop/recorded_sad.wav")
Esempio n. 5
0
    def initialize(self):
        chrisMartinez = Ann()
        #chrisMartinez.print()
        #chrisMartinez.printLayer(3)
        """
        for i in range(0, 9):
            chrisMartinez.trainShit()
        """

        self.data[0] = chrisMartinez
        for i in range(1, ANN_COUNT):
            #temp = copy.copy(chrisMartinez)
            #self.mutateAnn(temp)
            temp = Ann()
            #temp.printAnn()
            self.data[i] = temp
Esempio n. 6
0
 def __init__(self, delay, environments, ann_weights, layers_list, activation_functions,
              fitness_log_average, fitness_log_best, standard_deviation_log):
     tk.Tk.__init__(self)
     self.delay = delay
     self.flatlands = environments
     self.ann = Ann(weights=ann_weights, hidden_layers=layers_list, activation_functions=activation_functions)
     self.current_timestep = 0
     self.max_timestep = 60
     self.standard_deviation_log = standard_deviation_log
     self.fitness_log_best = fitness_log_best
     self.fitness_log_average = fitness_log_average
     self.title("Flatland")
     self.grid = {}
     self.food_texts = []
     self.poison_texts = []
     self.step_text = None
     self.cell_components = {}
     self.agent_component = [[] for _ in range(len(self.flatlands))]
     self.cell_size = (SCREEN_WIDTH - (max(2, len(environments) + 1)) * GRID_OFFSET) / \
                      (FLATLAND_WIDTH*max(2, len(environments)))
     self.canvas = tk.Canvas(self, width=SCREEN_WIDTH, height=(self.cell_size + 4)*self.flatlands[0].height, background='white', borderwidth=0)
     self.canvas.pack(side="top", fill="both", expand="true")
     self.pause = True
     self.bind('<space>', self.toggle_pause)
     self.bind('<n>', self.decrease_simulation_speed)
     self.bind('<m>', self.increase_simulation_speed)
     self.reset_button = tk.Button(self, text="Reset board", command=self.reset_gui_with_new_environment).pack()
     self.draw_text()
     self.draw_board()
     self.update_text()
     self.draw_agents()
     self.run_simulation()
Esempio n. 7
0
def perceptron(size, weights):
    """Creates a perceptron network
    """
    inputLayer = Layer(LayerType.INPUT, size[0], activation=None)
    outputLayer = Layer(LayerType.OUTPUT, size[1], activation=Neuron.HEAVISIDE)
    pp.pprint(weights)
    inputLayer.connect(outputLayer, weights)
    return Ann({'input': inputLayer, 'hidden': [], 'output': outputLayer})
Esempio n. 8
0
    def create_ann(self):
        self.extract_layers()

        self.extract_modules()

        self.extract_links()

        return Ann(self.layers, self.links, self.parse_execution_order())
Esempio n. 9
0
 def calculate_phenotype(self):
     self.phenotype = Ann(FlatLandGenotype.num_inputs,
                          FlatLandGenotype.num_outputs)
     for i in range(self.phenotype.num_edges):
         j = i * FlatLandGenotype.bits_per_weight
         weight = sum(self.genotype.dna[j:j +
                                        FlatLandGenotype.bits_per_weight])
         weight = 4 * float(weight) / FlatLandGenotype.bits_per_weight - 2
         self.phenotype.weights[i] = weight
Esempio n. 10
0
def initialize_agents():
    agents = []

    ann = Ann(name='Ann', args=('Ann', routes, routing_table, graph))
    ann.daemon = True
    ann.start()
    agents.append(ann)
    time.sleep(1)

    chan = Chan(name='Chan', args=('Chan', routes, routing_table, graph))
    chan.daemon = True
    chan.start()
    agents.append(chan)
    time.sleep(1)

    jan = Jan(name='Jan', args=('Jan', routes, routing_table, graph))
    jan.daemon = True
    jan.start()
    agents.append(jan)

    return agents
Esempio n. 11
0
def multi_layer_network(size, weights):
    """Creates a 3 layer network (1 hidden)
    """
    inputLayer = Layer(LayerType.INPUT, size[0], activation=None)
    hiddenLayer = Layer(LayerType.HIDDEN, size[1], activation=Neuron.LOGISTIC)
    outputLayer = Layer(LayerType.OUTPUT, size[2], activation=Neuron.LOGISTIC)
    inputLayer.connect(hiddenLayer, weights[1])
    hiddenLayer.connect(outputLayer, weights[0])
    return Ann({
        'input': inputLayer,
        'hidden': [hiddenLayer],
        'output': outputLayer
    })
Esempio n. 12
0
 def run_simulation(self, environments_copy):
     weights = self.prepare_weights_for_ann()
     ann = Ann(weights=weights, hidden_layers=self.hidden_layers, activation_functions=self.activation_functions)
     for j in range(len(environments_copy)):
         environment = environments_copy[j]
         agent = environments_copy[j].agent
         for k in range(60):
             agent_sensor_output = agent.sense_front_left_right(environment)
             ann_inputs = [
                 1 if agent_sensor_output[0] == CellItem.food else 0,
                 1 if agent_sensor_output[1] == CellItem.food else 0,
                 1 if agent_sensor_output[2] == CellItem.food else 0,
                 1 if agent_sensor_output[0] == CellItem.poison else 0,
                 1 if agent_sensor_output[1] == CellItem.poison else 0,
                 1 if agent_sensor_output[2] == CellItem.poison else 0,
             ]
             prediction = ann.predict(inputs=ann_inputs)
             best_index = prediction.argmax()
             if best_index == 1:
                 agent.move_left()
             elif best_index == 2:
                 agent.move_right()
             agent.move_forward(environment)
Esempio n. 13
0
def deep_network(size, weights):
    """Creates a 5 layer network (3 hidden)
    """
    inputLayer = Layer(LayerType.INPUT, size[0], activation=None)
    hiddenLayer1 = Layer(LayerType.HIDDEN, size[1], activation=Neuron.LOGISTIC)
    hiddenLayer2 = Layer(LayerType.HIDDEN, size[2], activation=Neuron.LOGISTIC)
    hiddenLayer3 = Layer(LayerType.HIDDEN, size[3], activation=Neuron.LOGISTIC)
    outputLayer = Layer(LayerType.OUTPUT, size[4], activation=Neuron.LOGISTIC)
    inputLayer.connect(hiddenLayer1, weights[3])
    hiddenLayer1.connect(hiddenLayer2, weights[2])
    hiddenLayer2.connect(hiddenLayer3, weights[1])
    hiddenLayer3.connect(outputLayer, weights[0])
    return Ann({
        'input': inputLayer,
        'hidden': [hiddenLayer1, hiddenLayer2, hiddenLayer3],
        'output': outputLayer
    })
def mains():
    # for ann - load the path weights ..
    file_r = open('data/min_error_weights.pkl', 'r')
    ann_weights = pickle.load(file_r)
    file_r.close()

    hidden_weights = ann_weights[0]
    output_weights = ann_weights[1]

    ann_obj = Ann(1, parameters.no_of_hidden, 4)

    i = 0
    for hnode in ann_obj.hidden_nodes:
        j = 0
        for w in hnode.weights:
            hnode.weights[j] = hidden_weights[i]
            i += 1
            j += 1
    i = 0
    for onode in ann_obj.output_nodes:
        j = 0
        for w in onode.weights:
            onode.weights[j] = output_weights[i]
            i += 1
            j += 1

    # prediction model starts ..
    obj = pmodel()
    obj_p = p_clustering(parameters.no_of_clusters, parameters.dimensionality)

    time = 3

    train_status = obj.initial_model()
    print "\n..Clusters trained. Real Time handling of data started"
    obj_p.prepare_fclusters(obj.cluster_info, time)

    ch = 'y'
    while ch == 'y':
        obj.real_time_handle(obj_p, time, ann_obj, train_status)
        ch = raw_input("\n..Want to continue ? (y/n) : ")
        time += 1
        train_status = False

    print '\n..Exiting'
Esempio n. 15
0
    arg_parser.add_argument(
        '-g',
        '--generation',
        dest='generation',
        help=
        'If dynamic mode, this specifies which generation to pick board(s) from',
        type=int,
        required=False,
        default=0)

    args = arg_parser.parse_args()

    with open('best_individual.json') as best_agent_weights:
        weights = json.load(best_agent_weights)

    a = Ann(num_inputs=6, num_outputs=3)
    a.weights = weights

    g = gfx.Gfx()
    g.fps = 8

    for i in range(args.num_scenarios):
        grid_seed = i + (
            (997 * args.generation) if args.mode == 'dynamic' else 0)
        print 'grid_seed', grid_seed
        f = Flatland(ann=a, grid_seed=grid_seed, should_visualize=True)
        f.gfx = g
        f.run()
        print '{0} food items, {1} poison items'.format(
            f.agent.num_food_consumed, f.agent.num_poison_consumed)
Esempio n. 16
0
File: main.py Progetto: fspinolo/ANN
#!/usr/bin/python
from __future__ import print_function

from ann import Ann

flines = []
with open('optdigits_train.txt', 'r') as f:
    flines = f.readlines()

data = [
    [float(x) for x in line.strip().split(',')[:-1]]
    for line in flines
]
answers = [line.strip().split(',')[-1] for line in flines]

nn = Ann()
for i in range(100):
    print(nn.train(data, answers))
Esempio n. 17
0
 def __init__(self):
     self.data = [Ann()] * ANN_COUNT
     self.highScores = []
     self.avgScores = []
Esempio n. 18
0
from ann import ATOMModel, Ann
from ase.io import read

p1 = read('au55.xyz', index=0, format='xyz')
model = ATOMModel(restore='atom_model', n=55)
p1.set_calculator(Ann(atoms=p1, ann_model=model))
print p1.get_potential_energy()
print p1.get_forces()
Esempio n. 19
0
    'type': 'output'
}, {
    'id': 'o2',
    'x': 6,
    'y': 3,
    'size': 1,
    'label': 'motor_right',
    'type': 'output'
}]

with open('best_individual.json') as best_agent_weights:
    weights = json.load(best_agent_weights)

num_inputs = 6
num_outputs = 3
a = Ann(num_inputs, num_outputs)
a.weights = weights

edges = []
i = 0

for output_index in range(num_outputs):
    for input_index in range(num_inputs + 1):
        idx = a.convert_2d_to_1d(input_index, output_index)
        weight = a.weights[idx]

        edges.append({
            'id': 'e{}'.format(i),
            'source': 'i{}'.format(input_index),
            'target': 'o{}'.format(output_index),
            'weight': weight
Esempio n. 20
0
from ann import Ann
import operator
import random
from breeding import Breeding

loveShack = Breeding()
lovers = []

for i in range(0, 4):
    lovers.append(Ann())
    lovers[i].giveName("John")
    score = random.uniform(0, 100)
    lovers[i].setScore(score)
    lovers[i].Print()

print
print "Lets Make Love!!!"
print

loveShack.getGen(lovers)
loveShack.getNextGeneration()


Esempio n. 21
0
                                  '_ann' + str(i)))  #create new filename
                currAnn.saveFile()
                #currAnn.printScore()
                i += 1
            # stuff b/w generations
            breeder.getNextGeneration()
        """
            if WE_HAVE_ANN_AGENT:
                poolOfAnns = breeder.data
                #for NUMBER_OF_GENERATIONS
                #for ann in ANN_POOL_SIZE
                    score = runGames( **args )
                print score
            else:
                runGames
        """

    elif sys.argv[2:3] == ['LoaderAgent']:
        print 'Using file: ' + FILE_TO_BE_LOADED
        temp = Ann()
        temp.loadFile(FILE_TO_BE_LOADED)
        args['pacman'].setAnn(temp)  #set ann to the one gotten from .txt file
        runGames(**args)
    else:
        print 'Continue as normal'
        runGames(**args)

    # import cProfile
    # cProfile.run("runGames( **args )")
    pass
Esempio n. 22
0
class FlatlandGui(tk.Tk):
    def __init__(self, delay, environments, ann_weights, layers_list, activation_functions,
                 fitness_log_average, fitness_log_best, standard_deviation_log):
        tk.Tk.__init__(self)
        self.delay = delay
        self.flatlands = environments
        self.ann = Ann(weights=ann_weights, hidden_layers=layers_list, activation_functions=activation_functions)
        self.current_timestep = 0
        self.max_timestep = 60
        self.standard_deviation_log = standard_deviation_log
        self.fitness_log_best = fitness_log_best
        self.fitness_log_average = fitness_log_average
        self.title("Flatland")
        self.grid = {}
        self.food_texts = []
        self.poison_texts = []
        self.step_text = None
        self.cell_components = {}
        self.agent_component = [[] for _ in range(len(self.flatlands))]
        self.cell_size = (SCREEN_WIDTH - (max(2, len(environments) + 1)) * GRID_OFFSET) / \
                         (FLATLAND_WIDTH*max(2, len(environments)))
        self.canvas = tk.Canvas(self, width=SCREEN_WIDTH, height=(self.cell_size + 4)*self.flatlands[0].height, background='white', borderwidth=0)
        self.canvas.pack(side="top", fill="both", expand="true")
        self.pause = True
        self.bind('<space>', self.toggle_pause)
        self.bind('<n>', self.decrease_simulation_speed)
        self.bind('<m>', self.increase_simulation_speed)
        self.reset_button = tk.Button(self, text="Reset board", command=self.reset_gui_with_new_environment).pack()
        self.draw_text()
        self.draw_board()
        self.update_text()
        self.draw_agents()
        self.run_simulation()

    def toggle_pause(self, event=None):
        self.pause = not self.pause

    def increase_simulation_speed(self, event=None):
        self.delay = max(self.delay - 10, 1)

    def decrease_simulation_speed(self, event=None):
        self.delay += 10

    def draw_board(self):
        for i in range(len(self.flatlands)):
            offset = (i * (FLATLAND_WIDTH * self.cell_size + GRID_OFFSET))
            for y in range(FLATLAND_HEIGHT):
                for x in range(FLATLAND_WIDTH):
                    self.grid[i, x, y] = self.canvas.create_rectangle(
                            x * self.cell_size + GRID_OFFSET + offset,
                            y * self.cell_size + GRID_OFFSET,
                            (x + 1) * self.cell_size + GRID_OFFSET + offset,
                            (y + 1) * self.cell_size + GRID_OFFSET)
                    if self.flatlands[i].board[y][x] == CellItem.food:
                        self.cell_components[i, x, y] = self.canvas.create_oval(
                                x * self.cell_size + GRID_OFFSET + offset,
                                y * self.cell_size + GRID_OFFSET,
                                (x + 1) * self.cell_size + GRID_OFFSET + offset,
                                (y + 1) * self.cell_size + GRID_OFFSET,
                                fill="green")
                    elif self.flatlands[i].board[y][x] == CellItem.poison:
                        self.cell_components[i, x, y] = self.canvas.create_oval(
                                x * self.cell_size + GRID_OFFSET + offset,
                                y * self.cell_size + GRID_OFFSET,
                                (x + 1) * self.cell_size + GRID_OFFSET + offset,
                                (y + 1) * self.cell_size + GRID_OFFSET,
                                fill="red")


    def draw_text(self):
        for i in range(len(self.flatlands)):
            self.food_texts.append(self.canvas.create_text(
                    GRID_OFFSET + (i * (FLATLAND_WIDTH * self.cell_size + GRID_OFFSET)),
                    GRID_OFFSET + FLATLAND_HEIGHT * self.cell_size,
                    anchor=tk.NW))
            self.poison_texts.append(self.canvas.create_text(
                    GRID_OFFSET + (i * (FLATLAND_WIDTH * self.cell_size + GRID_OFFSET)),
                    GRID_OFFSET*1.5 + FLATLAND_HEIGHT * self.cell_size,
                    anchor=tk.NW))
        self.step_text = self.canvas.create_text(GRID_OFFSET, GRID_OFFSET/2)

    def update_text(self):
        for i in range(len(self.flatlands)):
            self.canvas.itemconfig(self.food_texts[i], text="Food eaten:" +
                                                            str(self.flatlands[i].agent.food_eaten) + "/" +
                                                            str(self.flatlands[i].food_count))
            self.canvas.itemconfig(self.poison_texts[i], text="Poison eaten:" +
                                                              str(self.flatlands[i].agent.poison_eaten) + "/" +
                                                              str(self.flatlands[i].poison_count))
        self.canvas.itemconfig(self.step_text, text="Step: " + str(self.current_timestep + 1))

    def draw_agents(self):
        for i in range(len(self.flatlands)):
            agent = self.flatlands[i].agent
            for j in range(len(self.agent_component[i])):
                self.canvas.delete(self.agent_component[i][j])
            offset = (i * (FLATLAND_WIDTH * self.cell_size + GRID_OFFSET))
            self.agent_component[i] = []
            self.agent_component[i].append(self.canvas.create_polygon(
                agent.x * self.cell_size + (self.cell_size * 1/5) + GRID_OFFSET + offset,
                agent.y * self.cell_size + (self.cell_size * 2/5) + GRID_OFFSET,
                agent.x * self.cell_size + (self.cell_size * 2/5) + GRID_OFFSET + offset,
                agent.y * self.cell_size + (self.cell_size * 1/5) + GRID_OFFSET,
                agent.x * self.cell_size + (self.cell_size * 3/5) + GRID_OFFSET + offset,
                agent.y * self.cell_size + (self.cell_size * 1/5) + GRID_OFFSET,
                agent.x * self.cell_size + (self.cell_size * 4/5) + GRID_OFFSET + offset,
                agent.y * self.cell_size + (self.cell_size * 2/5) + GRID_OFFSET,
                agent.x * self.cell_size + (self.cell_size * 4/5) + GRID_OFFSET + offset,
                agent.y * self.cell_size + (self.cell_size * 3/5) + GRID_OFFSET,
                agent.x * self.cell_size + (self.cell_size * 3/5) + GRID_OFFSET + offset,
                agent.y * self.cell_size + (self.cell_size * 4/5) + GRID_OFFSET,
                agent.x * self.cell_size + (self.cell_size * 2/5) + GRID_OFFSET + offset,
                agent.y * self.cell_size + (self.cell_size * 4/5) + GRID_OFFSET,
                agent.x * self.cell_size + (self.cell_size * 1/5) + GRID_OFFSET + offset,
                agent.y * self.cell_size + (self.cell_size * 3/5) + GRID_OFFSET,
                outline='red', fill='blue', width=1))
            if agent.direction == Direction.north or agent.direction == Direction.east:
                self.agent_component[i].append(self.canvas.create_oval(
                    agent.x * self.cell_size + (self.cell_size * 12/20) + GRID_OFFSET + offset,
                    agent.y * self.cell_size + (self.cell_size * 6/20) + GRID_OFFSET,
                    agent.x * self.cell_size + (self.cell_size * 14/20) - 1 + GRID_OFFSET + offset,
                    agent.y * self.cell_size + (self.cell_size * 8/20) + GRID_OFFSET,
                    fill="yellow"
                ))
            if agent.direction == Direction.east or agent.direction == Direction.south:
                self.agent_component[i].append(self.canvas.create_oval(
                    agent.x * self.cell_size + (self.cell_size * 12/20) + GRID_OFFSET + offset,
                    agent.y * self.cell_size + (self.cell_size * 12/20) + GRID_OFFSET,
                    agent.x * self.cell_size + (self.cell_size * 14/20) + GRID_OFFSET + offset,
                    agent.y * self.cell_size + (self.cell_size * 14/20) + GRID_OFFSET,
                    fill="yellow"
                ))
            if agent.direction == Direction.south or agent.direction == Direction.west:
                self.agent_component[i].append(self.canvas.create_oval(
                    agent.x * self.cell_size + (self.cell_size * 6/20) + GRID_OFFSET + offset,
                    agent.y * self.cell_size + (self.cell_size * 12/20) + GRID_OFFSET,
                    agent.x * self.cell_size + (self.cell_size * 8/20) + GRID_OFFSET + offset,
                    agent.y * self.cell_size + (self.cell_size * 14/20) + GRID_OFFSET,
                    fill="yellow"
                ))
            if agent.direction == Direction.west or agent.direction == Direction.north:
                self.agent_component[i].append(self.canvas.create_oval(
                    agent.x * self.cell_size + (self.cell_size * 6/20) + GRID_OFFSET + offset,
                    agent.y * self.cell_size + (self.cell_size * 6/20) + GRID_OFFSET,
                    agent.x * self.cell_size + (self.cell_size * 8/20) + GRID_OFFSET + offset,
                    agent.y * self.cell_size + (self.cell_size * 8/20) + GRID_OFFSET,
                    fill="yellow"
                ))
            if agent.direction == Direction.north or \
                    agent.direction == Direction.east or \
                    agent.direction == Direction.west:
                self.agent_component[i].append(self.canvas.create_line(
                    agent.x * self.cell_size + (self.cell_size * 2/5) + GRID_OFFSET + offset,
                    agent.y * self.cell_size + (self.cell_size * 1/5) + GRID_OFFSET,
                    agent.x * self.cell_size + (self.cell_size * 1/2) + GRID_OFFSET + offset,
                    agent.y * self.cell_size - (self.cell_size * 1/5) + GRID_OFFSET,
                    agent.x * self.cell_size + (self.cell_size * 3/5) + GRID_OFFSET + offset,
                    agent.y * self.cell_size + (self.cell_size * 1/5) + GRID_OFFSET
                ))
            if agent.direction == Direction.south or \
                    agent.direction == Direction.east or \
                    agent.direction == Direction.west:
                self.agent_component[i].append(self.canvas.create_line(
                    agent.x * self.cell_size + (self.cell_size * 2/5) + GRID_OFFSET + offset,
                    agent.y * self.cell_size + (self.cell_size * 4/5) + GRID_OFFSET,
                    agent.x * self.cell_size + (self.cell_size * 1/2) + GRID_OFFSET + offset,
                    agent.y * self.cell_size + (self.cell_size * 6/5) + GRID_OFFSET,
                    agent.x * self.cell_size + (self.cell_size * 3/5) + GRID_OFFSET + offset,
                    agent.y * self.cell_size + (self.cell_size * 4/5) + GRID_OFFSET
                ))
            if agent.direction == Direction.west or \
                    agent.direction == Direction.north or \
                    agent.direction == Direction.south:
                self.agent_component[i].append(self.canvas.create_line(
                    agent.x * self.cell_size + (self.cell_size * 1/5) + GRID_OFFSET + offset,
                    agent.y * self.cell_size + (self.cell_size * 2/5) + GRID_OFFSET,
                    agent.x * self.cell_size - (self.cell_size * 1/5) + GRID_OFFSET + offset,
                    agent.y * self.cell_size + (self.cell_size * 1/2) + GRID_OFFSET,
                    agent.x * self.cell_size + (self.cell_size * 1/5) + GRID_OFFSET + offset,
                    agent.y * self.cell_size + (self.cell_size * 3/5) + GRID_OFFSET
                ))
            if agent.direction == Direction.east or \
                    agent.direction == Direction.north or \
                    agent.direction == Direction.south:
                self.agent_component[i].append(self.canvas.create_line(
                    agent.x * self.cell_size + (self.cell_size * 4/5) + GRID_OFFSET + offset,
                    agent.y * self.cell_size + (self.cell_size * 2/5) + GRID_OFFSET,
                    agent.x * self.cell_size + (self.cell_size * 6/5) + GRID_OFFSET + offset,
                    agent.y * self.cell_size + (self.cell_size * 1/2) + GRID_OFFSET,
                    agent.x * self.cell_size + (self.cell_size * 4/5) + GRID_OFFSET + offset,
                    agent.y * self.cell_size + (self.cell_size * 3/5) + GRID_OFFSET
                ))

    def start_simulation(self):
        self.current_timestep = 0
        self.run_simulation()

    def run_simulation(self):
        if not self.pause:
            if self.current_timestep < self.max_timestep:
                for i in range(len(self.flatlands)):
                    agent_sensor_output = self.flatlands[i].agent.sense_front_left_right(self.flatlands[i])
                    ann_inputs = [1 if agent_sensor_output[0] == CellItem.food else 0,
                                  1 if agent_sensor_output[1] == CellItem.food else 0,
                                  1 if agent_sensor_output[2] == CellItem.food else 0,
                                  1 if agent_sensor_output[0] == CellItem.poison else 0,
                                  1 if agent_sensor_output[1] == CellItem.poison else 0,
                                  1 if agent_sensor_output[2] == CellItem.poison else 0]
                    prediction = self.ann.predict(inputs=ann_inputs)
                    best_index = prediction.argmax()
                    if best_index == 1:
                        self.flatlands[i].agent.move_left()
                    elif best_index == 2:
                        self.flatlands[i].agent.move_right()
                    self.flatlands[i].agent.move_forward(self.flatlands[i])
                    # Remove cell component
                    if (i, self.flatlands[i].agent.x, self.flatlands[i].agent.y) in self.cell_components:
                        self.canvas.delete(self.cell_components[i, self.flatlands[i].agent.x, self.flatlands[i].agent.y])
                self.draw_agents()
                self.update_text()
                self.current_timestep += 1
            else:
                print "Simulation over"
                self.plot_data(self.fitness_log_average, self.fitness_log_best, self.standard_deviation_log)
                return
        self.after(self.delay, lambda: self.run_simulation())

    def reset_gui_with_new_environment(self, event=None):
        self.flatlands = [Flatland(width=self.flatlands[0].width,
                                   height=self.flatlands[0].height,
                                   food_probability=self.flatlands[0].food_probability,
                                   poison_probability=self.flatlands[0].poison_probability)
                          for _ in range(len(self.flatlands))]
        self.pause = True
        for key, val in self.grid.items():
            self.canvas.delete(val)
        for key, val in self.cell_components.items():
            self.canvas.delete(val)
        self.draw_board()
        self.draw_agents()
        self.start_simulation()

    @staticmethod
    def plot_data(fitness_log_average, fitness_log_best, standard_deviation_log):
        plt.figure(1)
        plt.subplot(211)
        plt.plot(fitness_log_average[-1], label="Average fitness")
        plt.plot(fitness_log_best[-1], label="Best fitness")
        plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=2, mode="expand", borderaxespad=0.)

        plt.subplot(212)
        plt.plot(standard_deviation_log[-1], label="Standard deviation")
        #plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=2, mode="expand", borderaxespad=0.)
        plt.show()
Esempio n. 23
0
    window.update_view( board.generateState(board.grid) )
    #Feedback about how the game went

    print "Your best tile is: " + str(int(2 ** board.bestTile))
    return int(2 ** board.bestTile)

root = Tk()
window = GameWindow(root)

EPOCHS_PER_GAME             = 10
NEURONS_IN_HIDDEN_LAYERS    = [17,500,500,4]
LIST_OF_FUNCTIONS           = ["rectify","rectify","softmax"]
LEARNING_RATE               = 0.003
MOMENTUM_RATE               = 0.9

a = Ann(neuronsInHiddenLayers=NEURONS_IN_HIDDEN_LAYERS, listOfFunctions=LIST_OF_FUNCTIONS, learningRate=LEARNING_RATE, momentumRate=MOMENTUM_RATE, errorFunc=10)
for i in range (50):
    trX, trY = get_data.get_training_data('training/train_data_'+str(i+1))
    print "Training on data "+str(i+1)
    a.training(trX, trY,len(trX)-1,EPOCHS_PER_GAME)

random_res = []
#random_res = [128, 128, 256, 64, 256, 128, 64, 64, 32, 64, 128, 128, 128, 128, 128, 128, 64, 128, 64, 128, 128, 64, 64, 64, 64, 128, 128, 128, 64, 128, 128, 128, 128, 128, 128, 128, 32, 128, 256, 64, 64, 128, 64, 64, 64, 128, 128, 64, 128, 64]

for i in range(50):
    print "Random game: "+str(i+1)
    board = Board()
    window.update_view( board.generateState(board.grid) )
    solver = Solver(board, window, root)
    solver.startSolver("random")
    random_res.append(int(2**board.bestTile))
Esempio n. 24
0
 def __init__(self):
    self.avgScoreOfAnns = 0
    self.highScoreOfAnns = -999
    self.data = [Ann()] * ANN_COUNT
Esempio n. 25
0
from ann import Ann
from breeding import Breeding

net = Ann()

net.Print()
#net.PrintLayer(3)
#net.PrintNeuron(3, 1)