Skip to content

Commit

Permalink
sat: update go/cpmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizux committed Sep 25, 2024
1 parent 858d4a7 commit b8dad90
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 95 deletions.
10 changes: 5 additions & 5 deletions ortools/sat/go/cpmodel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ go_library(
srcs = [
"cp_model.go",
"cp_solver.go",
"cp_solver_c.cc",
"cp_solver_c.h",
#"cp_solver_c.cc",
#"cp_solver_c.h",
"domain.go",
],
cdeps = [":cp_solver_c"],
Expand Down Expand Up @@ -57,12 +57,12 @@ cc_library(
srcs = ["cp_solver_c.cc"],
hdrs = ["cp_solver_c.h"],
deps = [
"//ortools/base:memutil",
"//ortools/sat:cp_model_cc_proto",
"//ortools/sat:cp_model_solver",
"//ortools/sat:model",
"//ortools/sat:sat_parameters_cc_proto",
"//ortools/util:time_limit",
"@com_google_absl//absl/log",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/log:check",
],
)
133 changes: 59 additions & 74 deletions ortools/sat/go/cpmodel/cp_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,8 @@ func (cp *Builder) NewOptionalIntervalVar(start, size, end LinearArgument, prese
Start: start.asLinearExpressionProto(),
Size: size.asLinearExpressionProto(),
End: end.asLinearExpressionProto(),
},
}})
}},
})

return IntervalVar{cpb: cp, ind: ind}
}
Expand Down Expand Up @@ -803,11 +803,10 @@ func (cp *Builder) AddMinEquality(target LinearArgument, exprs ...LinearArgument
}

return cp.appendConstraint(&cmpb.ConstraintProto{
Constraint: &cmpb.ConstraintProto_LinMax{
&cmpb.LinearArgumentProto{
Target: asNegatedLinearExpressionProto(target),
Exprs: protos,
}},
Constraint: &cmpb.ConstraintProto_LinMax{&cmpb.LinearArgumentProto{
Target: asNegatedLinearExpressionProto(target),
Exprs: protos,
}},
})
}

Expand All @@ -819,11 +818,10 @@ func (cp *Builder) AddMaxEquality(target LinearArgument, exprs ...LinearArgument
}

return cp.appendConstraint(&cmpb.ConstraintProto{
Constraint: &cmpb.ConstraintProto_LinMax{
&cmpb.LinearArgumentProto{
Target: target.asLinearExpressionProto(),
Exprs: protos,
}},
Constraint: &cmpb.ConstraintProto_LinMax{&cmpb.LinearArgumentProto{
Target: target.asLinearExpressionProto(),
Exprs: protos,
}},
})
}

Expand All @@ -835,53 +833,49 @@ func (cp *Builder) AddMultiplicationEquality(target LinearArgument, exprs ...Lin
}

return cp.appendConstraint(&cmpb.ConstraintProto{
Constraint: &cmpb.ConstraintProto_IntProd{
&cmpb.LinearArgumentProto{
Target: target.asLinearExpressionProto(),
Exprs: protos,
}},
Constraint: &cmpb.ConstraintProto_IntProd{&cmpb.LinearArgumentProto{
Target: target.asLinearExpressionProto(),
Exprs: protos,
}},
})
}

// AddDivisionEquality adds the constraint: target == num / denom.
func (cp *Builder) AddDivisionEquality(target, num, denom LinearArgument) Constraint {
return cp.appendConstraint(&cmpb.ConstraintProto{
Constraint: &cmpb.ConstraintProto_IntDiv{
&cmpb.LinearArgumentProto{
Target: target.asLinearExpressionProto(),
Exprs: []*cmpb.LinearExpressionProto{
num.asLinearExpressionProto(),
denom.asLinearExpressionProto(),
},
}},
Constraint: &cmpb.ConstraintProto_IntDiv{&cmpb.LinearArgumentProto{
Target: target.asLinearExpressionProto(),
Exprs: []*cmpb.LinearExpressionProto{
num.asLinearExpressionProto(),
denom.asLinearExpressionProto(),
},
}},
})
}

// AddAbsEquality adds the constraint: target == Abs(expr).
func (cp *Builder) AddAbsEquality(target, expr LinearArgument) Constraint {
return cp.appendConstraint(&cmpb.ConstraintProto{
Constraint: &cmpb.ConstraintProto_LinMax{
&cmpb.LinearArgumentProto{
Target: target.asLinearExpressionProto(),
Exprs: []*cmpb.LinearExpressionProto{
expr.asLinearExpressionProto(),
asNegatedLinearExpressionProto(expr),
},
}},
Constraint: &cmpb.ConstraintProto_LinMax{&cmpb.LinearArgumentProto{
Target: target.asLinearExpressionProto(),
Exprs: []*cmpb.LinearExpressionProto{
expr.asLinearExpressionProto(),
asNegatedLinearExpressionProto(expr),
},
}},
})
}

