def annBreedingWeights(self, mom, dad):
        child = Ann()
        momScore = mom.highScore
        dadScore = dad.highScore
        #print "momScore: ", momScore, " and dadScore: ", dadScore
        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
                for k in range(
                        0, len(mom.data[i]
                               [j].weights)):  #iterate through neurons weights
                    chance = random.uniform(0, 1)
                    if chance < percent:
                        child.data[i][j].weights[k] = parents[fittest].data[i][
                            j].weights[k]
                    else:
                        child.data[i][j].weights[k] = parents[notFit].data[i][
                            j].weights[k]

        return child
   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
    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
Beispiel #4
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)
Beispiel #5
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})
Beispiel #6
0
    def create_ann(self):
        self.extract_layers()

        self.extract_modules()

        self.extract_links()

        return Ann(self.layers, self.links, self.parse_execution_order())
Beispiel #7
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
Beispiel #8
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
    })
Beispiel #9
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")
Beispiel #10
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'
Beispiel #12
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
Beispiel #13
0
 def __init__(self):
    self.avgScoreOfAnns = 0
    self.highScoreOfAnns = -999
    self.data = [Ann()] * ANN_COUNT
Beispiel #14
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))
Beispiel #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)
Beispiel #16
0
from ann import Ann
from breeding import Breeding

net = Ann()

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

Beispiel #17
0
 def __init__(self):
     self.data = [Ann()] * ANN_COUNT
     self.highScores = []
     self.avgScores = []
Beispiel #18
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
Beispiel #19
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()


Beispiel #20
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
Beispiel #21
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()