Google OR-Tools
v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
solver.h
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
14
// MathOpt's C API for solving optimization models given as serialized protocol
15
// buffers.
16
//
17
// The MathOpt protocol buffers are used as inputs and outputs for many
18
// functions in this API, defined at ortools/math_opt/.*.proto.
19
// Protocol buffers have a language and machine independent binary format, and
20
// each supported language can serialize a message into this format. This API
21
// represents these serialized messages as void* and a size_t saying how many
22
// bytes long the void* is.
23
//
24
// Every language has a different mechanism for constructing a protocol buffer
25
// and serializing it. This API consumes the serialized proto directly, as it is
26
// designed for providing access to MathOpt from non-C languages that cannot
27
// call C++ functions directly, but can call C functions. Callers are expected
28
// to build protos in their language, serialize them, call these C functions,
29
// and then deserialize the returned bytes in their language.
30
//
31
// See cpp_example.cc for a minimal example of creating and serializing these
32
// protos from C++, calling the C API to solve the model, and then deserializing
33
// the returned protos.
34
#ifndef ORTOOLS_MATH_OPT_CORE_C_API_SOLVER_H_
35
#define ORTOOLS_MATH_OPT_CORE_C_API_SOLVER_H_
36
37
#include <stddef.h>
38
39
#ifdef __cplusplus
40
extern
"C"
{
41
#endif
// __cplusplus
42
43
// Notifies MathOptSolve() if the user has requested that the solve stop early.
44
//
45
// This is passed as an argument to MathOptSolve(). From any thread, before or
46
// after the solve begins, you can trigger interruption with MathOptInterrupt().
47
//
48
// This is an opaque type you create with MathOptNewInterrupter(), pass by
49
// pointer, and then delete with MathOptFreeInterrupter() when done. You cannot
50
// copy or stack allocate this type.
51
struct
MathOptInterrupter
;
52
53
// Returns a new interrupter that has not been triggered. The caller must free
54
// this with MathOptFreeInterrupter().
55
struct
MathOptInterrupter
*
MathOptNewInterrupter
();
56
57
// Frees interrupter, has no effect when interrupter is NULL.
58
void
MathOptFreeInterrupter
(
struct
MathOptInterrupter
* interrupter);
59
60
// Triggers the interrupter.
61
//
62
// Will CHECK fail if interrupter is NULL. This is threadsafe.
63
void
MathOptInterrupt
(
struct
MathOptInterrupter
* interrupter);
64
65
// Checks if the interrupter is triggered.
66
//
67
// Will CHECK fail if interrupter is NULL. This is threadsafe.
68
int
MathOptIsInterrupted
(
const
struct
MathOptInterrupter
* interrupter);
69
70
// Solves an optimization model with MathOpt and returns the result.
71
//
72
// Arguments:
73
// * model: a serialized ModelProto to solve. The function fails if this
74
// cannot be parsed, or if this is NULL and model_size > 0.
75
// * model_size: the size of model in bytes. Must be at most MAX_INT or the
76
// the function fails.
77
// * solver_type: which solver to use, see SolverTypeProto for numeric values.
78
// * interrupter: ignored if NULL. If interrupted before the solve begins, or
79
// from another thread while the solve is running, the solve will
80
// terminate early with whatever results are available. MathOptSolve()
81
// will not change the state (interrupted or not) of interrupter. It is
82
// safe for concurrent calls to MathOptSolve() to share a single
83
// interrupter. The interrupter must survive all calls to MathOptSolve().
84
// * solve_result: an output argument, ignored if NULL. On success,
85
// `*solve_result` is filled with a serialized SolveResultProto from
86
// solving `model`. The caller must free `*solve_result` in this case with
87
// MathOptFree(). On failure, `*solve_result` is set to NULL.
88
// * solve_result_size: an output argument, ignored if NULL. On success,
89
// `*solve_result_size` has the size in bytes of the serialized
90
// SolveResultProto from solving `model` (the size of `*solve_result` if
91
// set). On failure, `*solve_result_size` is set to zero.
92
// * status_msg: an output argument. If NULL, this output is ignored. On
93
// success, `*status_msg` is set to NULL. On failure, `*status_msg` is set
94
// to a null terminated string describing the error. The caller must free
95
// `*status_msg` with MathOptFree() in this case.
96
//
97
// Note that `solve_result_size` holds the size of the serialized proto returned
98
// in `solve_result`. Typically, you should make `solve_result` and
99
// `solve_result_size` either both NULL or both not NULL. You cannot safely
100
// consume `solve_result` without `solve_result_size`.
101
//
102
// Returns 0 if successful and a nonzero value on failure (the value is an
103
// absl::StatusCode enum).
104
int
MathOptSolve
(
const
void
* model,
size_t
model_size,
int
solver_type,
105
struct
MathOptInterrupter
* interrupter,
void
** solve_result,
106
size_t
* solve_result_size,
char
** status_msg);
107
108
// Frees memory allocated by the MathOpt C API, e.g. the solve_result or
109
// status_msg output arguments from MathOptSolve(). If `ptr` is NULL, has no
110
// effect.
111
void
MathOptFree
(
void
* ptr);
112
113
#ifdef __cplusplus
114
}
// extern "C"
115
#endif
// __cplusplus
116
117
#endif
// ORTOOLS_MATH_OPT_CORE_C_API_SOLVER_H_
MathOptFree
void MathOptFree(void *ptr)
Definition
solver.cc:147
MathOptSolve
int MathOptSolve(const void *model, size_t model_size, int solver_type, struct MathOptInterrupter *interrupter, void **solve_result, size_t *solve_result_size, char **status_msg)
Definition
solver.cc:112
MathOptInterrupt
void MathOptInterrupt(struct MathOptInterrupter *interrupter)
Definition
solver.cc:103
MathOptFreeInterrupter
void MathOptFreeInterrupter(struct MathOptInterrupter *interrupter)
Definition
solver.cc:99
MathOptIsInterrupted
int MathOptIsInterrupted(const struct MathOptInterrupter *interrupter)
MathOptNewInterrupter
struct MathOptInterrupter * MathOptNewInterrupter()
Definition
solver.cc:97
MathOptInterrupter
Definition
solver.cc:35
ortools
math_opt
core
c_api
solver.h
Generated by
1.15.0