private void AddTargetFunction() { PrintDebugRessourcesBefore("AddTargetFunction"); var factory = new TargetFunctionFactory(solverData); var targetFunction = new GLS.LinearExpr(); targetFunction += factory.CreateTargetFunction(TargetType.MinTime, WaytimeWeight); targetFunction += factory.CreateTargetFunction(TargetType.TryVisitDesired, DesiredWeight); solverData.Solver.Minimize(targetFunction); // constraint target function based on presolved solution if (solverData.Input.Presolved.Length > 0) { var totalTimePresolved = 0; for (int i = 1; i < solverData.Input.Presolved.Length; i++) { totalTimePresolved += solverData.Input.VisitsDuration[Array.IndexOf(solverData.Input.VisitIds, solverData.Input.Presolved[i] - 1)]; totalTimePresolved += solverData.Input.Distances[i > 1 ? Array.IndexOf(solverData.Input.VisitIds, solverData.Input.Presolved[i - 1] - 1) : 0, Array.IndexOf(solverData.Input.VisitIds, solverData.Input.Presolved[i] - 1)]; } totalTimePresolved += solverData.Input.Distances[Array.IndexOf(solverData.Input.VisitIds, solverData.Input.Presolved.Last() - 1), 0]; solver.Add(targetFunction <= totalTimePresolved * WaytimeWeight); } var minWayTime = solverData.Input.Distances.Cast <int>().Where(i => i > 0).Min(); solver.Add(targetFunction >= (minWayTime * (solverData.NumberOfVisits + 1) + solverData.Input.VisitsDuration.Sum()) * (WaytimeWeight - DesiredWeight)); PrintDebugRessourcesAfter(); }
public void Maximize(LinearExpr expr) { Objective().Clear(); Objective().SetMaximization(); Dictionary<Variable, double> coefficients = new Dictionary<Variable, double>(); double constant = expr.Visit(coefficients); foreach (KeyValuePair<Variable, double> pair in coefficients) { Objective().SetCoefficient(pair.Key, pair.Value); } Objective().SetOffset(constant); }
public SumArray(LinearExpr[] array) { this.array_ = array; }
public Sum(LinearExpr left, LinearExpr right) { this.left_ = left; this.right_ = right; }
public SumCst(LinearExpr expr, double coeff) { this.coeff_ = coeff; this.expr_ = expr; }
public ProductCst(LinearExpr expr, double coeff) { this.coeff_ = coeff; this.expr_ = expr; }
public Equality(LinearExpr left, LinearExpr right, bool equality) { this.left_ = left; this.right_ = right; this.equality_ = equality; }
public RangeConstraint(LinearExpr expr, double lb, double ub) { this.expr_ = expr; this.lb_ = lb; this.ub_ = ub; }
static void TestSumArray() { Console.WriteLine("Running TestSumArray"); Solver solver = new Solver("TestSumArray", Solver.CLP_LINEAR_PROGRAMMING); Variable[] x = solver.MakeBoolVarArray(10, "x"); Constraint ct1 = solver.Add(x.Sum() == 3); CheckDoubleEq(ct1.GetCoefficient(x[0]), 1.0, "test1"); Constraint ct2 = solver.Add(-2 * x.Sum() == 3); CheckDoubleEq(ct2.GetCoefficient(x[0]), -2.0, "test2"); LinearExpr[] array = new LinearExpr[] { x[0]+ 2.0, x[0] + 3, x[0] + 4 }; Constraint ct3 = solver.Add(array.Sum() == 1); CheckDoubleEq(ct3.GetCoefficient(x[0]), 3.0, "test3"); CheckDoubleEq(ct3.Lb(), -8.0, "test4"); CheckDoubleEq(ct3.Ub(), -8.0, "test5"); }