Esempio n. 1
0
        public IObjectiveFunction Create(
            IObjectiveFactory objectiveFactory,
            It t,
            IΛ Λ,
            IΡ Ρ,
            IIHat IHat)
        {
            IObjectiveFunction objectiveFunction = null;

            try
            {
                objectiveFunction = new ObjectiveFunction(
                    objectiveFactory,
                    t,
                    Λ,
                    Ρ,
                    IHat);
            }
            catch (Exception exception)
            {
                this.Log.Error("Exception message: " + exception.Message + " and stacktrace " + exception.StackTrace);
            }

            return(objectiveFunction);
        }
        /// <summary>
        ///   Constructs a new quadratic constraint in the form <c>x'Ax + x'b</c>.
        /// </summary>
        /// 
        /// <param name="objective">The objective function to which this constraint refers.</param>
        /// <param name="quadraticTerms">The matrix of <c>A</c> quadratic terms.</param>
        /// <param name="linearTerms">The vector <c>b</c> of linear terms.</param>
        /// <param name="shouldBe">How the left hand side of the constraint should be compared to the given <paramref name="value"/>.</param>
        /// <param name="value">The right hand side of the constraint equation.</param>
        /// <param name="withinTolerance">The tolerance for violations of the constraint. Equality
        ///   constraints should set this to a small positive value. Default is 0.</param>
        ///
        public QuadraticConstraint(IObjectiveFunction objective,
            double[,] quadraticTerms, double[] linearTerms = null,
            ConstraintType shouldBe = ConstraintType.LesserThanOrEqualTo,
            double value = 0, double withinTolerance = 0.0)
        {
            int n = objective.NumberOfVariables;

            if (quadraticTerms == null)
                throw new ArgumentNullException("quadraticTerms");

            if (quadraticTerms.GetLength(0) != quadraticTerms.GetLength(1))
                throw new DimensionMismatchException("quadraticTerms", "Matrix must be square.");

            if (quadraticTerms.GetLength(0) != n)
                throw new DimensionMismatchException("quadraticTerms", 
                    "Matrix rows must match the number of variables in the objective function.");

            if (linearTerms != null)
            {
                if (linearTerms.Length != n)
                    throw new DimensionMismatchException("linearTerms",
                        "The length of the linear terms vector must match the "+
                        "number of variables in the objective function.");
            }
            else
            {
                linearTerms = new double[n];
            }

            this.QuadraticTerms = quadraticTerms;
            this.LinearTerms = linearTerms;

            Create(objective, function, shouldBe, value, gradient, withinTolerance);
        }
Esempio n. 3
0
        /// <summary>
        ///   Constructs a new nonlinear constraint.
        /// </summary>
        ///
        /// <param name="objective">The objective function to which this constraint refers.</param>
        /// <param name="function">A lambda expression defining the left hand side of the
        ///   constraint equation.</param>
        /// <param name="shouldBe">How the left hand side of the constraint should be
        ///   compared to the given <paramref name="value"/>.</param>
        /// <param name="value">The right hand side of the constraint equation. Default is 0.</param>
        ///
        public NonlinearConstraint(IObjectiveFunction objective,
                                   Func <double[], double> function, ConstraintType shouldBe, double value)
        {
            int n = objective.NumberOfVariables;

            this.Create(objective.NumberOfVariables, function, shouldBe, value, null, DEFAULT_TOL);
        }
Esempio n. 4
0
        /// <summary>
        ///   Constructs a new nonlinear constraint.
        /// </summary>
        ///
        /// <param name="objective">The objective function to which this constraint refers.</param>
        ///
        /// <param name="function">A lambda expression defining the left hand side of the constraint equation.</param>
        /// <param name="gradient">A lambda expression defining the gradient of the <paramref name="function">
        /// left hand side of the constraint equation</paramref>.</param>
        /// <param name="shouldBe">How the left hand side of the constraint should be compared to the given <paramref name="value"/>.</param>
        /// <param name="value">The right hand side of the constraint equation.</param>
        /// <param name="withinTolerance">The tolerance for violations of the constraint. Equality
        ///   constraints should set this to a small positive value. Default is 0.</param>
        ///
        ///
        public NonlinearConstraint(IObjectiveFunction objective,
                                   Expression <Func <double> > function, ConstraintType shouldBe, double value,
                                   Expression <Func <double[]> > gradient = null, double withinTolerance = 0.0)
        {
            this.NumberOfVariables = objective.NumberOfVariables;
            this.ShouldBe          = shouldBe;

            // Generate lambda functions
            var func = ExpressionParser.Replace(function, objective.Variables);

            this.Function  = func.Compile();
            this.Value     = value;
            this.Tolerance = withinTolerance;

            if (gradient != null)
            {
                var grad = ExpressionParser.Replace(gradient, objective.Variables);
                this.Gradient = grad.Compile();

                int      n     = NumberOfVariables;
                double[] probe = new double[n];
                double[] g     = Gradient(probe);
                if (g.Length != n)
                {
                    throw new DimensionMismatchException("gradient",
                                                         "The length of the gradient vector must match the number of variables in the objective function.");
                }
            }
        }
        /// <summary>
        /// Test a scaling operation of the high point, and replace it if it is an improvement
        /// </summary>
        /// <param name="scaleFactor"></param>
        /// <param name="errorProfile"></param>
        /// <param name="vertices"></param>
        /// <param name="errorValues"></param>
        /// <param name="objectiveFunction"></param>
        /// <returns></returns>
        static double TryToScaleSimplex(double scaleFactor, ref ErrorProfile errorProfile, Vector <double>[] vertices,
                                        double[] errorValues, IObjectiveFunction objectiveFunction)
        {
            // find the centroid through which we will reflect
            Vector <double> centroid = ComputeCentroid(vertices, errorProfile);

            // define the vector from the centroid to the high point
            Vector <double> centroidToHighPoint = vertices[errorProfile.HighestIndex].Subtract(centroid);

            // scale and position the vector to determine the new trial point
            Vector <double> newPoint = centroidToHighPoint.Multiply(scaleFactor).Add(centroid);

            // evaluate the new point
            objectiveFunction.EvaluateAt(newPoint);
            double newErrorValue = objectiveFunction.Value;

            // if it's better, replace the old high point
            if (newErrorValue < errorValues[errorProfile.HighestIndex])
            {
                vertices[errorProfile.HighestIndex]    = newPoint;
                errorValues[errorProfile.HighestIndex] = newErrorValue;
            }

            return(newErrorValue);
        }
Esempio n. 6
0
 public ObjectiveChecker(IObjectiveFunction objective, Action <IEvaluation> value_checker, Action <IEvaluation> gradient_checker, Action <IEvaluation> hessian_checker)
 {
     this.InnerObjective  = objective;
     this.ValueChecker    = value_checker;
     this.GradientChecker = gradient_checker;
     this.HessianChecker  = hessian_checker;
 }
