示例#1
0
        private void Rlm_net_CycleComplete(RlmCyclecompleteArgs e)
        {
            if (e.RlmType != RlmNetworkType.Supervised) // Unsupervised & Predict only
            {
                currentCycleID = e.CycleOutput.CycleID;
                var aiOutput = Convert.ToInt16(e.CycleOutput.Outputs.First().Value);

                // queue AI's output - the Direction the AI will go
                GameRef.DirectionsStack.Enqueue(aiOutput);
            }
        }
        public CycleOutputParams RunCycle(RunCycleParams data)
        {
            var retVal = new CycleOutputParams();

            RlmNetworkWebAPI network = LoadNetworkFromCache(data);

            var inputsCycle = new List <RlmIOWithValue>();

            foreach (var ins in data.Inputs)
            {
                inputsCycle.Add(new RlmIOWithValue(network.Inputs.Where(item => item.Name == ins.IOName).First(), ins.Value));
            }

            var Cycle = new RlmCycle();
            RlmCyclecompleteArgs cycleOutput = null;

            // supervised training
            if (data.Outputs == null ||
                (data.Outputs != null && data.Outputs.Count > 0))
            {
                var outputsCycle = new List <RlmIOWithValue>();
                foreach (var outs in data.Outputs)
                {
                    outputsCycle.Add(new RlmIOWithValue(network.Outputs.First(a => a.Name == outs.IOName), outs.Value));
                }

                cycleOutput = Cycle.RunCycle(network, network.CurrentSessionID, inputsCycle, data.Learn, outputsCycle);
            }
            else // unsupervised training
            {
                cycleOutput = Cycle.RunCycle(network, network.CurrentSessionID, inputsCycle, data.Learn);
            }

            if (cycleOutput != null)
            {
                retVal = new CycleOutputParams {
                    RlmType = cycleOutput.RlmType, CycleId = cycleOutput.CycleOutput.CycleID
                };
                var outputs = cycleOutput.CycleOutput.Outputs;
                for (int i = 0; i < outputs.Count(); i++)
                {
                    RlmIOWithValue output = outputs.ElementAt(i);
                    retVal.Outputs.Add(new RlmIOWithValuesParams()
                    {
                        IOName = output.Name, Value = output.Value
                    });
                }
            }

            return(retVal);
        }
        //End Cycle
        internal RlmCyclecompleteArgs EndCycle(RlmCycleOutput cycleOutput, RlmNetworkType rnnType)
        {
            var retVal = new RlmCyclecompleteArgs(cycleOutput, this, rnnType);

            CurrentCase.Order = ++CaseOrder;

            //Fire CycleComplete Event
            if (CycleComplete != null)
            {
                CycleComplete(retVal);
            }

            return(retVal);
        }
