#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();
};
- 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.
#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;
} elseif (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++;
}
}