def __init__(self, numGames, genes=None):
     self.neuralNetwork = NeuralNetwork(
     ) if genes is None else NeuralNetwork(genes)
     self.fitness = None
     self.accumulatedFitness = None
     self.numGames = numGames
     self.score = [0 for i in range(numGames)]
     self.genes = []
def make_net(sensor_count= 16):
    net = NeuralNetwork([sensor_count, 7, 2], learn_rate=0.05)
    test_db = create_tests(sensor_count)
    # print "\n".join([str(x) for x in test_db])
    random.shuffle(test_db)

    for i in range(5):
        answ = net.calculate(test_db[i][0])
        print(test_db[i][0], test_db[i][1], answ)

    errs = []
    for _ in range(200):
        epoch_error = 0
        for test in test_db:
            y = net.calculate(test[0])
            err = math.sqrt((test[1][0]-y[0])**2 + (test[1][1]-y[1])**2)
            if err < 1.0:
                net.teach(-0.004)
            else:
                net.teach(0.004)
            epoch_error += err
        epoch_error /= len(test_db)
        errs.append(epoch_error)
        print epoch_error

    for i in range(10):
        answ = net.calculate(test_db[i][0])
        print(test_db[i][0], test_db[i][1], answ)

    if __name__=='__main__':
        plt.plot(errs)
        plt.show()

    return net
def get_new_brain(sensor_count):
    global cbrain
    if cbrain is None:
        brain = NeuralNetwork([sensor_count, 2, 2])
        database = []

        for line in open('memory.txt','r'):
            line = [float(x) for x in line.split()]
            database.append((line[:-2], line[-2:]))

        for _ in range(15):
            err = brain.teach_by_sample(database)
            print(err)
        cbrain = brain
    return cbrain
class NeuralNetworkIndividuals:
    def __init__(self, numGames, genes=None):
        self.neuralNetwork = NeuralNetwork(
        ) if genes is None else NeuralNetwork(genes)
        self.fitness = None
        self.accumulatedFitness = None
        self.numGames = numGames
        self.score = [0 for i in range(numGames)]
        self.genes = []

    def calcFitness(self):
        averageScore = sum(self.score) / self.numGames
        self.fitness = averageScore

        if self.fitness < 0:
            self.fitness = 0.1

    def getNetGenes(self):
        return self.neuralNetwork.getParams()

    def makemove(self, board):
        return self.neuralNetwork.makemove(board)

    def getFitness(self):
        return self.fitness

    def setFitness(self, fitness):
        self.fitness = fitness

    def getAccumulatedFitness(self):
        return self.accumulatedFitness

    def setAccumulatedFitness(self, fitness):
        self.accumulatedFitness = fitness

    def setScore(self, i, value):
        self.score[i] = value

    def printData(self):
        print("Winner: ", self.score)

    def setParams(self, genes):
        self.neuralNetwork.setParams(genes)
    def create_brain(dna, world_constants):
        def dna_iter(dna):
            for i in range(0, len(dna), world_constants.DNA_BRAIN_VALUE_LEN):
                cur = dna[i:i + world_constants.DNA_BRAIN_VALUE_LEN]
                yield (int(cur, world_constants.DNA_BASE) -
                       world_constants.DNA_HALF_MAX_VALUE
                       ) / world_constants.DNA_HALF_MAX_VALUE

        dna = dna_iter(dna)
        brain = NeuralNetwork(world_constants.NEURAL_NETWORK_SHAPE)
        for layer in brain:
            for neuron in layer:
                neuron.w = [dna.next() for _ in range(len(neuron.w))]
        return brain
    def __init__(self, world):
        self.world = world
        self._x = randint(0, self.world.width)
        self._y = randint(0, self.world.height)
        self.size = 7
        self.angle = 0

        self.sensor_count = 7
        self._sensor_count_in_head = int(self.sensor_count * Animal.SENSOR_COUNT_IN_HEAD_RATIO)
        self._sensor_count_not_in_head = self.sensor_count - self._sensor_count_in_head
        self.sensor_values = []
        self._sensors_positions = []
        self._sensors_positions_calculated = False

        self.energy = self.ENERGY_FOR_BUD
        self.readiness_to_bud = 0

        self.brain = NeuralNetwork([self.sensor_count, 2, 2])