Esempio n. 7
0
        protected int DoBfgsUpdate(ref ExitCondition currentExitCondition, WolfeLineSearch lineSearcher, ref Matrix <double> inversePseudoHessian, ref Vector <double> lineSearchDirection, ref IObjectiveFunction previousPoint, ref LineSearchResult lineSearchResult, ref IObjectiveFunction candidate, ref Vector <double> step, ref int totalLineSearchSteps, ref int iterationsWithNontrivialLineSearch)
        {
            int iterations;

            for (iterations = 1; iterations < MaximumIterations; ++iterations)
            {
                double startingStepSize;
                double maxLineSearchStep;
                lineSearchDirection = CalculateSearchDirection(ref inversePseudoHessian, out maxLineSearchStep, out startingStepSize, previousPoint, candidate, step);

                try
                {
                    lineSearchResult = lineSearcher.FindConformingStep(candidate, lineSearchDirection, startingStepSize, maxLineSearchStep);
                }
                catch (Exception e)
                {
                    throw new InnerOptimizationException("Line search failed.", e);
                }

                iterationsWithNontrivialLineSearch += lineSearchResult.Iterations > 0 ? 1 : 0;
                totalLineSearchSteps += lineSearchResult.Iterations;

                step          = lineSearchResult.FunctionInfoAtMinimum.Point - candidate.Point;
                previousPoint = candidate;
                candidate     = lineSearchResult.FunctionInfoAtMinimum;

                currentExitCondition = ExitCriteriaSatisfied(candidate, previousPoint, iterations);
                if (currentExitCondition != ExitCondition.None)
                {
                    break;
                }
            }

            return(iterations);
        }
        /// <summary>
        ///   Constructs a new nonlinear constraint.
        /// </summary>
        /// 
        /// <param name="objective">The objective function to which this constraint refers.</param>
        /// <param name="function">A lambda expression defining the left hand side of the 
        ///   constraint equation.</param>
        /// <param name="shouldBe">How the left hand side of the constraint should be 
        ///   compared to the given <paramref name="value"/>.</param>
        /// <param name="value">The right hand side of the constraint equation.</param>
        /// 
        public NonlinearConstraint(IObjectiveFunction objective,
            Func<double[], double> function, ConstraintType shouldBe, double value)
        {
            int n = objective.NumberOfVariables;

            this.Create(objective.NumberOfVariables, function, shouldBe, value, null, 0.0);
        }
Esempio n. 9
0
        protected override Vector <double> CalculateSearchDirection(ref Matrix <double> inversePseudoHessian,
                                                                    out double maxLineSearchStep,
                                                                    out double startingStepSize,
                                                                    IObjectiveFunction previousPoint,
                                                                    IObjectiveFunction candidate,
                                                                    Vector <double> step)
        {
            startingStepSize  = 1.0;
            maxLineSearchStep = double.PositiveInfinity;

            Vector <double> lineSearchDirection;
            var             y = candidate.Gradient - previousPoint.Gradient;

            double sy = step * y;

            inversePseudoHessian = inversePseudoHessian + ((sy + y * inversePseudoHessian * y) / Math.Pow(sy, 2.0)) * step.OuterProduct(step) - ((inversePseudoHessian * y.ToColumnMatrix()) * step.ToRowMatrix() + step.ToColumnMatrix() * (y.ToRowMatrix() * inversePseudoHessian)) * (1.0 / sy);
            lineSearchDirection  = -inversePseudoHessian * candidate.Gradient;

            if (lineSearchDirection * candidate.Gradient >= 0.0)
            {
                lineSearchDirection  = -candidate.Gradient;
                inversePseudoHessian = CreateMatrix.DenseIdentity <double>(candidate.Point.Count);
            }

            return(lineSearchDirection);
        }
Esempio n. 10
0
        /// <summary>
        /// Implementation of the actual code to search for the zeroes of
        /// the <see cref="IObjectiveFunction"/>.
        /// </summary>
        /// <remarks>
        /// It assumes that:
        /// <list type="bullet">
        /// <item>
        ///   <description>
        ///		<see cref="Solver1D.XMin"/> and <see cref="Solver1D.XMax"/> form a valid bracket;
        ///	  </description>
        /// </item>
        /// <item>
        ///   <description>
        ///     <see cref="Solver1D.FXMin"/> and <see cref="Solver1D.FXMax"/> contain the values of the
        ///		function in <see cref="Solver1D.XMin"/> and <see cref="Solver1D.XMax"/>;
        ///	  </description>
        /// </item>
        /// <item>
        ///   <description>
        ///     <see cref="Solver1D.Root"/> was initialized to a valid initial guess.
        ///	  </description>
        /// </item>
        /// </list>
        /// </remarks>
        /// <param name="objectiveFunction">The <see cref="IObjectiveFunction"/>.</param>
        /// <param name="xAccuracy">Given accuracy.</param>
        /// <returns>The zero of the objective function.</returns>
        /// <exception cref="ApplicationException">
        /// Thrown when a bracket is not found after the maximum
        /// number of evaluations (see <see cref="Solver1D.MaxEvaluations"/>).
        /// </exception>
        protected override double SolveImpl(IObjectiveFunction objectiveFunction,
                                            double xAccuracy)
        {
            // Orient the search so that f>0 lies at root_+dx
            double dx;

            if (FXMin < 0.0)
            {
                dx   = XMax - XMin;
                Root = XMin;
            }
            else
            {
                dx   = XMin - XMax;
                Root = XMax;
            }
            while (EvaluationNumber++ < MaxEvaluations)
            {
                dx /= 2.0;
                double xMid = Root + dx;
                double fMid = objectiveFunction.Value(xMid);
                if (fMid <= 0.0)
                {
                    Root = xMid;
                }
                if (Math.Abs(dx) < xAccuracy || fMid == 0.0)
                {
                    return(Root);
                }
            }
            throw new ApplicationException("SlvMaxEval");
        }
        /// <summary>
        /// Searchs for the optimal tour using a localsearch method
        /// </summary>
        /// <param name="objective">The objective function</param>
        /// <param name="initialSolution">The initial tour</param>
        /// <returns></returns>
        protected double Solve(IObjectiveFunction objective, List <int> initialSolution)
        {
            var localSearch = CreateLocalSearchMethod(objective, initialSolution);

            localSearch.Solve();
            return(localSearch.Cost);
        }
