/// <summary> /// Starts optimization based on the array of items and simulation setting that were passed in during instantiation /// </summary> /// <returns>The final (predicted) output of the optimization</returns> public PlanogramOptResults StartOptimization() { UpdateStatus?.Invoke("Training..."); var retVal = new PlanogramOptResults(); // checks what type of simulation we are running int sessions = simSettings.SimType == SimulationType.Sessions ? simSettings.Sessions.Value : DEFAULT_SESSIONS_PER_BATCH; double hours = simSettings.SimType == SimulationType.Time ? simSettings.Hours.Value : 0; simSettings.StartedOn = DateTime.Now; simSettings.EndsOn = DateTime.Now.AddHours(hours); // simulation scenarios: // if we are doing SimulationType.Sessions then the do-while loop will fail, just does the codeblock once, and trains the network with the specific number of sessions // if we are doing SimulationType.Time then as default it will train for 100 sessions at a time (per loop) and continues to do so until time runs out // if we are doing SimulationType.Score then it will execute just like the Time option except that it will continue to do so until the specified Score is met or surpassed do { // settings network.NumSessions = sessions; // you can change this and start experimenting which range is best // NOTE if you decide to have multiple facings (by changing the NumFacings output Max to greater than 1) and enforce item uniqueness then the EndRandomness must not be 0 (i suggest 5 as the minimum) // as the planogram needs a bit of randomness towards the end since we have a condition where we enforce uniqueness of items. In the event that the RLM outputs an item that // is already in the planogram then having enough randomness left will allow it to still have a chance to suggest a different item otherwise the RLM will suggest the same item over and over again and cause an infinite loop network.StartRandomness = START_RANDOMNESS; network.EndRandomness = END_RANDOMNESS; //(simSettings.SimType == SimulationType.Sessions) ? 0 : network.StartRandomness; //network.MaxLinearBracket = MAX_LINEAR; //network.MinLinearBracket = MIN_LINEAR; // since we might be retraining the network (i.e, SimulationType.Time or Score), we need to call this method to reset the randomization counter of the RLM network.ResetRandomizationCounter(); // training, train 90% per session batch if not Session Type var trainingTimes = (simSettings.SimType == SimulationType.Sessions) ? sessions : sessions - PREDICT_SESSIONS; retVal = Optimize(trainingTimes, true, simSettings.EnableSimDisplay); // for non Sessions type, we predict {PREDICT_SESSIONS}-times per session batch if (simSettings.SimType != SimulationType.Sessions) { int predictTimes = PREDICT_SESSIONS; if (simSettings.SimType == SimulationType.Score && predictTimes < RPOCSimpleSimSettings.NUM_SCORE_HITS) { predictTimes = RPOCSimpleSimSettings.NUM_SCORE_HITS; } retVal = Optimize(predictTimes, false, simSettings.EnableSimDisplay); } } while ((simSettings.SimType == SimulationType.Time && simSettings.EndsOn > DateTime.Now) || (simSettings.SimType == SimulationType.Score && RPOCSimpleSimSettings.NUM_SCORE_HITS > numScoreHits)); // we do a final prediction and to ensure we update the plangoram display for the final output retVal = Optimize(1, false, true); network.TrainingDone(); IsTrainingDone = true; UpdateStatus?.Invoke("Processing data...", true); return(retVal); }
public void TrainingDone(TimeSpan time, int sessions) { network.TrainingDone(); Console.WriteLine("End of Simulation"); SimulationDone = true; }
public void TrainingDone() { rlmNet.TrainingDone(); }
public void LogisticTrain() { Console.WriteLine("\nRLM network settings:"); int sessions = Util.GetInput("\nEnter Number of Session [default 100]: ", 100); //Gets user input for the number of tries the game will play int startRand = Util.GetInput("Enter Start Randomness [default 100]: ", 100); //Gets user input for start randomness int endRand = Util.GetInput("Enter End Randomness [default 0]: ", 0); //Gets user input for end randomness var dbName = $"RLM_logistic_" + Guid.NewGuid().ToString("N"); var networkName = "Logicstics Network"; LogisticSimulator simulator = null; IEnumerable <int> customerOrders = LogisticInitialValues.CustomerOrders; try { //IRlmDbData rlmDbData = new RlmDbDataPostgreSqlServer(dbName); IRlmDbData rlmDbData = new RlmDbDataSQLServer(dbName); RlmNetwork network = new RlmNetwork(rlmDbData); //Make an instance of rlm_network passing the database name as parameter network.DataPersistenceComplete += Network_DataPersistenceComplete; network.DataPersistenceProgress += Network_DataPersistenceProgress; if (!network.LoadNetwork(networkName)) { var inputs = new List <RlmIO>() { new RlmIO("X", typeof(Int32).ToString(), 1, 1, RlmInputType.Distinct), }; double minFrom = LogisticInitialValues.PlayerMinRange[0]; double minTo = LogisticInitialValues.PlayerMinRange[1]; double maxFrom = LogisticInitialValues.PlayerMaxRange[0]; double maxTo = LogisticInitialValues.PlayerMaxRange[1]; var outputs = new List <RlmIO>() { new RlmIO("Retailer_Min", typeof(Int16).ToString(), minFrom, minTo), new RlmIO("Retailer_Max", typeof(Int16).ToString(), maxFrom, maxTo), new RlmIO("WholeSaler_Min", typeof(Int16).ToString(), minFrom, minTo), new RlmIO("WholeSaler_Max", typeof(Int16).ToString(), maxFrom, maxTo), new RlmIO("Distributor_Min", typeof(Int16).ToString(), minFrom, minTo), new RlmIO("Distributor_Max", typeof(Int16).ToString(), maxFrom, maxTo), new RlmIO("Factory_Min", typeof(Int16).ToString(), minFrom, minTo), new RlmIO("Factory_Max", typeof(Int16).ToString(), maxFrom, maxTo), new RlmIO("Factory_Units_Per_Day", typeof(Int16).ToString(), LogisticInitialValues.FactoryRange[0], LogisticInitialValues.FactoryRange[1]), }; network.NewNetwork(networkName, inputs, outputs); } // execute it on another thread as not to block the RLM training Console.WriteLine("\nPress 'd' to show Data persistence progress\n"); Task.Run(() => { while (!Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.D) { showDataPersistProgress = true; } }); network.NumSessions = sessions; // num of sessioins default 100 network.StartRandomness = startRand; network.EndRandomness = endRand; simulator = new LogisticSimulator(LogisticInitialValues.StorageCost, LogisticInitialValues.BacklogCost, LogisticInitialValues.InitialInventory, LogisticInitialValues.InitialInventory, LogisticInitialValues.InitialInventory, LogisticInitialValues.InitialInventory); Stopwatch watch = new Stopwatch(); watch.Start(); Console.WriteLine("\n\nTraining:\n"); IEnumerable <LogisticSimulatorOutput> predictedLogisticOutputs = null; network.ResetRandomizationCounter(); for (int i = 0; i < sessions; i++) { var sessId = network.SessionStart(); var inputs = new List <RlmIOWithValue>(); inputs.Add(new RlmIOWithValue(network.Inputs.First(), "1")); var cycle = new RlmCycle(); var outputs = cycle.RunCycle(network, sessId, inputs, true); var simOutputs = outputs.CycleOutput.Outputs .Select(a => new LogisticSimulatorOutput() { Name = a.Name, Value = Convert.ToInt32(a.Value) }) .ToList(); simulator.ResetSimulationOutput(); simulator.Start(simOutputs, 50, customerOrders); network.ScoreCycle(outputs.CycleOutput.CycleID, 0); var totalCosts = simulator.SumAllCosts(); network.SessionEnd(totalCosts); Console.WriteLine($"Session #{i + 1} \t Score: {Math.Abs(totalCosts).ToString("$#,##0"),10}"); if (i == sessions - 1) { predictedLogisticOutputs = simOutputs; } } watch.Stop(); Console.WriteLine("\nPredicted outputs:"); string resultText = ""; foreach (var item in predictedLogisticOutputs) { resultText += "\n" + item.Name + ": " + item.Value; } Console.WriteLine(resultText); Console.WriteLine($"\nElapsed: {watch.Elapsed}"); network.TrainingDone(); } catch (Exception e) { if (e.InnerException != null && e.InnerException is RlmDefaultConnectionStringException) { Console.WriteLine($"Error: {e.InnerException.Message}"); } else { Console.WriteLine($"ERROR: {e.Message}"); } } Console.ReadLine(); }
public void TrainingDone() { network.TrainingDone(); }
static void Main(string[] args) { Console.WriteLine("XOR"); Console.WriteLine("\nRLM settings"); // user inputs for the RLM settings int sessions = Util.GetInput("Number of sessions [default 50]: ", 50); int startRandomness = Util.GetInput("Start randomness [default 50]: ", 50); int endRandomness = Util.GetInput("End randomness [default 0]: ", 0); // use Sql Server as Rlm Db IRlmDbData rlmDBData = new RlmDbDataSQLServer($"RLM_XOR_SAMPLE_{Guid.NewGuid().ToString("N")}"); //IRlmDbData rlmDBData = new RlmDbDataPostgreSqlServer($"RLM_XOR_SAMPLE_{Guid.NewGuid().ToString("N")}"); // the appended Guid is just to have a unique RLM network every time we run this example. // you can remove this or simply change the name to something static to use the same network all the time var rlmNet = new RlmNetwork(rlmDBData); // subscribe to events to know the status of the Data Persistence that works in the background rlmNet.DataPersistenceComplete += RlmNet_DataPersistenceComplete; rlmNet.DataPersistenceProgress += RlmNet_DataPersistenceProgress; // checks to see if the network already exists and loads it to memory if (!rlmNet.LoadNetwork("XOR_SAMPLE")) { // declare our inputs var ins = new List <RlmIO>(); ins.Add(new RlmIO("XORInput1", typeof(bool).ToString(), 0, 1, RlmInputType.Distinct)); ins.Add(new RlmIO("XORInput2", typeof(bool).ToString(), 0, 1, RlmInputType.Distinct)); // declare our outputs var outs = new List <RlmIO>(); outs.Add(new RlmIO("XOROutput", typeof(bool).ToString(), 0, 1)); // creates a new network rlmNet.NewNetwork("XOR_SAMPLE", ins, outs); } // execute it on another thread as not to block the RLM training Console.WriteLine("\nPress 'd' to show Data persistence progress\n"); Task.Run(() => { while (!Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.D) { showDataPersistProgress = true; } }); // set rlm training settings rlmNet.NumSessions = sessions; rlmNet.StartRandomness = startRandomness; rlmNet.EndRandomness = endRandomness; Console.WriteLine("Training Session started"); double sumOfCycleScores = 0; // this is just good practice, usually you need to call this when you want to do multiple trainings on the same network over and over again. // it resets the randomization of the RLM back to the beginning (start randomness) after each training set // i.e, training for 50 sessions, then training for another 50 sessions, and so on and so forth rlmNet.ResetRandomizationCounter(); for (int i = 0; i < sessions; i++) { // start session long sessionId = rlmNet.SessionStart(); sumOfCycleScores = 0; foreach (var xor in xorTable) { //Populate input values var invs = new List <RlmIOWithValue>(); // get value for Input1 and associate it with the Input instance string input1Value = xor.Input1; invs.Add(new RlmIOWithValue(rlmNet.Inputs.Where(item => item.Name == "XORInput1").First(), input1Value)); // get value for Input2 and associate it with the Input instance string input2Value = xor.Input2; invs.Add(new RlmIOWithValue(rlmNet.Inputs.Where(item => item.Name == "XORInput2").First(), input2Value)); //Build and run a new RlmCycle var Cycle = new RlmCycle(); RlmCyclecompleteArgs result = Cycle.RunCycle(rlmNet, sessionId, invs, true); // scores the RLM on how well it did for this cycle // each cycle with a correct output is rewarded a 100 score, 0 otherwise double score = ScoreCycle(result, xor); sumOfCycleScores += score; // sets the score rlmNet.ScoreCycle(result.CycleOutput.CycleID, score); } Console.WriteLine($"Session #{i} - score: {sumOfCycleScores}"); // end the session with the sum of the cycle scores // with the way the scoring is set, a perfect score would be 400 meaning all cycles got it right rlmNet.SessionEnd(sumOfCycleScores); } Console.WriteLine("Training Session ended"); Console.WriteLine(); Console.WriteLine("Predict:"); // PREDICT... see how well the RLM learned // NOTE that the only difference with Predict from Training is that we passed 'false' to the learn argument on the Cycle.RunCycle() method // and of course, the inputs which we used our xor table to check if the RLM has learned as expected long SessionID = rlmNet.SessionStart(); sumOfCycleScores = 0; foreach (var xor in xorTable) { var invs = new List <RlmIOWithValue>(); invs.Add(new RlmIOWithValue(rlmNet.Inputs.First(a => a.Name == "XORInput1"), xor.Input1)); invs.Add(new RlmIOWithValue(rlmNet.Inputs.First(a => a.Name == "XORInput2"), xor.Input2)); RlmCycle Cycle = new RlmCycle(); RlmCyclecompleteArgs result = Cycle.RunCycle(rlmNet, SessionID, invs, false); double score = ScoreCycle(result, xor, true); sumOfCycleScores += score; // sets the score rlmNet.ScoreCycle(result.CycleOutput.CycleID, score); } rlmNet.SessionEnd(sumOfCycleScores); // must call this to let the Data persistence know we are done training/predicting rlmNet.TrainingDone(); Console.ReadLine(); }
public virtual void TrainingDone() { rlmNet.TrainingDone(); }