示例#1
0
        public double DoEvaluation(Network net, HardMaze env, int runcount = 1000)
        {
            Session session = new Session(env,
                                          new FitnessHandler(env.compute_fitness),
                                          new NWSELib.EventHandler(doEventHandler),
                                          new InstinctActionHandler(HardMaze.createInstinctAction),
                                          new OptimaGestureHandler(env.GetOptimaGestureHandler),
                                          new TaskBeginHandler(env.TaskBeginHandler));

            (List <double> obs, List <double> gesture) = env.reset(net);
            net.Reset();
            double reward = 0.0;
            bool   end    = false;

            for (int time = 0; time < runcount; time++)
            {
                List <double> inputs = new List <double>(obs);
                inputs.AddRange(gesture);
                List <double> actions = net.Activate(inputs, time, session, reward);
                (obs, gesture, actions, reward, end) = env.action(net, actions);
                if (end)
                {
                    break;
                }
            }

            net.Fitness       = Session.fitnessHandler == null ? 0 : Session.fitnessHandler(net, session);
            net.TaskCompleted = end;

            return(net.Fitness);
        }
示例#2
0
        public void Execute(String networkPaths, String mazePath, NWSELib.EventHandler handler, int runcount = 1200)
        {
            netDirectoryInfo  = new DirectoryInfo(networkPaths);
            mazeDirectionInfo = new DirectoryInfo(mazePath);

            netFileInfos  = netDirectoryInfo.GetFiles("*.ind");
            mazeFileInfos = mazeDirectionInfo.GetFiles("*.xml");

            for (int i = 0; i < netFileInfos.Length; i++)
            {
                Network       net          = null;
                List <double> performances = new List <double>();

                for (int j = 0; j < mazeFileInfos.Length; j++)
                {
                    net = Network.Load(netFileInfos[i].FullName);
                    HardMaze env   = HardMaze.loadEnvironment(mazeFileInfos[j].FullName);
                    double   value = DoEvaluation(net, env, runcount);
                    performances.Add(value);
                }

                String log = netFileInfos[i].Name + "," + net.Id.ToString() + "," + performances.Average().ToString() + "," + Utility.toString(performances);
                int    g   = int.Parse(DateTime.Now.ToString("HHmmss"));
                handler(Session.EVT_LOG, g, log);
                //logger.Info(log);
            }
        }
示例#3
0
        public virtual void update()
        {
            HardMaze env      = this.Owner.maze;
            Point2D  location = new Point2D(Owner.Location.X, Owner.Location.Y);
            Wall     hit;

            DistanceToClosestObject = this.raycast(env, Angle, MaxRange, location, Owner, out hit);
        }
示例#4
0
        /// <summary>
        /// Casts a ray from the robot's center point according to the specified Angle and returns the Distance to the closest object.
        /// </summary>
        /// <param name="Angle">Angle of sensor, in radians. Also see the "absolute" parameter.</param>
        /// <param name="maxRange">Max Distance that the robot can see in front of itself.</param>
        /// <param name="point">2D location of the robot's center.</param>
        /// <param name="Owner">The currently active robot.</param>
        /// <param name="hit">** Output variable ** - true if another object intersects the cast ray, false otherwise.</param>
        /// <param name="absolute">If false, the Angle parameter is relative to the agent's Heading. Otherwise it follows the standard unit AreaOfImpact.</param>
        /// <returns></returns>
        public static double raycast(this IAgentSensor sensor, HardMaze maze, double angle, double maxRange, Point2D point, RobotAgent owner, out Wall hit, bool absolute = false)
        {
            hit = null;
            Point2D casted   = new Point2D(point);
            double  distance = maxRange;

            // Cast point casted out from the robot's center point along the sensor direction
            if (!absolute)
            {
                casted.X += Math.Cos(angle + owner.Heading) * distance;
                casted.Y += Math.Sin(angle + owner.Heading) * distance;
            }
            else
            {
                casted.X += Math.Cos(angle) * distance;
                casted.Y += Math.Sin(angle) * distance;
            }

            // Create line segment from robot's center to casted point
            Line2D cast = new Line2D(point, casted);

            // Now do naive detection of collision of casted rays with objects
            // First for all walls
            foreach (Wall wall in maze.walls)
            {
                if (!wall.Visible)
                {
                    continue;
                }
                bool    found        = false;
                Point2D intersection = wall.Line.intersection(cast, out found);
                if (found)
                {
                    double new_distance = intersection.distance(point);
                    if (new_distance < distance)
                    {
                        distance = new_distance;
                        hit      = wall;
                    }
                }
            }
            return(distance);
        }
