forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValCstPair.cs
300 lines (263 loc) · 8.42 KB
/
ValCstPair.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
namespace Google.OrTools.ConstraintSolver
{
using System;
using System.Collections.Generic;
public interface IConstraintWithStatus
{
Solver solver();
IntVar Var();
}
public abstract class BaseEquality : IConstraintWithStatus
{
abstract public Solver solver();
abstract public IntVar Var();
public static IntExpr operator+(BaseEquality a, BaseEquality b) {
return a.solver().MakeSum(a.Var(), b.Var());
}
public static IntExpr operator+(BaseEquality a, long v) {
return a.solver().MakeSum(a.Var(), v);
}
public static IntExpr operator+(long v, BaseEquality a) {
return a.solver().MakeSum(a.Var(), v);
}
public static IntExpr operator-(BaseEquality a, BaseEquality b) {
return a.solver().MakeDifference(a.Var(), b.Var());
}
public static IntExpr operator-(BaseEquality a, long v) {
return a.solver().MakeSum(a.Var(), -v);
}
public static IntExpr operator-(long v, BaseEquality a) {
return a.solver().MakeDifference(v, a.Var());
}
public static IntExpr operator*(BaseEquality a, BaseEquality b) {
return a.solver().MakeProd(a.Var(), b.Var());
}
public static IntExpr operator*(BaseEquality a, long v) {
return a.solver().MakeProd(a.Var(), v);
}
public static IntExpr operator*(long v, BaseEquality a) {
return a.solver().MakeProd(a.Var(), v);
}
public static IntExpr operator/(BaseEquality a, long v) {
return a.solver().MakeDiv(a.Var(), v);
}
public static IntExpr operator-(BaseEquality a) {
return a.solver().MakeOpposite(a.Var());
}
public IntExpr Abs() {
return this.solver().MakeAbs(this.Var());
}
public IntExpr Square() {
return this.solver().MakeSquare(this.Var());
}
public static WrappedConstraint operator ==(BaseEquality a, long v) {
return new WrappedConstraint(a.solver().MakeEquality(a.Var(), v));
}
public static WrappedConstraint operator ==(long v, BaseEquality a) {
return new WrappedConstraint(a.solver().MakeEquality(a.Var(), v));
}
public static WrappedConstraint operator !=(BaseEquality a, long v) {
return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v));
}
public static WrappedConstraint operator !=(long v, BaseEquality a) {
return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v));
}
public static WrappedConstraint operator >=(BaseEquality a, long v) {
return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), v));
}
public static WrappedConstraint operator >=(long v, BaseEquality a) {
return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), v));
}
public static WrappedConstraint operator >(BaseEquality a, long v) {
return new WrappedConstraint(a.solver().MakeGreater(a.Var(), v));
}
public static WrappedConstraint operator >(long v, BaseEquality a) {
return new WrappedConstraint(a.solver().MakeLess(a.Var(), v));
}
public static WrappedConstraint operator <=(BaseEquality a, long v) {
return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), v));
}
public static WrappedConstraint operator <=(long v, BaseEquality a) {
return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), v));
}
public static WrappedConstraint operator <(BaseEquality a, long v) {
return new WrappedConstraint(a.solver().MakeLess(a.Var(), v));
}
public static WrappedConstraint operator <(long v, BaseEquality a) {
return new WrappedConstraint(a.solver().MakeGreater(a.Var(), v));
}
public static WrappedConstraint operator >=(BaseEquality a, BaseEquality b) {
return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(),
b.Var()));
}
public static WrappedConstraint operator >(BaseEquality a, BaseEquality b) {
return new WrappedConstraint(a.solver().MakeGreater(a.Var(), b.Var()));
}
public static WrappedConstraint operator <=(BaseEquality a, BaseEquality b) {
return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), b.Var()));
}
public static WrappedConstraint operator <(BaseEquality a, BaseEquality b) {
return new WrappedConstraint(a.solver().MakeLess(a.Var(), b.Var()));
}
public static ConstraintEquality operator ==(BaseEquality a, BaseEquality b) {
return new ConstraintEquality(a, b, true);
}
public static ConstraintEquality operator !=(BaseEquality a, BaseEquality b) {
return new ConstraintEquality(a, b, false);
}
}
public class WrappedConstraint : BaseEquality
{
public bool Val { get; set; }
public Constraint Cst { get; set; }
public WrappedConstraint(Constraint cst) : this(true, cst) {}
public WrappedConstraint(bool val) : this(val, null) {}
public WrappedConstraint(bool val, Constraint cst)
{
this.Val = val;
this.Cst = cst;
}
public static implicit operator bool(WrappedConstraint valCstPair)
{
return valCstPair.Val;
}
public static implicit operator Constraint(WrappedConstraint valCstPair)
{
return valCstPair.Cst;
}
public static implicit operator IntVar(WrappedConstraint eq)
{
return eq.Var();
}
public static implicit operator IntExpr(WrappedConstraint eq)
{
return eq.Var();
}
public override Solver solver()
{
return this.Cst.solver();
}
public override IntVar Var()
{
return Cst.Var();
}
}
public class IntExprEquality : BaseEquality
{
public IntExprEquality(IntExpr a, IntExpr b, bool equality)
{
this.left_ = a;
this.right_ = b;
this.equality_ = equality;
}
bool IsTrue()
{
return (object)left_ == (object)right_ ? equality_ : !equality_;
}
Constraint ToConstraint()
{
return equality_ ?
left_.solver().MakeEquality(left_.Var(), right_.Var()) :
left_.solver().MakeNonEquality(left_.Var(), right_.Var());
}
public static bool operator true(IntExprEquality eq)
{
return eq.IsTrue();
}
public static bool operator false(IntExprEquality eq)
{
return !eq.IsTrue();
}
public static implicit operator Constraint(IntExprEquality eq)
{
return eq.ToConstraint();
}
public override IntVar Var()
{
return equality_ ?
left_.solver().MakeIsEqualVar(left_, right_) :
left_.solver().MakeIsDifferentVar(left_, right_);
}
public static implicit operator IntVar(IntExprEquality eq)
{
return eq.Var();
}
public static implicit operator IntExpr(IntExprEquality eq)
{
return eq.Var();
}
public override Solver solver()
{
return left_.solver();
}
private IntExpr left_;
private IntExpr right_;
private bool equality_;
}
public class ConstraintEquality : BaseEquality
{
public ConstraintEquality(IConstraintWithStatus a,
IConstraintWithStatus b,
bool equality)
{
this.left_ = a;
this.right_ = b;
this.equality_ = equality;
}
bool IsTrue()
{
return (object)left_ == (object)right_ ? equality_ : !equality_;
}
Constraint ToConstraint()
{
return equality_ ?
left_.solver().MakeEquality(left_.Var(), right_.Var()) :
left_.solver().MakeNonEquality(left_.Var(), right_.Var());
}
public static bool operator true(ConstraintEquality eq)
{
return eq.IsTrue();
}
public static bool operator false(ConstraintEquality eq)
{
return !eq.IsTrue();
}
public static implicit operator Constraint(ConstraintEquality eq)
{
return eq.ToConstraint();
}
public override IntVar Var()
{
return equality_ ?
left_.solver().MakeIsEqualVar(left_.Var(), right_.Var()) :
left_.solver().MakeIsDifferentVar(left_.Var(), right_.Var());
}
public static implicit operator IntVar(ConstraintEquality eq)
{
return eq.Var();
}
public static implicit operator IntExpr(ConstraintEquality eq)
{
return eq.Var();
}
public override Solver solver()
{
return left_.solver();
}
private IConstraintWithStatus left_;
private IConstraintWithStatus right_;
private bool equality_;
}
} // namespace Google.OrTools.ConstraintSolver