Example #1
0
        public void update(SimulatorExperiment engine, Environment environment)
        {
            if (first)
            {
                distances = new double[environment.POIPosition.Count, engine.robots.Count];
                for (int v = 0; v < environment.POIPosition.Count; v++)
                {
                    for (int w = 0; w < engine.robots.Count; w++)
                    {
                        distances[v, w] = radius;
                    }
                }
                first = false;
                return;
            }

            double dist = 0;

            for (int k = 0; k < engine.robots.Count; k++)
            {
                Robot r = engine.robots[k];
                for (int j = 0; j < environment.POIPosition.Count; j++)
                {
                    dist = EngineUtilities.euclideanDistance(r.location, new Point2D(environment.POIPosition[j].X, environment.POIPosition[j].Y));
                    if (dist < distances[j, k])
                    {
                        distances[j, k] = dist;
                    }
                }
            }
        }
Example #2
0
        public override bool RobotCollide(Robot robot)
        {
            foreach (Wall wall in env.walls)
            {
                if (EngineUtilities.collide(robot, wall))
                {
                    return(true);
                }
            }
            if (!agentCollide)
            {
                return(false);
            }

            foreach (Robot robot2 in rbts)
            {
                if (robot == robot2)
                {
                    continue;
                }
                if (EngineUtilities.collide(robot, robot2))
                {
                    return(true);
                }
            }

            return(false);
        }
        void IFitnessFunction.update(SimulatorExperiment Experiment, Environment environment, instance_pack ip)
        {
            // Initialize variables on first time step
            if (ip.timeSteps == 1)
            {
                livingCost = 0;
                rewards    = 0;
                // Fitness values must be positive. Therefore, a high positive fitness is assigned in advance,
                // and the cost of living subtracts from it.
                // time / steps is the number of actual time steps, and the cost is multiplied by number of enemies
                fitness = (Experiment.evaluationTime / Experiment.timestep) * Experiment.numEnemies;
            }

            // Find closest active prey
            bool  allCaptured = true; // becomes false if an active prey is found
            Robot evolved     = ip.robots[0];

            for (int i = 1; i < ip.robots.Count; i++)
            {
                // Assumes all but first robot are EnemyRobot instances
                EnemyRobot er = (EnemyRobot)ip.robots[i];
                if (!er.disabled) // Not captured yet
                {
                    //Console.WriteLine("Robot "+i+" not disabled");

                    allCaptured = false;
                    // The collisionWithEvolved bool should always be the primary means of detecting these
                    // collisions, but the other call is here as a precaution. This check is needed because
                    // when the evolved bot normally collides with the enemy, the "undo" method is called,
                    // which prevents the collision from being detected using normal means in this method.
                    // Fortunately, the collisionWithEvolved member is used to remember when this collision
                    // occurs.
                    if (er.collisionWithEvolved || EngineUtilities.collide(evolved, er))
                    { // Reward evolved bot for colliding with prey, and disable prey
                        er.disabled = true;
                        er.stopped  = true;
                        rewards    += PREY_REWARD;
                        fitness    += PREY_REWARD; // This is the value that matters
                        //Console.WriteLine("\treward:" + rewards + " from " + PREY_REWARD);
                    }
                    else
                    { // Each active prey punishes bot for not being caltured yet
                        double distance = evolved.location.distance(er.location);
                        double cost     = distance / environment.maxDistance;
                        livingCost += cost;
                        fitness    -= cost; // This is the value that matters
                        //Console.WriteLine("\tCost: " + (distance / 1000.0) + " to be " + livingCost + " raw distance: " + distance);
                    }
                }
            }

            // End evaluation and stop accruing negative fitness if all prey are captured
            if (allCaptured)
            {                                                   // Disabling prevents further action
                ip.elapsed         = Experiment.evaluationTime; // force end time: only affects non-visual evaluation
                Experiment.running = false;                     // Ends visual evaluation
            }
        }
        public override void networkResults(float[] outputs)
        {
            double distortion;

            if (effectorNoise > 0)
            {
                for (int k = 0; k < outputs.Length; k++)
                {
                    distortion  = 1.0 + (EngineUtilities.random.NextDouble() - 0.5) * 2.0 * (effectorNoise) / 100.0;
                    outputs[k] *= (float)distortion;
                    outputs[k]  = (float)EngineUtilities.clamp(outputs[k], 0, 1);
                }
            }
            decideAction(outputs, timeStep);
            updatePosition();
        }
        public override bool RobotCollide(Robot robot)
        {
            List <collision_grid_square> squares =
                grid.circle(robot.location.x, robot.location.y, robot.radius);
            List <Wall>  checked_walls  = new List <Wall>();
            List <Robot> checked_robots = new List <Robot>();

            foreach (collision_grid_square square in squares)
            {
                //TODO
                if (this.agentCollide)
                {
                    foreach (SimulatorObject o in square.dynamic_objs)
                    {
                        Robot r = (Robot)o;

                        if (r == robot)
                        {
                            continue;
                        }

                        if (!checked_robots.Contains(r))
                        {
                            if (EngineUtilities.collide(r, robot))
                            {
                                return(true);
                            }
                            checked_robots.Add(r);
                        }
                    }
                }

                foreach (SimulatorObject o in square.static_objs)
                {
                    Wall w = (Wall)o;
                    if (!checked_walls.Contains(w))
                    {
                        if (EngineUtilities.collide(robot, w))
                        {
                            return(true);
                        }
                        checked_walls.Add(w);
                    }
                }
            }
            return(false);
        }
