示例#1
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();
        }
示例#2
0
        private void button2_Click(object sender, EventArgs e)
        {
            int runs = 64;
            int trials = 50;
            int stepCap = 200;
            double[][] results = new double[trials * 2][];
            for (int i = 0; i < (trials * 2); i++)
                results[i] = new double[runs];

            int scale = 2;

            Bitmap map = new Bitmap("C:\\Users\\Eric\\Google Drive\\Lethbridge Projects\\waterMazeSim\\Open.bmp");
            Bitmap resized = new Bitmap(map.Width * 3, map.Height * 3);
            using (Graphics g = Graphics.FromImage(resized))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
                g.DrawImage(map, 0, 0, map.Width * scale, map.Height * scale);
            }
            resized.Save("temp.bmp");

            map = new Bitmap("C:\\Users\\Eric\\Google Drive\\Lethbridge Projects\\waterMazeSim\\Third.bmp");
            resized = new Bitmap(map.Width * 3, map.Height * 3);
            using (Graphics g = Graphics.FromImage(resized))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
                g.DrawImage(map, 0, 0, map.Width * scale, map.Height * scale);
            }
            resized.Save("temp2.bmp");

            Parallel.For(0, runs, run =>  // for (int run = 0; run < runs; run++)
            {
                GridWorld world = new GridWorld();

                world.Load("temp.bmp");
                world.addAgent(typeof(EGreedyPolicy<,>), typeof(MultiResValue<,>), 1, 0);

                PerformanceStats stats = new PerformanceStats();
                while (stats.stepsToGoal.Count < (trials + 1))
                {
                    stats = world.stepAgent();
                    //pictureBox1.Image = world.showState(300, 300);
                    //pictureBox1.Refresh();
                    //System.Threading.Thread.Sleep(1);

                    if (stats.stepsToGoal.Last() >= stepCap)
                    {
                        stats.TallyStepsToGoal(true);
                        world.Load("temp.bmp");
                    }
                }

                world.Load("temp2.bmp");
                while (stats.stepsToGoal.Count < (trials * 2 + 1))
                {
                    stats = world.stepAgent();
                    //pictureBox1.Image = world.showState(300, 300);
                    //pictureBox1.Refresh();
                    //System.Threading.Thread.Sleep(1);

                    if (stats.stepsToGoal.Last() >= stepCap)
                    {
                        stats.TallyStepsToGoal(true);
                        world.Load("temp2.bmp");
                    }
                }

                for (int i = 0; i < trials * 2; i++)
                {
                    results[i][run] = stats.stepsToGoal[i];
                }
            });

            for (int i = 0; i < trials * 2; i++)
            {
                textBox1.AppendText(results[i].Average() + Environment.NewLine);
            }
        }
示例#3
-1
        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);
        }