/// <summary>
        /// Constructs the behavior vector at the end of an individual evaluation.
        /// </summary>
        List<double> IBehaviorCharacterization.calculate(SimulatorExperiment exp, instance_pack ip)
        {
            // initialize the BC vector
            BehaviorVector = new List<double>();

            // If the robot never Stopped, set the max evaluation time as the end tick
            if (endTick == 0)
                endTick = exp.evaluationTime;

            // Adjust end tick by the fraction we are sampling
            endTick = (int)(endTick * 0.5);

            // Calculate when to perform an update
            int numBehaviorChunks = VectorLength / 2;
            chunkSize = Convert.ToInt32(Math.Floor((double)endTick / (double)numBehaviorChunks)) * 2;

            float x, y;

            for (int chunkNum = 1; chunkNum < numBehaviorChunks + 1; chunkNum++)
            {
                // Take bc samples from the internal Trajectory store
                x = Trajectory[chunkNum * chunkSize - 2];
                x = (x - ip.env.AOIRectangle.Left) / ip.env.AOIRectangle.Width;
                BehaviorVector.Add(x);

                y = Trajectory[chunkNum * chunkSize - 1];
                y = (y - ip.env.AOIRectangle.Top) / ip.env.AOIRectangle.Height;
                BehaviorVector.Add(y);
            }

            return BehaviorVector;
        }
        /// <summary>
        /// Constructs the behavior vector at the end of an individual evaluation.
        /// </summary>
        List <double> IBehaviorCharacterization.calculate(SimulatorExperiment exp, instance_pack ip)
        {
            // initialize the BC vector
            BehaviorVector = new List <double>();

            // If the robot never Stopped, set the max evaluation time as the end tick
            if (endTick == 0)
            {
                endTick = exp.evaluationTime;
            }

            // Adjust end tick by the fraction we are sampling
            endTick = (int)(endTick * 0.5);

            // Calculate when to perform an update
            int numBehaviorChunks = VectorLength / 2;

            chunkSize = Convert.ToInt32(Math.Floor((double)endTick / (double)numBehaviorChunks)) * 2;

            float x, y;

            for (int chunkNum = 1; chunkNum < numBehaviorChunks + 1; chunkNum++)
            {
                // Take bc samples from the internal Trajectory store
                x = Trajectory[chunkNum * chunkSize - 2];
                x = (x - ip.env.AOIRectangle.Left) / ip.env.AOIRectangle.Width;
                BehaviorVector.Add(x);

                y = Trajectory[chunkNum * chunkSize - 1];
                y = (y - ip.env.AOIRectangle.Top) / ip.env.AOIRectangle.Height;
                BehaviorVector.Add(y);
            }

            return(BehaviorVector);
        }
        /// <summary>
        /// Records the individual's location at each tick of the simulation.
        /// </summary>
        void IBehaviorCharacterization.update(SimulatorExperiment exp, instance_pack ip)
        {
            if (ip.robots[0].Stopped)
            {
                // If this is the first update after the robot has Stopped,
                // send the endpoint to be the current simulation tick
                if (endTick == 0)
                {
                    endTick = Convert.ToInt32(ip.timeSteps * exp.timestep);
                }
            }

            // initialize the Trajectory list
            if (!Initialized)
            {
                // initialize the sensor value sampling/storage components
                Trajectory  = new List <int>(exp.evaluationTime * 2);
                Initialized = true;
            }

            // update the Trajectory at every tick
            Trajectory.Add(Convert.ToInt16(ip.robots[0].Location.X));
            Trajectory.Add(Convert.ToInt16(ip.robots[0].Location.Y));
        }
        /// <summary>
        /// Records the individual's location at each tick of the simulation. 
        /// </summary>
        void IBehaviorCharacterization.update(SimulatorExperiment exp, instance_pack ip)
        {
            if (ip.robots[0].Stopped)
            {
                // If this is the first update after the robot has Stopped, 
                // send the endpoint to be the current simulation tick
                if (endTick == 0)
                    endTick = Convert.ToInt32(ip.timeSteps * exp.timestep);
            }

            // initialize the Trajectory list
            if (!Initialized)
            {
                // initialize the sensor value sampling/storage components
                Trajectory = new List<int>(exp.evaluationTime * 2);
                Initialized = true;
            }

            // update the Trajectory at every tick
            Trajectory.Add(Convert.ToInt16(ip.robots[0].Location.X));
            Trajectory.Add(Convert.ToInt16(ip.robots[0].Location.Y));
        }
