Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            string stepsToGoalFilename = "C:\\Users\\Eric\\Desktop\\Maps\\stepsToGoal.csv";
            string modelUseFilename    = "C:\\Users\\Eric\\Desktop\\Maps\\modelUse.csv";
            string cumRewardFilename   = "C:\\Users\\Eric\\Desktop\\Maps\\cumReward.csv";
            string mapsDirectory       = "C:\\Users\\Eric\\Desktop\\Maps\\";

            int runs   = 96;
            int goalCt = 10;

            List <double>[] stepsToGoal = new List <double> [runs];
            List <double>[] cumModelUse = new List <double> [runs];
            List <double>[] cumReward   = new List <double> [runs];

            string[]      mapNames = Directory.GetFiles(mapsDirectory, "*.bmp");
            List <string> maps     = new List <string>();

            maps.AddRange(mapNames); //maps.AddRange(mapNames);

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();

            ParallelOptions op = new ParallelOptions()
            {
                MaxDegreeOfParallelism = 8
            };

            //for(int run=0; run< runs; run++)
            Parallel.For(0, runs, op, (run) =>
            {
                cumModelUse[run] = new List <double>();
                cumReward[run]   = new List <double>();

                // instantiate world
                World thisWorld = new GridWorld();

                // add agent
                System.Threading.Thread.Sleep(run * 101);                                              // staggered instantiation to avoid identical random number generators
                thisWorld.addAgent(typeof(EGreedyPolicy <,>), typeof(ContextSwitchValue <,>), 8, 100); // this line for context-switch + adaptation
                //thisWorld.addAgent(typeof(EGreedyPolicy<,>), typeof(ContextSwitchValue<,>), 1, 100); // this line for context-switch only
                //thisWorld.addAgent(typeof(EGreedyPolicy<,>), typeof(ModelBasedValue<,>)); // this line for standard MBRL

                PerformanceStats stats = new PerformanceStats();

                for (int mapNumber = 0; mapNumber < maps.Count; mapNumber++)
                {
                    // load map
                    thisWorld.Load(maps[mapNumber]);

                    // go
                    while (stats.stepsToGoal.Count < goalCt * (mapNumber + 1))
                    {
                        stats = thisWorld.stepAgent("");
                        if (stats.stepsToGoal.Last() == 0)
                        {
                            cumModelUse[run].Add(stats.modelAccesses + stats.modelUpdates);
                            cumReward[run].Add(stats.cumulativeReward);
                            Console.WriteLine("run " + run.ToString() + " goal count: " + (stats.stepsToGoal.Count - 1) + " steps: " + stats.stepsToGoal[mapNumber]);
                        }
                    }

                    stepsToGoal[run] = stats.stepsToGoal;
                }
            });
            //}

            saveToCSV(stepsToGoalFilename, stepsToGoal);
            saveToCSV(modelUseFilename, cumModelUse);
            saveToCSV(cumRewardFilename, cumReward);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            int    wS = 48;                                                //WorldSize p[0]
            bool   RL = true;                                              //RLMethod p[1];  'F' for QL, 'T' For MB
            double a  = 0.1;                                               //alpha p[2];
            double g  = 0.8;                                               //Gamma p[3];
            int    tO = wS;                                                //timeOut p[4];
            double mR = 1;                                                 //Manager Rewards p[5];
            Policy <int[], int[]> cP = new EGreedyPolicy <int[], int[]>(); //chosen Policy p[6]

            // task-switch test
            int runs   = 48;
            int goalCt = 10;

            List <double>[] stepsToGoal = new List <double> [runs];
            List <double>[] cumModelUse = new List <double> [runs];

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();

            ParallelOptions op = new ParallelOptions()
            {
                MaxDegreeOfParallelism = 8
            };

            //for (int run = 0; run < runs; run++)
            Parallel.For(0, runs, op, run =>
            {
                cumModelUse[run] = new List <double>();

                // instantiate world
                World thisWorld = new GridWorld();

                // load 1st map
                thisWorld.Load("C:\\Users\\Eric\\Google Drive\\Lethbridge Projects\\map10.bmp");

                // add agent
                System.Threading.Thread.Sleep(run * 100); // staggered instantiation to avoid identical random number generators

                //thisWorld.addAgent(typeof(EGreedyPolicy<,>), typeof(MultiGridWorldModel<,>), 8);
                //  thisWorld.addAgent(typeof(EGreedyPolicy<,>), typeof(Boss<,>), wS, RL, a, g, tO, mR, cP);

                // run
                PerformanceStats stats = new PerformanceStats();
                while (stats.stepsToGoal.Count <= goalCt)
                {
                    stats = thisWorld.stepAgent("");
                    if (stats.stepsToGoal.Last() == 0)
                    {
                        cumModelUse[run].Add(stats.modelAccesses + stats.modelUpdates);
                        Console.WriteLine("run " + run.ToString() + " goal count: " + stats.stepsToGoal.Count);
                    }
                }

                // switch task
                thisWorld.Load("C:\\Users\\Eric\\Google Drive\\Lethbridge Projects\\map10a.bmp");

                // run again
                while (stats.stepsToGoal.Count <= goalCt * 2)
                {
                    stats = thisWorld.stepAgent("");
                    if (stats.stepsToGoal.Last() == 0)
                    {
                        cumModelUse[run].Add(stats.modelAccesses + stats.modelUpdates);
                        Console.WriteLine("run " + run.ToString() + " goal count: " + stats.stepsToGoal.Count);
                    }
                }

                stepsToGoal[run] = stats.stepsToGoal;
            });

            System.IO.StreamWriter writer = new System.IO.StreamWriter("C:\\Users\\Eric\\Google Drive\\Lethbridge Projects\\stepsToGoal.csv");
            for (int i = 0; i < stepsToGoal[0].Count; i++)
            {
                List <string> line = new List <string>();
                foreach (List <double> series in stepsToGoal)
                {
                    line.Add(series[i].ToString());
                }
                writer.WriteLine(string.Join(",", line));
            }
            writer.Flush();
            writer.Close();
            writer = new System.IO.StreamWriter("C:\\Users\\Eric\\Google Drive\\Lethbridge Projects\\modelUse.csv");
            for (int i = 0; i < cumModelUse[0].Count; i++)
            {
                List <string> line = new List <string>();
                foreach (List <double> series in cumModelUse)
                {
                    line.Add(series[i].ToString());
                }
                writer.WriteLine(string.Join(",", line));
            }
            writer.Flush();
            writer.Close();
        }