scip-sys 0.1.21

Bindings for the C SCIP solver.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*                                                                           */
/*               This file is part of the program and library                */
/*    PaPILO --- Parallel Presolve for Integer and Linear Optimization       */
/*                                                                           */
/* Copyright (C) 2020-2022 Konrad-Zuse-Zentrum                               */
/*                     fuer Informationstechnik Berlin                       */
/*                                                                           */
/* This program is free software: you can redistribute it and/or modify      */
/* it under the terms of the GNU Lesser General Public License as published  */
/* by the Free Software Foundation, either version 3 of the License, or      */
/* (at your option) any later version.                                       */
/*                                                                           */
/* This program is distributed in the hope that it will be useful,           */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of            */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
/* GNU Lesser General Public License for more details.                       */
/*                                                                           */
/* You should have received a copy of the GNU Lesser General Public License  */
/* along with this program.  If not, see <https://www.gnu.org/licenses/>.    */
/*                                                                           */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#ifndef _PAPILO_INTERFACES_HIGHS_INTERFACE_HPP_
#define _PAPILO_INTERFACES_HIGHS_INTERFACE_HPP_

#include "papilo/misc/String.hpp"
#include "papilo/misc/Vec.hpp"
#include <cassert>
#include <stdexcept>
#include <type_traits>

#include "Highs.h"
#include "papilo/core/Problem.hpp"
#include "papilo/interfaces/SolverInterface.hpp"

namespace papilo
{

template <typename REAL>
class HighsInterface : public SolverInterface<REAL>
{
 private:
   Highs solver;
   HighsOptions opts;
   static constexpr double inf = std::numeric_limits<double>::infinity();

 public:
   HighsInterface() {}

   void
   setTimeLimit( double tlim ) override
   {
      opts.time_limit = tlim;
   }

   void
   setUp( const Problem<REAL>& problem, const Vec<int>& row_maps,
          const Vec<int>& col_maps ) override
   {
      int ncols = problem.getNCols();
      int nrows = problem.getNRows();
      const Vec<String>& varNames = problem.getVariableNames();
      const Vec<String>& consNames = problem.getConstraintNames();
      const VariableDomains<REAL>& domains = problem.getVariableDomains();
      const Objective<REAL>& obj = problem.getObjective();
      const ConstraintMatrix<REAL>& consMatrix = problem.getConstraintMatrix();
      const auto& lhs_values = consMatrix.getLeftHandSides();
      const auto& rhs_values = consMatrix.getRightHandSides();
      const auto& rflags = problem.getRowFlags();

      HighsLp model;

      model.sense_ = ObjSense::kMinimize;
      model.offset_ = double( obj.offset );

      model.num_row_ = nrows;
      model.num_col_ = ncols;

      model.col_cost_.resize( ncols );
      model.col_lower_.resize( ncols );
      model.col_upper_.resize( ncols );
      model.integrality_.resize( ncols );

      model.row_lower_.resize( nrows );
      model.row_upper_.resize( nrows );

      for( int i = 0; i != nrows; ++i )
      {
         model.row_lower_[i] = rflags[i].test( RowFlag::kLhsInf )
                                  ? -inf
                                  : double( lhs_values[i] );
         model.row_upper_[i] =
             rflags[i].test( RowFlag::kRhsInf ) ? inf : double( rhs_values[i] );
      }

      model.a_index_.resize( consMatrix.getNnz() );
      model.a_value_.resize( consMatrix.getNnz() );
      model.a_start_.resize( ncols + 1 );
      model.a_start_[ncols] = consMatrix.getNnz();

      int start = 0;

      for( int i = 0; i < ncols; ++i )
      {
         assert( !domains.flags[i].test( ColFlag::kInactive ) );

         model.col_lower_[i] = domains.flags[i].test( ColFlag::kLbInf )
                                  ? -inf
                                  : double( domains.lower_bounds[i] );
         model.col_upper_[i] = domains.flags[i].test( ColFlag::kUbInf )
                                  ? inf
                                  : double( domains.upper_bounds[i] );

         model.col_cost_[i] = double( obj.coefficients[i] );

         model.integrality_[i] =
             domains.flags[i].test( ColFlag::kImplInt )
                 ? HighsVarType::kImplicitInteger
                 : ( domains.flags[i].test( ColFlag::kIntegral )
                         ? HighsVarType::kInteger
                         : HighsVarType::kContinuous );

         auto colvec = consMatrix.getColumnCoefficients( i );

         int collen = colvec.getLength();
         const int* colrows = colvec.getIndices();
         const REAL* colvals = colvec.getValues();

         model.a_start_[i] = start;

         for( int k = 0; k != collen; ++k )
         {
            model.a_value_[start + k] = double( colvals[k] );
            model.a_index_[start + k] = colrows[k];
         }

         start += collen;
      }

      solver.passModel( std::move( model ) );
   }

