Skip to content

Commit 2b22356

Browse files
committed
[CP-SAT] more work on python layer
1 parent 39248b0 commit 2b22356

File tree

3 files changed

+316
-212
lines changed

3 files changed

+316
-212
lines changed

ortools/sat/python/linear_expr.cc

Lines changed: 150 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ LinearExpr* LinearExpr::Constant(int64_t value) {
189189
return new IntConstant(value);
190190
}
191191

192-
LinearExpr* LinearExpr::Add(LinearExpr* other) {
193-
return new BinaryAdd(this, other);
192+
LinearExpr* LinearExpr::Add(LinearExpr* expr) {
193+
return new BinaryAdd(this, expr);
194194
}
195195

196196
LinearExpr* LinearExpr::AddInt(int64_t cst) {
@@ -203,40 +203,40 @@ LinearExpr* LinearExpr::AddDouble(double cst) {
203203
return new FloatAffine(this, 1.0, cst);
204204
}
205205

206-
LinearExpr* LinearExpr::Sub(ExprOrValue other) {
207-
if (other.expr != nullptr) {
208-
return new IntWeightedSum({this, other.expr}, {1, -1}, 0);
209-
} else if (other.double_value != 0.0) {
210-
return new FloatAffine(this, 1.0, -other.double_value);
211-
} else if (other.int_value != 0) {
212-
return new IntAffine(this, 1, -other.int_value);
213-
} else {
214-
return this;
215-
}
206+
LinearExpr* LinearExpr::Sub(LinearExpr* expr) {
207+
return new IntWeightedSum({this, expr}, {1, -1}, 0);
216208
}
217209

218-
LinearExpr* LinearExpr::RSub(ExprOrValue other) {
219-
if (other.expr != nullptr) {
220-
return new IntWeightedSum({this, other.expr}, {-1, 1}, 0);
221-
} else if (other.double_value != 0.0) {
222-
return new FloatAffine(this, -1.0, other.double_value);
223-
} else {
224-
return new IntAffine(this, -1, other.int_value);
225-
}
210+
LinearExpr* LinearExpr::SubInt(int64_t cst) {
211+
if (cst == 0) return this;
212+
return new IntAffine(this, 1, -cst);
226213
}
227214

228-
LinearExpr* LinearExpr::Mul(double cst) {
229-
if (cst == 0.0) return new IntConstant(0);
230-
if (cst == 1.0) return this;
231-
return new FloatAffine(this, cst, 0.0);
215+
LinearExpr* LinearExpr::SubDouble(double cst) {
216+
if (cst == 0.0) return this;
217+
return new FloatAffine(this, 1.0, -cst);
232218
}
233219

234-
LinearExpr* LinearExpr::Mul(int64_t cst) {
220+
LinearExpr* LinearExpr::RSubInt(int64_t cst) {
221+
return new IntAffine(this, -1, cst);
222+
}
223+
224+
LinearExpr* LinearExpr::RSubDouble(double cst) {
225+
return new FloatAffine(this, -1.0, cst);
226+
}
227+
228+
LinearExpr* LinearExpr::MulInt(int64_t cst) {
235229
if (cst == 0) return new IntConstant(0);
236230
if (cst == 1) return this;
237231
return new IntAffine(this, cst, 0);
238232
}
239233

234+
LinearExpr* LinearExpr::MulDouble(double cst) {
235+
if (cst == 0.0) return new IntConstant(0);
236+
if (cst == 1.0) return this;
237+
return new FloatAffine(this, cst, 0.0);
238+
}
239+
240240
LinearExpr* LinearExpr::Neg() { return new IntAffine(this, -1, 0); }
241241

242242
void FloatExprVisitor::AddToProcess(LinearExpr* expr, double coeff) {
@@ -449,152 +449,142 @@ std::string FloatAffine::DebugString() const {
449449
return absl::StrCat("FloatAffine(expr=", expr_->DebugString(),
450450
", coeff=", coeff_, ", offset=", offset_, ")");
451451
}
452+
BoundedLinearExpression* LinearExpr::Eq(LinearExpr* rhs) {
453+
IntExprVisitor lin;
454+
lin.AddToProcess(this, 1);
455+
lin.AddToProcess(rhs, -1);
456+
std::vector<BaseIntVar*> vars;
457+
std::vector<int64_t> coeffs;
458+
int64_t offset;
459+
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
460+
return new BoundedLinearExpression(vars, coeffs, offset, Domain(0));
461+
}
452462

453-
BoundedLinearExpression* LinearExpr::Eq(ExprOrValue other) {
454-
if (other.double_value != 0.0) return nullptr;
455-
if (other.expr != nullptr) {
456-
IntExprVisitor lin;
457-
lin.AddToProcess(this, 1);
458-
lin.AddToProcess(other.expr, -1);
459-
std::vector<BaseIntVar*> vars;
460-
std::vector<int64_t> coeffs;
461-
int64_t offset;
462-
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
463-
return new BoundedLinearExpression(vars, coeffs, offset, Domain(0));
464-
} else {
465-
IntExprVisitor lin;
466-
lin.AddToProcess(this, 1);
467-
std::vector<BaseIntVar*> vars;
468-
std::vector<int64_t> coeffs;
469-
int64_t offset;
470-
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
471-
return new BoundedLinearExpression(vars, coeffs, offset,
472-
Domain(other.int_value));
473-
}
463+
BoundedLinearExpression* LinearExpr::EqCst(int64_t rhs) {
464+
IntExprVisitor lin;
465+
lin.AddToProcess(this, 1);
466+
std::vector<BaseIntVar*> vars;
467+
std::vector<int64_t> coeffs;
468+
int64_t offset;
469+
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
470+
return new BoundedLinearExpression(vars, coeffs, offset, Domain(rhs));
474471
}
475472

476-
BoundedLinearExpression* LinearExpr::Ne(ExprOrValue other) {
477-
if (other.double_value != 0.0) return nullptr;
478-
if (other.expr != nullptr) {
479-
IntExprVisitor lin;
480-
lin.AddToProcess(this, 1);
481-
lin.AddToProcess(other.expr, -1);
482-
std::vector<BaseIntVar*> vars;
483-
std::vector<int64_t> coeffs;
484-
int64_t offset;
485-
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
486-
return new BoundedLinearExpression(vars, coeffs, offset,
487-
Domain(0).Complement());
488-
} else {
489-
IntExprVisitor lin;
490-
lin.AddToProcess(this, 1);
491-
std::vector<BaseIntVar*> vars;
492-
std::vector<int64_t> coeffs;
493-
int64_t offset;
494-
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
495-
return new BoundedLinearExpression(vars, coeffs, offset,
496-
Domain(other.int_value).Complement());
497-
}
473+
BoundedLinearExpression* LinearExpr::Ne(LinearExpr* rhs) {
474+
IntExprVisitor lin;
475+
lin.AddToProcess(this, 1);
476+
lin.AddToProcess(rhs, -1);
477+
std::vector<BaseIntVar*> vars;
478+
std::vector<int64_t> coeffs;
479+
int64_t offset;
480+
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
481+
return new BoundedLinearExpression(vars, coeffs, offset,
482+
Domain(0).Complement());
498483
}
499484

500-
BoundedLinearExpression* LinearExpr::Le(ExprOrValue other) {
501-
if (other.double_value != 0.0) return nullptr;
502-
if (other.expr != nullptr) {
503-
IntExprVisitor lin;
504-
lin.AddToProcess(this, 1);
505-
lin.AddToProcess(other.expr, -1);
506-
std::vector<BaseIntVar*> vars;
507-
std::vector<int64_t> coeffs;
508-
int64_t offset;
509-
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
510-
return new BoundedLinearExpression(
511-
vars, coeffs, offset, Domain(std::numeric_limits<int64_t>::min(), 0));
512-
} else {
513-
IntExprVisitor lin;
514-
lin.AddToProcess(this, 1);
515-
std::vector<BaseIntVar*> vars;
516-
std::vector<int64_t> coeffs;
517-
int64_t offset;
518-
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
519-
return new BoundedLinearExpression(
520-
vars, coeffs, offset,
521-
Domain(std::numeric_limits<int64_t>::min(), other.int_value));
522-
}
485+
BoundedLinearExpression* LinearExpr::NeCst(int64_t rhs) {
486+
IntExprVisitor lin;
487+
lin.AddToProcess(this, 1);
488+
std::vector<BaseIntVar*> vars;
489+
std::vector<int64_t> coeffs;
490+
int64_t offset;
491+
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
492+
return new BoundedLinearExpression(vars, coeffs, offset,
493+
Domain(rhs).Complement());
523494
}
524495

525-
BoundedLinearExpression* LinearExpr::Lt(ExprOrValue other) {
526-
if (other.double_value != 0.0) return nullptr;
527-
if (other.expr != nullptr) {
528-
IntExprVisitor lin;
529-
lin.AddToProcess(this, 1);
530-
lin.AddToProcess(other.expr, -1);
531-
std::vector<BaseIntVar*> vars;
532-
std::vector<int64_t> coeffs;
533-
int64_t offset;
534-
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
535-
return new BoundedLinearExpression(
536-
vars, coeffs, offset, Domain(std::numeric_limits<int64_t>::min(), -1));
537-
} else {
538-
IntExprVisitor lin;
539-
lin.AddToProcess(this, 1);
540-
std::vector<BaseIntVar*> vars;
541-
std::vector<int64_t> coeffs;
542-
int64_t offset;
543-
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
544-
return new BoundedLinearExpression(
545-
vars, coeffs, offset,
546-
Domain(std::numeric_limits<int64_t>::min(), other.int_value - 1));
547-
}
496+
BoundedLinearExpression* LinearExpr::Le(LinearExpr* rhs) {
497+
IntExprVisitor lin;
498+
lin.AddToProcess(this, 1);
499+
lin.AddToProcess(rhs, -1);
500+
std::vector<BaseIntVar*> vars;
501+
std::vector<int64_t> coeffs;
502+
int64_t offset;
503+
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
504+
return new BoundedLinearExpression(
505+
vars, coeffs, offset, Domain(std::numeric_limits<int64_t>::min(), 0));
548506
}
549507

550-
BoundedLinearExpression* LinearExpr::Ge(ExprOrValue other) {
551-
if (other.double_value != 0.0) return nullptr;
552-
if (other.expr != nullptr) {
553-
IntExprVisitor lin;
554-
lin.AddToProcess(this, 1);
555-
lin.AddToProcess(other.expr, -1);
556-
std::vector<BaseIntVar*> vars;
557-
std::vector<int64_t> coeffs;
558-
int64_t offset;
559-
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
560-
return new BoundedLinearExpression(
561-
vars, coeffs, offset, Domain(0, std::numeric_limits<int64_t>::max()));
562-
} else {
563-
IntExprVisitor lin;
564-
lin.AddToProcess(this, 1);
565-
std::vector<BaseIntVar*> vars;
566-
std::vector<int64_t> coeffs;
567-
int64_t offset;
568-
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
569-
return new BoundedLinearExpression(
570-
vars, coeffs, offset,
571-
Domain(other.int_value, std::numeric_limits<int64_t>::max()));
572-
}
508+
BoundedLinearExpression* LinearExpr::LeCst(int64_t rhs) {
509+
IntExprVisitor lin;
510+
lin.AddToProcess(this, 1);
511+
std::vector<BaseIntVar*> vars;
512+
std::vector<int64_t> coeffs;
513+
int64_t offset;
514+
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
515+
return new BoundedLinearExpression(
516+
vars, coeffs, offset, Domain(std::numeric_limits<int64_t>::min(), rhs));
573517
}
574518

575-
BoundedLinearExpression* LinearExpr::Gt(ExprOrValue other) {
576-
if (other.double_value != 0.0) return nullptr;
577-
if (other.expr != nullptr) {
578-
IntExprVisitor lin;
579-
lin.AddToProcess(this, 1);
580-
lin.AddToProcess(other.expr, -1);
581-
std::vector<BaseIntVar*> vars;
582-
std::vector<int64_t> coeffs;
583-
int64_t offset;
584-
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
585-
return new BoundedLinearExpression(
586-
vars, coeffs, offset, Domain(1, std::numeric_limits<int64_t>::max()));
587-
} else {
588-
IntExprVisitor lin;
589-
lin.AddToProcess(this, 1);
590-
std::vector<BaseIntVar*> vars;
591-
std::vector<int64_t> coeffs;
592-
int64_t offset;
593-
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
594-
return new BoundedLinearExpression(
595-
vars, coeffs, offset,
596-
Domain(other.int_value + 1, std::numeric_limits<int64_t>::max()));
597-
}
519+
BoundedLinearExpression* LinearExpr::Lt(LinearExpr* rhs) {
520+
IntExprVisitor lin;
521+
lin.AddToProcess(this, 1);
522+
lin.AddToProcess(rhs, -1);
523+
std::vector<BaseIntVar*> vars;
524+
std::vector<int64_t> coeffs;
525+
int64_t offset;
526+
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
527+
return new BoundedLinearExpression(
528+
vars, coeffs, offset, Domain(std::numeric_limits<int64_t>::min(), -1));
529+
}
530+
531+
BoundedLinearExpression* LinearExpr::LtCst(int64_t rhs) {
532+
IntExprVisitor lin;
533+
lin.AddToProcess(this, 1);
534+
std::vector<BaseIntVar*> vars;
535+
std::vector<int64_t> coeffs;
536+
int64_t offset;
537+
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
538+
return new BoundedLinearExpression(
539+
vars, coeffs, offset,
540+
Domain(std::numeric_limits<int64_t>::min(), rhs - 1));
541+
}
542+
543+
BoundedLinearExpression* LinearExpr::Ge(LinearExpr* rhs) {
544+
IntExprVisitor lin;
545+
lin.AddToProcess(this, 1);
546+
lin.AddToProcess(rhs, -1);
547+
std::vector<BaseIntVar*> vars;
548+
std::vector<int64_t> coeffs;
549+
int64_t offset;
550+
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
551+
return new BoundedLinearExpression(
552+
vars, coeffs, offset, Domain(0, std::numeric_limits<int64_t>::max()));
553+
}
554+
555+
BoundedLinearExpression* LinearExpr::GeCst(int64_t rhs) {
556+
IntExprVisitor lin;
557+
lin.AddToProcess(this, 1);
558+
std::vector<BaseIntVar*> vars;
559+
std::vector<int64_t> coeffs;
560+
int64_t offset;
561+
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
562+
return new BoundedLinearExpression(
563+
vars, coeffs, offset, Domain(rhs, std::numeric_limits<int64_t>::max()));
564+
}
565+
566+
BoundedLinearExpression* LinearExpr::Gt(LinearExpr* rhs) {
567+
IntExprVisitor lin;
568+
lin.AddToProcess(this, 1);
569+
lin.AddToProcess(rhs, -1);
570+
std::vector<BaseIntVar*> vars;
571+
std::vector<int64_t> coeffs;
572+
int64_t offset;
573+
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
574+
return new BoundedLinearExpression(
575+
vars, coeffs, offset, Domain(1, std::numeric_limits<int64_t>::max()));
576+
}
577+
578+
BoundedLinearExpression* LinearExpr::GtCst(int64_t rhs) {
579+
IntExprVisitor lin;
580+
lin.AddToProcess(this, 1);
581+
std::vector<BaseIntVar*> vars;
582+
std::vector<int64_t> coeffs;
583+
int64_t offset;
584+
if (!lin.Process(&vars, &coeffs, &offset)) return nullptr;
585+
return new BoundedLinearExpression(
586+
vars, coeffs, offset,
587+
Domain(rhs + 1, std::numeric_limits<int64_t>::max()));
598588
}
599589

600590
void IntExprVisitor::AddToProcess(LinearExpr* expr, int64_t coeff) {

ortools/sat/python/linear_expr.h

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,30 @@ class LinearExpr {
7878
static LinearExpr* Constant(int64_t value);
7979
static LinearExpr* Constant(double value);
8080

81-
LinearExpr* Add(LinearExpr* other);
81+
LinearExpr* Add(LinearExpr* expr);
8282
LinearExpr* AddInt(int64_t cst);
8383
LinearExpr* AddDouble(double cst);
84-
LinearExpr* Sub(ExprOrValue other);
85-
LinearExpr* RSub(ExprOrValue other);
86-
LinearExpr* Mul(double cst);
87-
LinearExpr* Mul(int64_t cst);
84+
LinearExpr* Sub(LinearExpr* expr);
85+
LinearExpr* SubInt(int64_t cst);
86+
LinearExpr* SubDouble(double cst);
87+
LinearExpr* RSubInt(int64_t cst);
88+
LinearExpr* RSubDouble(double cst);
89+
LinearExpr* MulInt(int64_t cst);
90+
LinearExpr* MulDouble(double cst);
8891
LinearExpr* Neg();
8992

90-
BoundedLinearExpression* Eq(ExprOrValue other);
91-
BoundedLinearExpression* Ne(ExprOrValue other);
92-
BoundedLinearExpression* Ge(ExprOrValue other);
93-
BoundedLinearExpression* Le(ExprOrValue other);
94-
BoundedLinearExpression* Lt(ExprOrValue other);
95-
BoundedLinearExpression* Gt(ExprOrValue other);
93+
BoundedLinearExpression* Eq(LinearExpr* rhs);
94+
BoundedLinearExpression* EqCst(int64_t rhs);
95+
BoundedLinearExpression* Ne(LinearExpr* rhs);
96+
BoundedLinearExpression* NeCst(int64_t rhs);
97+
BoundedLinearExpression* Ge(LinearExpr* rhs);
98+
BoundedLinearExpression* GeCst(int64_t rhs);
99+
BoundedLinearExpression* Le(LinearExpr* rhs);
100+
BoundedLinearExpression* LeCst(int64_t rhs);
101+
BoundedLinearExpression* Lt(LinearExpr* rhs);
102+
BoundedLinearExpression* LtCst(int64_t rhs);
103+
BoundedLinearExpression* Gt(LinearExpr* rhs);
104+
BoundedLinearExpression* GtCst(int64_t rhs);
96105
};
97106

98107
// Compare the indices of variables.

0 commit comments

Comments
 (0)