Esempio n. 1
0
        public Records.Iteration ReadIteration(int iteration)
        {
            Records.Iteration ret = new Records.Iteration();

            object[] vals = QueryFirst("SELECT `best_id`, `time` FROM iteration WHERE `iteration` = @0", iteration);

            int bestId = As <int>(vals[0]);

            ret.Index = iteration;
            ret.Time  = FromUnixTimestamp(As <int>(vals[1]));

            Dictionary <int, Records.Solution> idmap = new Dictionary <int, Records.Solution>();

            Query(@"SELECT solution.iteration AS `the_iteration`, solution.`index` AS `the_index`, solution.*, fitness.*, parameter_values.*, data.* FROM solution
                    LEFT JOIN fitness ON (fitness.`index` = solution.`index` AND
                                          fitness.`iteration` = solution.`iteration`)
                    LEFT JOIN parameter_values ON (`parameter_values`.`index` = solution.`index` AND
                                            `parameter_values`.`iteration` = solution.`iteration`)
                    LEFT JOIN data ON (data.`index` = solution.`index` AND
                                       data.`iteration` = solution.`iteration`)
                    WHERE `solution`.`iteration` = @0", delegate(IDataReader reader) {
                Records.Solution sol = CreateSolution(reader);

                idmap[sol.Index] = sol;
                ret.Solutions.Add(sol);
                return(true);
            }, iteration);

            if (idmap.ContainsKey(bestId))
            {
                ret.Best = idmap[bestId];
            }

            return(ret);
        }
Esempio n. 2
0
        /* Data retrieval */
        private Records.Solution CreateSolution(IDataReader reader)
        {
            Records.Solution solution = new Records.Solution();

            solution.Index     = As <int>(reader["the_index"]);
            solution.Iteration = As <int>(reader["the_iteration"]);

            for (int i = 0; i < reader.FieldCount; ++i)
            {
                string name = reader.GetName(i);

                object val = reader.GetValue(i);

                if (name.StartsWith("_f_"))
                {
                    solution.Fitness.Add(name.Substring(3), As <double>(val));
                }
                else if (name.StartsWith("_p_"))
                {
                    solution.Parameters.Add(name.Substring(3), As <double>(val));
                }
                else if (name.StartsWith("_d_"))
                {
                    solution.Data.Add(name.Substring(3), As <string>(val));
                }
            }

            solution.FitnessValue = As <double>(reader["fitness"]);
            return(solution);
        }
Esempio n. 3
0
        public Records.Solution ReadSolution(int iteration, int id)
        {
            Records.Solution solution  = null;
            string           condition = "";

            if (iteration >= 0 && id >= 0)
            {
                condition = "`solution`.`iteration` = @0 AND `solution`.`index` = @1";
            }
            else if (iteration >= 0)
            {
                condition = "`solution`.`iteration` = @0";
            }
            else if (id >= 0)
            {
                condition = "`solution`.`index` = @0";
                iteration = id;
            }

            if (condition != "")
            {
                condition = "WHERE " + condition;
            }

            string q = String.Format(@"SELECT solution.iteration AS the_iteration, solution.`index` AS the_index, solution.*, fitness.*, parameter_values.*, data.* FROM solution
                    LEFT JOIN fitness ON (fitness.`index` = solution.`index` AND
                                          fitness.`iteration` = solution.`iteration`)
                    LEFT JOIN parameter_values ON (parameter_values.`index` = solution.`index` AND
                                             parameter_values.`iteration` = solution.`iteration`)
                    LEFT JOIN data ON (data.`index` = solution.`index` AND
                                       data.`iteration` = solution.`iteration`)
                    {0} ORDER BY solution.fitness DESC LIMIT 1", condition);

            Query(q, delegate(IDataReader reader) {
                solution = CreateSolution(reader);
                return(false);
            }, iteration, id);

            return(solution);
        }
        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();
        }
        public virtual void FromStorage(Storage.Storage storage, Storage.Records.Optimizer optimizer, Storage.Records.Solution solution, Optimization.Solution sol)
        {
            sol.Parameters.Clear();

            foreach (KeyValuePair <string, double> parameter in solution.Parameters)
            {
                sol.Parameters.Add(new Parameter(parameter.Key, parameter.Value, Parameter(parameter.Key).Boundary));
            }

            sol.FromStorage(storage, optimizer, solution);
        }
        public virtual void FromStorage(Storage.Storage storage, Storage.Records.Optimizer optimizer, Storage.Records.Solution solution)
        {
            Data.Clear();

            foreach (KeyValuePair <string, string> data in solution.Data)
            {
                Data[data.Key] = data.Value;
            }

            Fitness.Reset();

            foreach (KeyValuePair <string, double> fit in solution.Fitness)
            {
                Fitness.Values[fit.Key] = fit.Value;
            }

            Fitness.Update();
        }