Beispiel #7
0
#Clean data. Outliers removed.
#Testing data. m = 66749. Locations: Nottingham and Reading.

#Set up neural networks

from NeuralNetwork.NeuralNetwork import NeuralNetwork
import numpy as np
import scipy.special
import csv

i_n = 11  #no. of input nodes
h_n = 21  #no. of hidden nodes 11 ~ sqrt(i_n * o_n)
o_n = 1  #np. of output nodes
lr = 0.15  #learning rate

NN1 = NeuralNetwork(i_n, h_n, o_n, lr)  #setting up neural networks
NN2 = NeuralNetwork(i_n, h_n, o_n, lr)
NN3 = NeuralNetwork(i_n, h_n, o_n, lr)
NN4 = NeuralNetwork(i_n, h_n, o_n, lr)
NN5 = NeuralNetwork(i_n, h_n, o_n, lr)
NN6 = NeuralNetwork(i_n, h_n, o_n, lr)
NN7 = NeuralNetwork(i_n, h_n, o_n, lr)
NN8 = NeuralNetwork(i_n, h_n, o_n, lr)

NN1.load('a7', 'b7')
NN2.load('c7', 'd7')
NN3.load('e7', 'f7')
NN4.load('g7', 'h7')
NN5.load('i7', 'j7')
NN6.load('k7', 'l7')
NN7.load('m7', 'n7')
#Testing data. m = 66749. Locations: Nottingham and Reading.

#Set up neural networks

from NeuralNetwork.NeuralNetwork import NeuralNetwork
import numpy as np
import scipy.special
import csv

i_n = 14  #no. of input nodes
h_n = 21  #no. of hidden nodes 11 ~ sqrt(i_n * o_n)
o_n = 1  #np. of output nodes
lr = 0.15  #learning rate
epochs = 5  #no. of epochs

NN1 = NeuralNetwork(i_n, h_n, o_n, lr)  #setting up neural networks
NN2 = NeuralNetwork(i_n, h_n, o_n, lr)
NN3 = NeuralNetwork(i_n, h_n, o_n, lr)
NN4 = NeuralNetwork(i_n, h_n, o_n, lr)
NN5 = NeuralNetwork(i_n, h_n, o_n, lr)
NN6 = NeuralNetwork(i_n, h_n, o_n, lr)
NN7 = NeuralNetwork(i_n, h_n, o_n, lr)
NN8 = NeuralNetwork(i_n, h_n, o_n, lr)

train_data_file = open(
    'X:/Documents/Carbon Emissions Data/Data/TrainingData4Final.csv',
    'r')  #loading training data
train_data = train_data_file.readlines()
train_data_file.close()

Beispiel #9
0
import time

from NeuralNetwork.NeuralNetwork import NeuralNetwork

net = NeuralNetwork(2, 1, 1, 2)

inputs_outputs = [{
    "input": [1, 1],
    "output": [0]
}, {
    "input": [1, 0],
    "output": [1]
}, {
    "input": [0, 1],
    "output": [1]
}, {
    "input": [0, 0],
    "output": [0]
}]
for input_output in inputs_outputs:
    print(
        f"input = {input_output['input']}  , output = {net.GetOutputs(input_output['input'])}"
    )
print()

last_loss = 1

