Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
cp_solver_c.cc
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
15
16#include <atomic>
17#include <string>
18
19#include "absl/log/check.h"
21#include "ortools/sat/cp_model.pb.h"
23#include "ortools/sat/model.h"
24#include "ortools/sat/sat_parameters.pb.h"
26
28
29namespace {
30
31CpSolverResponse solveWithParameters(std::atomic<bool>* const limit_reached,
32 const CpModelProto& proto,
33 const SatParameters& params) {
34 Model model;
35 model.Add(NewSatParameters(params));
36 model.GetOrCreate<TimeLimit>()->RegisterExternalBooleanAsLimit(limit_reached);
37 return SolveCpModel(proto, &model);
38}
39
40} // namespace
41
42extern "C" {
43
44void SolveCpModelWithParameters(const void* creq, int creq_len,
45 const void* cparams, int cparams_len,
46 void** cres, int* cres_len) {
47 return SolveCpInterruptible(nullptr, creq, creq_len, cparams, cparams_len,
48 cres, cres_len);
49}
50
51void* SolveCpNewAtomicBool() { return new std::atomic<bool>(false); }
52
53void SolveCpDestroyAtomicBool(void* const atomic_bool) {
54 delete static_cast<std::atomic<bool>*>(atomic_bool);
55}
56
57void SolveCpStopSolve(void* const atomic_bool) {
58 *static_cast<std::atomic<bool>*>(atomic_bool) = true;
59}
60
61void SolveCpInterruptible(void* const limit_reached, const void* creq,
62 int creq_len, const void* cparams, int cparams_len,
63 void** cres, int* cres_len) {
64 CpModelProto req;
65 CHECK(req.ParseFromArray(creq, creq_len));
66
67 SatParameters params;
68 CHECK(params.ParseFromArray(cparams, cparams_len));
69
70 CpSolverResponse res = solveWithParameters(
71 static_cast<std::atomic<bool>*>(limit_reached), req, params);
72
73 std::string res_str;
74 CHECK(res.SerializeToString(&res_str));
75
76 *cres_len = static_cast<int>(res_str.size());
77 *cres = strings::memdup(res_str.data(), *cres_len);
78 CHECK(*cres != nullptr);
79}
80
81} // extern "C"
82
83} // namespace operations_research::sat
std::function< SatParameters(Model *)> NewSatParameters(const std::string &params)
void SolveCpInterruptible(void *const limit_reached, const void *creq, int creq_len, const void *cparams, int cparams_len, void **cres, int *cres_len)
void SolveCpModelWithParameters(const void *creq, int creq_len, const void *cparams, int cparams_len, void **cres, int *cres_len)
void SolveCpStopSolve(void *const atomic_bool)
CpSolverResponse SolveCpModel(const CpModelProto &model_proto, Model *model)
void SolveCpDestroyAtomicBool(void *const atomic_bool)
char * memdup(const char *s, size_t slen)
Definition memutil.h:23