// AddModuloEquality adds the constraint: target == v % mod.
func (cp *Builder) AddModuloEquality(target, v, mod LinearArgument) Constraint {
return cp.appendConstraint(&cmpb.ConstraintProto{
Constraint: &cmpb.ConstraintProto_IntMod{
&cmpb.LinearArgumentProto{
Target: target.asLinearExpressionProto(),
Exprs: []*cmpb.LinearExpressionProto{
v.asLinearExpressionProto(),
mod.asLinearExpressionProto(),
},
}},
Constraint: &cmpb.ConstraintProto_IntMod{&cmpb.LinearArgumentProto{
Target: target.asLinearExpressionProto(),
Exprs: []*cmpb.LinearExpressionProto{
v.asLinearExpressionProto(),
mod.asLinearExpressionProto(),
},
}},
})
}

Expand All @@ -894,37 +888,33 @@ func (cp *Builder) AddNoOverlap(vars ...IntervalVar) Constraint {
}

return cp.appendConstraint(&cmpb.ConstraintProto{
Constraint: &cmpb.ConstraintProto_NoOverlap{
&cmpb.NoOverlapConstraintProto{
Intervals: intervals,
}},
Constraint: &cmpb.ConstraintProto_NoOverlap{&cmpb.NoOverlapConstraintProto{
Intervals: intervals,
}},
})
}

// AddNoOverlap2D adds a no_overlap2D constraint that prevents a set of boxes from overlapping.
func (cp *Builder) AddNoOverlap2D() NoOverlap2DConstraint {
return NoOverlap2DConstraint{cp.appendConstraint(&cmpb.ConstraintProto{
Constraint: &cmpb.ConstraintProto_NoOverlap_2D{
&cmpb.NoOverlap2DConstraintProto{},
}})}
Constraint: &cmpb.ConstraintProto_NoOverlap_2D{&cmpb.NoOverlap2DConstraintProto{},
}})}
}

// AddCircuitConstraint adds a circuit constraint to the model. The circuit constraint is
// defined on a graph where the arcs are present if the corresponding literals are set to true.
func (cp *Builder) AddCircuitConstraint() CircuitConstraint {
return CircuitConstraint{cp.appendConstraint(&cmpb.ConstraintProto{
Constraint: &cmpb.ConstraintProto_Circuit{
&cmpb.CircuitConstraintProto{},
Constraint: &cmpb.ConstraintProto_Circuit{&cmpb.CircuitConstraintProto{},
}})}
}

// AddMultipleCircuitConstraint adds a multiple circuit constraint to the model, aka the "VRP"
// (Vehicle Routing Problem) constraint.
func (cp *Builder) AddMultipleCircuitConstraint() MultipleCircuitConstraint {
return MultipleCircuitConstraint{cp.appendConstraint(&cmpb.ConstraintProto{
Constraint: &cmpb.ConstraintProto_Routes{
&cmpb.RoutesConstraintProto{},
}})}
Constraint: &cmpb.ConstraintProto_Routes{&cmpb.RoutesConstraintProto{},
}})}
}

// AddAllowedAssignments adds an allowed assignments constraint to the model. When all variables
Expand All @@ -937,9 +927,8 @@ func (cp *Builder) AddAllowedAssignments(vars ...IntVar) TableConstraint {
}

return TableConstraint{cp.appendConstraint(&cmpb.ConstraintProto{
Constraint: &cmpb.ConstraintProto_Table{
&cmpb.TableConstraintProto{Vars: varsInd},
}})}
Constraint: &cmpb.ConstraintProto_Table{&cmpb.TableConstraintProto{Vars: varsInd},
}})}
}