timer = time.time()
for _ in range(8000):
    loss = 0
    for input_output in inputs_outputs:
            if (self.outputs[ip] == [[1, 0, 0]]):
                print("Iris-sentosa")
            elif (self.outputs[ip] == [[0, 1, 0]]):
                print("Iris-versicolor")
            elif (self.outputs[ip] == [[0, 0, 1]]):
                print("Iris-virginica")

            print("\nPredicted Output : ")
            op = nn.feedforward(self.inputs[ip])
            if (op.index(max(op)) == 0):
                print("Iris-sentosa")
            elif (op.index(max(op)) == 1):
                print("Iris-versicolor")
            elif (op.index(max(op)) == 2):
                print("Iris-virginica")

            print("Continue ? (1 - Y 2 - N)")
            choice = int(input())

            if (choice == 2):
                break


iris = Iris()
iris.extract()
nn = NeuralNetwork(4, 10, 3)
# iris.visualize()
print("Training Model")
iris.train(0.1, 30000)
iris.test()
class Animal(object):
    DEBUG = False
    MAX_ENERGY = 30
    ENERGY_FOR_EXIST = 0.007
    MOVE_ENERGY_RATIO = 0.01

    # sensor_count_in_head / sensor_count
    SENSOR_COUNT_IN_HEAD_RATIO = 0.5
    # head angle
    HEAD_ANGLE = math.pi / 4.0
    HALF_HEAD_ANGLE = HEAD_ANGLE / 2.0

    READINESS_TO_BUD_THREADSHOULD = 30
    READINESS_TO_BUD_INCREASEMENT = 0.2
    ENERGY_FULLNES_TO_BUD = 0.7
    ENERGY_FOR_BUD = 5
    MIN_CHILD_COUNT = 1
    MAX_CHILD_COUNT = 3

    MUTATE_VALUE = 0.4
    HALF_MUTATE_VALUE = MUTATE_VALUE / 2
    MUTATE_CHANCE = 0.6

    def __init__(self, world):
        self.world = world
        self._x = randint(0, self.world.width)
        self._y = randint(0, self.world.height)
        self.size = 7
        self.angle = 0

        self.sensor_count = 7
        self._sensor_count_in_head = int(self.sensor_count * Animal.SENSOR_COUNT_IN_HEAD_RATIO)
        self._sensor_count_not_in_head = self.sensor_count - self._sensor_count_in_head
        self.sensor_values = []
        self._sensors_positions = []
        self._sensors_positions_calculated = False

        self.energy = self.ENERGY_FOR_BUD
        self.readiness_to_bud = 0

        self.brain = NeuralNetwork([self.sensor_count, 2, 2])
        # import BrainTrainer
        # self.brain = clone_brain(BrainTrainer.get_new_brain(self.sensor_count))

    @property
    def sensors_positions(self):
        # on 45 degrees (pi/4) of main angle located 75% of all sensors
        if not self._sensors_positions_calculated:
            self._sensors_positions = []

            # calc sensor positions in head


            delta_angle = Animal.HEAD_ANGLE / (self._sensor_count_in_head-1)
            angle = -Animal.HALF_HEAD_ANGLE + self.angle
            for _ in range(self._sensor_count_in_head):
                self._sensors_positions.append(
                    (math.cos(angle) * self.size + self._x, math.sin(angle) * self.size + self._y))
                angle += delta_angle

            # calc sensor positions in body
            delta_angle = (TWO_PI - Animal.HEAD_ANGLE) / (self._sensor_count_not_in_head+1)
            angle = Animal.HALF_HEAD_ANGLE + self.angle
            for _ in range(self._sensor_count_not_in_head):
                angle += delta_angle
                self._sensors_positions.append(
                    (math.cos(angle) * self.size + self._x, math.sin(angle) * self.size + self._y))

            self._sensors_positions_calculated = True
        return self._sensors_positions

    def update(self, sensor_values):
        self.sensor_values = sensor_values
        answer = self.brain.calculate(self.sensor_values)
        self.answer = answer

        self.energy -= Animal.ENERGY_FOR_EXIST

        if self.energy / Animal.MAX_ENERGY > Animal.ENERGY_FULLNES_TO_BUD:
            self.readiness_to_bud += Animal.READINESS_TO_BUD_INCREASEMENT
        if self.readiness_to_bud >= Animal.READINESS_TO_BUD_THREADSHOULD:
            self.readiness_to_bud = 0
            self.bud()

        self.move(answer[0], answer[1])

    def bud(self):
        child_count = randint(Animal.MIN_CHILD_COUNT, Animal.MAX_CHILD_COUNT)
        # if it tries to bud more child than it can - bud so many as it can and die.
        if child_count*Animal.ENERGY_FOR_BUD > self.energy:
            child_count = int(self.energy / Animal.ENERGY_FOR_BUD)
            self.energy = 0

        for _ in range(child_count):
            self.energy -= Animal.ENERGY_FOR_BUD
            child = Animal(self.world)
            child.x = self.x + randint(-30, 30)
            child.y = self.y + randint(-30, 30)
            child.brain = clone_brain(self.brain)
            self.world.add_animal(child)

    def eat(self, food):
        value = min(World.World.EATING_VALUE, max(0, Animal.MAX_ENERGY - self.energy))
        value = food.beating(value)
        self.energy += value

    def move(self, move, rotate):
        self.energy -= (abs(move) + abs(rotate))*Animal.MOVE_ENERGY_RATIO

        self._sensors_positions_calculated = False
        self.angle += rotate
        self._x += math.cos(self.angle) * move * 2.0
        self._y += math.sin(self.angle) * move * 2.0

    @property
    def x(self):
        return self._x

    @x.setter
    def x(self, value):
        self._x = value
        self._sensors_positions_calculated = False

    @property
    def y(self):
        return self._y

    @y.setter
    def y(self, value):
        self._y = value
        self._sensors_positions_calculated = False
