Files
ortools-clone/ortools/math_opt/solvers/glop_solver.h
2026-01-07 15:50:13 +01:00

116 lines
4.4 KiB
C++

// Copyright 2010-2025 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ORTOOLS_MATH_OPT_SOLVERS_GLOP_SOLVER_H_
#define ORTOOLS_MATH_OPT_SOLVERS_GLOP_SOLVER_H_
#include <stdint.h>
#include <memory>
#include "absl/base/nullability.h"
#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/time/time.h"
#include "absl/types/span.h"
#include "ortools/glop/lp_solver.h"
#include "ortools/glop/parameters.pb.h"
#include "ortools/lp_data/lp_data.h"
#include "ortools/lp_data/lp_types.h"
#include "ortools/math_opt/callback.pb.h"
#include "ortools/math_opt/core/inverted_bounds.h"
#include "ortools/math_opt/core/solver_interface.h"
#include "ortools/math_opt/infeasible_subsystem.pb.h"
#include "ortools/math_opt/model.pb.h"
#include "ortools/math_opt/model_parameters.pb.h"
#include "ortools/math_opt/model_update.pb.h"
#include "ortools/math_opt/parameters.pb.h"
#include "ortools/math_opt/result.pb.h"
#include "ortools/math_opt/solution.pb.h"
#include "ortools/math_opt/sparse_containers.pb.h"
#include "ortools/util/solve_interrupter.h"
namespace operations_research {
namespace math_opt {
class GlopSolver : public SolverInterface {
public:
static absl::StatusOr<std::unique_ptr<SolverInterface>> New(
const ModelProto& model, const InitArgs& init_args);
absl::StatusOr<SolveResultProto> Solve(
const SolveParametersProto& parameters,
const ModelSolveParametersProto& model_parameters,
MessageCallback message_cb,
const CallbackRegistrationProto& callback_registration, Callback cb,
const SolveInterrupter* absl_nullable interrupter) override;
absl::StatusOr<bool> Update(const ModelUpdateProto& model_update) override;
absl::StatusOr<ComputeInfeasibleSubsystemResultProto>
ComputeInfeasibleSubsystem(
const SolveParametersProto& parameters, MessageCallback message_cb,
const SolveInterrupter* absl_nullable interrupter) override;
// Returns the merged parameters and a list of warnings from any parameter
// settings that are invalid for this solver.
static absl::StatusOr<glop::GlopParameters> MergeSolveParameters(
const SolveParametersProto& solve_parameters, bool setting_initial_basis,
bool has_message_callback, bool is_maximization);
private:
GlopSolver();
void AddVariables(const VariablesProto& variables);
void AddLinearConstraints(const LinearConstraintsProto& linear_constraints);
void DeleteVariables(absl::Span<const int64_t> ids_to_delete);
void DeleteLinearConstraints(absl::Span<const int64_t> ids_to_delete);
void SetOrUpdateObjectiveCoefficients(
const SparseDoubleVectorProto& linear_objective_coefficients);
void SetOrUpdateConstraintMatrix(
const SparseDoubleMatrixProto& linear_constraint_matrix);
void UpdateVariableBounds(const VariableUpdatesProto& variable_updates);
void UpdateLinearConstraintBounds(
const LinearConstraintUpdatesProto& linear_constraint_updates);
// Returns the ids of variables and linear constraints with inverted bounds.
InvertedBounds ListInvertedBounds() const;
void FillSolution(glop::ProblemStatus status,
const ModelSolveParametersProto& model_parameters,
SolveResultProto& solve_result);
absl::StatusOr<SolveResultProto> MakeSolveResult(
glop::ProblemStatus status,
const ModelSolveParametersProto& model_parameters,
const SolveInterrupter* absl_nullable interrupter,
absl::Duration solve_time);
absl::Status FillSolveStats(absl::Duration solve_time,
SolveStatsProto& solve_stats);
void SetGlopBasis(const BasisProto& basis);
glop::LinearProgram linear_program_;
glop::LPSolver lp_solver_;
absl::flat_hash_map<int64_t, glop::ColIndex> variables_;
absl::flat_hash_map<int64_t, glop::RowIndex> linear_constraints_;
};
} // namespace math_opt
} // namespace operations_research
#endif // ORTOOLS_MATH_OPT_SOLVERS_GLOP_SOLVER_H_