Ejemplo 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);
        }
Ejemplo n.º 2
0
        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();
        }