Beispiel #12
0
theBoard = {
    '7': ' ',
    '8': ' ',
    '9': ' ',
    '4': ' ',
    '5': ' ',
    '6': ' ',
    '1': ' ',
    '2': ' ',
    '3': ' '
}

board_keys = []
neural_net1 = LoadFromFile("neural_net1.pkl")
if not neural_net1:
    neural_net1 = NeuralNetwork(27, 9, 4, 9)
neural_net2 = LoadFromFile("neural_net2.pkl")
if not neural_net2:
    neural_net2 = NeuralNetwork(27, 9, 4, 9)

for key in theBoard:
    board_keys.append(key)
''' We will have to print the updated board after every move in the game and 
    thus we will make a function in which we'll define the printBoard function
    so that we can easily print the board everytime by calling this function. '''


def printBoard(board):
    print(board['7'] + '|' + board['8'] + '|' + board['9'])
    print('-+-+-')
    print(board['4'] + '|' + board['5'] + '|' + board['6'])
Beispiel #13
0
from NeuralNetwork.NeuralNetwork import NeuralNetwork, LoadFromFile

nets = []
for _ in range(100):
    nets.append(NeuralNetwork(1, 1, 0, 0))

best_net = None
best_loss = 1

for index in range(len(nets)):
    nets[index] = nets[index].GetRandomizedBest([1], [1], 0.5, 100)
    loss = nets[index].GetLoss([1], [1])
    if loss < best_loss:
        best_net = nets[index]
        best_loss = loss
    if loss < 0.5:
        nets[index] = None

for _ in range(30000):
    best_net.CalculateSlopeValues([1], [1])
    best_net.MutateSlopeValues(1)

print(best_net.GetOutputs([1]))
print(best_net.GetOutputs([0]))
print(best_net)

# best_net.SaveToFile("Neural.pkl")
best_net = LoadFromFile("Neural.pkl")
print("after pickle")

print(best_net.GetOutputs([1]))
Beispiel #14
0
#Testing data. m = 66749. Locations: Nottingham and Reading.

#Set up neural networks

