Exemple #1
0
        public override void Solve()
        {
            file = new StreamWriter("Log.txt");

            result = new Result(problem);
            Mv     = new double[problem.Constraints.Count + 1 + 1];

            problems = new SortedList <double, InternalProblem>();
            AddProblem(0, Double.NegativeInfinity);
            do
            {
                int             bestProblemIndex = FindBestProblem();
                InternalProblem bestProblem      = problems.Values[bestProblemIndex];

                MakeIteration(ref bestProblem);

                problems[problems.Values[bestProblemIndex].Y0] = bestProblem;
                foreach (Trial trial in problems.Values[0].Trials.Values)
                {
                    if (trial.Y[0] == bestProblem.Y0)
                    {
                        trial.Evals[0] = bestProblem.BestValue;
                    }
                }
            }while ((result.Iterations <= (int)options.GetValue("MaxIters")) && !problems.Values[0].IsStop);

            file.Close();
        }
Exemple #2
0
        private void MakeIteration(ref InternalProblem internalProblem)
        {
            double y = 0.0;
            {
                Trial Tt  = internalProblem.Trials.Values[internalProblem.T];
                Trial Tt1 = internalProblem.Trials.Values[internalProblem.T - 1];

                if (Tt.Y[0] - Tt1.Y[0] <= (double)options.GetValue("Epsilon"))
                {
                    internalProblem.IsStop = true;
                }

                double Zt  = (internalProblem.T == internalProblem.Trials.Count - 1) ? 0 : Tt.Evals[Tt.Index - 1];
                double Zt1 = (internalProblem.T == 1) ? 0 : Tt1.Evals[Tt1.Index - 1];

                double R = (double)options.GetValue("R");

                if (Tt.Index != Tt1.Index)
                {
                    y = (Tt.Y[0] + Tt1.Y[0]) / 2;
                }
                else
                {
                    y = (Tt.Y[0] + Tt1.Y[0]) / 2 - (Zt - Zt1) / (2 * R * Mv[Tt.Index]);
                }
            }

            Trial trial = MakeTrial(Double.IsInfinity(internalProblem.Y0) ? 0 : 1,
                                    internalProblem.Y0, y, internalProblem.Functions);

            if (trial.Index == internalProblem.Functions.Count)
            {
                if (trial.Evals[internalProblem.Functions.Count - 1] < internalProblem.BestValue)
                {
                    internalProblem.BestValue = trial.Evals[internalProblem.Functions.Count - 1];
                }
            }
            internalProblem.Trials.Add(y, trial);
        }
Exemple #3
0
        private void AddProblem(int dim, double y0)
        {
            InternalProblem internalProblem = new InternalProblem();

            internalProblem.Y0 = y0;

            internalProblem.Functions = new List <FunctionDelegate>();
            if (dim == 0)
            {
                internalProblem.Functions.Add(delegate(double[] args)
                {
                    AddProblem(1, args[0]);
                    return(problems[args[0]].BestValue);
                });
            }
            else
            {
                if (problem.Constraints.Count >= 1)
                {
                    internalProblem.Functions.Add(delegate(double[] args)
                    {
                        return(problem.Constraints[0](new double[] { y0, args[0] }));
                    });
                }
                if (problem.Constraints.Count >= 2)
                {
                    internalProblem.Functions.Add(delegate(double[] args)
                    {
                        return(problem.Constraints[1](new double[] { y0, args[0] }));
                    });
                }
                if (problem.Constraints.Count >= 3)
                {
                    internalProblem.Functions.Add(delegate(double[] args)
                    {
                        return(problem.Constraints[2](new double[] { y0, args[0] }));
                    });
                }
                internalProblem.Functions.Add(delegate(double[] args)
                {
                    return(problem.TargetFunction(new double[] { y0, args[0] }));
                });
            }

            internalProblem.Trials = new SortedList <double, Trial>();
            Trial trial = new Trial();

            trial.Index = 0;
            trial.Y     = new double[1];
            trial.Y[0]  = 0.0;
            internalProblem.Trials.Add(0.0, trial);

            trial       = new Trial();
            trial.Index = 0;
            trial.Y     = new double[1];
            trial.Y[0]  = 1.0;
            internalProblem.Trials.Add(1.0, trial);

            trial = MakeTrial(dim, y0, 0.5, internalProblem.Functions);
            internalProblem.Trials.Add(0.5, trial);

            internalProblem.BestValue = internalProblem.Trials[0.5].Evals[internalProblem.Trials[0.5].Index - 1];
            internalProblem.IsStop    = false;
            internalProblem.MaxR      = 1.0;
            problems.Add(y0, internalProblem);
        }