Esempio n. 12
0
        public void QuadraticConstraintConstructorTest()
        {
            IObjectiveFunction objective = null;

            double[,] quadraticTerms =
            {
                { 1, 2, 3 },
                { 4, 5, 6 },
                { 7, 8, 9 },
            };

            double[] linearTerms = { 1, 2, 3 };

            objective = new NonlinearObjectiveFunction(3, f => f[0] + f[1] + f[2]);

            QuadraticConstraint target = new QuadraticConstraint(objective,
                                                                 quadraticTerms, linearTerms,
                                                                 ConstraintType.LesserThanOrEqualTo, 0);

            var function = target.Function;
            var gradient = target.Gradient;

            FiniteDifferences fd = new FiniteDifferences(3, function);

            double[][] x =
            {
                new double[] {      1,  2,   3 },
                new double[] {      3,  1,   4 },
                new double[] {     -6,  5,   9 },
                new double[] {     31, 25, 246 },
                new double[] { -0.102,  0,  10 },
            };


            { // Function test
                for (int i = 0; i < x.Length; i++)
                {
                    double expected =
                        (x[i].Multiply(quadraticTerms)).InnerProduct(x[i])
                        + linearTerms.InnerProduct(x[i]);

                    double actual = function(x[i]);

                    Assert.AreEqual(expected, actual, 1e-8);
                }
            }

            { // Gradient test
                for (int i = 0; i < x.Length; i++)
                {
                    double[] expected = fd.Compute(x[i]);
                    double[] actual   = gradient(x[i]);

                    for (int j = 0; j < actual.Length; j++)
                    {
                        Assert.AreEqual(expected[j], actual[j], 1e-8);
                    }
                }
            }
        }
Esempio n. 13
0
        /// <summary>
        ///   Constructs a new linear constraint.
        /// </summary>
        ///
        /// <param name="function">The objective function to which
        ///   this constraint refers to.</param>
        /// <param name="constraint">A <see cref="System.String"/>
        ///   specifying this constraint, such as "ax + b = c".</param>
        ///
        /// <remarks>
        ///   The constraint string is always parsed using
        ///   <see cref="System.Globalization.CultureInfo.InvariantCulture"/>.
        ///   This means numbers should be written using the English format,
        ///   using the dot (.) as the decimal separator.
        /// </remarks>
        ///
        public LinearConstraint(IObjectiveFunction function, string constraint)
        {
            parseString(function, constraint);

            this.Function = compute;
            this.Gradient = gradient;
        }
Esempio n. 14
0
        /// <summary>
        ///   Constructs a new linear constraint.
        /// </summary>
        ///
        /// <param name="function">The objective function to which this
        ///   constraint refers to.</param>
        /// <param name="constraint">A <see cref="Expression{T}"/> specifying
        ///   this constraint in the form of a lambda expression.</param>
        ///
        public LinearConstraint(IObjectiveFunction function, Expression <Func <bool> > constraint)
        {
            parseExpression(function, constraint);

            this.Function = compute;
            this.Gradient = gradient;
        }
Esempio n. 15
0
 /// <summary>
 /// Overloaded constructor
 /// </summary>
 /// <param name="objective">A tour length objectiveulator object</param>
 /// <param name="initialSolution">An initial solution</param>
 /// <param name="swapPattern">A method for swapping the order of cities</param>
 public SteepestDecent(IObjectiveFunction objective, List<int> initialSolution, ICitySwapPattern swapMethod)
 {
     this.objective = objective;
     this.solution = initialSolution;
     this.currentTourLength = objective.Value(initialSolution);
     this.swapPattern = swapMethod;
 }
Esempio n. 16
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="objective">A tour length objectiveulator object</param>
 /// <param name="initialSolution">An initial solution</param>
 public OrdinaryDecent(IObjectiveFunction objective, List<int> initialSolution)
 {
     this.objective = objective;
     this.solution = initialSolution;
     this.currentTourLength = objective.Value(initialSolution);
     this.swapPattern = new TwoCitySwapPattern(initialSolution);
 }
        /// <summary>
        ///   Constructs a new nonlinear constraint.
        /// </summary>
        ///
        /// <param name="objective">The objective function to which this constraint refers.</param>
        /// <param name="function">A lambda expression defining the left hand side of the
        ///   constraint equation.</param>
        /// <param name="gradient">A lambda expression defining the gradient of the <paramref name="function">
        ///   left hand side of the constraint equation</paramref>.</param>
        /// <param name="shouldBe">How the left hand side of the constraint should be
        ///   compared to the given <paramref name="value"/>.</param>
        /// <param name="value">The right hand side of the constraint equation.</param>
        ///
        public NonlinearConstraint(IObjectiveFunction objective,
                                   Func <double[], double> function, ConstraintType shouldBe, double value,
                                   Func <double[], double[]> gradient)
        {
            int n = objective.NumberOfVariables;

            this.Create(objective.NumberOfVariables, function, shouldBe, value, gradient, 0.0);
        }
Esempio n. 18
0
 public ProbabilityMatrix(IPheromoneMatrix pheromoneMatrix, IRouteService routeService, IObjectiveFunction objectiveFunction, IRandomNumberGenerator randomNumberGenerator, IRouteStatisticsService routeStatisticsService)
 {
     _routeService           = routeService;
     _objectiveFunction      = objectiveFunction;
     _randomNumberGenerator  = randomNumberGenerator;
     _routeStatisticsService = routeStatisticsService;
     PheromoneMatrix         = pheromoneMatrix;
 }
Esempio n. 19
0
        /// <summary>
        ///   Constructs a new linear constraint.
        /// </summary>
        ///
        /// <param name="function">The objective function to which
        ///   this constraint refers to.</param>
        /// <param name="constraint">A <see cref="System.String"/>
        ///   specifying this constraint, such as "ax + b = c".</param>
        /// <param name="format">The culture information specifying how
        ///   numbers written in the <paramref name="constraint"/> should
        ///   be parsed. Default is CultureInfo.InvariantCulture.</param>
        ///
        public LinearConstraint(IObjectiveFunction function, string constraint, CultureInfo format)
            : this()
        {
            parseString(function, constraint, format);

            this.Function = compute;
            this.Gradient = gradient;
        }
Esempio n. 20
0
        /// <summary>
        ///   Constructs a new nonlinear constraint.
        /// </summary>
        ///
        /// <param name="objective">The objective function to which this constraint refers.</param>
        /// <param name="function">A lambda expression defining the left hand side of the
        ///   constraint equation.</param>
        /// <param name="gradient">A lambda expression defining the gradient of the <paramref name="function">
        ///   left hand side of the constraint equation.</paramref>.</param>
        ///
        public NonlinearConstraint(IObjectiveFunction objective,
                                   Func <double[], double> function,
                                   Func <double[], double[]> gradient)
        {
            int n = objective.NumberOfVariables;

            this.Create(objective.NumberOfVariables, function,
                        ConstraintType.GreaterThanOrEqualTo, 0.0, gradient, DEFAULT_TOL);
        }
