Skip to content

Commit

Permalink
Squashed 'scipy/optimize/_highs/' changes from 46edda35a..57f9aa928
Browse files Browse the repository at this point in the history
57f9aa928 Merge remote-tracking branch 'upstream/master'
f6b518d13 Remove print statements
9970e1ca7 Change order of model status enums and export enums to make consistency automatic; found upstream bug in simplex solver
1e06c8a51 Merge pull request scipy#333 from ERGO-Code/dev-presolve
e6b6f3162 actually moved files now
5d5faf597 scaffold files copied out. TestPresolve runs OK: now time to extract exec details from presolve.
689a24ebc path to files of HiGHS or scaffold fixed. TestPresolve works.
c391eb362 instances added
6d339204b test presolve now not finding test files: the scaffold lib does not know where check/ is. it is easiest if we add files to scaffold and use them for test.
ba10975f8 cmake fixes scaffold building with Highs.h in TestPresolve
e53363dab added failing line to TestPresolve. cmake files fixes removed two issues. still get an error
eef016f10 Merge pull request scipy#332 from ERGO-Code/ImproveCAPIexample
aec2f6f5a Added a lot of comments to examples/call_highs_from_c.c
84e9f0e50 Names of arrays in examples/call_highs_from_c.c now match those in highs_c_api.cpp, and Highs_passLp is now correct in handling astart with only numcol entries
8b85654d8 command line options library moved to app/ so out of src/
4d1534338 uses interface libraries in cmake
b6a902fd6 Merge pull request scipy#318 from ERGO-Code/newlpfilereader
134c2b0f3 Merge branch 'master' into dev-presolve
deed95b6e Merge remote-tracking branch 'ergo/master'
04472e386 fixed bug in .lp filewriter
8f54ecee3 fixed compilation issues
8483eaf12 Merge branch 'master' into newlpfilereader
79cec5994 Check primal feasibility to know if solution exists
02e0c8cb4 separated test from dev namespaces. next: add tests for component and dev
1fab41ba5 Merge pull request #4 from galabovaa/dev-presolve
c15eeaa25 removed unnecesary code in LP filereader
94e82f41d added missing header
29055001c updated LP filereader
d3108f2cc pull bugfix from reader repository
f1b684001 replace LP filereader

git-subtree-dir: scipy/optimize/_highs
git-subtree-split: 57f9aa928c5232092819807e726d3444650d42ad
  • Loading branch information
mckib2 committed Apr 29, 2020
1 parent ad4d9fd commit c6aeba2
Show file tree
Hide file tree
Showing 24 changed files with 1,471 additions and 1,711 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ add_subdirectory(app)
add_subdirectory(check)
add_subdirectory(src)

# # Comment out for scaffold/ tests
# # # Comment out for scaffold/ tests
# add_subdirectory(scaffold)

# # Only needed if it defines a target like a main executable. For methods stick
# # to header-only.
# # # Only needed if it defines a target like a main executable. For methods stick
# # # to header-only.
# add_subdirectory(dev_presolve)
9 changes: 9 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# create highs binary using library without pic
add_executable(highs)
target_sources(highs PRIVATE RunHighs.cpp)
target_include_directories(highs PRIVATE ${HIGHS_SOURCE_DIR}/app)
target_link_libraries(highs libhighs)

# install the binary
install(TARGETS highs EXPORT highs-targets
RUNTIME DESTINATION bin)
File renamed without changes.
232 changes: 232 additions & 0 deletions examples/call_highs_from_c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
#include "interfaces/highs_c_api.h"

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

// gcc call_highs_from_c.c -o highstest -I ../build/install_folder/include/ -L ../build/install_folder/lib/ -lhighs

