Exemple #1
0
        /// <summary>
        /// This is where encog network settings are configured and training is being processed.
        /// </summary>
        public static void LogisticTrain()
        {
            Console.WriteLine("\n\nEncog network structure: \nNumber of Input neurons 1 \nNumber of output neurons 9 ");

            int hiddenLayers       = Util.GetInput("Number of hidden layers [default 1]: ", 1);
            int hiddenLayerNeurons = Util.GetInput("Hidden layer neurons [default 100]: ", 100);
            int type       = Util.GetInput("\nSelect a training method [Annealing - 0][Genetic - 1 default]:", 1);
            int numOfEpoch = Util.GetInput("Number of Epochs [default 10]:", 10);

            BasicNetwork network    = CreateNetwork(hiddenLayers, hiddenLayerNeurons);
            var          pilotScore = new EncogLogisticScore();
            IMLTrain     train;

            if (type == 0)
            {
                int startTemp = Util.GetInput("Start Temperature [default 10]:", 10);
                int endTemp   = Util.GetInput("End Temperature [default 2]:", 2);
                int cycles    = Util.GetInput("Cycles [default 10]:", 10);
                train = new NeuralSimulatedAnnealing(network, pilotScore, endTemp, startTemp, cycles);
            }
            else
            {
                int populationSize = Util.GetInput("Population Size [default 10]:", 10);
                train = new MLMethodGeneticAlgorithm(() => {
                    BasicNetwork result = CreateNetwork(hiddenLayers, hiddenLayerNeurons);
                    ((IMLResettable)result).Reset();
                    return(result);
                }, pilotScore, populationSize); // population size
            }

            Stopwatch watch = new Stopwatch();

            watch.Start();
            Console.WriteLine("\n\nTraining: \n");

            for (int i = 0; i < numOfEpoch; i++) // num of epochs
            {
                train.Iteration();

                double totalCosts    = train.Error;
                string currencyScore = totalCosts.ToString("$#,##0");
                Console.WriteLine($"Epoch # {i} \t Score: {currencyScore,10}");
            }

            watch.Stop();

            Console.WriteLine("\nPredicted outputs:");
            network = (BasicNetwork)train.Method;
            var pilot = new EncogLogisticSimulator(network, true);

            pilot.CalculateScore(LogisticSimulator.GenerateCustomerOrders(), true);

            Console.WriteLine($"\nElapsed: {watch.Elapsed}");
            Console.WriteLine("\nThe total number of times it tried the Logistic Simulation for training: " + pilotScore.SessionCnt);
            Console.ReadLine();
        }
 public EncogLogisticsPilot(BasicNetwork network, LogisticSimulationMetadata_Encog metadata, bool turnOffMqtt = true)
 {
     _network = network;
     sim      = new LogisticSimulator(
         metadata.StorageCostPerDay,
         metadata.BacklogCostPerDay,
         metadata.RetailerInitialInventory,
         metadata.DistributorInitialInventory,
         metadata.WholesalerInitialInventory,
         metadata.FactoryInitialInventory);
 }
        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();
        }
Exemple #4
0
        public int GetScore(LogisticSimulator sim)
        {
            var score = sim.SumAllCosts() * -1;

            return(Convert.ToInt32(20000 - score));
        }