Esempio n. 21
0
 /// <summary>
 ///   Constructs a new nonlinear constraint.
 /// </summary>
 ///
 /// <param name="objective">The objective function to which this constraint refers.</param>
 /// <param name="function">A lambda expression defining the left hand side of the
 ///   constraint equation.</param>
 /// <param name="gradient">A lambda expression defining the gradient of the <paramref name="function">
 ///   left hand side of the constraint equation</paramref>.</param>
 /// <param name="shouldBe">How the left hand side of the constraint should be
 ///   compared to the given <paramref name="value"/>.</param>
 /// <param name="value">The right hand side of the constraint equation.</param>
 ///
 public NonlinearConstraint(
     IObjectiveFunction objective,
     Func <double[], double> function,
     ConstraintType shouldBe,
     double value,
     Func <double[], double[]> gradient
     ) : this(objective, function, shouldBe, value, gradient, 0.0)
 {
 }
Esempio n. 22
0
        /// <summary>
        ///   Constructs a new nonlinear constraint.
        /// </summary>
        ///
        /// <param name="objective">The objective function to which this constraint refers.</param>
        /// <param name="function">A lambda expression defining the left hand side of the constraint equation.</param>
        ///
        public NonlinearConstraint(
            IObjectiveFunction objective,
            Func <double[], double> function
            )
        {
            int n = objective.NumberOfVariables;

            this.Create(objective.NumberOfVariables, function, ConstraintType.GreaterThanOrEqualTo, 0.0, null, 0.0);
        }
 public ForwardDifferenceGradientObjectiveFunction(IObjectiveFunction valueOnlyObj, Vector <double> lowerBound, Vector <double> upperBound, double relativeIncrement = 1e-5, double minimumIncrement = 1e-8)
 {
     InnerObjectiveFunction = valueOnlyObj;
     LowerBound             = lowerBound;
     UpperBound             = upperBound;
     _gradient         = new LinearAlgebra.Double.DenseVector(LowerBound.Count);
     RelativeIncrement = relativeIncrement;
     MinimumIncrement  = minimumIncrement;
 }
Esempio n. 24
0
        public PheromoneMatrix(double initialPheromoneValue, double rho, double q, IObjectiveFunction objectiveFunction)
        {
            InitialPheromoneValue = initialPheromoneValue;
            Rho = rho;
            Q   = q;

            _objectiveFunction = objectiveFunction;
            _pheromoneMatrix   = new Dictionary <Tuple <INode, INode>, double>();
        }
Esempio n. 25
0
 /// <summary>
 ///   Constructs a new nonlinear constraint.
 /// </summary>
 ///
 /// <param name="objective">The objective function to which this constraint refers.</param>
 /// <param name="function">A lambda expression defining the left hand side of the constraint equation.</param>
 /// <param name="gradient">A lambda expression defining the gradient of the <paramref name="function">
 /// left hand side of the constraint equation</paramref>.</param>
 /// <param name="shouldBe">How the left hand side of the constraint should be compared to the given <paramref name="value"/>.</param>
 /// <param name="value">The right hand side of the constraint equation. Default is 0.</param>
 /// <param name="withinTolerance">The tolerance for violations of the constraint. Equality
 ///   constraints should set this to a small positive value. Default is 1e-8.</param>
 ///
 public NonlinearConstraint(IObjectiveFunction objective,
                            Func <double[], double> function,
                            ConstraintType shouldBe            = ConstraintType.GreaterThanOrEqualTo,
                            double value                       = 0,
                            Func <double[], double[]> gradient = null,
                            double withinTolerance             = DEFAULT_TOL)
 {
     this.Create(objective.NumberOfVariables, function, shouldBe, value, gradient, withinTolerance);
 }
 /// <summary>
 /// Evaluate the objective function at each vertex to create a corresponding
 /// list of error values for each vertex
 /// </summary>
 /// <param name="vertices"></param>
 /// <param name="objectiveFunction"></param>
 /// <returns></returns>
 static double[] InitializeErrorValues(Vector <double>[] vertices, IObjectiveFunction objectiveFunction)
 {
     double[] errorValues = new double[vertices.Length];
     for (int i = 0; i < vertices.Length; i++)
     {
         objectiveFunction.EvaluateAt(vertices[i]);
         errorValues[i] = objectiveFunction.Value;
     }
     return(errorValues);
 }
Esempio n. 27
0
 public ProbabilityMatrix(IPheromoneMatrix pheromoneMatrix, IRouteService routeService, IObjectiveFunction objectiveFunction, IRandomNumberGenerator randomNumberGenerator)
 {
     _routeService          = routeService;
     _objectiveFunction     = objectiveFunction;
     _randomNumberGenerator = randomNumberGenerator;
     PheromoneMatrix        = pheromoneMatrix;
     Alpha = 5;
     Beta  = 1;
     Zeta  = 100;
 }
Esempio n. 28
0
        /// <summary>
        /// Finds the minimum of the objective function without an intial pertubation, the default values used
        /// by fminsearch() in Matlab are used instead
        /// http://se.mathworks.com/help/matlab/math/optimizing-nonlinear-functions.html#bsgpq6p-11
        /// </summary>
        /// <param name="objectiveFunction">The objective function, no gradient or hessian needed</param>
        /// <param name="initialGuess">The intial guess</param>
        /// <returns>The minimum point</returns>
        public MinimizationResult FindMinimum(IObjectiveFunction objectiveFunction, Vector <double> initialGuess)
        {
            var initalPertubation = new LinearAlgebra.Double.DenseVector(initialGuess.Count);

            for (int i = 0; i < initialGuess.Count; i++)
            {
                initalPertubation[i] = initialGuess[i] == 0.0 ? 0.00025 : initialGuess[i] * 0.05;
            }
            return(FindMinimum(objectiveFunction, initialGuess, initalPertubation));
        }