   void
   setUp( const Problem<REAL>& problem, const Vec<int>& row_maps,
          const Vec<int>& col_maps, const Components& components,
          const ComponentInfo& component ) override
   {
      const Vec<String>& varNames = problem.getVariableNames();
      const Vec<String>& consNames = problem.getConstraintNames();
      const VariableDomains<REAL>& domains = problem.getVariableDomains();
      const Objective<REAL>& obj = problem.getObjective();
      const auto& consMatrix = problem.getConstraintMatrix();
      const auto& lhs_values = consMatrix.getLeftHandSides();
      const auto& rhs_values = consMatrix.getRightHandSides();
      const auto& rflags = problem.getRowFlags();
      const int* rowset = components.getComponentsRows( component.componentid );
      const int* colset = components.getComponentsCols( component.componentid );
      int numrows = components.getComponentsNumRows( component.componentid );
      int numcols = components.getComponentsNumCols( component.componentid );

      HighsLp model;

      /* set the objective sense and offset */
      model.sense_ = ObjSense::kMinimize;
      model.offset_ = 0;

      model.num_row_ = numrows;
      model.num_col_ = numcols;

      model.col_cost_.resize( numcols );
      model.col_lower_.resize( numcols );
      model.col_upper_.resize( numcols );
      model.integrality_.resize( numcols );

      model.row_lower_.resize( numrows );
      model.row_upper_.resize( numrows );

      for( int i = 0; i != numrows; ++i )
      {
         int row = rowset[i];

         assert( components.getRowComponentIdx( row ) == i );

         model.row_lower_[i] = rflags[row].test( RowFlag::kLhsInf )
                                  ? -inf
                                  : double( lhs_values[row] );
         model.row_upper_[i] = rflags[row].test( RowFlag::kRhsInf )
                                  ? inf
                                  : double( rhs_values[row] );
      }

      model.a_index_.resize( component.nnonz );
      model.a_value_.resize( component.nnonz );
      model.a_start_.resize( numcols + 1 );
      model.a_start_[numcols] = component.nnonz;

      int start = 0;

      for( int i = 0; i != numcols; ++i )
      {
         int col = colset[i];

         assert( components.getColComponentIdx( col ) == i );
         assert( !domains.flags[col].test( ColFlag::kInactive ) );

         model.col_lower_[i] = domains.flags[col].test( ColFlag::kLbInf )
                                  ? -inf
                                  : double( domains.lower_bounds[col] );
         model.col_upper_[i] = domains.flags[col].test( ColFlag::kUbInf )
                                  ? inf
                                  : double( domains.upper_bounds[col] );

         model.col_cost_[i] = double( obj.coefficients[col] );

         model.integrality_[i] =
             domains.flags[col].test( ColFlag::kImplInt )
                 ? HighsVarType::kImplicitInteger
                 : ( domains.flags[col].test( ColFlag::kIntegral )
                         ? HighsVarType::kInteger
                         : HighsVarType::kContinuous );

         auto colvec = consMatrix.getColumnCoefficients( col );

         int collen = colvec.getLength();
         const int* colrows = colvec.getIndices();
         const REAL* colvals = colvec.getValues();

         model.a_start_[i] = start;

         for( int k = 0; k != collen; ++k )
         {
            model.a_value_[start + k] = double( colvals[k] );
            model.a_index_[start + k] =
                components.getRowComponentIdx( colrows[k] );
         }

         start += collen;
      }

      solver.passModel( std::move( model ) );
   }

   void
   solve() override
   {
      solver.passHighsOptions( opts );

      if( solver.run() == HighsStatus::kError )
      {
         this->status = SolverStatus::kError;
         return;
      }

      HighsModelStatus stat = solver.getModelStatus();

      switch( stat )
      {
      default:
         this->status = SolverStatus::kError;
         return;
      case HighsModelStatus::kInfeasible:
         this->status = SolverStatus::kInfeasible;
         return;
      case HighsModelStatus::kUnbounded:
         this->status = SolverStatus::kUnbounded;
         return;
      case HighsModelStatus::kTimeLimit:
      case HighsModelStatus::kIterationLimit:
         this->status = SolverStatus::kInterrupted;
         return;
      case HighsModelStatus::kOptimal:
         this->status = SolverStatus::kOptimal;
      }
   }

