OR-Tools  9.3
model_builder_helper.h
Go to the documentation of this file.
1// Copyright 2010-2021 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#ifndef OR_TOOLS_MODEL_BUILDER_WRAPPERS_MODEL_BUILDER_HELPER_H_
15#define OR_TOOLS_MODEL_BUILDER_WRAPPERS_MODEL_BUILDER_HELPER_H_
16
17#include <atomic>
18#include <functional>
19#include <string>
20
21#include "ortools/linear_solver/linear_solver.pb.h"
24
25namespace operations_research {
26
27// The arguments of the functions defined below must follow these rules
28// to be wrapped by SWIG correctly:
29// 1) Their types must include the full operations_research::
30// namespace.
31// 2) Their names must correspond to the ones declared in the .i
32// file (see the java/ and csharp/ subdirectories).
33
34// Helper for importing/exporting models and model protobufs.
35//
36// Wrapping global function is brittle with SWIG. It is much easier to
37// wrap static class methods.
38//
39// Note: all these methods rely on c++ code that uses absl::Status or
40// absl::StatusOr. Unfortunately, these are inconsistently wrapped in non C++
41// languages. As a consequence, we need to provide an API that does not involve
42// absl::Status or absl::StatusOr.
44 public:
45 static std::string ExportModelProtoToMpsString(
46 const operations_research::MPModelProto& input_model,
49
50 static std::string ExportModelProtoToLpString(
51 const operations_research::MPModelProto& input_model,
54
55 static operations_research::MPModelProto ImportFromMpsString(
56 const std::string& mps_string);
57 static operations_research::MPModelProto ImportFromMpsFile(
58 const std::string& mps_file);
59 static operations_research::MPModelProto ImportFromLpString(
60 const std::string& lp_string);
61 static operations_research::MPModelProto ImportFromLpFile(
62 const std::string& lp_file);
63};
64
65// Simple director class for C#.
67 public:
68 virtual ~LogCallback() {}
69 virtual void NewMessage(const std::string& message) = 0;
70};
71
72// Class used to solve a request. This class is not meant to be exposed to the
73// public. Its responsibility is to bridge the MPModelProto in the non-C++
74// languages with the C++ Solve method.
75//
76// It contains 2 helper objects: a logger, and an atomic bool to interrupt
77// search.
79 public:
80 operations_research::MPSolutionResponse Solve(
81 const operations_research::MPModelRequest& request);
82
83 // Returns true if the interrupt signal was correctly sent, that is if the
84 // underlying solver supports it.
85 bool InterruptSolve();
86
87 void SetLogCallback(std::function<void(const std::string&)> log_callback);
89
90 private:
91 std::atomic<bool> interrupt_solve_;
92 std::function<void(const std::string&)> log_callback_;
93};
94
95} // namespace operations_research
96
97#endif // OR_TOOLS_MODEL_BUILDER_WRAPPERS_MODEL_BUILDER_HELPER_H_
virtual void NewMessage(const std::string &message)=0
static operations_research::MPModelProto ImportFromLpFile(const std::string &lp_file)
static operations_research::MPModelProto ImportFromMpsString(const std::string &mps_string)
static operations_research::MPModelProto ImportFromMpsFile(const std::string &mps_file)
static std::string ExportModelProtoToMpsString(const operations_research::MPModelProto &input_model, const operations_research::MPModelExportOptions &options=MPModelExportOptions())
static operations_research::MPModelProto ImportFromLpString(const std::string &lp_string)
static std::string ExportModelProtoToLpString(const operations_research::MPModelProto &input_model, const operations_research::MPModelExportOptions &options=MPModelExportOptions())
void SetLogCallbackFromDirectorClass(LogCallback *log_callback)
void SetLogCallback(std::function< void(const std::string &)> log_callback)
operations_research::MPSolutionResponse Solve(const operations_research::MPModelRequest &request)
Collection of objects used to extend the Constraint Solver library.
std::string message
Definition: trace.cc:398