private void CreateInitialPopulation(SequentialNN model, int populationSize) { //create initial population Population = new List <EvolutionalEntity <Action> >(); for (int i = 0; i < populationSize; i++) { //copy population based on model SequentialNN newNN = new SequentialNN(); foreach (Layer layer in model.Layers) { Layer newLayer = new Layer(layer.Perceptrons.Count); newNN.AddLayer(newLayer); } newNN.AddLayer(new Layer(typeof(Action).GetEnumNames().Count())); NNEvolutionalEntity <Action> entity = new NNEvolutionalEntity <Action>(newNN); Population.Add(entity); States.Add(entity, DefaultState); } }
static void Main(string[] args) { const int TICKS_TRAIN = 120; const int TICKS_TEST = 120; const int INPUT_HOURS = 24; const int GENERATION_TO_STOP_TRAINING = 50; const int POPULATION_SIZE = 100; StockEnvironment stockEnvironment; //build model SequentialNN model = new SequentialNN(); model.AddLayer(new Layer(5)); model.AddLayer(new Layer(5)); StockNNES stockNNES = new StockNNES(model, POPULATION_SIZE); StockNNES stockNNESRandom = new StockNNES(model, POPULATION_SIZE); //train for (int i = 0; i < GENERATION_TO_STOP_TRAINING; i++) { Console.Clear(); Console.WriteLine("Training Generation: {0}", stockNNES.Generation); //create new environment stockEnvironment = new StockEnvironment("XVG", INPUT_HOURS, TICKS_TRAIN, TICKS_TEST + INPUT_HOURS - 1); stockNNES.Environment = stockEnvironment; for (int j = 0; j < TICKS_TRAIN; j++) { stockNNES.RunTick(); } if (i != GENERATION_TO_STOP_TRAINING - 1) { stockNNES.CreateNextGeneration(); } } //print performances Console.WriteLine(); Console.WriteLine("Performance of trained population"); Console.WriteLine("----------------"); PrintPerformance(stockNNES); EvolutionalEntity <Action> bestTrainingPerformer = stockNNES.Performances.OrderBy(p => p.Value).Last().Key; //reset performance stockNNES.ResetState(); //test trained Console.WriteLine("Testing Trained"); stockEnvironment = new StockEnvironment("BTC", INPUT_HOURS, TICKS_TEST); stockNNES.Environment = stockEnvironment; for (int i = 0; i < TICKS_TEST; i++) { stockNNES.RunTick(); } //test random Console.WriteLine("Testing Random"); stockEnvironment = new StockEnvironment("BTC", INPUT_HOURS, TICKS_TEST); stockNNESRandom.Environment = stockEnvironment; for (int i = 0; i < TICKS_TEST; i++) { stockNNESRandom.RunTick(); } //print performances of random population Console.WriteLine(); Console.WriteLine("Performance of random population"); Console.WriteLine("----------------"); PrintPerformance(stockNNESRandom); //print performances Console.WriteLine(); Console.WriteLine("Performance of trained population"); Console.WriteLine("----------------"); PrintPerformance(stockNNES, bestTrainingPerformer); }
public TrainReturn Train(TrainParameters trainParameters) { TrainReturn trainReturn = new TrainReturn(); List <NNStockStatistics> trainStatistics = new List <NNStockStatistics>(); List <NNStockStatistics> testStatistics = new List <NNStockStatistics>(); List <NNStockStatistics> trainAverage = new List <NNStockStatistics>(); List <NNStockStatistics> testAverage = new List <NNStockStatistics>(); SequentialNN model = new SequentialNN(); model.AddLayer(new Layer(5)); model.AddLayer(new Layer(5)); StockNNES stockNNES = new StockNNES(model, 100); for (int i = 0; i < trainParameters.generations; i++) { //train stockNNES.Environment = new StockEnvironment(trainParameters.stockName, trainParameters.inputSize, trainParameters.ticksTrain, trainParameters.ticksTest + trainParameters.inputSize - 1); for (int j = 0; j < trainParameters.ticksTrain; j++) { stockNNES.RunTick(); } KeyValuePair <EvolutionalEntity <Action>, float> bestPerformerTrain = stockNNES.Performances.OrderBy(p => p.Value).Last(); trainStatistics.Add(new NNStockStatistics { EvolutionalEntity = bestPerformerTrain.Key, Performance = bestPerformerTrain.Value, Generation = i + 1 }); trainAverage.Add(new NNStockStatistics { EvolutionalEntity = null, Performance = GetAverage(stockNNES), Generation = i + 1 }); Dictionary <EvolutionalEntity <Action>, float> performance = new Dictionary <EvolutionalEntity <Action>, float>(stockNNES.Performances); //test stockNNES.Environment = new StockEnvironment(trainParameters.stockName, trainParameters.inputSize, trainParameters.ticksTest); for (int j = 0; j < trainParameters.ticksTest; j++) { stockNNES.RunTick(); } KeyValuePair <EvolutionalEntity <Action>, float> bestPerformerTest = stockNNES.Performances.OrderBy(p => p.Value).Last(); testStatistics.Add(new NNStockStatistics { EvolutionalEntity = bestPerformerTest.Key, Performance = bestPerformerTest.Value, Generation = i + 1 }); testAverage.Add(new NNStockStatistics { EvolutionalEntity = null, Performance = GetAverage(stockNNES), Generation = i + 1 }); GetAverage(stockNNES); //reset performance stockNNES.Performances = performance; if (i != trainParameters.generations - 1) { stockNNES.CreateNextGeneration(); } } trainReturn.trainStatistics = trainStatistics; trainReturn.testStatistics = testStatistics; trainReturn.trainAverage = trainAverage; trainReturn.testAverage = testAverage; return(trainReturn); }