void minimal_api() {
// This illustrates the use of Highs_call, the simple C interface to
// HiGHS. It's designed to solve the general LP problem
//
// Min c^Tx subject to L <= Ax <= U; l <= x <= u
//
// where A is a matrix with m rows and n columns
//
// The scalar n is numcol
// The scalar m is numrow
//
// The vector c is colcost
// The vector l is collower
// The vector u is colupper
// The vector L is rowlower
// The vector U is rowupper
//
// The matrix A is represented in packed column-wise form: only its
// nonzeros are stored
//
// * The number of nonzeros in A is nnz
//
// * The row indices of the nonnzeros in A are stored column-by-column
// in aindex
//
// * The values of the nonnzeros in A are stored column-by-column in
// avalue
//
// * The position in aindex/avalue of the index/value of the first
// nonzero in each column is stored in astart
//
// Note that astart[0] must be zero
//
// After a successful call to Highs_call, the primal and dual
// solution, and the simplex basis are returned as follows
//
// The vector x is colvalue
// The vector Ax is rowvalue
// The vector of dual values for the variables x is coldual
// The vector of dual values for the variables Ax is rowdual
// The basic/nonbasic status of the variables x is colbasisstatus
// The basic/nonbasic status of the variables Ax is rowbasisstatus
//
// The status of the solution obtained is modelstatus
//
// To solve maximization problems, the values in c must be negated
//
// The use of Highs_call is illustrated for the LP
//
// Min f = 2x_0 + 3x_1
// s.t. x_1 <= 6
// 10 <= x_0 + 2x_1 <= 14
// 8 <= 2x_0 + x_1
// 0 <= x_0 <= 3; 1 <= x_1

int numcol = 2;
int numrow = 3;
int nnz = 5;

// Define the column costs, lower bounds and upper bounds
double colcost[numcol] = {2.0, 3.0};
double collower[numcol] = {0.0, 1.0};
double colupper[numcol] = {3.0, 1.0e30};
// Define the row lower bounds and upper bounds
double rowlower[numrow] = {-1.0e30, 10.0, 8.0};
double rowupper[numrow] = {6.0, 14.0, 1.0e30};
// Define the constraint matrix column-wise
int astart[numcol] = {0, 2};
int aindex[nnz] = {1, 2, 0, 1, 2};
double avalue[nnz] = {1.0, 2.0, 1.0, 2.0, 1.0};

double* colvalue = (double*)malloc(sizeof(double) * numcol);
double* coldual = (double*)malloc(sizeof(double) * numcol);
double* rowvalue = (double*)malloc(sizeof(double) * numrow);
double* rowdual = (double*)malloc(sizeof(double) * numrow);

int* colbasisstatus = (int*)malloc(sizeof(int) * numcol);
int* rowbasisstatus = (int*)malloc(sizeof(int) * numrow);

int modelstatus;

int status = Highs_call(numcol, numrow, nnz,
colcost, collower, colupper,
rowlower, rowupper,
astart, aindex, avalue,
colvalue, coldual, rowvalue, rowdual,
colbasisstatus, rowbasisstatus,
&modelstatus);

assert(status == 0);

printf("Run status = %d; Model status = %d\n", status, modelstatus);

int i;
if (modelstatus == 9) {
// Report the column primal and dual values, and basis status
for (i = 0; i < numcol; i++) {
printf("Col%d = %lf; dual = %lf; status = %d; \n", i, colvalue[i], coldual[i], colbasisstatus[i]);
}
// Report the row primal and dual values, and basis status
for (i = 0; i < numrow; i++) {
printf("Row%d = %lf; dual = %lf; status = %d; \n", i, rowvalue[i], rowdual[i], rowbasisstatus[i]);
}
}

free(colvalue);
free(coldual);
free(rowvalue);
free(rowdual);
free(colbasisstatus);
free(rowbasisstatus);
}