// AddReservoirConstraint adds a reservoir constraint with optional refill/emptying events.
Expand All @@ -961,12 +950,10 @@ func (cp *Builder) AddAllowedAssignments(vars ...IntVar) TableConstraint {
// optional events incrementally after construction.
func (cp *Builder) AddReservoirConstraint(min, max int64) ReservoirConstraint {
return ReservoirConstraint{
cp.appendConstraint(
&cmpb.ConstraintProto{
Constraint: &cmpb.ConstraintProto_Reservoir{
&cmpb.ReservoirConstraintProto{
MinLevel: min, MaxLevel: max,
}}},
cp.appendConstraint(&cmpb.ConstraintProto{
Constraint: &cmpb.ConstraintProto_Reservoir{&cmpb.ReservoirConstraintProto{
MinLevel: min, MaxLevel: max,
}}},
), cp.NewConstant(1).Index()}
}

Expand Down Expand Up @@ -1001,12 +988,11 @@ func (cp *Builder) AddAutomaton(transitionVars []IntVar, startState int64, final
transitions = append(transitions, int32(v.Index()))
}
return AutomatonConstraint{cp.appendConstraint(&cmpb.ConstraintProto{
Constraint: &cmpb.ConstraintProto_Automaton{
&cmpb.AutomatonConstraintProto{
Vars: transitions,
StartingState: startState,
FinalStates: finalStates,
}},
Constraint: &cmpb.ConstraintProto_Automaton{&cmpb.AutomatonConstraintProto{
Vars: transitions,
StartingState: startState,
FinalStates: finalStates,
}},
})}
}

Expand All @@ -1015,11 +1001,10 @@ func (cp *Builder) AddAutomaton(transitionVars []IntVar, startState int64, final
// capacity.
func (cp *Builder) AddCumulative(capacity LinearArgument) CumulativeConstraint {
return CumulativeConstraint{cp.appendConstraint(&cmpb.ConstraintProto{
Constraint: &cmpb.ConstraintProto_Cumulative{
&cmpb.CumulativeConstraintProto{
Capacity: capacity.asLinearExpressionProto(),
},
}})}
Constraint: &cmpb.ConstraintProto_Cumulative{&cmpb.CumulativeConstraintProto{
Capacity: capacity.asLinearExpressionProto(),
}},
})}
}

// Minimize adds a linear minimization objective.
Expand Down Expand Up @@ -1141,7 +1126,7 @@ func (cp *Builder) AddDecisionStrategy(vars []IntVar, vs cmpb.DecisionStrategyPr
Variables: indices,
VariableSelectionStrategy: vs,
DomainReductionStrategy: ds,
})
})
}

// Model returns the built CP model proto. The proto returned is a pointer to the proto in Builder,
Expand Down
3 changes: 1 addition & 2 deletions ortools/sat/go/cpmodel/cp_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import (
"sort"
"testing"

log "github.com/golang/glog"
"github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/testing/protocmp"

log "github.com/golang/glog"
cmpb "github.com/google/or-tools/ortools/sat/proto/cpmodel"
)

Expand Down
4 changes: 2 additions & 2 deletions ortools/sat/go/cpmodel/cp_solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
"sync"
"unsafe"

"google.golang.org/protobuf/proto"

cmpb "github.com/google/or-tools/ortools/sat/proto/cpmodel"
sppb "github.com/google/or-tools/ortools/sat/proto/satparameters"

"google.golang.org/protobuf/proto"
)

/*
Expand Down
16 changes: 4 additions & 12 deletions ortools/sat/go/cpmodel/cp_solver_c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,19 @@
#include "ortools/sat/go/cpmodel/cp_solver_c.h"

#include <atomic>
#include <string>

#include "absl/status/status.h"
#include "absl/strings/internal/memutil.h"
#include "ortools/base/logging.h"
#include "absl/log/check.h"
#include "ortools/base/memutil.h"
#include "ortools/sat/cp_model.pb.h"
#include "ortools/sat/cp_model_solver.h"
#include "ortools/sat/model.h"
#include "ortools/sat/sat_parameters.pb.h"
#include "ortools/util/time_limit.h"

namespace operations_research::sat {

namespace {

char* memdup(const char* s, size_t slen) {
void* copy;
if ((copy = malloc(slen)) == nullptr) return nullptr;
memcpy(copy, s, slen);
return reinterpret_cast<char*>(copy);
}

CpSolverResponse solveWithParameters(std::atomic<bool>* const limit_reached,
const CpModelProto& proto,
const SatParameters& params) {
Expand Down Expand Up @@ -81,7 +73,7 @@ void SolveCpInterruptible(void* const limit_reached, const void* creq,
CHECK(res.SerializeToString(&res_str));

*cres_len = static_cast<int>(res_str.size());
*cres = memdup(res_str.data(), *cres_len);
*cres = strings::memdup(res_str.data(), *cres_len);
CHECK(*cres != nullptr);
}

Expand Down

0 comments on commit b8dad90

Please sign in to comment.