Esempio n. 29
0
        protected BaseStopTimeHarmonyGenerator(IObjectiveFunction <StopTimeInfo> function, Graph <int, StopTimeInfo> graph, Location source, Location destination) : base(function)
        {
            Graph       = graph ?? throw new ArgumentNullException(nameof(graph));
            Destination = destination ?? throw new ArgumentNullException(nameof(destination));
            Source      = source ?? throw new ArgumentNullException(nameof(source));

            // Set up source and destination nodes
            ReferentialDestinationStop = graph.GetReferenceDestinationStop(Destination.Name);
            SourceNodes = graph.GetSourceNodes(Source.Name, ReferentialDestinationStop);
        }
        /// <summary>
        /// Finds the minimum of the objective function without an initial perturbation, the default values used
        /// by fminsearch() in Matlab are used instead
        /// http://se.mathworks.com/help/matlab/math/optimizing-nonlinear-functions.html#bsgpq6p-11
        /// </summary>
        /// <param name="objectiveFunction">The objective function, no gradient or hessian needed</param>
        /// <param name="initialGuess">The initial guess</param>
        /// <returns>The minimum point</returns>
        public static MinimizationResult Minimum(IObjectiveFunction objectiveFunction, Vector <double> initialGuess, double convergenceTolerance = 1e-8, int maximumIterations = 1000)
        {
            var initalPertubation = new LinearAlgebra.Double.DenseVector(initialGuess.Count);

            for (int i = 0; i < initialGuess.Count; i++)
            {
                initalPertubation[i] = initialGuess[i] == 0.0 ? 0.00025 : initialGuess[i] * 0.05;
            }
            return(Minimum(objectiveFunction, initialGuess, initalPertubation, convergenceTolerance, maximumIterations));
        }
Esempio n. 31
0
 /// <summary>
 ///   Constructs a new nonlinear constraint.
 /// </summary>
 ///
 /// <param name="objective">The objective function to which this constraint refers.</param>
 /// <param name="function">A lambda expression defining the left hand side of the constraint equation.</param>
 /// <param name="gradient">A lambda expression defining the gradient of the <paramref name="function">
 /// left hand side of the constraint equation</paramref>.</param>
 /// <param name="shouldBe">How the left hand side of the constraint should be compared to the given <paramref name="value"/>.</param>
 /// <param name="value">The right hand side of the constraint equation.</param>
 /// <param name="withinTolerance">The tolerance for violations of the constraint. Equality
 ///   constraints should set this to a small positive value. Default is 0.</param>
 ///
 public NonlinearConstraint(
     IObjectiveFunction objective,
     Func <double[], double> function,
     ConstraintType shouldBe,
     double value,
     Func <double[], double[]> gradient,
     double withinTolerance
     )
 {
     this.Create(objective.NumberOfVariables, function, shouldBe, value, gradient, withinTolerance);
 }
Esempio n. 32
0
        /// <summary>
        ///    Creates a nonlinear constraint.
        /// </summary>
        ///
        protected void Create(IObjectiveFunction objective,
                              Func <double[], double> function, ConstraintType shouldBe, double value,
                              Func <double[], double[]> gradient, double tolerance)
        {
            this.NumberOfVariables = objective.NumberOfVariables;
            this.ShouldBe          = shouldBe;
            this.Value             = value;
            this.Tolerance         = tolerance;

            this.Function = function;
            this.Gradient = gradient;
        }
Esempio n. 33
0
        /// <summary>
        /// This method returns the zero of the <see cref="IObjectiveFunction"/>
        /// <pramref name="objectiveFunction"/>, determined with the given accuracy.
        /// </summary>
        /// <remarks>
        /// <i>x</i> is considered a zero if <i>|f(x)| &lt; accuracy</i>.
        /// An initial guess must be supplied, as well as two values which
        /// must bracket the zero (i.e., either
        /// <i>f(x<sub>min</sub>) &gt; 0</i> &amp;&amp;
        /// <i>f(x<sub>max</sub>) &lt; 0</i>, or
        /// <i>f(x<sub>min</sub>) &lt; 0</i> &amp;&amp;
        /// <i>f(x<sub>max</sub>) &gt; 0</i> must be true).
        /// </remarks>
        /// <param name="objectiveFunction">The <see cref="IObjectiveFunction"/>.</param>
        /// <param name="xAccuracy">Given accuracy.</param>
        /// <param name="guess">Initial guess used to scan the range
        /// of the possible bracketing values.</param>
        /// <param name="xMin">Lower bracket.</param>
        /// <param name="xMax">Upper bracket.</param>
        /// <returns>The zero of the objective function.</returns>
        /// <exception cref="ApplicationException">
        /// Thrown when a bracket is not found after the maximum
        /// number of evaluations (see <see cref="MaxEvaluations"/>).
        /// </exception>
        public double Solve(IObjectiveFunction objectiveFunction, double xAccuracy,
                            double guess, double xMin, double xMax)
        {
            #region Check that input data is valid

            if (xMin >= xMax)
            {
                throw new ArgumentException($"Solver minimum bracket ({xMin}) must be below maximum bracket ({xMax}).");
            }
            if (LowerBoundEnforced && xMin < LowerBound)
            {
                throw new ArgumentException(
                          $"Solver minimum bracket ({xMin}) is less than lower bound ({LowerBound}).");
            }
            if (UpperBoundEnforced && xMax > _upperBound)
            {
                throw new ArgumentException(
                          $"Solver maximum bracket ({xMax}) is more than upper bound ({_upperBound}).");
            }
            // Check if the guess is within specified range (xMin, xMax)
            //
            if (guess < xMin)
            {
                throw new ApplicationException($"Solver guess ({guess}) is less than minimum bracket ({xMin}).");
            }
            if (guess > xMax)
            {
                throw new ApplicationException($"Solver guess ({guess}) is more than maximum bracket ({xMax}).");
            }

            #endregion

            XMin  = xMin;
            XMax  = xMax;
            FXMin = objectiveFunction.Value(xMin);
            if (Math.Abs(FXMin) < xAccuracy)
            {
                return(xMin);
            }
            FXMax = objectiveFunction.Value(xMax);
            if (Math.Abs(FXMax) < xAccuracy)
            {
                return(xMax);
            }
            EvaluationNumber = 2;
            if (FXMin * FXMax >= 0.0)
            {
                throw new ApplicationException(
                          $"Solver bounds must return different values with different signs; it returned {FXMin} and {FXMax}.");
            }
            Root = guess;
            return(SolveImpl(objectiveFunction, Math.Max(Math.Abs(xAccuracy), Double.Epsilon)));
        }
