Google OR-Tools v9.14
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 <string>
17
18#include "absl/log/check.h"
22#include "ortools/sat/model.h"
24#include "ortools/sat/util.h"
25
27namespace {
28
29struct CpSatEnv {
30 CpSatEnv() : shared_time_limit(model.GetOrCreate<ModelSharedTimeLimit>()) {}
31 void StopSearch() { shared_time_limit->Stop(); }
32
33 Model model;
34 ModelSharedTimeLimit* shared_time_limit;
35};
36
37} // namespace
38} // namespace operations_research::sat
39
40extern "C" {
41
42void SolveCpModelWithParameters(const void* creq, int creq_len,
43 const void* cparams, int cparams_len,
44 void** cres, int* cres_len) {
45 operations_research::sat::CpSatEnv env;
46 SolveCpInterruptible(&env, creq, creq_len, cparams, cparams_len, cres,
47 cres_len);
48}
49
50void* SolveCpNewEnv() { return new operations_research::sat::CpSatEnv(); }
51
52void SolveCpDestroyEnv(void* const cenv) {
53 delete static_cast<operations_research::sat::CpSatEnv*>(cenv);
54}
55
56void SolveCpStopSearch(void* cenv) {
57 static_cast<operations_research::sat::CpSatEnv*>(cenv)->StopSearch();
58}
59
60void SolveCpInterruptible(void* const cenv, const void* creq, int creq_len,
61 const void* cparams, int cparams_len, void** cres,
62 int* cres_len) {
64 CHECK(req.ParseFromArray(creq, creq_len));
65
67 CHECK(params.ParseFromArray(cparams, cparams_len));
68
69 operations_research::sat::CpSatEnv* env =
70 static_cast<operations_research::sat::CpSatEnv*>(cenv);
71
72 env->model.Add(NewSatParameters(params));
74 SolveCpModel(req, &env->model);
75
76 std::string res_str;
77 CHECK(res.SerializeToString(&res_str));
78
79 *cres_len = static_cast<int>(res_str.size());
80 *cres = strings::memdup(res_str.data(), *cres_len);
81 CHECK(*cres != nullptr);
82}
83
84} // extern "C"
T Add(std::function< T(Model *)> f)
Definition model.h:87
void * SolveCpNewEnv()
void SolveCpInterruptible(void *const cenv, const void *creq, int creq_len, const void *cparams, int cparams_len, void **cres, int *cres_len)
void SolveCpDestroyEnv(void *const cenv)
void SolveCpStopSearch(void *cenv)
void SolveCpModelWithParameters(const void *creq, int creq_len, const void *cparams, int cparams_len, void **cres, int *cres_len)
void StopSearch(Model *model)
Stops the current search.
char * memdup(const char *s, size_t slen)
Definition memutil.h:21