Пример #5
0
        public static void Main(string[] args)
        {
            string folder = "";
            string environment_name = null;                // "hallway.xml";
            string substrate_name = null;                  // "substrate.xml";
            string filename = null;                        // "seedGenome2x.xml";
            string to_eval = null;
            bool   homogenous = true;                      // = true;
            bool   overrideHomogenousSetting = false;
            int    generations = 1000, populationSize = 0; //was 500
            bool   novelty = false;
            bool   eval                             = false;
            bool   evolveSubstrate                  = false;
            bool   leo_setting                      = false;
            bool   overrideLeo                      = false;
            bool   overrideInputDensity             = false;
            bool   overrideHiddenDensity            = false;
            bool   overrideFitnessFunction          = false;
            bool   overrideBehaviorCharacterization = false;
            bool   seed_population                  = false;
            bool   overrideAgentCount               = false;
            bool   multiobjective                   = false;
            int    input_density_override           = -1;
            int    hidden_density_override          = -1;
            int    agent_count_override             = -1;
            string fitness_function_override        = "";
            string behavior_override                = "";
            string experimentName                   = null;
            bool   benchmark                        = false;
            bool   overrideES                       = false;

            if (args.Length != 0 && args[0] == "-help")
            {
                showHelp();
                return;
            }
            //evolve -experiment multiagent_exp.xml -seed seedGenome2x.xml
            if (!(args.Length == 0) && args[0] == "evolve")
            {
                for (int j = 1; j < args.Length; j++)
                {
                    if (j <= args.Length - 2)
                    {
                        switch (args[j])
                        {
                        case "-benchmark":
                            benchmark = true;
                            break;

                        case "-multiobjective":
                            multiobjective = true;
                            break;

                        case "-es":
                            evolveSubstrate = Convert.ToBoolean(args[++j]);
                            overrideES      = true;
                            Console.WriteLine("Evolvable-substrate: " + evolveSubstrate);
                            break;

                        case "-leo":
                            leo_setting = Convert.ToBoolean(args[++j]);
                            overrideLeo = true;
                            break;

                        case "-experiment":
                            experimentName = args[++j];
                            break;

                        case "-homogenous":
                            homogenous = Convert.ToBoolean(args[++j]);
                            overrideHomogenousSetting = true;
                            break;

                        case "-populationSize": if (!int.TryParse(args[++j], out populationSize))
                            {
                                Console.WriteLine("Invalid number of runs specified.");
                            }
                            break;

                        case "-generations": if (!int.TryParse(args[++j], out generations))
                            {
                                Console.WriteLine("Invalid number of generations specified.");
                            }
                            break;

                        case "-agent_count":
                            overrideAgentCount   = true;
                            agent_count_override = Convert.ToInt32(args[++j]);
                            break;

                        case "-novelty":
                            novelty = true;
                            Console.WriteLine("Novelty search enabled...");
                            //TODO   j++;
                            break;

                        case "-fitness_function":
                            overrideFitnessFunction   = true;
                            fitness_function_override = args[++j];
                            Console.WriteLine("Setting fitness function to " + fitness_function_override);
                            break;

                        case "-behavior_characterization":
                            overrideBehaviorCharacterization = true;
                            behavior_override = args[++j];
                            Console.WriteLine("Setting behavior characterization to " + behavior_override);
                            break;

                        case "-hidden_density":
                            overrideHiddenDensity   = true;
                            hidden_density_override = Convert.ToInt32(args[++j]);
                            Console.WriteLine("Setting hidden density to " + hidden_density_override);
                            break;

                        case "-input_density":
                            overrideInputDensity   = true;
                            input_density_override = Convert.ToInt32(args[++j]);
                            Console.WriteLine("Setting rangefinder input density to " + input_density_override);
                            break;

                        case "-rng_seed": int seed = Convert.ToInt32(args[++j]);
                            Utilities.random.Reinitialise(seed);
                            SharpNeatLib.Maths.RouletteWheel.random = new Random(seed);
                            Console.WriteLine("Using RNG seed " + seed);
                            break;

                        case "-environment": environment_name = args[++j];
                            Console.WriteLine("Using environment " + environment_name);
                            break;

                        case "-substrate": substrate_name = args[++j];
                            Console.WriteLine("Using substrate " + substrate_name);
                            break;

                        case "-eval": to_eval = args[++j];
                            eval = true;
                            Console.WriteLine("Attempting to evaluate file " + to_eval);
                            break;

                        case "-seed": filename = args[++j];
                            Console.WriteLine("Attempting to use seed from file " + filename);
                            break;

                        case "-seed_pop": filename = args[++j];
                            seed_population        = true;
                            Console.WriteLine("Attempting to use seed population from file " + filename);
                            break;

                        case "-folder": folder = args[++j];
                            Console.WriteLine("Attempting to output to folder " + folder);
                            break;
                        }
                    }
                }

                if (!homogenous && evolveSubstrate)
                {
                    Console.WriteLine("Evolvalbe-Substrate not supported for heterogenous teams");
                    return;
                }

                if (experimentName == null)
                {
                    Console.WriteLine("Missing [experimentName].");
                    Console.WriteLine("See help \"-help\"");
                    return;
                }

                ExperimentWrapper   wr         = ExperimentWrapper.load(experimentName);
                SimulatorExperiment experiment = wr.experiment;

                if (populationSize != 0)
                {
                    experiment.populationSize = populationSize;
                }
                else
                {
                    populationSize = experiment.populationSize;
                }

                if (overrideHomogenousSetting)
                {
                    if (experiment is MultiAgentExperiment)
                    {
                        ((MultiAgentExperiment)experiment).homogeneousTeam = homogenous;
                    }
                }

                if (environment_name != null)
                {
                    experiment.environmentName = environment_name;
                    experiment.environmentList.Add(Engine.Environment.loadEnvironment(environment_name));
                }

                //change sensor density if requested
                if (overrideInputDensity)
                {
                    experiment.rangefinderSensorDensity = input_density_override;
                }

                if (overrideAgentCount)
                {
                    if (experiment is MultiAgentExperiment)
                    {
                        ((MultiAgentExperiment)experiment).numberRobots = agent_count_override;
                    }
                }


                if (substrate_name != null)
                {
                    experiment.substrateDescription = new SubstrateDescription(substrate_name);
                }



                //  se.substrateDescription = substrate;
                if (filename != null)
                {
                    if (!seed_population)
                    {
                        experiment.loadGenome(filename);
                    }
                }



                if (eval)
                {
                    experiment.eval = true;
                    Console.WriteLine("Time to evaluate");
                    experiment.loadGenome(to_eval);
                    experiment.initialize();
                    if (benchmark)
                    {
                        experiment.timesToRunEnvironments = 6;                       //was 25
                        MultiAgentExperiment exp = (MultiAgentExperiment)experiment;
                        exp.benchmark = true;
                    }
                    Engine.NetworkEvaluator x = new Engine.NetworkEvaluator(experiment);

                    // Schrum: Loop used for testing: See how consistent evals are
                    // for (int i = 0; i < 3; i++) {

                    SharpNeatLib.BehaviorType           behavior;
                    SharpNeatLib.NeuralNetwork.INetwork network = experiment.genome.Decode(null);

                    Console.WriteLine("Links: " + network.NumLinks);
                    Console.WriteLine("Output Modules: " + network.NumOutputModules);
                    Console.WriteLine("Fitness score:" + x.EvaluateNetwork(network, out behavior));
                    // schrum2: Prevent this from crashing in domains with no behavior measure defined
                    if (behavior.behaviorList != null)
                    {
                        Console.Write("Behavior: ");
                        foreach (double d in behavior.behaviorList)
                        {
                            Console.Write(d + " ");
                        }
                        Console.WriteLine();
                    }
                    // Schrum: This will actually print out different objective scores.
                    // In the case of FourTasks, there is an objective for each environment.
                    if (behavior != null)
                    {
                        Console.Write("MO: ");
                        foreach (double d in behavior.objectives)
                        {
                            Console.Write(d + " ");
                        }
                        Console.WriteLine();
                    }

                    // }  // Schrum: End of test loop commented out above.
                    return;
                }

                Console.WriteLine("Experiment initialize");
                experiment.initialize();
                Console.WriteLine("Experiment done initializing");

                if (experiment.adaptableANN && evolveSubstrate)
                {
                    Console.WriteLine("Right now ES-HyperNEAT does not support plastic ANNs. Disable adaptation or use the fixed-substrate HyperNEAT");
                    return;
                }

                if (overrideLeo)
                {
                    experiment.substrateDescription.useLeo = leo_setting;
                }


                Console.WriteLine("Override fitness?");
                //change fitness function if requested
                if (overrideFitnessFunction)
                {
                    experiment.setFitnessFunction(fitness_function_override);
                }

                if (overrideBehaviorCharacterization)
                {
                    experiment.setBehavioralCharacterization(behavior_override);
                }

                if (overrideES)
                {
                    experiment.evolveSubstrate = evolveSubstrate;
                }

                //make sure substrate's input density matches # of inputs in environment
                uint number_of_sensors = Convert.ToUInt32(experiment.rangefinderSensorDensity);

                uint dx = 0, dy = 0;

                /*
                 * experiment.substrateDescription.getNeuronDensity(0, out dx, out dy);
                 *
                 * //TODO maybe updateInputDensity of AgentBrain needs to be called
                 *
                 * if (dx != number_of_sensors)
                 * {
                 * Console.WriteLine("Input density forced to match number of sensors.");
                 * Console.WriteLine("Input Density beforehand: " + dx.ToString() + " " + dy.ToString());
                 * experiment.substrateDescription.setNeuronDensity(0, number_of_sensors, 1);
                 * experiment.substrateDescription.getNeuronDensity(0, out dx, out dy);
                 * Console.WriteLine("Input Density afterwards: " + dx.ToString() + " " + dy.ToString());
                 * }
                 */
                if (overrideHiddenDensity)
                {
                    experiment.substrateDescription.getNeuronDensity(1, out dx, out dy);
                    Console.WriteLine("Hidden Density beforehand: " + dx.ToString() + " " + dy.ToString());
                    experiment.substrateDescription.setNeuronDensity(0, Convert.ToUInt32(hidden_density_override), 1);
                    experiment.substrateDescription.getNeuronDensity(0, out dx, out dy);
                    Console.WriteLine("Hidden Density afterwards: " + dx.ToString() + " " + dy.ToString());
                }

                Console.WriteLine("New evolver");
                HyperNEATEvolver evolve = new HyperNEATEvolver(experiment);

                if (novelty)
                {
                    evolve.enableNoveltySearch(true);
                }

                if (multiobjective)
                {
                    evolve.experiment.DefaultNeatParameters.multiobjective = true;
                }

                Console.WriteLine("Set output: " + folder);
                evolve.setOutputFolder(folder);

                if (filename != null)
                {
                    if (seed_population)
                    {
                        evolve.initializeEvolutionFromPopFile(filename);
                    }
                    else
                    {
                        evolve.initializeEvolution(populationSize, experiment.genome);
                    }
                }
                else
                {
                    evolve.initializeEvolution(populationSize);
                }

                Console.WriteLine("About to evolve");
                evolve.evolve(generations);

                /*for (int i = 0; i < generations; i++)
                 * {
                 * evolve.oneGeneration(i);
                 * }*/
            }
            else
            {
                experimentName = "EXP_FourTasks-avg-1M.xml";

                for (int j = 0; j < args.Length; j++)
                {
                    if (j <= args.Length - 2)
                    {
                        switch (args[j])
                        {
                        case "-experiment":
                            experimentName = args[++j];
                            break;

                        case "-genome":
                            filename = args[++j];
                            break;
                        }
                    }
                }
                SimulatorVisualizer vis = new SimulatorVisualizer(experimentName, filename);
                Application.Run(vis);
            }
        }