   void
   setVerbosity( VerbosityLevel verbosity ) override
   {
      switch( verbosity )
      {
      case VerbosityLevel::kQuiet:
         opts.output_flag = false;
         solver.setHighsOptionValue( "output_flag", false );
         break;
      case VerbosityLevel::kError:
      case VerbosityLevel::kWarning:
      case VerbosityLevel::kInfo:
      case VerbosityLevel::kDetailed:
         opts.output_flag = true;
         solver.setHighsOptionValue( "output_flag", true );
      }
   }

   REAL
   getDualBound() override
   {

      //      TODO:
      //      if( this->status == SolverStatus::kOptimal )
      //         return -inf;
      return solver.getHighsInfo().mip_dual_bound;
   }

   bool
   getSolution( Solution<REAL>& sol ) override
   {
      const HighsSolution& highsSol = solver.getSolution();
      int numcols = solver.getNumCols();
      int numrows = solver.getNumRows();

      if( highsSol.col_value.size() != numcols )
         return false;

      // get primal values
      sol.primal.resize( numcols );
      for( int i = 0; i < numcols; ++i )
         sol.primal[i] = highsSol.col_value[i];

      // return if no dual requested
      if( sol.type == SolutionType::kPrimal )
         return true;

      if( highsSol.col_dual.size() != numcols ||
          highsSol.row_dual.size() != numrows )
      {
         sol.type = SolutionType::kPrimal;
         return true;
      }

      // get reduced costs
      sol.reducedCosts.resize( numcols );
      for( int i = 0; i < numcols; ++i )
         sol.reducedCosts[i] = REAL( highsSol.col_dual[i] );

      // get row duals
      sol.dual.resize( numrows );
      for( int i = 0; i < numrows; ++i )
         sol.dual[i] = - REAL( highsSol.row_dual[i] );

      sol.basisAvailabe = false;
      sol.varBasisStatus.resize( numcols, VarBasisStatus::UNDEFINED );
      sol.rowBasisStatus.resize( numrows, VarBasisStatus::UNDEFINED );

      return true;
   }

   bool
   getSolution( const Components& components, int component,
                Solution<REAL>& sol ) override
   {
      if( this->status != SolverStatus::kOptimal )
         return false;

      int numcols = solver.getNumCols();
      int numrows = solver.getNumRows();
      const HighsSolution& highsSol = solver.getSolution();
      if( highsSol.col_value.size() != numcols )
         return false;

      assert( components.getComponentsNumCols( component ) == numcols );

      const int* compcols = components.getComponentsCols( component );
      for( int i = 0; i != numcols; ++i )
         sol.primal[compcols[i]] = REAL( highsSol.col_value[i] );

      if( sol.type == SolutionType::kPrimal )
         return true;

      if( highsSol.col_dual.size() != numcols ||
          highsSol.row_dual.size() != numrows )
      {
         sol.type = SolutionType::kPrimal;
         return true;
      }

      for( int i = 0; i != numcols; ++i )
         sol.reducedCosts[compcols[i]] = REAL( highsSol.col_dual[i] );

      const int* comprows = components.getComponentsRows( component );

      for( int i = 0; i != numrows; ++i )
         sol.dual[comprows[i]] = REAL( highsSol.row_dual[i] );

      return true;
   }

   void
   addParameters( ParameterSet& paramSet ) override
   {
   }

   bool
   is_dual_solution_available() override
   {
      return false;
   }

   SolverType
   getType() override
   {
      return SolverType::MIP;
   }

   String
   getName() override
   {
      return "HiGHS";
   }

   void
   printDetails() override
   {
   }
};

template <typename REAL>
class HighsFactory : public SolverFactory<REAL>
{
   HighsFactory() = default;

 public:
   std::unique_ptr<SolverInterface<REAL>>
   newSolver( VerbosityLevel verbosity ) const override
   {
      auto highs =
          std::unique_ptr<SolverInterface<REAL>>( new HighsInterface<REAL>() );

      highs->setVerbosity( verbosity );

      return std::move( highs );
   }

   virtual void
   add_parameters( ParameterSet& parameter ) const
   {
   }
   static std::unique_ptr<SolverFactory<REAL>>
   create()
   {
      return std::unique_ptr<SolverFactory<REAL>>( new HighsFactory<REAL>() );
   }
};

} // namespace papilo

#endif