Exemple #4
0
        private void CalculateRv()
        {
            CalculateMv();

            for (int k = 0; k < problems.Count; k++)
            {
                InternalProblem internalProblem = problems.Values[k];

                List <int>[] Iv = new List <int> [internalProblem.Functions.Count + 1];
                for (int i = 0; i < internalProblem.Functions.Count + 1; i++)
                {
                    Iv[i] = new List <int>();
                }
                int V = Int32.MinValue;
                for (int i = 0; i < internalProblem.Trials.Count; i++)
                {
                    Iv[internalProblem.Trials.Values[i].Index].Add(i);
                    if (internalProblem.Trials.Values[i].Index > V)
                    {
                        V = internalProblem.Trials.Values[i].Index;
                    }
                }

                double[] Zv = new double[internalProblem.Functions.Count + 1];
                for (int v = 1; v <= internalProblem.Functions.Count; v++)
                {
                    if (v < V)
                    {
                        Zv[v] = 0.0;
                    }
                    else
                    {
                        Zv[v] = Double.MaxValue;
                        for (int i = 0; i < Iv[v].Count; i++)
                        {
                            Trial  Ti   = internalProblem.Trials.Values[Iv[v][i]];
                            double temp = Ti.Evals[Ti.Index - 1];
                            if (temp < Zv[v])
                            {
                                Zv[v] = temp;
                            }
                        }
                    }
                }

                double[] Rv = new double[internalProblem.Trials.Count - 1];
                for (int i = 1; i < internalProblem.Trials.Count; i++)
                {
                    Trial Ti  = internalProblem.Trials.Values[i];
                    Trial Ti1 = internalProblem.Trials.Values[i - 1];

                    double Zi  = (i == internalProblem.Trials.Count - 1) ? 0 : Ti.Evals[Ti.Index - 1];
                    double Zi1 = (i == 1) ? 0 : Ti1.Evals[Ti1.Index - 1];

                    double D = Ti.Y[0] - Ti1.Y[0];
                    double R = (double)options.GetValue("R");

                    if (Ti.Index < Ti1.Index)
                    {
                        Rv[i - 1] = 2 * D - 4 * (Zi1 - Zv[Ti1.Index]) / (Mv[Ti1.Index] * R);
                    }
                    if (Ti.Index > Ti1.Index)
                    {
                        Rv[i - 1] = 2 * D - 4 * (Zi - Zv[Ti.Index]) / (Mv[Ti.Index] * R);
                    }
                    if (Ti.Index == Ti1.Index)
                    {
                        Rv[i - 1] = D + (Zi - Zi1) * (Zi - Zi1) / (D * R * R * Mv[Ti.Index] * Mv[Ti.Index]) -
                                    2 * (Zi + Zi1 - 2 * Zv[Ti.Index]) / (R * Mv[Ti.Index]);
                    }
                }

                internalProblem.T    = 1;
                internalProblem.MaxR = Rv[0];
                for (int i = 1; i < internalProblem.Trials.Count; i++)
                {
                    if (Rv[i - 1] > Rv[internalProblem.T - 1])
                    {
                        internalProblem.T    = i;
                        internalProblem.MaxR = Rv[i - 1];
                    }
                }

                problems[internalProblem.Y0] = internalProblem;
            }
        }