Skip to content

Commit

Permalink
sat: build go samples
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizux committed Sep 27, 2024
1 parent 9a3c8a2 commit 820be10
Show file tree
Hide file tree
Showing 24 changed files with 82 additions and 60 deletions.
2 changes: 0 additions & 2 deletions ortools/sat/go/cpmodel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ go_library(
srcs = [
"cp_model.go",
"cp_solver.go",
#"cp_solver_c.cc",
#"cp_solver_c.h",
"domain.go",
],
cdeps = [":cp_solver_c"],
Expand Down
45 changes: 24 additions & 21 deletions ortools/sat/samples/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load(":code_samples.bzl", "code_sample_cc_py", "code_sample_java", "code_sample_py")
load(":code_samples.bzl",
"code_sample_cc_go_py", "code_sample_cc_py",
"code_sample_go", "code_sample_java", "code_sample_py")

code_sample_py(name = "all_different_except_zero_sample_sat")

Expand All @@ -23,19 +25,20 @@ code_sample_cc_py(name = "assignment_task_sizes_sat")

code_sample_cc_py(name = "assignment_teams_sat")

code_sample_cc_py(name = "assumptions_sample_sat")
code_sample_cc_go_py(name = "assumptions_sample_sat")

code_sample_cc_py(name = "binpacking_problem_sat")
code_sample_cc_go_py(name = "binpacking_problem_sat")

code_sample_py(name = "bin_packing_sat")

code_sample_py(name = "bool_and_int_var_product_sample_sat")

code_sample_cc_py(name = "bool_or_sample_sat")
code_sample_cc_go_py(name = "bool_or_sample_sat")

code_sample_go(name = "boolean_product_sample_sat")
code_sample_py(name = "boolean_product_sample_sat")

code_sample_cc_py(name = "channeling_sample_sat")
code_sample_cc_go_py(name = "channeling_sample_sat")

code_sample_cc_py(name = "clone_model_sample_sat")

Expand All @@ -45,55 +48,55 @@ code_sample_cc_py(name = "cp_sat_example")

code_sample_py(name = "cumulative_variable_profile_sample_sat")

code_sample_cc_py(name = "earliness_tardiness_cost_sample_sat")
code_sample_cc_go_py(name = "earliness_tardiness_cost_sample_sat")

code_sample_py(name = "index_first_boolvar_true_sample_sat")

code_sample_py(name = "interval_relations_sample_sat")

code_sample_cc_py(name = "interval_sample_sat")
code_sample_cc_go_py(name = "interval_sample_sat")

code_sample_cc_py(name = "minimal_jobshop_sat")

code_sample_cc_py(name = "literal_sample_sat")
code_sample_cc_go_py(name = "literal_sample_sat")

code_sample_cc_py(name = "multiple_knapsack_sat")

code_sample_cc_py(name = "non_linear_sat")

code_sample_cc_py(name = "nqueens_sat")
code_sample_cc_go_py(name = "no_overlap_sample_sat")

code_sample_cc_py(name = "nurses_sat")
code_sample_cc_go_py(name = "nqueens_sat")

code_sample_cc_py(name = "optional_interval_sample_sat")
code_sample_cc_go_py(name = "nurses_sat")

code_sample_cc_py(name = "no_overlap_sample_sat")
code_sample_cc_go_py(name = "optional_interval_sample_sat")

code_sample_py(name = "overlapping_intervals_sample_sat")

code_sample_cc_py(name = "rabbits_and_pheasants_sat")
code_sample_cc_go_py(name = "rabbits_and_pheasants_sat")

code_sample_py(name = "ranking_circuit_sample_sat")

code_sample_cc_py(name = "ranking_sample_sat")
code_sample_cc_go_py(name = "ranking_sample_sat")

code_sample_cc_py(name = "reified_sample_sat")
code_sample_cc_go_py(name = "reified_sample_sat")

code_sample_cc_py(name = "schedule_requests_sat")

code_sample_py(name = "scheduling_with_calendar_sample_sat")

code_sample_cc_py(name = "simple_sat_program")
code_sample_cc_go_py(name = "search_for_all_solutions_sample_sat")

code_sample_cc_py(name = "search_for_all_solutions_sample_sat")
code_sample_cc_go_py(name = "simple_sat_program")

code_sample_cc_py(name = "solution_hinting_sample_sat")
code_sample_cc_go_py(name = "solution_hinting_sample_sat")

code_sample_cc_py(name = "solve_and_print_intermediate_solutions_sample_sat")
code_sample_cc_go_py(name = "solve_and_print_intermediate_solutions_sample_sat")

code_sample_cc_py(name = "step_function_sample_sat")
code_sample_cc_go_py(name = "solve_with_time_limit_sample_sat")

code_sample_cc_py(name = "solve_with_time_limit_sample_sat")
code_sample_cc_go_py(name = "step_function_sample_sat")

code_sample_cc_py(name = "stop_after_n_solutions_sample_sat")

Expand Down
2 changes: 1 addition & 1 deletion ortools/sat/samples/assumptions_sample_sat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"fmt"

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

func assumptionsSampleSat() error {
Expand Down
2 changes: 1 addition & 1 deletion ortools/sat/samples/binpacking_problem_sat.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"fmt"

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

const (
Expand Down
2 changes: 1 addition & 1 deletion ortools/sat/samples/bool_or_sample_sat.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package main

import (
"ortools/sat/go/cpmodel"
"github.com/google/or-tools/ortools/sat/go/cpmodel"
)

func boolOrSampleSat() {
Expand Down
6 changes: 3 additions & 3 deletions ortools/sat/samples/boolean_product_sample_sat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"fmt"

log "github.com/golang/glog"
"github.com/google/or-tools/ortools/sat/go/cpmodel"
sppb "github.com/google/or-tools/ortools/sat/proto/satparameters"
"google.golang.org/protobuf/proto"
"ortools/sat/go/cpmodel"
)

func booleanProductSample() error {
Expand All @@ -44,11 +44,11 @@ func booleanProductSample() error {
}
// Set `fill_additional_solutions_in_response` and `enumerate_all_solutions` to true so
// the solver returns all solutions found.
params := sppb.SatParameters_builder{
params := &sppb.SatParameters{
FillAdditionalSolutionsInResponse: proto.Bool(true),
EnumerateAllSolutions: proto.Bool(true),
SolutionPoolSize: proto.Int32(4),
}.Build()
}
response, err := cpmodel.SolveCpModelWithParameters(m, params)
if err != nil {
return fmt.Errorf("failed to solve the model: %w", err)
Expand Down
6 changes: 3 additions & 3 deletions ortools/sat/samples/channeling_sample_sat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
"fmt"

log "github.com/golang/glog"
"github.com/google/or-tools/ortools/sat/go/cpmodel"
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"
"ortools/sat/go/cpmodel"
)

func channelingSampleSat() error {
Expand Down Expand Up @@ -53,12 +53,12 @@ func channelingSampleSat() error {
if err != nil {
return fmt.Errorf("failed to instantiate the CP model: %w", err)
}
params := sppb.SatParameters_builder{
params := &sppb.SatParameters{
FillAdditionalSolutionsInResponse: proto.Bool(true),
EnumerateAllSolutions: proto.Bool(true),
SolutionPoolSize: proto.Int32(11),
SearchBranching: sppb.SatParameters_FIXED_SEARCH.Enum(),
}.Build()
}
response, err := cpmodel.SolveCpModelWithParameters(m, params)
if err != nil {
return fmt.Errorf("failed to solve the model: %w", err)
Expand Down
25 changes: 23 additions & 2 deletions ortools/sat/samples/code_samples.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@

"""Helper macro to compile and test code samples."""

load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_test")
load("@pip_deps//:requirements.bzl", "requirement")
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test")
load("@rules_python//python:defs.bzl", "py_binary", "py_test")

def code_sample_cc(name):
native.cc_binary(
cc_binary(
name = name + "_cc",
srcs = [name + ".cc"],
deps = [
Expand All @@ -28,7 +30,7 @@ def code_sample_cc(name):
],
)

native.cc_test(
cc_test(
name = name + "_cc_test",
size = "small",
srcs = [name + ".cc"],
Expand All @@ -41,6 +43,20 @@ def code_sample_cc(name):
],
)

def code_sample_go(name):
go_test(
name = name + "_go_test",
size = "small",
srcs = [name + ".go"],
deps = [
"//ortools/sat:cp_model_go_proto",
"//ortools/sat:sat_parameters_go_proto",
"//ortools/sat/go/cpmodel",
"@com_github_golang_glog//:glog",
"@org_golang_google_protobuf//proto",
],
)

def code_sample_py(name):
py_binary(
name = name + "_py3",
Expand Down Expand Up @@ -74,6 +90,11 @@ def code_sample_py(name):
srcs_version = "PY3",
)

def code_sample_cc_go_py(name):
code_sample_cc(name = name)
code_sample_go(name = name)
code_sample_py(name = name)

def code_sample_cc_py(name):
code_sample_cc(name = name)
code_sample_py(name = name)
Expand Down
6 changes: 3 additions & 3 deletions ortools/sat/samples/earliness_tardiness_cost_sample_sat.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
"fmt"

log "github.com/golang/glog"
"github.com/google/or-tools/ortools/sat/go/cpmodel"
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"
"ortools/sat/go/cpmodel"
)

const (
Expand Down Expand Up @@ -62,12 +62,12 @@ func earlinessTardinessCostSampleSat() error {
if err != nil {
return fmt.Errorf("failed to instantiate the CP model: %w", err)
}
params := sppb.SatParameters_builder{
params := &sppb.SatParameters{
FillAdditionalSolutionsInResponse: proto.Bool(true),
EnumerateAllSolutions: proto.Bool(true),
SolutionPoolSize: proto.Int32(21),
SearchBranching: sppb.SatParameters_FIXED_SEARCH.Enum(),
}.Build()
}
response, err := cpmodel.SolveCpModelWithParameters(m, params)
if err != nil {
return fmt.Errorf("failed to solve the model: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion ortools/sat/samples/interval_sample_sat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"fmt"

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

const horizon = 100
Expand Down
2 changes: 1 addition & 1 deletion ortools/sat/samples/literal_sample_sat.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package main

import (
log "github.com/golang/glog"
"ortools/sat/go/cpmodel"
"github.com/google/or-tools/ortools/sat/go/cpmodel"
)

func literalSampleSat() {
Expand Down
2 changes: 1 addition & 1 deletion ortools/sat/samples/no_overlap_sample_sat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"fmt"

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

const horizon = 21 // 3 weeks
Expand Down
2 changes: 1 addition & 1 deletion ortools/sat/samples/nqueens_sat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"fmt"

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

const boardSize = 8
Expand Down
2 changes: 1 addition & 1 deletion ortools/sat/samples/nurses_sat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"fmt"

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

const (
Expand Down
2 changes: 1 addition & 1 deletion ortools/sat/samples/optional_interval_sample_sat.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"fmt"

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

const horizon = 100
Expand Down
2 changes: 1 addition & 1 deletion ortools/sat/samples/rabbits_and_pheasants_sat.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"fmt"

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

const numAnimals = 20
Expand Down
2 changes: 1 addition & 1 deletion ortools/sat/samples/ranking_sample_sat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"fmt"

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

const (
Expand Down
2 changes: 1 addition & 1 deletion ortools/sat/samples/reified_sample_sat.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package main

import (
"ortools/sat/go/cpmodel"
"github.com/google/or-tools/ortools/sat/go/cpmodel"
)

func reifiedSampleSat() {
Expand Down
6 changes: 3 additions & 3 deletions ortools/sat/samples/search_for_all_solutions_sample_sat.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
"fmt"

log "github.com/golang/glog"
"github.com/google/or-tools/ortools/sat/go/cpmodel"
sppb "github.com/google/or-tools/ortools/sat/proto/satparameters"
"google.golang.org/protobuf/proto"
"ortools/sat/go/cpmodel"
)

func searchForAllSolutionsSampleSat() error {
Expand All @@ -41,11 +41,11 @@ func searchForAllSolutionsSampleSat() error {
// Currently, the CpModelBuilder does not allow for callbacks, so each feasible solution cannot
// be printed while solving. However, the CP Solver can return all of the enumerated solutions
// in the response by setting the following parameters.
params := sppb.SatParameters_builder{
params := &sppb.SatParameters{
EnumerateAllSolutions: proto.Bool(true),
FillAdditionalSolutionsInResponse: proto.Bool(true),
SolutionPoolSize: proto.Int32(27),
}.Build()
}
response, err := cpmodel.SolveCpModelWithParameters(m, params)
if err != nil {
return fmt.Errorf("failed to solve the model: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion ortools/sat/samples/simple_sat_program.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"fmt"

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

func simpleSatProgram() error {
Expand Down
Loading

0 comments on commit 820be10

Please sign in to comment.