Example #6
0
        public void update(Environment env, List <Robot> robots)
        {
            bool hitRobot;
            bool agentsVisible = false; //TODO

            offsetx = Math.Cos(owner.heading + delta_theta) * delta_r;
            offsety = Math.Sin(owner.heading + delta_theta) * delta_r;
            if (agentsVisible)
            {
                distance = EngineUtilities.raycast(angle, max_range, new Point2D(offsetx + owner.location.x, offsety + owner.location.y), owner.heading, out hitRobot,
                                                   env.walls, robots, owner);
            }
            else
            {
                distance = EngineUtilities.raycast(angle, max_range, new Point2D(offsetx + owner.circle.p.x, offsety + owner.circle.p.y), owner.heading, env.walls, owner);
            }

            Debug.Assert(!Double.IsNaN(distance), "NaN in inputs");
        }
        double IFitnessFunction.calculate(SimulatorExperiment engine, Environment environment, out double[] objectives)
        {
            objectives = null;

            /*  double a = 0;
             *
             * grid = ((GridCollision)((MultiAgentExperiment)engine).collisionManager).grid;
             *
             * int dim = grid.coarseness;
             * for (int x = 0; x < dim; x++)
             * {
             *    for (int y = 0; y < dim; y++)
             *    {
             *        int gx = (int)((double)x * grid.gridx) + (int)(grid.gridx / 2.0);
             *        int gy = (int)((double)y * grid.gridy) + (int)(grid.gridy / 2.0);
             *        if ((environment.AOIRectangle.Contains(gx, gy)))
             *            a += grid.grid[x, y].viewed;
             *    }
             * }*/

            double fit = 0;

            if (allLeft)
            {
                for (int j = 0; j < engine.robots.Count; j++)
                {
                    if (!environment.AOIRectangle.Contains((int)engine.robots[j].location.x, (int)engine.robots[j].location.y))
                    {
                        fit += 50;
                    }
                    else if (!engine.robots[j].collide_last)
                    {
                        fit += 10.0 * (1.0 - (EngineUtilities.euclideanDistance(engine.robots[j].location, environment.goal_point) / 145.0));
                    }
                }
            }

            return(accum + fit);
        }
Example #8
0
        public override void networkResults(float[] outputs)
        {
            //  controller.SetInputSignals(inputs);

            //TODO: Should be based on network depth or something
            //   controller.MultipleSteps(2);

            // float[] outputs = new float[controller.OutputNeuronCount];

            double distortion;

            if (effectorNoise > 0)
            {
                for (int k = 0; k < outputs.Length; k++)
                {
                    distortion  = 1.0 + (EngineUtilities.random.NextDouble() - 0.5) * 2.0 * (effectorNoise) / 100.0;
                    outputs[k] *= (float)distortion;
                    outputs[k]  = (float)EngineUtilities.clamp(outputs[k], 0, 1);
                }
            }
            decideAction(outputs, timeStep);
            updatePosition();
        }
        public override void draw(Graphics g, CoordinateFrame frame)
        {
            Point     upperleft = frame.to_display((float)(circle.p.x - radius), (float)(circle.p.y - radius));
            int       size      = (int)((radius * 2) / frame.scale);
            Rectangle r         = new Rectangle(upperleft.X, upperleft.Y, size, size);

            // Schrum: Colored rectangle for mode tracking
            //Rectangle mode = new Rectangle(upperleft.X + (size / 4), upperleft.Y + (size / 4), size / 2, size / 2);
            // Schrum: Draw the mode rectangle
            //g.FillRectangle(EngineUtilities.modePen(currentBrainMode), mode);

            // Schrum: Alternative mode coloring method
            g.FillEllipse(EngineUtilities.modePen(currentBrainMode), r);

            if (disabled)
            {
                g.DrawEllipse(EngineUtilities.YellowPen, r);
            }
            else if (collide_last || confused)
            {
                g.DrawEllipse(EngineUtilities.RedPen, r);
            }
            else if (corrected)
            {
                g.DrawEllipse(EngineUtilities.YellowPen, r);
            }
            else if (autopilot)
            {
                g.DrawEllipse(EngineUtilities.GreendPen, r);
            }
            else
            {
                g.DrawEllipse(EngineUtilities.BluePen, r);
            }
            int sensCount = 0;

            if (display_debug)
            {
                foreach (float f in output_copy)
                {
                    Color      col    = Color.FromArgb(0, 0, (int)(f * 255)); //(int)(val*255),(int)(val*255));
                    SolidBrush newpen = new SolidBrush(col);
                    g.FillRectangle(newpen, sensCount * 40 + 400, 500 + 30 * id, 40, 30);
                    sensCount += 1;
                }
            }
            sensCount = 0;

            foreach (ISensor sensor in sensors)
            {
                sensor.draw(g, frame);
                if (draw_sensors)
                {
                    double val = sensor.get_value();
                    if (val < 0.0)
                    {
                        val = 0.0;
                    }
                    if (val > 1.0)
                    {
                        val = 1.0;
                    }
                    if (display_debug)
                    {
                        Color      col    = Color.FromArgb((int)(val * 255), 0, 0); //(int)(val*255),(int)(val*255));
                        SolidBrush newpen = new SolidBrush(col);
                        g.FillRectangle(newpen, sensCount * 40, 500 + 30 * id, 40, 30);
                    }
                    sensCount += 1;
                }
            }
        }
 public static bool collide(Robot a, Wall b)
 {
     return(EngineUtilities.collide(b, a));
 }