public override HashSet <A> PerformActions(Dictionary <string, float> inputs) { Dictionary <string, float> outputs = SequentialNN.Predict(inputs); KeyValuePair <string, float>?maxOuput = null; foreach (KeyValuePair <string, float> ouput in outputs) { if (maxOuput == null) { maxOuput = ouput; } else if (ouput.Value > maxOuput.Value.Value) { maxOuput = ouput; } } A action = (A)Enum.Parse(typeof(A), maxOuput.Value.Key); HashSet <A> actions = new HashSet <A>(); actions.Add(action); return(actions); }
public override EvolutionalEntity <A> CreateOffspring() { SequentialNN childSequentialNN = new SequentialNN(SequentialNN); //mutate child foreach (Layer layer in childSequentialNN.Layers) { foreach (Perceptron perceptron in layer.Perceptrons) { float randomModifier; foreach (KeyValuePair <string, float> weight in perceptron.Weights.ToList()) { randomModifier = (float)(random.NextDouble() * 2 - 1) * LearningTolerance; perceptron.Weights[weight.Key] = weight.Value + randomModifier; } randomModifier = (float)(random.NextDouble() * 2 - 1) * LearningTolerance; perceptron.BiasWeight += randomModifier; } } //float learningTolerance = LearningTolerance - 0.01f; //if (learningTolerance < 0) //{ // learningTolerance = 0; //} //return new NNEvolutionalEntity<A>(childSequentialNN) { LearningTolerance = learningTolerance }; return(new NNEvolutionalEntity <A>(childSequentialNN)); }
public StockNNES(SequentialNN model, int populationSize) { State defaultState = new State(); defaultState.Cash = 1000; defaultState.Stocks = 0; DefaultState = defaultState; CreateInitialPopulation(model, populationSize); }
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 NNEvolutionalEntity(SequentialNN sequentialNN) { SequentialNN = sequentialNN; LearningTolerance = 1; }
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); }