Evolution sim program (finished)

Lemme know what you think and give me some tips on how to optimize it. Fiidback is necessary.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
 #include "pch.h"
#include "AILogic.h"
#include <iostream>
#include <chrono>
#include <random>
//NeuronMain is where all functions meet up to build the program
void NeuralNet::NeuronMain()
{  
	int l = 0;
	int n = 0;

	std::cout << "How many generations do you want to run for: ";
	std::cin >> n;
	std::cout << "\n\n";

	for (int i = 0; i <= n; ++i)
	{
		std::cout << "you are on generation: " << GenCount << "\n";
		std::cout << "you are on Species: " << Species << "\n";
		NeuronPath();
		std::cout << "\nPlay next generation ";
		std::cin >> l;
		std::cout << "\n\n";
		GenCount++;
	}
}
int main()
{
	NeuralNet UseDatSquishyBrain;
	UseDatSquishyBrain.NeuronMain();


}
//NeuronPath Basically controls the evolution of each Neuron
//it also checks to find which Neuron has the largest value
//then takes that value and uses it as the base level for the next generation
void NeuralNet::NeuronPath()
{
	unsigned RandEvolution = std::chrono::system_clock::now().time_since_epoch().count();
	std::mt19937 generator(RandEvolution);
	std::uniform_int_distribution<int> distribution(1, 20);
	distribution(generator);

	Neuron1 = Neuron1 + distribution(generator);
	Neuron2 = Neuron2 + distribution(generator);
	Neuron3 = Neuron3 + distribution(generator);
	Neuron4 = Neuron4 + distribution(generator);
	Neuron5 = Neuron5 + distribution(generator);

	
	if (Neuron1 > Neuron2 && Neuron1 > Neuron3 && Neuron1 > Neuron4 && Neuron1 > Neuron5)
	{
		Neuron1 = Neuron1;
		Neuron2 = Neuron1;
		Neuron3 = Neuron1;
		Neuron4 = Neuron1;
		Neuron5 = Neuron1;

		std::cout << "Generation: " << GenCount << " n1-" << Neuron1;

	}
	else if (Neuron2 > Neuron1 && Neuron2 > Neuron3 && Neuron2 > Neuron4 && Neuron2 > Neuron5)
	{
		Neuron1 = Neuron2;
		Neuron2 = Neuron2;
		Neuron3 = Neuron2;
		Neuron4 = Neuron2;
		Neuron5 = Neuron2;

		std::cout << "Generation: " << GenCount << " n2-" << Neuron1;

	}
	else if (Neuron3 > Neuron2 && Neuron3 > Neuron1 && Neuron3 > Neuron4 && Neuron3 > Neuron5)
	{
		Neuron1 = Neuron3;
		Neuron2 = Neuron3;
		Neuron3 = Neuron3;
		Neuron4 = Neuron3;
		Neuron5 = Neuron3;

		std::cout << "Generation: " << GenCount << " n3-" << Neuron1;

	}
	else if (Neuron4 > Neuron2 && Neuron4 > Neuron3 && Neuron4 > Neuron1 && Neuron4 > Neuron5)
	{
		Neuron1 =  Neuron4;
		Neuron2 =  Neuron4;
		Neuron3 =  Neuron4;
		Neuron4 =  Neuron4;
		Neuron5 =  Neuron4;

		std::cout << "Generation: " << GenCount << " n4-" << Neuron1;

	}
	else if (Neuron5 > Neuron2 && Neuron5 > Neuron3 && Neuron5 > Neuron4 && Neuron5 > Neuron1)
	{
	  Neuron1 = Neuron5;
	  Neuron2 = Neuron5;
	  Neuron3 = Neuron5;
	  Neuron4 = Neuron5;
	  Neuron5 = Neuron5;

		std::cout << "Generation: " << GenCount << " n5-" << Neuron1;
	}
	else
	{
		std::cout << "\n\nThis Species has Gone extinct\n\n";
		    GenCount = 0;
		    Neuron1 = 0,
			Neuron2 = 0,
			Neuron3 = 0,
			Neuron4 = 0,
			Neuron5 = 0;
			Species++;
	}
}


HEADER FILE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#pragma once
#include <chrono>
#include <random>

//Global_obj_ptr_var holds global info shared between all classes
class Global_obj_ptr_var 
{
public:
//Neurons represent individuals. Their values can me thought of as levels. they all start at level 1
	double
		Neuron1 = 0,
		Neuron2 = 0,
		Neuron3 = 0,
		Neuron4 = 0,
		Neuron5 = 0;

	int GenCount = 0;
	int Species = 1;
};
class NeuralNet : public Global_obj_ptr_var
{
public:
void NeuronMain();
void NeuronPath();

};
Last edited on
- Any time you see yourself creating variables named var1, var2, var3, etc., you probably need an array or vector.
- Don't reseed the random number generator each time. If you call the function quickly, you'll get the same seed. You can avoid this by making it static.
- I've used range-based for loops to go over all items in the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <chrono>
#include <random>
#include <array>

//Global_obj_ptr_var holds global info shared between all classes
class Global_obj_ptr_var
{
public:
//Neurons represent individuals. Their values can me thought of as
//levels. they all start at level 1
    std::array < double, 5 > Neurons;
    int GenCount = 0;
    int Species = 1;
};
class NeuralNet:public Global_obj_ptr_var
{
public:
    void NeuronMain();
    void NeuronPath();

};
#include <iostream>
#include <chrono>
#include <random>
//NeuronMain is where all functions meet up to build the program
void
NeuralNet::NeuronMain()
{
    int l = 0;
    int n = 0;

    std::cout << "How many generations do you want to run for: ";
    std::cin >> n;
    std::cout << "\n\n";

    for (int i = 0; i <= n; ++i) {
	std::cout << "you are on generation: " << GenCount << "\n";
	std::cout << "you are on Species: " << Species << "\n";
	NeuronPath();
	std::cout << "\nPlay next generation ";
	std::cin >> l;
	std::cout << "\n\n";
	GenCount++;
    }
}

int
main()
{
    NeuralNet UseDatSquishyBrain;
    UseDatSquishyBrain.NeuronMain();

}

//NeuronPath Basically controls the evolution of each Neuron
//it also checks to find which Neuron has the largest value
//then takes that value and uses it as the base level for the next generation
void
NeuralNet::NeuronPath()
{
    unsigned RandEvolution = std::chrono::system_clock::now().time_since_epoch().count();
    static std::mt19937 generator(RandEvolution);
    std::uniform_int_distribution < int >distribution(1, 20);
    distribution(generator);

    int maxIdx = 0;
    int maxCount = 1;		// number of times max occurs
    Neurons[0] += distribution(generator);
    for (unsigned i = 1; i < Neurons.size(); ++i) {
	Neurons[i] += distribution(generator);
	if (Neurons[i] > Neurons[maxIdx]) {
	    maxIdx = i;
	    maxCount = 1;
	} else if (Neurons[i] == Neurons[maxIdx]) {
	    ++maxCount;
	}
    }

    if (maxCount == 1) {
      for (double &Neuron:Neurons) {
	    Neuron = Neurons[maxIdx];
	}
	std::cout << "Generation: " << GenCount
	    << " n" << maxIdx + 1 << "-" << Neurons[maxIdx];
    } else {
	std::cout << "\n\nThis Species has Gone extinct\n\n";
	GenCount = 0;
      for (double &Neuron:Neurons) {
	    Neuron = 0;
	}
	Species++;
    }
}

Topic archived. No new replies allowed.