Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
solver_port_c_test.c
Go to the documentation of this file.
1// Copyright 2010-2024 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// A minimal use of MathOptSolve() from C to ensure the code compiles in C.
15// NOTE: this file is .c, not .cc, so the blaze will test this compiles as C.
16
17#include <malloc.h>
18#include <stddef.h>
19#include <stdio.h>
20#include <stdlib.h>
21
23
25 // Empty model
26 const size_t model_size = 0;
27 const void* model = NULL;
28 // Solve with glop
29 const int solver_type = 3;
30 void* solve_result = NULL;
31 size_t solve_result_size = 0;
32 char* status_msg = NULL;
33 const int error = MathOptSolve(model, model_size, solver_type, /*interrupter=*/NULL, &solve_result,
34 &solve_result_size, &status_msg);
35 if (error) {
36 printf("error on MathOptSolve, status code: %d, status message: %s", error, status_msg);
37 // If you handle the error instead of crashing, be sure to free status_msg.
38 abort();
39 }
40 MathOptFree(status_msg);
41 MathOptFree(solve_result);
42}
43
45 // Empty model
46 const size_t model_size = 0;
47 const void* model = NULL;
48 // Solve with glop
49 const int solver_type = 3;
50 void* solve_result = NULL;
51 size_t solve_result_size = 0;
52 char* status_msg = NULL;
53 struct MathOptInterrupter* interrupter = MathOptNewInterrupter();
54 MathOptInterrupt(interrupter);
55 const int error = MathOptSolve(model, model_size, solver_type, /*interrupter=*/NULL, &solve_result,
56 &solve_result_size, &status_msg);
57 if (error) {
58 printf("error on MathOptSolve, status code: %d, status message: %s", error, status_msg);
59 // If you handle the error instead of crashing, be sure to free status_msg.
60 abort();
61 }
62 if(MathOptIsInterrupted(interrupter) == 0) {
63 printf("interrupter should be interrupted");
64 abort();
65 }
66 MathOptFreeInterrupter(interrupter);
67 MathOptFree(status_msg);
68 MathOptFree(solve_result);
69}
70
71int main(int argc, char** argv) {
74 return EXIT_SUCCESS;
75}
void MathOptFree(void *ptr)
Definition solver.cc:147
int MathOptIsInterrupted(const MathOptInterrupter *interrupter)
Definition solver.cc:107
void MathOptInterrupt(MathOptInterrupter *interrupter)
Definition solver.cc:103
int MathOptSolve(const void *model, const size_t model_size, const int solver_type, MathOptInterrupter *const interrupter, void **solve_result, size_t *solve_result_size, char **status_msg)
Definition solver.cc:112
MathOptInterrupter * MathOptNewInterrupter()
Definition solver.cc:97
void MathOptFreeInterrupter(MathOptInterrupter *interrupter)
Frees interrupter, has no effect when interrupter is NULL.
Definition solver.cc:99
GRBmodel * model
int main(int argc, char **argv)
void TestSolveEmptyModel()
void TestInterruptSolveEmptyModel()