Example #1
0
	def testSinglePreviousEvaluate(self):
		previousNeuron = InputNeuron()
		previousNeuron.setValue(1)
		previousRow = [previousNeuron]
		
		neuron = Neuron(previousRow)
		self.assertGreater(neuron.evaluate(), 1/2)
Example #2
0
	def testEvaluate(self):
		neuron = InputNeuron()
		neuronValue = random.randint(0,10)
		neuron.setValue(neuronValue)
		
		coefficient = random.randint(0,10)
		weightedNeuron = WeightedNeuron(neuron, coefficient)

		expectedValue = neuronValue * coefficient
		self.assertEqual(expectedValue, weightedNeuron.evaluate())
Example #3
0
 def __init__(self,
              nbNeurons=5,
              nbConnexions=5,
              nbInputsNeurons=2,
              nbOutputsNeurons=1,
              genes=None):
     self.fitness = 0.0
     if (genes != None):
         neuronGenes = genes[0]
         connexionGenes = genes[1]
         # print(neuronGenes)
         # print(connexionGenes)
         self.nbNeurons = 0
         self.neuronList = []
         self.inputNeuronList = []
         self.outputNeuronList = []
         self.connexionList = []
         for neuronGene in neuronGenes:
             neuron = None
             if neuronGene[1] == 'n':
                 neuron = Neuron(self.nbNeurons, bias=neuronGene[0])
             elif neuronGene[1] == 'i':
                 neuron = InputNeuron(self.nbNeurons, bias=neuronGene[0])
                 self.inputNeuronList.append(neuron)
             elif neuronGene[1] == 'o':
                 neuron = OutputNeuron(self.nbNeurons, bias=neuronGene[0])
                 self.outputNeuronList.append(neuron)
             self.neuronList.append(neuron)
             self.nbNeurons += 1
         for connGene in connexionGenes:
             fromNeuron = self.neuronList[connGene[0]]
             toNeuron = self.neuronList[connGene[1]]
             self.connexionList.append(
                 Connexion(fromNeuron, toNeuron, connGene[2]))
         self.nbConnexions = len(self.connexionList)
         self.species = str(self.nbNeurons) + "_" + str(self.nbConnexions)
     else:
         self.nbNeurons = 0
         self.neuronList = []
         self.inputNeuronList = []
         self.outputNeuronList = []
         self.nbConnexions = 0
         self.connexionList = []
         self.species = str(self.nbNeurons) + "_" + str(self.nbConnexions)
         for idInputNeuron in range(nbInputsNeurons):
             self.createNeuron("input")
         for idNeuron in range(nbNeurons - nbInputsNeurons -
                               nbOutputsNeurons):
             self.createNeuron()
         for idOutputNeuron in range(nbOutputsNeurons):
             self.createNeuron("output")
         for idConnexion in range(nbConnexions):
             self.createConnexion()
         self.clean()
Example #4
0
 def createNeuron(self, type="norm"):
     if type == "input":
         neuron = InputNeuron(self.nbNeurons)
         self.neuronList.append(neuron)
         self.inputNeuronList.append(neuron)
     elif type == "output":
         neuron = OutputNeuron(self.nbNeurons)
         self.neuronList.append(neuron)
         self.outputNeuronList.append(neuron)
     else:
         neuron = Neuron(self.nbNeurons)
         self.neuronList.append(neuron)
     self.nbNeurons += 1
Example #5
0
    def __init__(self, weightList, E, A, isBiasNeuronEnabled):
        self.E = Decimal(E)  #learning rate
        self.A = Decimal(A)  #momentum
        self.inputs = []
        self.outputs = []
        self.idealOutput = []
        self.isBiasNeuronEnabled = isBiasNeuronEnabled

        # Creating input layer neurons
        currentLayer = []
        previousLayer = []
        for i in range(len(weightList[0][0]) - int(isBiasNeuronEnabled)):
            self.inputs.append(InputNeuron())
        if isBiasNeuronEnabled:
            self.inputs.append(BiasNeuron())
        previousLayer = self.inputs

        # Creating hidden layer neurons
        for i in range(len(weightList) -
                       1):  # -1 because start from second neuron layer
            currentLayer = []
            for j in range(len(
                    weightList[i])):  # -1 because last check on bias neuron
                neuron = HiddenNeuron()
                currentLayer.append(neuron)
                for k in range(len(weightList[i][j])):
                    self.__connect(Sinaps(Decimal(weightList[i][j][k])),
                                   previousLayer[k], neuron)
            if isBiasNeuronEnabled:
                currentLayer.append(BiasNeuron())
            previousLayer = currentLayer

        # Creating output layer neurons
        i = len(weightList) - 1
        self.outputs = []
        for j in range(len(weightList[i])):
            neuron = OutputNeuron()
            self.outputs.append(neuron)
            for k in range(len(weightList[i][j])):
                self.__connect(Sinaps(Decimal(weightList[i][j][k])),
                               previousLayer[k], neuron)
Example #6
0
    def _start_net(self):
        #INICIA A REDE NEURAL COM OS PARAMETROS
        #MONTANDo A REDE NO SENTINDO CONTRARIO

        input = InputNeuron(self.NUMBER_INPUT_NEURONS)
        self.input_layer = input

        hidden = HiddenNeuron(self.NUMBER_HIDDEN_NEURONS, input)
        self.hiddens_layers = []
        self.hiddens_layers.append(hidden)

        if (self.NUMBER_OF_LAYERS > 1):
            for i in range(0, self.NUMBER_OF_LAYERS - 1):
                hidden = HiddenNeuron(self.NUMBER_HIDDEN_NEURONS,
                                      self.hiddens_layers[-1])
                self.hiddens_layers.append(hidden)

        output = OutputNeuron(self.NUMBER_OUTPUT_NEURONS,
                              self.hiddens_layers[-1])
        self.output_layer = output

        print("Created Input Layer:\t", self.NUMBER_INPUT_NEURONS)
        print("Created Hidden Layer:\t", self.NUMBER_HIDDEN_NEURONS)
        print("Created Output Layer:\t", self.NUMBER_OUTPUT_NEURONS)
Example #7
0
	def testInputNeuronValue(self):
		neuron = InputNeuron()
		expectedValue = random.randint(0,10)
		neuron.setValue(expectedValue)
		self.assertEqual(expectedValue, neuron.evaluate())
Example #8
0
	def testInputNeuronConstructor(self):
		neuron = InputNeuron()
		expectedValue = 0
		self.assertEqual(expectedValue, neuron.evaluate())