本文整理汇总了C#中IntVar类的典型用法代码示例。如果您正苦于以下问题:C# IntVar类的具体用法?C# IntVar怎么用?C# IntVar使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IntVar类属于命名空间,在下文中一共展示了IntVar类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MakeIntVarArray
public IntVar[] MakeIntVarArray(int count, int[] values) {
IntVar[] array = new IntVar[count];
for (int i = 0; i < count; ++i) {
array[i] = MakeIntVar(values);
}
return array;
}
示例2: MakeBoolVarArray
public IntVar[] MakeBoolVarArray(int count) {
IntVar[] array = new IntVar[count];
for (int i = 0; i < count; ++i) {
array[i] = MakeBoolVar();
}
return array;
}
示例3: MyMod
/**
*
* A simple propagator for modulo constraint.
*
* This implementation is based on the ECLiPSe version
* mentioned in "A Modulo propagator for ECLiPSE"
* http://www.hakank.org/constraint_programming_blog/2010/05/a_modulo_propagator_for_eclips.html
* The ECLiPSe Prolog source code:
* http://www.hakank.org/eclipse/modulo_propagator.ecl
*
*/
public static void MyMod(Solver solver, IntVar x, IntVar y, IntVar r) {
long lbx = x.Min();
long ubx = x.Max();
long ubx_neg = -ubx;
long lbx_neg = -lbx;
int min_x = (int)Math.Min(lbx, ubx_neg);
int max_x = (int)Math.Max(ubx, lbx_neg);
IntVar d = solver.MakeIntVar(min_x, max_x, "d");
// r >= 0
solver.Add(r >= 0);
// x*r >= 0
solver.Add( x*r >= 0);
// -abs(y) < r
solver.Add(-y.Abs() < r);
// r < abs(y)
solver.Add(r < y.Abs());
// min_x <= d, i.e. d > min_x
solver.Add(d > min_x);
// d <= max_x
solver.Add(d <= max_x);
// x == y*d+r
solver.Add(x - (y*d + r) == 0);
}
示例4: IntVarExprVar
protected IntVarExprVar( IntVar var0, IntVar var1, IntVar var2 )
: base(var0.Solver, new Variable[] { var0, var1, var2 })
{
m_Var0 = var0;
m_Var1 = var1;
m_Var2 = var2;
}
示例5: minus
public static void minus(Solver solver,
IntVar x,
IntVar y,
IntVar z)
{
solver.Add(z == (x - y).Abs());
}
示例6: MoreMoney
public MoreMoney()
: base(0, 1000000)
{
IntVar d = new IntVar( m_Solver, 0, 9, "d");
IntVar e = new IntVar( m_Solver, 0, 9, "e");
IntVar m = new IntVar( m_Solver, 1, 9, "m");
IntVar n = new IntVar( m_Solver, 0, 9, "n");
IntVar o = new IntVar( m_Solver, 0, 9, "o");
IntVar r = new IntVar( m_Solver, 0, 9, "r");
IntVar s = new IntVar( m_Solver, 1, 9, "s");
IntVar y = new IntVar( m_Solver, 0, 9, "y");
IntVarList list = new IntVarList( m_Solver, new IntVar[] { d, e, m, n, o, r, s, y } );
m_Solver.Add( list.AllDifferent() );
IntVarListDotProduct send = new IntVarListDotProduct( m_Solver,
new IntVar[] { s, e, n, d },
new int[] { 1000, 100, 10, 1 } );
IntVarListDotProduct more = new IntVarListDotProduct( m_Solver,
new IntVar[] { m, o, r, e },
new int[] { 1000, 100, 10, 1 } );
IntVarListDotProduct money = new IntVarListDotProduct( m_Solver,
new IntVar[] { m, o, n, e, y },
new int[] { 10000, 1000, 100, 10, 1 } );
m_Solver.Add( send );
m_Solver.Add( more );
m_Solver.Add( money );
IntVarExpr sendMore = send.Var0 + more.Var0;
m_Solver.Add( sendMore );
IntVarCmp cmp = sendMore.Var0 == money.Var0;
m_Solver.Add( cmp );
}
示例7: MyCumulative
/*
* Decompositon of cumulative.
*
* Inspired by the MiniZinc implementation:
* http://www.g12.csse.unimelb.edu.au/wiki/doku.php?id=g12:zinc:lib:minizinc:std:cumulative.mzn&s[]=cumulative
* The MiniZinc decomposition is discussed in the paper:
* A. Schutt, T. Feydy, P.J. Stuckey, and M. G. Wallace.
* "Why cumulative decomposition is not as bad as it sounds."
* Download:
* http://www.cs.mu.oz.au/%7Epjs/rcpsp/papers/cp09-cu.pdf
* http://www.cs.mu.oz.au/%7Epjs/rcpsp/cumu_lazyfd.pdf
*
*
* Parameters:
*
* s: start_times assumption: IntVar[]
* d: durations assumption: int[]
* r: resources assumption: int[]
* b: resource limit assumption: IntVar or int
*
*
*/
static void MyCumulative(Solver solver,
IntVar[] s,
int[] d,
int[] r,
IntVar b) {
int[] tasks = (from i in Enumerable.Range(0, s.Length)
where r[i] > 0 && d[i] > 0
select i).ToArray();
int times_min = tasks.Min(i => (int)s[i].Min());
int d_max = d.Max();
int times_max = tasks.Max(i => (int)s[i].Max() + d_max);
for(int t = times_min; t <= times_max; t++) {
ArrayList bb = new ArrayList();
foreach(int i in tasks) {
bb.Add(((s[i] <= t) * (s[i] + d[i]> t) * r[i]).Var());
}
solver.Add((bb.ToArray(typeof(IntVar)) as IntVar[]).Sum() <= b);
}
// Somewhat experimental:
// This constraint is needed to constrain the upper limit of b.
if (b is IntVar) {
solver.Add(b <= r.Sum());
}
}
示例8: Solve
/**
*
* Solve the Least diff problem
* For more info, see http://www.hakank.org/google_or_tools/least_diff.py
*
*/
private static void Solve()
{
Solver solver = new Solver("LeastDiff");
//
// Decision variables
//
IntVar A = solver.MakeIntVar(0, 9, "A");
IntVar B = solver.MakeIntVar(0, 9, "B");
IntVar C = solver.MakeIntVar(0, 9, "C");
IntVar D = solver.MakeIntVar(0, 9, "D");
IntVar E = solver.MakeIntVar(0, 9, "E");
IntVar F = solver.MakeIntVar(0, 9, "F");
IntVar G = solver.MakeIntVar(0, 9, "G");
IntVar H = solver.MakeIntVar(0, 9, "H");
IntVar I = solver.MakeIntVar(0, 9, "I");
IntVar J = solver.MakeIntVar(0, 9, "J");
IntVar[] all = new IntVar[] {A,B,C,D,E,F,G,H,I,J};
int[] coeffs = {10000,1000,100,10,1};
IntVar x = new IntVar[]{A,B,C,D,E}.ScalProd(coeffs).Var();
IntVar y = new IntVar[]{F,G,H,I,J}.ScalProd(coeffs).Var();
IntVar diff = (x - y).VarWithName("diff");
//
// Constraints
//
solver.Add(all.AllDifferent());
solver.Add(A > 0);
solver.Add(F > 0);
solver.Add(diff > 0);
//
// Objective
//
OptimizeVar obj = diff.Minimize(1);
//
// Search
//
DecisionBuilder db = solver.MakePhase(all,
Solver.CHOOSE_PATH,
Solver.ASSIGN_MIN_VALUE);
solver.NewSearch(db, obj);
while (solver.NextSolution()) {
Console.WriteLine("{0} - {1} = {2} ({3}",x.Value(), y.Value(), diff.Value(), diff.ToString());
}
Console.WriteLine("\nSolutions: {0}", solver.Solutions());
Console.WriteLine("WallTime: {0}ms", solver.WallTime());
Console.WriteLine("Failures: {0}", solver.Failures());
Console.WriteLine("Branches: {0} ", solver.Branches());
solver.EndSearch();
}
示例9: IntGenerate
public IntGenerate( Solver solver, IntVar[] list, IntVarSelector.Select select, IntSearch search )
: base(solver)
{
m_IntVarList = list;
m_SelectVar = select;
m_Search = search;
m_Depth = new RevValue<double>( solver.StateStack, 1 );
}
示例10: CPisFun
// We don't need helper functions here
// Csharp syntax is easier than C++ syntax!
private static void CPisFun (int kBase)
{
// Constraint Programming engine
Solver solver = new Solver ("CP is fun!");
// Decision variables
IntVar c = solver.MakeIntVar (1, kBase - 1, "C");
IntVar p = solver.MakeIntVar (0, kBase - 1, "P");
IntVar i = solver.MakeIntVar (1, kBase - 1, "I");
IntVar s = solver.MakeIntVar (0, kBase - 1, "S");
IntVar f = solver.MakeIntVar (1, kBase - 1, "F");
IntVar u = solver.MakeIntVar (0, kBase - 1, "U");
IntVar n = solver.MakeIntVar (0, kBase - 1, "N");
IntVar t = solver.MakeIntVar (1, kBase - 1, "T");
IntVar r = solver.MakeIntVar (0, kBase - 1, "R");
IntVar e = solver.MakeIntVar (0, kBase - 1, "E");
// We need to group variables in a vector to be able to use
// the global constraint AllDifferent
IntVar[] letters = new IntVar[] { c, p, i, s, f, u, n, t, r, e};
// Check if we have enough digits
if (kBase < letters.Length) {
throw new Exception("kBase < letters.Length");
}
// Constraints
solver.Add (letters.AllDifferent ());
// CP + IS + FUN = TRUE
solver.Add (p + s + n + kBase * (c + i + u) + kBase * kBase * f ==
e + kBase * u + kBase * kBase * r + kBase * kBase * kBase * t);
SolutionCollector all_solutions = solver.MakeAllSolutionCollector();
// Add the interesting variables to the SolutionCollector
all_solutions.Add(c);
all_solutions.Add(p);
// Create the variable kBase * c + p
IntVar v1 = solver.MakeSum(solver.MakeProd(c, kBase), p).Var();
// Add it to the SolutionCollector
all_solutions.Add(v1);
// Decision Builder: hot to scour the search tree
DecisionBuilder db = solver.MakePhase (letters,
Solver.CHOOSE_FIRST_UNBOUND,
Solver.ASSIGN_MIN_VALUE);
solver.Solve(db, all_solutions);
// Retrieve the solutions
int numberSolutions = all_solutions.SolutionCount();
Console.WriteLine ("Number of solutions: " + numberSolutions);
for (int index = 0; index < numberSolutions; ++index) {
Assignment solution = all_solutions.Solution(index);
Console.WriteLine ("Solution found:");
Console.WriteLine ("v1=" + solution.Value(v1));
}
}
示例11: ToNum
/**
*
* toNum(solver, a, num, base)
*
* channelling between the array a and the number num.
*
*/
private static Constraint ToNum(IntVar[] a, IntVar num, int bbase) {
int len = a.Length;
IntVar[] tmp = new IntVar[len];
for(int i = 0; i < len; i++) {
tmp[i] = (a[i]*(int)Math.Pow(bbase,(len-i-1))).Var();
}
return tmp.Sum() == num;
}
示例12: AllDifferentExcept0
//
// Decomposition of alldifferent_except_0
//
public static void AllDifferentExcept0(Solver solver, IntVar[] a) {
int n = a.Length;
for(int i = 0; i < n; i++) {
for(int j = 0; j < i; j++) {
solver.Add((a[i] != 0) * (a[j] != 0) <= (a[i] != a[j]));
}
}
}
示例13: calc
/**
* Ensure that the sum of the segments
* in cc == res
*
*/
public static void calc(Solver solver,
int[] cc,
IntVar[,] x,
int res)
{
int ccLen = cc.Length;
if (ccLen == 4) {
// for two operands there's
// a lot of possible variants
IntVar a = x[cc[0]-1, cc[1]-1];
IntVar b = x[cc[2]-1, cc[3]-1];
IntVar r1 = a + b == res;
IntVar r2 = a * b == res;
IntVar r3 = a * res == b;
IntVar r4 = b * res == a;
IntVar r5 = a - b == res;
IntVar r6 = b - a == res;
solver.Add(r1+r2+r3+r4+r5+r6 >= 1);
} else {
// For length > 2 then res is either the sum
// the the product of the segment
// sum the numbers
int len = cc.Length / 2;
IntVar[] xx = (from i in Enumerable.Range(0, len)
select x[cc[i*2]-1,cc[i*2+1]-1]).ToArray();
// Sum
IntVar this_sum = xx.Sum() == res;
// Product
// IntVar this_prod = (xx.Prod() == res).Var(); // don't work
IntVar this_prod;
if (xx.Length == 3) {
this_prod = (x[cc[0]-1,cc[1]-1] *
x[cc[2]-1,cc[3]-1] *
x[cc[4]-1,cc[5]-1]) == res;
} else {
this_prod = (
x[cc[0]-1,cc[1]-1] *
x[cc[2]-1,cc[3]-1] *
x[cc[4]-1,cc[5]-1] *
x[cc[6]-1,cc[7]-1]) == res;
}
solver.Add(this_sum + this_prod >= 1);
}
}
示例14: MyRegular
/*
* Global constraint regular
*
* This is a translation of MiniZinc's regular constraint (defined in
* lib/zinc/globals.mzn), via the Comet code refered above.
* All comments are from the MiniZinc code.
* """
* The sequence of values in array 'x' (which must all be in the range 1..S)
* is accepted by the DFA of 'Q' states with input 1..S and transition
* function 'd' (which maps (1..Q, 1..S) -> 0..Q)) and initial state 'q0'
* (which must be in 1..Q) and accepting states 'F' (which all must be in
* 1..Q). We reserve state 0 to be an always failing state.
* """
*
* x : IntVar array
* Q : number of states
* S : input_max
* d : transition matrix
* q0: initial state
* F : accepting states
*
*/
static void MyRegular(Solver solver,
IntVar[] x,
int Q,
int S,
int[,] d,
int q0,
int[] F) {
Debug.Assert(Q > 0, "regular: 'Q' must be greater than zero");
Debug.Assert(S > 0, "regular: 'S' must be greater than zero");
// d2 is the same as d, except we add one extra transition for
// each possible input; each extra transition is from state zero
// to state zero. This allows us to continue even if we hit a
// non-accepted input.
int[][] d2 = new int[Q+1][];
for(int i = 0; i <= Q; i++) {
int[] row = new int[S];
for(int j = 0; j < S; j++) {
if (i == 0) {
row[j] = 0;
} else {
row[j] = d[i-1,j];
}
}
d2[i] = row;
}
int[] d2_flatten = (from i in Enumerable.Range(0, Q+1)
from j in Enumerable.Range(0, S)
select d2[i][j]).ToArray();
// If x has index set m..n, then a[m-1] holds the initial state
// (q0), and a[i+1] holds the state we're in after processing
// x[i]. If a[n] is in F, then we succeed (ie. accept the
// string).
int m = 0;
int n = x.Length;
IntVar[] a = solver.MakeIntVarArray(n+1-m, 0,Q+1, "a");
// Check that the final state is in F
solver.Add(a[a.Length-1].Member(F));
// First state is q0
solver.Add(a[m] == q0);
for(int i = 0; i < n; i++) {
solver.Add(x[i] >= 1);
solver.Add(x[i] <= S);
// Determine a[i+1]: a[i+1] == d2[a[i], x[i]]
solver.Add(a[i+1] == d2_flatten.Element(((a[i]*S)+(x[i]-1))));
}
}
示例15: Copy
public IntVar[] Copy( IntVar[] other )
{
IntVar[] copy = new IntVar[ other.Length ];
for( int idx = 0; idx < other.Length; ++idx )
{
copy[ idx ] = m_IntVarList[ other[ idx ].Index ];
}
return copy;
}