from NeuralNetwork.NeuralNetwork import NeuralNetwork
import numpy as np
import scipy.special
import csv

i_n = 14 #no. of input nodes
h_n = 21 #no. of hidden nodes 11 ~ sqrt(i_n * o_n)
o_n = 1 #np. of output nodes
lr = 0.15 #learning rate
epochs = 1 #no. of epochs 

NN1 = NeuralNetwork(i_n, h_n, o_n, lr) #setting up neural networks
NN2 = NeuralNetwork(i_n, h_n, o_n, lr)
NN3 = NeuralNetwork(i_n, h_n, o_n, lr)
NN4 = NeuralNetwork(i_n, h_n, o_n, lr)
NN5 = NeuralNetwork(i_n, h_n, o_n, lr)
NN6 = NeuralNetwork(i_n, h_n, o_n, lr)
NN7 = NeuralNetwork(i_n, h_n, o_n, lr)
NN8 = NeuralNetwork(i_n, h_n, o_n, lr)

NN1.load('a6', 'b6')
NN2.load('c6', 'd6')
NN3.load('e6', 'f6')
NN4.load('g6', 'h6')
NN5.load('i6', 'j6')
NN6.load('k6', 'l6')
NN7.load('m6', 'n6')
#Testing data. m = 66749. Locations: Nottingham and Reading.

#Set up neural networks

from NeuralNetwork.NeuralNetwork import NeuralNetwork
import numpy as np
import scipy.special
import csv

i_n = 11  #no. of input nodes
h_n = 21  #no. of hidden nodes 11 ~ sqrt(i_n * o_n)
o_n = 1  #np. of output nodes
lr = 0.15  #learning rate
epochs = 5  #no. of epochs

NN1 = NeuralNetwork(i_n, h_n, o_n, lr)  #setting up neural networks
NN2 = NeuralNetwork(i_n, h_n, o_n, lr)
NN3 = NeuralNetwork(i_n, h_n, o_n, lr)
NN4 = NeuralNetwork(i_n, h_n, o_n, lr)
NN5 = NeuralNetwork(i_n, h_n, o_n, lr)
NN6 = NeuralNetwork(i_n, h_n, o_n, lr)
NN7 = NeuralNetwork(i_n, h_n, o_n, lr)
NN8 = NeuralNetwork(i_n, h_n, o_n, lr)

train_data_file = open(
    'X:/Documents/Carbon Emissions Data/Data/TrainingDataBasicFinal.csv',
    'r')  #loading training data
train_data = train_data_file.readlines()
train_data_file.close()

Beispiel #16
0
from NeuralNetwork.NeuralNetwork import NeuralNetwork
from NeuralNetwork.Node.BasicNodeClasses import InputLinkedNode, NodeLink

net = NeuralNetwork(2, 1, 1, 2)

# hidden_bias = np.array([[0.2, 0.6]])
net.hidden_nodes[0].bias = 0.2
net.hidden_nodes[1].bias = 0.6

# hidden_weights = np.array([[0.8, 0.4], [0.1, 0.05]])
net.hidden_nodes[0].input_links[0].factor = 0.8
net.hidden_nodes[0].input_links[1].factor = 0.1
net.hidden_nodes[1].input_links[0].factor = 0.4
net.hidden_nodes[1].input_links[1].factor = 0.05

# output_bias = np.array([[0.7]])
net.output_nodes[0].bias = 0.7

# output_weights = np.array([[0.35], [0.21]])
net.output_nodes[0].input_links[0].factor = 0.35
net.output_nodes[0].input_links[1].factor = 0.21

inputs = [[0, 0], [0, 1], [1, 0], [1, 1]]
expected_output = [[0], [1], [1], [0]]

for index in range(4):
    output = net.GetOutputs(inputs[index])
    print(output)
    net.CalculateSlopeValues(inputs[index], expected_output[index])

for node in net.node_dictionary.values():