Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
-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);
        }