示例#4
0
        static double ScoreCycle(RlmCyclecompleteArgs cycleResult, dynamic xor, bool showOutput = false)
        {
            double score  = 0;
            string output = null;

            // get output
            output = cycleResult.CycleOutput.Outputs.First().Value;

            // check if RLM output is correct based on our xor reference
            if (xor.Answer == output)
            {
                score = 100;
            }

            if (showOutput)
            {
                Console.WriteLine($"{ xor.Input1 } and { xor.Input2 } is { output }");
            }

            return(score);
        }
        private void MqttClient_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            string topic = e.Topic;
            string msg   = Encoding.UTF8.GetString(e.Message);

            if (topic == "init")
            {
                TOPIC_UID = msg; //Get unique id from client

                subscribe("create_load_network");
                subscribe("configure_network");
                subscribe("start_session");
                subscribe("run_cycle");
                subscribe("score_cycle");
                subscribe("end_session");
                subscribe("sessions");
                subscribe("session_cases");
                subscribe("io_details");
                subscribe("disconnect");

                publish(msg + "/init_result", TOPIC_UID);
            }
            else if (topic == createTopic("create_load_network"))
            {
                CreateLoadNetworkParams data = JsonConvert.DeserializeObject <CreateLoadNetworkParams>(msg);
                network = new RlmNetwork(data.RlmName);

                if (!network.LoadNetwork(data.NetworkName))
                {
                    IEnumerable <RlmIO> inputs  = data.Inputs.Select(a => new RlmIO(a.IOName, a.DotNetType, a.Min, a.Max, a.InputType)).ToList();
                    IEnumerable <RlmIO> outputs = data.Outputs.Select(a => new RlmIO(a.IOName, a.DotNetType, a.Min, a.Max)).ToList();

                    network.NewNetwork(data.NetworkName, inputs, outputs);
                }

                publish(createTopic("create_network_result"), "Network successfully loaded!");
            }
            else if (topic == createTopic("configure_network"))
            {
                RlmSettingsParams data = JsonConvert.DeserializeObject <RlmSettingsParams>(msg);

                network.NumSessions      = data.NumSessions;
                network.StartRandomness  = data.StartRandomness;
                network.EndRandomness    = data.EndRandomness;
                network.MaxLinearBracket = data.MaxLinearBracket;
                network.MinLinearBracket = data.MinLinearBracket;

                publish(createTopic("configure_result"), "Network successfully configured!");
            }
            else if (topic == createTopic("start_session"))
            {
                long sessionId = network.SessionStart();

                publish(createTopic("start_session_result"), sessionId.ToString());
            }
            else if (topic == createTopic("run_cycle"))
            {
                RunCycleParams data   = JsonConvert.DeserializeObject <RunCycleParams>(msg);
                var            retVal = new CycleOutputParams();


                var inputsCycle = new List <RlmIOWithValue>();
                foreach (var ins in data.Inputs)
                {
                    inputsCycle.Add(new RlmIOWithValue(network.Inputs.Where(item => item.Name == ins.IOName).First(), ins.Value));
                }

                var Cycle = new RlmCycle();
                RlmCyclecompleteArgs cycleOutput = null;

                // supervised training
                if (data.Outputs == null ||
                    (data.Outputs != null && data.Outputs.Count > 0))
                {
                    var outputsCycle = new List <RlmIOWithValue>();
                    foreach (var outs in data.Outputs)
                    {
                        outputsCycle.Add(new RlmIOWithValue(network.Outputs.First(a => a.Name == outs.IOName), outs.Value));
                    }

                    cycleOutput = Cycle.RunCycle(network, network.CurrentSessionID, inputsCycle, data.Learn, outputsCycle);
                }
                else // unsupervised training
                {
                    cycleOutput = Cycle.RunCycle(network, network.CurrentSessionID, inputsCycle, data.Learn);
                }

                if (cycleOutput != null)
                {
                    retVal = new CycleOutputParams {
                        RlmType = cycleOutput.RlmType, CycleId = cycleOutput.CycleOutput.CycleID
                    };
                    var outputs = cycleOutput.CycleOutput.Outputs;
                    for (int i = 0; i < outputs.Count(); i++)
                    {
                        RlmIOWithValue output = outputs.ElementAt(i);
                        retVal.Outputs.Add(new RlmIOWithValuesParams()
                        {
                            IOName = output.Name, Value = output.Value
                        });
                    }
                }

                var resultStr = JsonConvert.SerializeObject(retVal);

                publish(createTopic("run_cycle_result"), resultStr);
            }
            else if (topic == createTopic("score_cycle"))
            {
                ScoreCycleParams data = JsonConvert.DeserializeObject <ScoreCycleParams>(msg);

                network.ScoreCycle(data.CycleID, data.Score);

                publish(createTopic("score_cycle_result"), "Scoring cycle...");
            }
            else if (topic == createTopic("end_session"))
            {
                SessionEndParams data = JsonConvert.DeserializeObject <SessionEndParams>(msg);

                network.SessionEnd(data.SessionScore);

                publish(createTopic("end_session_result"), "Session ended!");
            }
            else if (topic == createTopic("sessions"))
            {
                dynamic data = JsonConvert.DeserializeObject <dynamic>(msg);


                string dbName = (String)data.RlmName;
                bool   withSignificantLearning = Convert.ToBoolean(((String)data.WithLearning).ToLower());
                int?   skip = Convert.ToInt32(((Int32)data.Skip));
                int?   take = Convert.ToInt32(((Int32)data.Take));

                if (skip == 0)
                {
                    skip = null;
                }

                if (take == 0)
                {
                    take = null;
                }

                string resultStr = "";
                if (withSignificantLearning)
                {
                    resultStr = JsonConvert.SerializeObject(getSessionsWithSignificantLearning(new RlmFilterResultParams {
                        Skip = skip, Take = take, RlmName = dbName
                    }));
                }
                else
                {
                    resultStr = JsonConvert.SerializeObject(getSessionHistory(new RlmFilterResultParams {
                        Skip = skip, Take = take, RlmName = dbName
                    }));
                }

                publish(createTopic("sessions_result"), resultStr);
            }
            else if (topic == createTopic("session_cases"))
            {
                RlmGetSessionCaseParams data = JsonConvert.DeserializeObject <RlmGetSessionCaseParams>(msg);

                RlmSessionCaseHistory hist = new RlmSessionCaseHistory(data.RlmName);

                var resultStr = JsonConvert.SerializeObject(hist.GetSessionCaseHistory(data.SessionId, data.Skip, data.Take));

                publish(createTopic("session_cases_result"), resultStr);
            }
            else if (topic == createTopic("io_details"))
            {
                RlmGetCaseIOParams data = JsonConvert.DeserializeObject <RlmGetCaseIOParams>(msg);

                RlmSessionCaseHistory hist = new RlmSessionCaseHistory(data.RlmName);

                var resultStr = JsonConvert.SerializeObject(hist.GetCaseIOHistory(data.CaseId, data.RneuronId, data.SolutionId));

                publish(createTopic("io_details_result"), resultStr);
            }
            else if (topic == createTopic("disconnect"))
            {
                if (MQTT_CLIENT != null && MQTT_CLIENT.IsConnected)
                {
                    MQTT_CLIENT.Disconnect();
                }
            }
        }
示例#6
0
        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();
        }