Esempio n. 34
0
        /// <summary>
        ///   Constructs a new nonlinear constraint.
        /// </summary>
        ///
        /// <param name="objective">The objective function to which this constraint refers.</param>
        /// <param name="constraint">A boolean lambda expression expressing the constraint. Please
        ///   see examples for details.</param>
        /// <param name="gradient">A lambda expression defining the gradient of the <paramref name="constraint">
        /// left hand side of the constraint equation</paramref>.</param>
        ///
        public NonlinearConstraint(IObjectiveFunction objective,
                                   Expression <Func <double[], bool> > constraint,
                                   Func <double[], double[]> gradient = null)
        {
            Func <double[], double> function;
            ConstraintType          shouldBe;
            double value;

            parse(constraint, out function, out shouldBe, out value);

            this.Create(objective.NumberOfVariables, function, shouldBe, value, gradient, DEFAULT_TOL);
        }
        /// <summary>
        ///   Constructs a new nonlinear constraint.
        /// </summary>
        /// 
        /// <param name="objective">The objective function to which this constraint refers.</param>
        /// <param name="function">A lambda expression defining the left hand side of the constraint equation.</param>
        /// 
        public NonlinearConstraint(IObjectiveFunction objective, Func<double[], double> function)
        {
            int n = objective.NumberOfVariables;

            this.Create(objective.NumberOfVariables, function, ConstraintType.GreaterThanOrEqualTo, 0.0, null, 0.0);
        }
 /// <summary>
 /// Searchs for the optimal tour using a localsearch method 
 /// </summary>
 /// <param name="objective">The objective function</param>
 /// <param name="initialSolution">The initial tour</param>
 /// <returns></returns>
 protected double Solve(IObjectiveFunction objective, List<int> initialSolution)
 {
     var localSearch = CreateLocalSearchMethod(objective, initialSolution);
     localSearch.Solve();
     return localSearch.Cost;
 }
Esempio n. 37
0
 /// <summary>
 ///   Constructs a new linear constraint.
 /// </summary>
 /// 
 /// <param name="function">The objective function to which
 ///   this constraint refers to.</param>
 /// <param name="constraint">A <see cref="System.String"/> 
 ///   specifying this constraint, such as "ax + b = c".</param>
 /// 
 /// <remarks>
 ///   The constraint string is always parsed using 
 ///   <see cref="System.Globalization.CultureInfo.InvariantCulture"/>.
 ///   This means numbers should be written using the English format, 
 ///   using the dot (.) as the decimal separator.
 /// </remarks>
 /// 
 public LinearConstraint(IObjectiveFunction function, string constraint)
 {
     parseString(function, constraint);
 }
Esempio n. 38
0
 /// <summary>
 ///   Constructs a new linear constraint.
 /// </summary>
 /// 
 /// <param name="function">The objective function to which this 
 ///   constraint refers to.</param>
 /// <param name="constraint">A <see cref="Expression{T}"/> specifying
 ///   this constraint in the form of a lambda expression.</param>
 /// 
 public LinearConstraint(IObjectiveFunction function, Expression<Func<bool>> constraint)
 {
     parseExpression(function, constraint);
 }
        private void parseExpression(IObjectiveFunction function, Expression<Func<bool>> constraint)
        {
            ConstraintType type;

            switch (constraint.Body.NodeType)
            {
                case ExpressionType.Equal:
                    type = ConstraintType.EqualTo; break;

                case ExpressionType.LessThanOrEqual:
                    type = ConstraintType.LesserThanOrEqualTo; break;

                case ExpressionType.GreaterThanOrEqual:
                    type = ConstraintType.GreaterThanOrEqualTo; break;

                default:
                    throw new FormatException("Unexpected expression.");
            }

            BinaryExpression b = constraint.Body as BinaryExpression;
            var terms = new Dictionary<string, double>();
            double value = 0;
            parse(terms, b.Left, ref value);

            ConstantExpression c = b.Right as ConstantExpression;
            value = (double)c.Value - value;

            List<int> indices = new List<int>();
            List<double> scalars = new List<double>();

            foreach (var term in terms)
            {
                indices.Add(function.Variables[term.Key]);
                scalars.Add(term.Value);
            }

            NumberOfVariables = indices.Count;
            VariablesAtIndices = indices.ToArray();
            CombinedAs = scalars.ToArray();
            ShouldBe = type;
            Value = value;
        }