示例#5
0
        /// <summary>
        /// Loads a CurrentEnvironment from the specified XML file and initializes it.
        /// </summary>
        public static HardMaze loadEnvironment(string name)
        {
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(HardMaze));
            TextReader infile = new StreamReader(name);
            HardMaze   k      = (HardMaze)x.Deserialize(infile);

            k.maxDistance = (float)Math.Sqrt(k.AOIRectangle.Width * k.AOIRectangle.Width + k.AOIRectangle.Height * k.AOIRectangle.Height);

            k.maxGoalDistance = float.MinValue;
            double d = k.goal_point.distance(new Point2D(k.AOIRectangle.X, k.AOIRectangle.Y));

            if (d > k.maxGoalDistance)
            {
                k.maxGoalDistance = d;
            }
            d = k.goal_point.distance(new Point2D(k.AOIRectangle.X + k.AOIRectangle.Width, k.AOIRectangle.Y));
            if (d > k.maxGoalDistance)
            {
                k.maxGoalDistance = d;
            }
            d = k.goal_point.distance(new Point2D(k.AOIRectangle.X, k.AOIRectangle.Y + k.AOIRectangle.Height));
            if (d > k.maxGoalDistance)
            {
                k.maxGoalDistance = d;
            }
            d = k.goal_point.distance(new Point2D(k.AOIRectangle.X + k.AOIRectangle.Width, k.AOIRectangle.Y + k.AOIRectangle.Height));
            if (d > k.maxGoalDistance)
            {
                k.maxGoalDistance = d;
            }

            k.name = name;

            Console.WriteLine("# walls: " + k.walls.Count);
            k.rng = new Random(k.seed);

            k.fitnessFunction = new OptimaTraceFitness(k.OptimaTraces, k.goal_point);
            return(k);
        }
示例#6
0
        /// <summary>
        /// Updates the sensor based on the current state of the environment.
        /// </summary>
        public virtual void update()
        {
            HardMaze env = this.Owner.maze;

            Activation = 0;
            Point2D temp;
            double  dist;
            double  angle;

            if (Type == "goal")
            {
                temp = new Point2D(env.goal_point.X, env.goal_point.Y);
            }
            else
            {
                temp = new Point2D(env.start_point.X, env.start_point.Y);
            }

            dist = EngineUtilities.euclideanDistance(temp, new Point2D(Owner.AreaOfImpact.Position.X, Owner.AreaOfImpact.Position.Y));

            //translate with respect to location of navigator
            temp.X -= (float)Owner.AreaOfImpact.Position.X;
            temp.Y -= (float)Owner.AreaOfImpact.Position.Y;

            angle = (float)temp.angle();

            angle *= 57.297f;
            angle -= (float)Owner.Heading * 57.297f;


            while (angle > 360)
            {
                angle -= 360;
            }
            while (angle < 0)
            {
                angle += 360;
            }

            if (angle >= StartAngle && angle < EndAngle)
            {
                if (Type == "goal")
                {
                    Activation = 1.0 - (dist > MaxRange ? 1.0 : dist / MaxRange);
                }
                else
                {
                    Activation = 1.0;
                }
            }

            else if (angle + 360.0 >= StartAngle && angle + 360.0 < EndAngle)
            {
                if (Type == "goal")
                {
                    Activation = 1.0 - (dist > MaxRange ? 1.0 : dist / MaxRange);
                }
                else
                {
                    Activation = 1.0;
                }
            }
        }