示例#1
0
 public Identify.Fitness.Type FitnessType(Fitness.Mode mode)
 {
     if (mode == Fitness.Mode.Minimize)
     {
         return(Optimization.Messages.Identify.Fitness.Type.Minimize);
     }
     else
     {
         return(Optimization.Messages.Identify.Fitness.Type.Maximize);
     }
 }
        public virtual void FromStorage(Storage.Storage storage, Storage.Records.Optimizer optimizer)
        {
            d_storage = storage;

            if (optimizer == null)
            {
                optimizer = d_storage.ReadJob().Optimizer;
            }

            /* Settings */
            d_settings.Clear();

            foreach (KeyValuePair <string, string> pair in optimizer.Settings)
            {
                d_settings[pair.Key] = pair.Value;
            }

            /* Boundaries */
            d_boundaries.Clear();
            d_boundaryHash.Clear();

            foreach (Storage.Records.Boundary boundary in optimizer.Boundaries)
            {
                Boundary bound = new Boundary(boundary.Name);

                bound.MinSetting.Representation        = boundary.Min;
                bound.MaxSetting.Representation        = boundary.Max;
                bound.MinInitialSetting.Representation = boundary.MinInitial;
                bound.MaxInitialSetting.Representation = boundary.MaxInitial;

                AddBoundary(bound);
            }

            /* Parameters */
            d_parameters.Clear();

            foreach (Storage.Records.Parameter parameter in optimizer.Parameters)
            {
                AddParameter(new Parameter(parameter.Name, 0, Boundary(parameter.Boundary.Name)));
            }

            /* Fitness */
            d_fitness.Clear();

            Fitness.Mode mode = Fitness.ModeFromString(optimizer.Fitness.Mode);

            if (mode != Fitness.Mode.Invalid)
            {
                Fitness.CompareMode = mode;
            }
            else
            {
                Fitness.CompareMode = Fitness.Mode.Default;
            }

            d_fitness.Expression.Parse(optimizer.Fitness.Expression);

            foreach (KeyValuePair <string, Storage.Records.Fitness.Variable> pair in optimizer.Fitness.Variables)
            {
                mode = Fitness.CompareMode;

                Fitness.Mode parsed = Fitness.ModeFromString(pair.Value.Mode);

                if (parsed != Fitness.Mode.Invalid)
                {
                    mode = parsed;
                }

                d_fitness.AddVariable(pair.Key, pair.Value.Expression, parsed);
            }

            /* Restore iteration, state */
            d_currentIteration = (uint)storage.ReadIterations();

            d_state.Settings.Clear();

            foreach (KeyValuePair <string, string> pair in optimizer.State.Settings)
            {
                d_state.Settings[pair.Key] = pair.Value;
            }

            d_state.Random = optimizer.State.Random;

            /* Restore population */
            d_population.Clear();

            if (d_currentIteration > 0)
            {
                Storage.Records.Iteration iteration = storage.ReadIteration((int)d_currentIteration - 1);

                foreach (Storage.Records.Solution solution in iteration.Solutions)
                {
                    Solution sol = CreateSolution((uint)solution.Index);
                    FromStorage(storage, optimizer, solution, sol);

                    Add(sol);
                }

                // Restore best solution
                Storage.Records.Solution best = storage.ReadSolution(-1, -1);

                if (best != null)
                {
                    d_best = CreateSolution((uint)best.Index);
                    FromStorage(storage, optimizer, best, d_best);
                }
                else
                {
                    d_best = null;
                }
            }
            else
            {
                InitializePopulation();
                d_best = null;
            }

            foreach (Extension ext in d_extensions)
            {
                ext.FromStorage(storage, optimizer);
            }

            Setup();
        }
        private void LoadFitness(XmlNode root)
        {
            XmlNode fitness = root.SelectSingleNode("fitness");

            Fitness.CompareMode = Fitness.Mode.Default;

            if (fitness == null)
            {
                return;
            }

            XmlAttribute attr = fitness.Attributes["mode"];

            if (attr != null)
            {
                Fitness.Mode mode = Fitness.ModeFromString(attr.Value);

                if (mode == Fitness.Mode.Invalid)
                {
                    throw new Exception(String.Format("Fitness mode is invalid: `{0}'", attr.Value));
                }

                Fitness.CompareMode = mode;
            }

            XmlNode expression = root.SelectSingleNode("fitness/expression");

            if (expression == null)
            {
                return;
            }

            if (!d_fitness.Parse(expression.InnerText))
            {
                throw new Exception("XML: Could not parse fitness");
            }

            XmlNodeList nodes = root.SelectNodes("fitness/variable");

            foreach (XmlNode node in nodes)
            {
                XmlAttribute nm = node.Attributes["name"];

                if (nm == null)
                {
                    throw new Exception("XML: Fitness variable has no name");
                }

                XmlAttribute hint = node.Attributes["mode"];

                Fitness.Mode mode = Fitness.CompareMode;

                if (hint != null)
                {
                    Fitness.Mode parsed = Fitness.ModeFromString(hint.Value);

                    if (parsed != Fitness.Mode.Invalid)
                    {
                        mode = parsed;
                    }
                }

                d_fitness.AddVariable(nm.Value, node.InnerText, mode);
            }
        }