Esempio n. 40
0
        /// <summary>
        ///   Constructs a new nonlinear constraint.
        /// </summary>
        /// 
        /// <param name="objective">The objective function to which this constraint refers.</param>
        /// <param name="function">A lambda expression defining the left hand side of the 
        ///   constraint equation.</param>
        /// <param name="gradient">A lambda expression defining the gradient of the <paramref name="function">
        ///   left hand side of the constraint equation</paramref>.</param>
        /// <param name="shouldBe">How the left hand side of the constraint should be 
        ///   compared to the given <paramref name="value"/>.</param>
        /// <param name="value">The right hand side of the constraint equation. Default is 0.</param>
        /// 
        public NonlinearConstraint(IObjectiveFunction objective,
            Func<double[], double> function, ConstraintType shouldBe, double value,
            Func<double[], double[]> gradient)
        {
            int n = objective.NumberOfVariables;

            this.Create(objective.NumberOfVariables, function, shouldBe, value, gradient, DEFAULT_TOL);
        }
        /// <summary>
        ///   Constructs a new linear constraint.
        /// </summary>
        /// 
        /// <param name="function">The objective function to which this 
        ///   constraint refers to.</param>
        /// <param name="constraint">A <see cref="Expression{T}"/> specifying
        ///   this constraint in the form of a lambda expression.</param>
        /// 
        public LinearConstraint(IObjectiveFunction function, Expression<Func<bool>> constraint)
        {
            parseExpression(function, constraint);

            this.Function = compute;
            this.Gradient = gradient;
        }
 /// <summary>
 /// TO BE encapsulated elsewhere....
 /// </summary>
 /// <param name="objective"></param>
 /// <param name="initialSolution"></param>
 /// <returns></returns>
 private ILocalSearchMethod CreateLocalSearchMethod(IObjectiveFunction objective, List<int> initialSolution)
 {
     if (this.options.TourSearchMethod == SearchMethod.OrdinaryDecent)
     {
         return new OrdinaryDecent(objective, initialSolution);
     }
     else if (this.options.TourSearchMethod == SearchMethod.SteepestDecent)
     {
         return new SteepestDecent(objective, initialSolution);
     }
     else
     {
         return new BruteForceSearch(objective, initialSolution);
     }
 }
 /// <summary>
 ///   Attempts to create a <see cref="LinearConstraint"/>
 ///   from a <see cref="System.String"/> representation.
 /// </summary>
 /// 
 /// <param name="str">The string containing the constraint in textual form.</param>
 /// <param name="function">The objective function to which this constraint refers to.</param>
 /// <param name="constraint">The resulting constraint, if it could be parsed.</param>
 /// 
 /// <returns><c>true</c> if the function could be parsed
 ///   from the string, <c>false</c> otherwise.</returns>
 /// 
 public static bool TryParse(string str,
     IObjectiveFunction function, out LinearConstraint constraint)
 {
     return TryParse(str, CultureInfo.InvariantCulture, function, out constraint);
 }
        private void parseString(IObjectiveFunction function, string constraint, CultureInfo culture)
        {
            if (String.IsNullOrEmpty(constraint))
                throw new FormatException("Constraint is empty.");

            string f = constraint.Replace("*", String.Empty).Replace(" ", String.Empty);

            if (f[0] != '-' || f[0] != '+')
                f = f.Insert(0, "+");

            ConstraintType type;

            string lhs, rhs;

            if (f.Contains(">="))
                type = ConstraintType.GreaterThanOrEqualTo;
            else if (f.Contains("<="))
                type = ConstraintType.LesserThanOrEqualTo;
            else if (f.Contains("="))
                type = ConstraintType.EqualTo;
            else
                throw new FormatException("Invalid constraint type.");

            var terms = new Dictionary<string, double>();

            string separator = culture.NumberFormat.NumberDecimalSeparator;

            Regex r = new Regex(@"[\-\+][\s]*(\d*\" + separator + @"{0,1}\d+)?[\s]*([a-zA-Z])*");
            Regex number = new Regex(@"\d*\" + separator + @"{0,1}\d+");
            Regex symbol = new Regex(@"[a-zA-Z]");
            Regex comp = new Regex(@"(>=|<=|=)");

            var sides = comp.Split(f);
            lhs = sides[0];
            rhs = sides[2];

            double value = Double.Parse(rhs, culture);

            MatchCollection matches = r.Matches(lhs, 0);

            foreach (Match m in matches)
            {
                string term = m.Value;

                double scalar = (term[0] == '-') ? -1 : 1;

                // Extract value
                MatchCollection coeff = number.Matches(term);

                foreach (Match c in coeff)
                    scalar *= Double.Parse(c.Value, culture);

                // Extract symbols
                MatchCollection symbols = symbol.Matches(term);

                if (symbols.Count == 1)
                {
                    terms.Add(symbols[0].Value, scalar);
                }
                else
                {
                    if (coeff.Count == 1)
                        value -= scalar;
                    else if (term != "+")
                        throw new FormatException("Unexpected expression.");
                }
            }

            List<int> indices = new List<int>();
            List<double> scalars = new List<double>();

            foreach (var term in terms)
            {
                indices.Add(function.Variables[term.Key]);
                scalars.Add(term.Value);
            }

            NumberOfVariables = indices.Count;
            VariablesAtIndices = indices.ToArray();
            CombinedAs = scalars.ToArray();
            ShouldBe = type;
            Value = value;
        }
Esempio n. 45
0
        /// <summary>
        ///   Constructs a new nonlinear constraint.
        /// </summary>
        /// 
        /// <param name="objective">The objective function to which this constraint refers.</param>
        /// 
        /// <param name="function">A lambda expression defining the left hand side of the constraint equation.</param>
        /// <param name="gradient">A lambda expression defining the gradient of the <paramref name="function">
        /// left hand side of the constraint equation</paramref>.</param>
        /// <param name="shouldBe">How the left hand side of the constraint should be compared to the given <paramref name="value"/>.</param>
        /// <param name="value">The right hand side of the constraint equation.</param>
        /// 
        public NonlinearConstraint(IObjectiveFunction objective,
            Func<double[], double> function,
            ConstraintType shouldBe, double value,
            Func<double[], double[]> gradient = null)
        {
            this.NumberOfVariables = objective.NumberOfVariables;
            this.ShouldBe = shouldBe;
            this.Value = value;

            this.Function = function;
            this.Gradient = gradient;
        }
Esempio n. 46
0
        /// <summary>
        ///    Creates a nonlinear constraint.
        /// </summary>
        /// 
        protected void Create(IObjectiveFunction objective,
            Func<double[], double> function, ConstraintType shouldBe, double value,
            Func<double[], double[]> gradient, double tolerance)
        {
            this.NumberOfVariables = objective.NumberOfVariables;
            this.ShouldBe = shouldBe;
            this.Value = value;
            this.Tolerance = tolerance;

            this.Function = function;
            this.Gradient = gradient;
        }
Esempio n. 47
0
        /// <summary>
        ///   Constructs a new nonlinear constraint.
        /// </summary>
        /// 
        /// <param name="objective">The objective function to which this constraint refers.</param>
        /// 
        /// <param name="function">A lambda expression defining the left hand side of the constraint equation.</param>
        /// <param name="gradient">A lambda expression defining the gradient of the <paramref name="function">
        /// left hand side of the constraint equation</paramref>.</param>
        /// <param name="shouldBe">How the left hand side of the constraint should be compared to the given <paramref name="value"/>.</param>
        /// <param name="value">The right hand side of the constraint equation.</param>
        /// 
        /// 
        public NonlinearConstraint(IObjectiveFunction objective,
            Expression<Func<double>> function,
            ConstraintType shouldBe, double value,
            Expression<Func<double[]>> gradient = null)
        {
            this.NumberOfVariables = objective.NumberOfVariables;
            this.ShouldBe = shouldBe;

            // Generate lambda functions
            var func = ExpressionParser.Replace(function, objective.Variables);
            this.Function = func.Compile();
            this.Value = value;

            if (gradient != null)
            {
                var grad = ExpressionParser.Replace(gradient, objective.Variables);
                this.Gradient = grad.Compile();
            }
        }
        /// <summary>
        ///   Attempts to create a <see cref="LinearConstraint"/>
        ///   from a <see cref="System.String"/> representation.
        /// </summary>
        /// 
        /// <param name="str">The string containing the constraint in textual form.</param>
        /// <param name="function">The objective function to which this constraint refers to.</param>
        /// <param name="constraint">The resulting constraint, if it could be parsed.</param>
        /// <param name="culture">The culture information specifying how
        ///   numbers written in the <paramref name="constraint"/> should
        ///   be parsed. Default is CultureInfo.InvariantCulture.</param>
        /// 
        /// <returns><c>true</c> if the function could be parsed
        ///   from the string, <c>false</c> otherwise.</returns>
        /// 
        public static bool TryParse(string str, CultureInfo culture,
            IObjectiveFunction function, out LinearConstraint constraint)
        {
            // TODO: implement this method without the try-catch block.

            try
            {
                constraint = new LinearConstraint(function, str, culture);
            }
            catch (FormatException)
            {
                constraint = null;
                return false;
            }

            return true;
        }
Esempio n. 49
0
        /// <summary>
        ///   Constructs a new nonlinear constraint.
        /// </summary>
        /// 
        /// <param name="objective">The objective function to which this constraint refers.</param>
        /// 
        /// <param name="function">A lambda expression defining the left hand side of the constraint equation.</param>
        /// <param name="gradient">A lambda expression defining the gradient of the <paramref name="function">
        /// left hand side of the constraint equation</paramref>.</param>
        /// <param name="shouldBe">How the left hand side of the constraint should be compared to the given <paramref name="value"/>.</param>
        /// <param name="value">The right hand side of the constraint equation.</param>
        /// <param name="withinTolerance">The tolerance for violations of the constraint. Equality
        ///   constraints should set this to a small positive value. Default is 0.</param>
        /// 
        public NonlinearConstraint(IObjectiveFunction objective,
            Func<double[], double> function, ConstraintType shouldBe, double value,
            Func<double[], double[]> gradient = null, double withinTolerance = 0.0)
        {
            int n = objective.NumberOfVariables;

            if (gradient != null)
            {
                double[] probe = new double[n];
                double[] g = gradient(probe);
                if (g.Length != n) throw new DimensionMismatchException("gradient",
                    "The length of the gradient vector must match the number of variables in the objective function.");
            }

            this.Create(objective, function, shouldBe, value, gradient, withinTolerance);
        }
Esempio n. 50
0
        /// <summary>
        ///   Constructs a new nonlinear constraint.
        /// </summary>
        /// 
        /// <param name="objective">The objective function to which this constraint refers.</param>
        /// <param name="function">A lambda expression defining the left hand side of the 
        ///   constraint equation.</param>
        /// <param name="gradient">A lambda expression defining the gradient of the <paramref name="function">
        ///   left hand side of the constraint equation.</paramref>.</param>
        /// 
        public NonlinearConstraint(IObjectiveFunction objective,
            Func<double[], double> function,
            Func<double[], double[]> gradient)
        {
            int n = objective.NumberOfVariables;

            this.Create(objective.NumberOfVariables, function,
                ConstraintType.GreaterThanOrEqualTo, 0.0, gradient, DEFAULT_TOL);
        }
Esempio n. 51
0
        /// <summary>
        ///   Constructs a new nonlinear constraint.
        /// </summary>
        /// 
        /// <param name="objective">The objective function to which this constraint refers.</param>
        /// <param name="function">A lambda expression defining the left hand side of the constraint equation.</param>
        /// <param name="gradient">A lambda expression defining the gradient of the <paramref name="function">
        ///   left hand side of the constraint equation</paramref>.</param>
        /// <param name="shouldBe">How the left hand side of the constraint 
        ///   should be compared to the given <paramref name="value"/>.</param>
        /// <param name="value">The right hand side of the constraint equation.</param>
        /// <param name="withinTolerance">The tolerance for violations of the constraint. Equality
        ///   constraints should set this to a small positive value. Default is 1e-8.</param>
        /// 
        public NonlinearConstraint(IObjectiveFunction objective,
            Expression<Func<double>> function, ConstraintType shouldBe, double value,
            Expression<Func<double[]>> gradient = null, double withinTolerance = DEFAULT_TOL)
        {
            this.NumberOfVariables = objective.NumberOfVariables;
            this.ShouldBe = shouldBe;

            // Generate lambda functions
            var func = ExpressionParser.Replace(function, objective.Variables);
            this.Function = func.Compile();
            this.Value = value;
            this.Tolerance = withinTolerance;

            if (gradient != null)
            {
                var grad = ExpressionParser.Replace(gradient, objective.Variables);
                this.Gradient = grad.Compile();

                int n = NumberOfVariables;
                double[] probe = new double[n];
                double[] g = Gradient(probe);

                if (g.Length != n)
                {
                    throw new DimensionMismatchException("gradient",
                    "The length of the gradient vector must match the number of variables in the objective function.");
                }
            }
        }
        /// <summary>
        ///   Constructs a new linear constraint.
        /// </summary>
        /// 
        /// <param name="function">The objective function to which
        ///   this constraint refers to.</param>
        /// <param name="constraint">A <see cref="System.String"/> 
        ///   specifying this constraint, such as "ax + b = c".</param>
        /// <param name="format">The culture information specifying how
        ///   numbers written in the <paramref name="constraint"/> should
        ///   be parsed. Default is CultureInfo.InvariantCulture.</param>
        /// 
        public LinearConstraint(IObjectiveFunction function, string constraint, CultureInfo format)
        {
            parseString(function, constraint, format);

            this.Function = compute;
            this.Gradient = gradient;
        }
Esempio n. 53
0
        /// <summary>
        ///   Constructs a new nonlinear constraint.
        /// </summary>
        /// 
        /// <param name="objective">The objective function to which this constraint refers.</param>
        /// <param name="constraint">A boolean lambda expression expressing the constraint. Please 
        ///   see examples for details.</param>
        /// 
        public NonlinearConstraint(IObjectiveFunction objective, 
            Expression<Func<double[], bool>> constraint)
        {
            int n = objective.NumberOfVariables;

            Func<double[], double> function;
            ConstraintType shouldBe;
            double value;

            parse(constraint, out function, out shouldBe, out value);

            this.Create(objective.NumberOfVariables, function, shouldBe, value, null, DEFAULT_TOL);
        }
 /// <summary>
 ///   Constructs a new linear constraint.
 /// </summary>
 /// 
 /// <param name="function">The objective function to which
 ///   this constraint refers to.</param>
 /// <param name="constraint">A <see cref="System.String"/> 
 ///   specifying this constraint, such as "ax + b = c".</param>
 /// 
 public LinearConstraint(IObjectiveFunction function, string constraint)
     : this(function, constraint, CultureInfo.InvariantCulture)
 {
 }
Esempio n. 55
0
 /// <summary>
 ///   Constructs a new nonlinear constraint.
 /// </summary>
 /// 
 /// <param name="objective">The objective function to which this constraint refers.</param>
 /// <param name="function">A lambda expression defining the left hand side of the constraint equation.</param>
 /// <param name="gradient">A lambda expression defining the gradient of the <paramref name="function">
 /// left hand side of the constraint equation</paramref>.</param>
 /// <param name="shouldBe">How the left hand side of the constraint should be compared to the given <paramref name="value"/>.</param>
 /// <param name="value">The right hand side of the constraint equation. Default is 0.</param>
 /// <param name="withinTolerance">The tolerance for violations of the constraint. Equality
 ///   constraints should set this to a small positive value. Default is 1e-8.</param>
 /// 
 public NonlinearConstraint(IObjectiveFunction objective,
     Func<double[], double> function, ConstraintType shouldBe, double value,
     Func<double[], double[]> gradient, double withinTolerance = DEFAULT_TOL)
 {
     this.Create(objective.NumberOfVariables, function, shouldBe, value, gradient, withinTolerance);
 }
Esempio n. 56
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="objective">A tour length objectiveulator object</param>
 /// <param name="initialSolution">An initial solution</param>
 public BruteForceSearch(IObjectiveFunction objective, List<int> initialSolution)
 {
     this.objective = objective;
     this.solution = initialSolution;
     this.currentTourLength = objective.Value(initialSolution);
 }