Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
ModelBuilderTests.cs
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14using System;
15using System.Collections.Generic;
16using Xunit;
18
20{
21public class ModelBuilderTest
22{
23
24 [Fact]
25 public void BasicApiTest()
26 {
27 Model model = new Model();
28 Variable v1 = model.NewIntVar(-10, 10, "v1");
29 Variable v2 = model.NewIntVar(-10, 10, "v2");
30 Variable v3 = model.NewIntVar(-100000, 100000, "v3");
31 model.AddLinearConstraint(v1 + v2, -1000000, 100000);
32 model.AddLinearConstraint(v1 + 2 * v2 - v3, 0, 100000);
33 model.Maximize(v3);
34
35 Solver solver = new Solver("scip");
36 if (!solver.SolverIsSupported())
37 {
38 return;
39 }
40 SolveStatus status = solver.Solve(model);
41 Assert.Equal(SolveStatus.OPTIMAL, status);
42
43 Assert.Equal(30, solver.ObjectiveValue);
44 Assert.Equal(10, solver.Value(v1));
45 Assert.Equal(10, solver.Value(v2));
46 Assert.Equal(30, solver.Value(v3));
47 }
48
49 [Fact]
51 {
52 Model model = new Model();
53 model.Name = "minimal enforced linear test";
54 double infinity = double.PositiveInfinity;
55 Variable x = model.NewNumVar(0.0, infinity, "x");
56 Variable y = model.NewNumVar(0.0, infinity, "y");
57 Variable z = model.NewBoolVar("z");
58
59 Assert.Equal(3, model.VariablesCount());
60
61 EnforcedLinearConstraint c0 = model.AddEnforced(x + 2 * y >= 10.0, z, false);
62 Assert.Equal(1, model.ConstraintsCount());
63 Assert.Equal(10.0, c0.LowerBound);
64 Assert.Equal(infinity, c0.UpperBound);
65 Assert.Equal(c0.IndicatorVariable.Index, z.Index);
66 Assert.False(c0.IndicatorValue);
67 }
68}
69
70} // namespace Google.OrTools.Tests
Wrapper around an enforced linear constraint stored in the ModelBuilderHelper instance.
double UpperBound
The upper bound of the constraint.
Variable IndicatorVariable
The indicator variable of the constraint.
double LowerBound
The lower bound of the constraint.
bool IndicatorValue
The indicator value of the constraint.
int ConstraintsCount()
Returns the number of constraints in the model.
Variable NewBoolVar(String name)
Creates a bool variable with the given name.
void Maximize(LinearExpr obj)
Maximize expression.
EnforcedLinearConstraint AddEnforced(BoundedLinearExpression lin, Variable iVar, bool iValue)
Adds an enforced Linear constraint to the model.
Variable NewIntVar(double lb, double ub, String name)
Creates an integer variable with domain [lb, ub].
LinearConstraint AddLinearConstraint(LinearExpr expr, double lb, double ub)
Adds the constraint expr in [lb, ub].
Variable NewNumVar(double lb, double ub, String name)
Creates a continuous variable with domain [lb, ub].
int VariablesCount()
Returns the number of variables in the model.
double Value(Variable var)
The value of a variable in the current solution. This raises a SolverException if no solution has bee...
double ObjectiveValue
The best objective value found during search. This raises a SolverException if no solution has been f...
SolveStatus Solve(Model model)
Solves given model, and returns the status of the response.
bool SolverIsSupported()
Returns whether solver specified during the ctor was found and correctly installed.