void full_api() {
// Form and solve the LP
// Min f = 2x_0 + 3x_1
// s.t. x_1 <= 6
// 10 <= x_0 + 2x_1 <= 14
// 8 <= 2x_0 + x_1
// 0 <= x_0 <= 3; 1 <= x_1

void* highs;

highs = Highs_create();

int numcol = 2;
int numrow = 3;
int nnz = 5;
int i;

// Define the column costs, lower bounds and upper bounds
double colcost[numcol] = {2.0, 3.0};
double collower[numcol] = {0.0, 1.0};
double colupper[numcol] = {3.0, 1.0e30};
// Define the row lower bounds and upper bounds
double rowlower[numrow] = {-1.0e30, 10.0, 8.0};
double rowupper[numrow] = {6.0, 14.0, 1.0e30};
// Define the constraint matrix row-wise, as it is added to the LP
// with the rows
int arstart[numrow] = {0, 1, 3};
int arindex[nnz] = {1, 0, 1, 0, 1};
double arvalue[nnz] = {1.0, 1.0, 2.0, 2.0, 1.0};

double* colvalue = (double*)malloc(sizeof(double) * numcol);
double* coldual = (double*)malloc(sizeof(double) * numcol);
double* rowvalue = (double*)malloc(sizeof(double) * numrow);
double* rowdual = (double*)malloc(sizeof(double) * numrow);

int* colbasisstatus = (int*)malloc(sizeof(int) * numcol);
int* rowbasisstatus = (int*)malloc(sizeof(int) * numrow);

// Add two columns to the empty LP
assert( Highs_addCols(highs, numcol, colcost, collower, colupper, 0, NULL, NULL, NULL) );
// Add three rows to the 2-column LP
assert( Highs_addRows(highs, numrow, rowlower, rowupper, nnz, arstart, arindex, arvalue) );

int* sense;
Highs_getObjectiveSense(highs, sense);
printf("LP problem has objective sense = %d\n", *sense);

*sense *= -1;
Highs_changeObjectiveSense(highs, *sense);

*sense *= -1;
Highs_changeObjectiveSense(highs, *sense);

Highs_getObjectiveSense(highs, sense);
printf("LP problem has old objective sense = %d\n", *sense);

int simplex_scale_strategy;
Highs_getHighsIntOptionValue(highs, "simplex_scale_strategy", &simplex_scale_strategy);
printf("simplex_scale_strategy = %d: setting it to 3\n", simplex_scale_strategy);
simplex_scale_strategy = 3;
Highs_setHighsIntOptionValue(highs, "simplex_scale_strategy", simplex_scale_strategy);

double primal_feasibility_tolerance;
Highs_getHighsDoubleOptionValue(highs, "primal_feasibility_tolerance", &primal_feasibility_tolerance);
printf("primal_feasibility_tolerance = %g: setting it to 1e-6\n", primal_feasibility_tolerance);
primal_feasibility_tolerance = 1e-6;
Highs_setHighsDoubleOptionValue(highs, "primal_feasibility_tolerance", primal_feasibility_tolerance);

int status = Highs_run(highs);
// Get the model status
const int scaled_model = 0;
int modelstatus = Highs_getModelStatus(highs, scaled_model);

printf("Run status = %d; Model status = %d\n", status, modelstatus);

double objective_function_value;
Highs_getHighsDoubleInfoValue(highs, "objective_function_value", &objective_function_value);
int simplex_iteration_count = 0;
Highs_getHighsIntInfoValue(highs, "simplex_iteration_count", &simplex_iteration_count);

printf("Objective value = %g; Iteration count = %d\n", objective_function_value, simplex_iteration_count);
if (modelstatus == 9) {
// Get the primal and dual solution
Highs_getSolution(highs, colvalue, coldual, rowvalue, rowdual);
// Get the basis
Highs_getBasis(highs, colbasisstatus, rowbasisstatus);
// Report the column primal and dual values, and basis status
for (i = 0; i < numcol; i++) {
printf("Col%d = %lf; dual = %lf; status = %d; \n", i, colvalue[i], coldual[i], colbasisstatus[i]);
}
// Report the row primal and dual values, and basis status
for (i = 0; i < numrow; i++) {
printf("Row%d = %lf; dual = %lf; status = %d; \n", i, rowvalue[i], rowdual[i], rowbasisstatus[i]);
}
}

free(colvalue);
free(coldual);
free(rowvalue);
free(rowdual);
free(colbasisstatus);
free(rowbasisstatus);

Highs_destroy(highs);
}

int main() {
minimal_api();
full_api();
return 0;
}
19 changes: 19 additions & 0 deletions external/filereaderlp/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2020 Michael Feldmeier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 24 additions & 0 deletions external/filereaderlp/builder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef __READERLP_BUILDER_HPP__
#define __READERLP_BUILDER_HPP__

#include <map>
#include <memory>
#include <string>

#include "model.hpp"

struct Builder {
std::map<std::string, std::shared_ptr<Variable>> variables;

Model model;

std::shared_ptr<Variable> getvarbyname(std::string name) {
if (variables.count(name) == 0) {
variables[name] = std::shared_ptr<Variable>(new Variable(name));
model.variables.push_back(variables[name]);
}
return variables[name];
}
};

#endif
63 changes: 63 additions & 0 deletions external/filereaderlp/model.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#ifndef __READERLP_MODEL_HPP__
#define __READERLP_MODEL_HPP__

#include <limits>
#include <memory>
#include <string>
#include <vector>

enum class VariableType {
CONTINUOUS,
BINARY,
GENERAL,
SEMICONTINUOUS
};

enum class ObjectiveSense {
MIN,
MAX
};

struct Variable {
VariableType type = VariableType::CONTINUOUS;
double lowerbound = 0.0;
double upperbound = std::numeric_limits<double>::infinity();
std::string name;

Variable(std::string n="") : name(n) {};
};

struct LinTerm {
std::shared_ptr<Variable> var;
double coef;
};

struct QuadTerm {
std::shared_ptr<Variable> var1;
std::shared_ptr<Variable> var2;
double coef;
};

struct Expression {
std::vector<std::shared_ptr<LinTerm>> linterms;
std::vector<std::shared_ptr<QuadTerm>> quadterms;
double offset = 0;
std::string name = "";
};

struct Constraint {
double lowerbound = -std::numeric_limits<double>::infinity();
double upperbound = std::numeric_limits<double>::infinity();
std::shared_ptr<Expression> expr;

Constraint() : expr(std::shared_ptr<Expression>(new Expression)) {};
};

struct Model {
std::shared_ptr<Expression> objective;
ObjectiveSense sense;
std::vector<std::shared_ptr<Constraint>> constraints;
std::vector<std::shared_ptr<Variable>> variables;
};

#endif
Loading

0 comments on commit c6aeba2

Please sign in to comment.