示例#1
0
 def test_output_is_zero_when_both_inputs_are_zero(self):
     genome = decode('CryogenicStorage/xor_genome.json')
     brain = NeuralNetwork.expressed_from(genome)
     brain.stimulate([("S1", 0),
                      ("S2", 0)])
     brain.electrify()
     activation = brain.outputs()["O1"].activation()
     self.assertLess(activation, 0.001)
     print "0, 0: %.8f" % activation
示例#2
0
 def test_output_is_one_when_s2_is_one(self):
     genome = decode('CryogenicStorage/xor_genome.json')
     brain = NeuralNetwork.expressed_from(genome)
     brain.stimulate([("S1", 1),
                      ("S2", 0)])
     brain.electrify()
     activation = brain.outputs()["O1"].activation()
     self.assertGreater(activation, 0.001)
     print "1, 0: %.8f" % activation
    generation = 1
    last_improvement = 0

    archive = "Experiments/Archive/xor/%s" % datetime.datetime.now().strftime(
        "%Y%m%d %H:%M:%S")
    os.makedirs(archive)
    archive_population(population, generation)

    timeline_path = "%s/timeline.txt" % archive
    timeline = open(timeline_path, "w")

    while best_fitness < target_fitness:

        for genome in population.organisms:
            genome["champ"] = None
            brain = NeuralNetwork.expressed_from(genome)

            inputs = [[("S1", 0), ("S2", 0)], [("S1", 0), ("S2", 1)],
                      [("S1", 1), ("S2", 0)], [("S1", 1), ("S2", 1)]]

            desired_outputs = [0, 1, 1, 0]
            actual_outputs = []

            try:
                for sensory_input in inputs:
                    brain.stimulate(sensory_input)
                    brain.electrify()
                    actual_outputs.append(brain.outputs()["O1"].activation())
                error = actual_outputs[0] + (1 - actual_outputs[1]) + (
                    1 - actual_outputs[2]) + actual_outputs[3]
                genome["fitness"] = math.pow(4 - error, 2)
    best_fitness = 0
    generation = 1
    last_improvement = 0

    archive = "Experiments/Archive/xor/%s" % datetime.datetime.now().strftime("%Y%m%d %H:%M:%S")
    os.makedirs(archive)
    archive_population(population, generation)

    timeline_path = "%s/timeline.txt" % archive
    timeline = open(timeline_path, "w")

    while best_fitness < target_fitness:

        for genome in population.organisms:
            genome["champ"] = None
            brain = NeuralNetwork.expressed_from(genome)

            inputs = [[("S1", 0), ("S2", 0)], [("S1", 0), ("S2", 1)], [("S1", 1), ("S2", 0)], [("S1", 1), ("S2", 1)]]

            desired_outputs = [0, 1, 1, 0]
            actual_outputs = []

            try:
                for sensory_input in inputs:
                    brain.stimulate(sensory_input)
                    brain.electrify()
                    actual_outputs.append(brain.outputs()["O1"].activation())
                error = actual_outputs[0] + (1 - actual_outputs[1]) + (1 - actual_outputs[2]) + actual_outputs[3]
                genome["fitness"] = math.pow(4 - error, 2)

            except UnstableNetworkError:
示例#5
0
 def test_extract_genome(self):
     expected_genome = self.read_genome('Experiments/CryogenicStorage/xor_genome.json')
     brain = NeuralNetwork.expressed_from(expected_genome)
     genome = brain.decode_genome()
     self.maxDiff = None
     self.assertEqual(genome, expected_genome)
        tomorrow = ftse100[min(index + 1, len(ftse100) - 1)]

        if tomorrow > today:
            if money > 0:
                shares = money / ftse100[index]
                money = 0
        else:
            if shares > 0:
                money = shares * ftse100[index]
                shares = 0

    print "Buy and hold: %s" % ((1000 / ftse100[0]) * ftse100[len(ftse100) - 1])

    print "Best possible result: %s" % money if money > 0 else shares * ftse100[len(ftse100) - 1]

    brain = NeuralNetwork.expressed_from(fund_manager)

    money = 1000
    shares = 0

    print "date,ftse100,prediction,balance,cash,shares"

    for index, quote in enumerate(ftse100):
        brain.stimulate([("S1", ftse100_t1[index]),
                         ("S2", ftse100_t2[index]),
                         ("S3", ftse100_t3[index]),
                         ("S4", volume_t1[index])
                         ])
        brain.electrify()
        prediction = brain.outputs()["O1"].activation()