From f972839da51841fbaf8774545dde0cc9d360539f Mon Sep 17 00:00:00 2001 From: Thiago Novaes <77932135+Thiago-NovaesB@users.noreply.github.com> Date: Fri, 26 Dec 2025 15:19:53 +0100 Subject: [PATCH 1/9] test --- src/interfaces/HighsInterface/Highs.sln | 25 + .../HighsInterface/Enums/BasisStatus.cs | 28 + .../HighsInterface/Enums/MatrixFormat.cs | 16 + .../HighsInterface/Enums/ModelStatus.cs | 75 + .../HighsInterface/Enums/ObjectiveSense.cs | 16 + .../HighsInterface/Enums/Status.cs | 20 + .../HighsInterface/Enums/VariableType.cs | 28 + .../HighsInterface/Highs.csproj | 9 + .../HighsInterface/Records/BasisInfo.cs | 21 + .../HighsInterface/Records/Model.cs | 57 + .../HighsInterface/Records/Solution.cs | 26 + .../HighsInterface/Records/SolutionInfo.cs | 21 + .../HighsInterface/HighsInterface/Solver.cs | 920 +++++++++ src/interfaces/highs_csharp_api.cs | 1833 +++++++++-------- 14 files changed, 2179 insertions(+), 916 deletions(-) create mode 100644 src/interfaces/HighsInterface/Highs.sln create mode 100644 src/interfaces/HighsInterface/HighsInterface/Enums/BasisStatus.cs create mode 100644 src/interfaces/HighsInterface/HighsInterface/Enums/MatrixFormat.cs create mode 100644 src/interfaces/HighsInterface/HighsInterface/Enums/ModelStatus.cs create mode 100644 src/interfaces/HighsInterface/HighsInterface/Enums/ObjectiveSense.cs create mode 100644 src/interfaces/HighsInterface/HighsInterface/Enums/Status.cs create mode 100644 src/interfaces/HighsInterface/HighsInterface/Enums/VariableType.cs create mode 100644 src/interfaces/HighsInterface/HighsInterface/Highs.csproj create mode 100644 src/interfaces/HighsInterface/HighsInterface/Records/BasisInfo.cs create mode 100644 src/interfaces/HighsInterface/HighsInterface/Records/Model.cs create mode 100644 src/interfaces/HighsInterface/HighsInterface/Records/Solution.cs create mode 100644 src/interfaces/HighsInterface/HighsInterface/Records/SolutionInfo.cs create mode 100644 src/interfaces/HighsInterface/HighsInterface/Solver.cs diff --git a/src/interfaces/HighsInterface/Highs.sln b/src/interfaces/HighsInterface/Highs.sln new file mode 100644 index 0000000000..73a7d438d9 --- /dev/null +++ b/src/interfaces/HighsInterface/Highs.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36811.4 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HighsInterface", "HighsInterface\HighsInterface.csproj", "{4177FE0E-0692-4E0F-987A-3C6DDC739E31}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4177FE0E-0692-4E0F-987A-3C6DDC739E31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4177FE0E-0692-4E0F-987A-3C6DDC739E31}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4177FE0E-0692-4E0F-987A-3C6DDC739E31}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4177FE0E-0692-4E0F-987A-3C6DDC739E31}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A40CDFF4-67AB-46EA-BC93-0D261FE12DA2} + EndGlobalSection +EndGlobal diff --git a/src/interfaces/HighsInterface/HighsInterface/Enums/BasisStatus.cs b/src/interfaces/HighsInterface/HighsInterface/Enums/BasisStatus.cs new file mode 100644 index 0000000000..cbb2b5db6d --- /dev/null +++ b/src/interfaces/HighsInterface/HighsInterface/Enums/BasisStatus.cs @@ -0,0 +1,28 @@ +namespace Highs.Enums; + +/// +/// This defines the status of a variable (or slack variable for a constraint) in a basis +/// +public enum BasisStatus +{ + /// + /// The variable is nonbasic at its lower bound (or fixed value) + /// + Lower = 0, + /// + /// The variable is basic + /// + Basic, + /// + /// he variable is at its upper bound + /// + Upper, + /// + /// A free variable is nonbasic and set to zero + /// + Zero, + /// + /// The variable is nonbasic + /// + Nonbasic +} diff --git a/src/interfaces/HighsInterface/HighsInterface/Enums/MatrixFormat.cs b/src/interfaces/HighsInterface/HighsInterface/Enums/MatrixFormat.cs new file mode 100644 index 0000000000..77f5bdb15a --- /dev/null +++ b/src/interfaces/HighsInterface/HighsInterface/Enums/MatrixFormat.cs @@ -0,0 +1,16 @@ +namespace Highs.Enums; + +/// +/// This defines the format of a HighsSparseMatrix +/// +public enum MatrixFormat +{ + /// + /// The matrix is stored column-wise + /// + ColumnWise = 1, + /// + /// The matrix is stored row-wise + /// + RowWise +} diff --git a/src/interfaces/HighsInterface/HighsInterface/Enums/ModelStatus.cs b/src/interfaces/HighsInterface/HighsInterface/Enums/ModelStatus.cs new file mode 100644 index 0000000000..29736814ba --- /dev/null +++ b/src/interfaces/HighsInterface/HighsInterface/Enums/ModelStatus.cs @@ -0,0 +1,75 @@ +namespace Highs.Enums; + +/// +/// This defines the status of the model after a call to run +/// +public enum ModelStatus +{ + /// + /// The model status has not been set + /// + Notset = 0, + LoadError, + /// + /// There is an error in the model + /// + ModelError, + PresolveError, + /// + /// There has been an error when solving the model + /// + SolveError, + PostsolveError, + /// + /// The model is empty + /// + ModelEmpty, + /// + /// The model has been solved to optimality + /// + Optimal, + /// + /// The model is infeasible + /// + Infeasible, + /// + /// The model is unbounded or infeasible + /// + UnboundedOrInfeasible, + /// + /// The model is unbounded + /// + Unbounded, + /// + /// The bound on the model objective value has been reached + /// + ObjectiveBound, + /// + /// The target value for the model objective has been reached + /// + ObjectiveTarget, + /// + /// The run time limit has been reached + /// + TimeLimit, + /// + /// The iteration limit has been reached + /// + IterationLimit, + /// + /// The model status is unknown + /// + Unknown, + /// + /// The MIP solver has reached the limit on the number of LPs solved + /// + SolutionLimit, + /// + /// The solver has been interrupted by the user + /// + Interrupt, + /// + /// The solver has been unable to allocate sufficient memory + /// + MemoryLimit +} diff --git a/src/interfaces/HighsInterface/HighsInterface/Enums/ObjectiveSense.cs b/src/interfaces/HighsInterface/HighsInterface/Enums/ObjectiveSense.cs new file mode 100644 index 0000000000..155741d4a3 --- /dev/null +++ b/src/interfaces/HighsInterface/HighsInterface/Enums/ObjectiveSense.cs @@ -0,0 +1,16 @@ +namespace Highs.Enums; + +/// +/// This defines optimization sense of a HighsLp +/// +public enum ObjectiveSense +{ + /// + /// The objective is to be minimized + /// + Minimize = 1, + /// + /// The objective is to be maximized + /// + Maximize = -1 +} diff --git a/src/interfaces/HighsInterface/HighsInterface/Enums/Status.cs b/src/interfaces/HighsInterface/HighsInterface/Enums/Status.cs new file mode 100644 index 0000000000..001f9a5de2 --- /dev/null +++ b/src/interfaces/HighsInterface/HighsInterface/Enums/Status.cs @@ -0,0 +1,20 @@ +namespace Highs.Enums; + +/// +/// This is (part of) the return value of most HiGHS methods +/// +public enum Status +{ + /// + /// The method has exposed an error + /// + Error = -1, + /// + /// The method has completed successfully + /// + Ok, + /// + /// The method has recovered from an unusual event, or has terminated due to reaching a time or iteration limit + /// + Warning +} diff --git a/src/interfaces/HighsInterface/HighsInterface/Enums/VariableType.cs b/src/interfaces/HighsInterface/HighsInterface/Enums/VariableType.cs new file mode 100644 index 0000000000..e22f4610a5 --- /dev/null +++ b/src/interfaces/HighsInterface/HighsInterface/Enums/VariableType.cs @@ -0,0 +1,28 @@ +namespace Highs.Enums; + +/// +/// This defines the feasible values of a variable within a model +/// +public enum VariableType +{ + /// + /// The variable can take continuous values between its bounds + /// + Continuous = 0, + /// + /// The variable must take integer values between its bounds + /// + Integer, + /// + /// The variable must be zero or take continuous values between its bounds + /// + SemiContinuous, + /// + /// The variable must be zero or take integer values between its bounds + /// + SemiInteger, + /// + /// The variable must take implicit integer values between its bounds + /// + ImplicitInteger, +} \ No newline at end of file diff --git a/src/interfaces/HighsInterface/HighsInterface/Highs.csproj b/src/interfaces/HighsInterface/HighsInterface/Highs.csproj new file mode 100644 index 0000000000..125f4c93bc --- /dev/null +++ b/src/interfaces/HighsInterface/HighsInterface/Highs.csproj @@ -0,0 +1,9 @@ + + + + net9.0 + enable + enable + + + diff --git a/src/interfaces/HighsInterface/HighsInterface/Records/BasisInfo.cs b/src/interfaces/HighsInterface/HighsInterface/Records/BasisInfo.cs new file mode 100644 index 0000000000..b6114a8aa3 --- /dev/null +++ b/src/interfaces/HighsInterface/HighsInterface/Records/BasisInfo.cs @@ -0,0 +1,21 @@ +using Highs.Enums; + +namespace Highs.Records; + +/// +/// This defines the basis status of the columns and rows in a basis +/// +/// The column basis status +/// The row basis status +public record BasisInfo(BasisStatus[] ColumnBasisStatus, BasisStatus[] RowBasisStatus) +{ + /// + /// The default constructor creates empty arrays + /// + /// The number of columns + /// The number of rows + public BasisInfo(int numberOfColumns, int numberOfRows) : this(new BasisStatus[numberOfColumns], + new BasisStatus[numberOfRows]) + { + } +} diff --git a/src/interfaces/HighsInterface/HighsInterface/Records/Model.cs b/src/interfaces/HighsInterface/HighsInterface/Records/Model.cs new file mode 100644 index 0000000000..2b6d672f8a --- /dev/null +++ b/src/interfaces/HighsInterface/HighsInterface/Records/Model.cs @@ -0,0 +1,57 @@ +using Highs.Enums; + +namespace Highs; +/// +/// This defines a model for Highs +/// +public record Model +{ + /// + /// The objective sense + /// + public ObjectiveSense ObjectiveSense; + /// + /// The objective constant + /// + public double Offset; + /// + /// The column costs + /// + public double[] ColumnCost = []; + /// + /// The column lower bounds + /// + public double[] ColumnLower = []; + /// + /// The column upper bounds + /// + public double[] ColumnUpper = []; + /// + /// The row lower bounds + /// + public double[] RowLower = []; + /// + /// The row upper bounds + /// + public double[] RowUpper = []; + /// + /// The format of the constraint matrix. + /// + public MatrixFormat MatrixFormat; + /// + /// The starting index of each column (or row) in MatrixIndices. + /// + public int[] MatrixStart = []; + /// + /// The indices of matrix entries + /// + public int[] MatrixIndices = []; + /// + /// The values of matrix entries + /// + public double[] MatrixValues = []; + /// + /// The integrality of the variables + /// + public VariableType[] VariableTypes = []; +} diff --git a/src/interfaces/HighsInterface/HighsInterface/Records/Solution.cs b/src/interfaces/HighsInterface/HighsInterface/Records/Solution.cs new file mode 100644 index 0000000000..a1fb0143d1 --- /dev/null +++ b/src/interfaces/HighsInterface/HighsInterface/Records/Solution.cs @@ -0,0 +1,26 @@ +namespace Highs; + +/// +/// The solution. +/// +/// The column value. +/// The column dual. +/// The row value. +/// The row dual. +public record Solution(double[] ColumnValue, + double[] ColumnDual, + double[] RowValue, + double[] RowDual) +{ + /// + /// The default constructor creates empty arrays + /// + /// The number of columns + /// The number of rows + public Solution(int numberOfColumns, int numberOfRows) : this(new double[numberOfColumns], + new double[numberOfColumns], + new double[numberOfRows], + new double[numberOfRows]) + { + } +} diff --git a/src/interfaces/HighsInterface/HighsInterface/Records/SolutionInfo.cs b/src/interfaces/HighsInterface/HighsInterface/Records/SolutionInfo.cs new file mode 100644 index 0000000000..21996a2551 --- /dev/null +++ b/src/interfaces/HighsInterface/HighsInterface/Records/SolutionInfo.cs @@ -0,0 +1,21 @@ +namespace Highs; + +/// +/// The solution info. +/// +/// The simplex iteration count. +/// The Interior Point Method (IPM) iteration count. +/// The PDLP iteration count. +/// The MIP gap. +/// The best dual bound. +/// The MIP node count. +/// The objective value. +public record SolutionInfo(int SimplexIterationCount, + int IpmIterationCount, + int PdlpIterationCount, + double MipGap, + double DualBound, + long NodeCount, + double ObjectiveValue) +{ +} diff --git a/src/interfaces/HighsInterface/HighsInterface/Solver.cs b/src/interfaces/HighsInterface/HighsInterface/Solver.cs new file mode 100644 index 0000000000..30b936df30 --- /dev/null +++ b/src/interfaces/HighsInterface/HighsInterface/Solver.cs @@ -0,0 +1,920 @@ +using System.Runtime.InteropServices; +using System.Text; +using Highs.Enums; +using Highs.Records; + +namespace Highs; + +/// +/// The Highs Solver interface. +/// +public class Solver : IDisposable +{ + /// + /// The pointer to the _highs instance. + /// + private readonly IntPtr _highs; + + /// + /// Indicates whether the instance has been disposed. + /// + private bool _disposed; + + /// + /// The name of the Highs library. + /// + private const string HighsLibName = "_highs"; + + #region Library Imports + [LibraryImport(HighsLibName)] + private static extern int Highs_call( + int numcol, + int numrow, + int numnz, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue, + double[] colvalue, + double[] coldual, + double[] rowvalue, + double[] rowdual, + int[] colbasisstatus, + int[] rowbasisstatus, + ref int modelstatus); + + [LibraryImport(HighsLibName)] + private static extern IntPtr Highs_create(); + + [LibraryImport(HighsLibName)] + private static extern void Highs_destroy(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_run(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_readModel(IntPtr _highs, string filename); + + [LibraryImport(HighsLibName)] + private static extern int Highs_writeModel(IntPtr _highs, string filename); + + [LibraryImport(HighsLibName)] + private static extern int Highs_writePresolvedModel(IntPtr _highs, string filename); + + [LibraryImport(HighsLibName)] + private static extern int Highs_writeSolutionPretty(IntPtr _highs, string filename); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getInfinity(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_passLp( + IntPtr _highs, + int numcol, + int numrow, + int numnz, + int aformat, + int sense, + double offset, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue); + + [LibraryImport(HighsLibName)] + private static extern int Highs_passMip( + IntPtr _highs, + int numcol, + int numrow, + int numnz, + int aformat, + int sense, + double offset, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue, + int[] highs_integrality); + + [LibraryImport(HighsLibName)] + private static extern int Highs_setOptionValue(IntPtr _highs, string option, string value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_setBoolOptionValue(IntPtr _highs, string option, int value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_setIntOptionValue(IntPtr _highs, string option, int value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_setDoubleOptionValue(IntPtr _highs, string option, double value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_setStringOptionValue(IntPtr _highs, string option, string value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getBoolOptionValue(IntPtr _highs, string option, out int value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getIntOptionValue(IntPtr _highs, string option, out int value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getDoubleOptionValue(IntPtr _highs, string option, out double value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getStringOptionValue(IntPtr _highs, string option, [Out] StringBuilder value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getSolution(IntPtr _highs, double[] colvalue, double[] coldual, double[] rowvalue, double[] rowdual); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getNumCol(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getNumRow(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getNumNz(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getBasis(IntPtr _highs, int[] colstatus, int[] rowstatus); + + [LibraryImport(HighsLibName)] + private static extern double Highs_getObjectiveValue(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getIterationCount(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getModelStatus(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_addRow(IntPtr _highs, double lower, double upper, int num_new_nz, int[] indices, double[] values); + + [LibraryImport(HighsLibName)] + private static extern int Highs_addRows( + IntPtr _highs, + int num_new_row, + double[] lower, + double[] upper, + int num_new_nz, + int[] starts, + int[] indices, + double[] values); + + [LibraryImport(HighsLibName)] + private static extern int Highs_addCol( + IntPtr _highs, + double cost, + double lower, + double upper, + int num_new_nz, + int[] indices, + double[] values); + + [LibraryImport(HighsLibName)] + private static extern int Highs_addCols( + IntPtr _highs, + int num_new_col, + double[] costs, + double[] lower, + double[] upper, + int num_new_nz, + int[] starts, + int[] indices, + double[] values); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeObjectiveSense(IntPtr _highs, int sense); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeColCost(IntPtr _highs, int col, double cost); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeColsCostBySet(IntPtr _highs, int num_set_entries, int[] set, double[] cost); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeColsCostByMask(IntPtr _highs, int[] mask, double[] cost); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeColBounds(IntPtr _highs, int col, double lower, double upper); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeColsBoundsByRange(IntPtr _highs, int from_col, int to_col, double[] lower, double[] upper); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeColsBoundsBySet(IntPtr _highs, int num_set_entries, int[] set, double[] lower, double[] upper); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeColsBoundsByMask(IntPtr _highs, int[] mask, double[] lower, double[] upper); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeRowBounds(IntPtr _highs, int row, double lower, double upper); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeRowsBoundsBySet(IntPtr _highs, int num_set_entries, int[] set, double[] lower, double[] upper); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeRowsBoundsByMask(IntPtr _highs, int[] mask, double[] lower, double[] upper); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeColsIntegralityByRange(IntPtr _highs, int from_col, int to_col, int[] integrality); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeCoeff(IntPtr _highs, int row, int col, double value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_deleteColsByRange(IntPtr _highs, int from_col, int to_col); + + [LibraryImport(HighsLibName)] + private static extern int Highs_deleteColsBySet(IntPtr _highs, int num_set_entries, int[] set); + + [LibraryImport(HighsLibName)] + private static extern int Highs_deleteColsByMask(IntPtr _highs, int[] mask); + + [LibraryImport(HighsLibName)] + private static extern int Highs_deleteRowsByRange(IntPtr _highs, int from_row, int to_row); + + [LibraryImport(HighsLibName)] + private static extern int Highs_deleteRowsBySet(IntPtr _highs, int num_set_entries, int[] set); + + [LibraryImport(HighsLibName)] + private static extern int Highs_deleteRowsByMask(IntPtr _highs, int[] mask); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getDoubleInfoValue(IntPtr _highs, string info, out double value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getIntInfoValue(IntPtr _highs, string info, out int value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getInt64InfoValue(IntPtr _highs, string info, out long value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_setSolution(IntPtr _highs, double[] col_value, double[] row_value, double[] col_dual, double[] row_dual); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getColsByRange( + IntPtr _highs, + int from_col, + int to_col, + ref int num_col, + double[] costs, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getColsBySet( + IntPtr _highs, + int num_set_entries, + int[] set, + ref int num_col, + double[] costs, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getColsByMask( + IntPtr _highs, + int[] mask, + ref int num_col, + double[] costs, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getRowsByRange( + IntPtr _highs, + int from_row, + int to_row, + ref int num_row, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getRowsBySet( + IntPtr _highs, + int num_set_entries, + int[] set, + ref int num_row, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getRowsByMask( + IntPtr _highs, + int[] mask, + ref int num_row, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getBasicVariables(IntPtr _highs, int[] basic_variables); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getBasisInverseRow(IntPtr _highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getBasisInverseCol(IntPtr _highs, int col, double[] col_vector, ref int col_num_nz, int[] col_indices); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getBasisSolve( + IntPtr _highs, + double[] rhs, + double[] solution_vector, + ref int solution_num_nz, + int[] solution_indices); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getBasisTransposeSolve( + IntPtr _highs, + double[] rhs, + double[] solution_vector, + ref int solution_nz, + int[] solution_indices); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getReducedRow(IntPtr _highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getReducedColumn(IntPtr _highs, int col, double[] col_vector, ref int col_num_nz, int[] col_indices); + + [LibraryImport(HighsLibName)] + private static extern int Highs_clearModel(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_clearSolver(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_passColName(IntPtr _highs, int col, string name); + + [LibraryImport(HighsLibName)] + private static extern int Highs_passRowName(IntPtr _highs, int row, string name); + + [LibraryImport(HighsLibName)] + private static extern int Highs_writeOptions(IntPtr _highs, string filename); + + [LibraryImport(HighsLibName)] + private static extern int Highs_writeOptionsDeviations(IntPtr _highs, string filename); + #endregion + + /// + /// Calls the Highs solver in a single call. + /// + /// + /// + /// + /// + /// + public static Status Call(Model model, ref Solution solution, out BasisInfo basisInfo, out ModelStatus modelStatus) + { + var numberOfColumns = model.ColumnCost.Length; + var numberOfRows = model.RowLower.Length; + var numberOfMatrixValues = model.MatrixValues.Length; + + var columnBasisStatus = new int[numberOfColumns]; + var rowBasisStatus = new int[numberOfRows]; + + var modelstate = 0; + + var status = (Status)Highs_call( + numberOfColumns, + numberOfRows, + numberOfMatrixValues, + model.ColumnCost, + model.ColumnLower, + model.ColumnUpper, + model.RowLower, + model.RowUpper, + model.MatrixStart, + model.MatrixIndices, + model.MatrixValues, + solution.ColumnValue, + solution.ColumnDual, + solution.RowValue, + solution.RowDual, + columnBasisStatus, + rowBasisStatus, + ref modelstate); + + modelStatus = (ModelStatus)modelstate; + basisInfo = new([.. columnBasisStatus.Select(x => (BasisStatus)x)], [.. rowBasisStatus.Select(x => (BasisStatus)x)]); + + return status; + } + + /// + /// The default constructor. + /// + public Solver() => _highs = Highs_create(); + + /// + /// The destructor. + /// + ~Solver() => Dispose(false); + + /// + /// Disposes the instance. + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Disposes the instance. + /// + /// + protected virtual void Dispose(bool disposing) + { + if (_disposed) + { + return; + } + Highs_destroy(_highs); + _disposed = true; + } + + /// + /// Runs the solver. + /// + /// + public Status Run() => (Status)Highs_run(_highs); + + /// + /// Reads a model from file. + /// + /// + /// + public Status ReadModel(string filename) => (Status)Highs_readModel(_highs, filename); + + /// + /// Writes the model to file. + /// + /// + /// + public Status WriteModel(string filename) => (Status)Highs_writeModel(_highs, filename); + + /// + /// Writes the presolved model to file. + /// + /// + /// + public Status WritePresolvedModel(string filename) => (Status)Highs_writePresolvedModel(_highs, filename); + + /// + /// Writes the solution to file in a pretty format. + /// + /// + /// + public Status WriteSolutionPretty(string filename) => (Status)Highs_writeSolutionPretty(_highs, filename); + + /// + /// Gets the infinity value. + /// + /// + public double GetInfinity() => Highs_getInfinity(_highs); + + public Status passLp(HighsModel model) + { + return (Status)Highs_passLp( + _highs, + model.colcost.Length, + model.rowlower.Length, + model.avalue.Length, + (int)model.a_format, + (int)model.sense, + model.offset, + model.colcost, + model.collower, + model.colupper, + model.rowlower, + model.rowupper, + model.astart, + model.aindex, + model.avalue); + } + + public Status passMip(HighsModel model) + { + return (Status)Highs_passMip( + _highs, + model.colcost.Length, + model.rowlower.Length, + model.avalue.Length, + (int)model.a_format, + (int)model.sense, + model.offset, + model.colcost, + model.collower, + model.colupper, + model.rowlower, + model.rowupper, + model.astart, + model.aindex, + model.avalue, + model.highs_integrality); + } + + /// + /// Sets the option value. + /// + /// + /// + /// + public Status SetOptionValue(string option, string value) => (Status)Highs_setOptionValue(_highs, option, value); + + /// + /// Sets the string option value. + /// + /// + /// + /// + public Status SetStringOptionValue(string option, string value) => (Status)Highs_setStringOptionValue(_highs, option, value); + + /// + /// Sets the boolean option value. + /// + /// + /// + /// + public Status SetBoolOptionValue(string option, int value) => (Status)Highs_setBoolOptionValue(_highs, option, value); + + /// + /// Sets the double option value. + /// + /// + /// + /// + public Status SetDoubleOptionValue(string option, double value) => (Status)Highs_setDoubleOptionValue(_highs, option, value); + + /// + /// Sets the integer option value. + /// + /// + /// + /// + public Status SetIntOptionValue(string option, int value) => (Status)Highs_setIntOptionValue(_highs, option, value); + + public Status getStringOptionValue(string option, out string value) + { + var stringBuilder = new StringBuilder(); + var result = (Status)Highs_getStringOptionValue(_highs, option, stringBuilder); + value = stringBuilder.ToString(); + return result; + } + + public Status getBoolOptionValue(string option, out int value) + { + return (Status)Highs_getBoolOptionValue(_highs, option, out value); + } + + public Status getDoubleOptionValue(string option, out double value) + { + return (Status)Highs_getDoubleOptionValue(_highs, option, out value); + } + + public Status getIntOptionValue(string option, out int value) + { + return (Status)Highs_getIntOptionValue(_highs, option, out value); + } + + public int getNumCol() + { + return Highs_getNumCol(_highs); + } + + public int getNumRow() + { + return Highs_getNumRow(_highs); + } + + public int getNumNz() + { + return Highs_getNumNz(_highs); + } + + public HighsSolution getSolution() + { + int numberOfColumns = getNumCol(); + int numberOfRows = getNumRow(); + + HighsSolution sol = new HighsSolution(numberOfColumns, numberOfRows); + Highs_getSolution(_highs, sol.colvalue, sol.coldual, sol.rowvalue, sol.rowdual); + + return sol; + } + + public HighsBasis getBasis() + { + int numberOfColumns = getNumCol(); + int numberOfRows = getNumRow(); + + int[] colbasstat = new int[numberOfColumns]; + int[] rowbasstat = new int[numberOfRows]; + + Highs_getBasis(_highs, colbasstat, rowbasstat); + HighsBasis bas = new HighsBasis( + colbasstat.Select(x => (HighsBasisStatus)x).ToArray(), + rowbasstat.Select(x => (HighsBasisStatus)x).ToArray()); + + return bas; + } + + public double getObjectiveValue() + { + return Highs_getObjectiveValue(_highs); + } + + public HighsModelStatus GetModelStatus() + { + return (HighsModelStatus)Highs_getModelStatus(_highs); + } + + public int getIterationCount() + { + return Highs_getIterationCount(_highs); + } + + public Status addRow(double lower, double upper, int[] indices, double[] values) + { + return (Status)Highs_addRow(_highs, lower, upper, indices.Length, indices, values); + } + + public Status addRows(double[] lower, double[] upper, int[] starts, int[] indices, double[] values) + { + return (Status)Highs_addRows(_highs, lower.Length, lower, upper, indices.Length, starts, indices, values); + } + + public Status addCol(double cost, double lower, double upper, int[] indices, double[] values) + { + return (Status)Highs_addCol(_highs, cost, lower, upper, indices.Length, indices, values); + } + + public Status addCols(double[] costs, double[] lower, double[] upper, int[] starts, int[] indices, double[] values) + { + return (Status)Highs_addCols( + _highs, + costs.Length, + costs, + lower, + upper, + indices.Length, + starts, + indices, + values); + } + + public Status changeObjectiveSense(HighsObjectiveSense sense) + { + return (Status)Highs_changeObjectiveSense(_highs, (int)sense); + } + + public Status changeColCost(int col, double cost) + { + return (Status)Highs_changeColCost(_highs, col, cost); + } + + public Status changeColsCostBySet(int[] cols, double[] costs) + { + return (Status)Highs_changeColsCostBySet(_highs, cols.Length, cols, costs); + } + + public Status changeColsCostByMask(bool[] mask, double[] cost) + { + return (Status)Highs_changeColsCostByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray(), cost); + } + + public Status changeColBounds(int col, double lower, double upper) + { + return (Status)Highs_changeColBounds(_highs, col, lower, upper); + } + + public Status changeColsBoundsByRange(int from, int to, double[] lower, double[] upper) + { + return (Status)Highs_changeColsBoundsByRange(_highs, from, to, lower, upper); + } + + public Status changeColsBoundsBySet(int[] cols, double[] lower, double[] upper) + { + return (Status)Highs_changeColsBoundsBySet(_highs, cols.Length, cols, lower, upper); + } + + public Status changeColsBoundsByMask(bool[] mask, double[] lower, double[] upper) + { + return (Status)Highs_changeColsBoundsByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray(), lower, upper); + } + + public Status changeRowBounds(int row, double lower, double upper) + { + return (Status)Highs_changeRowBounds(_highs, row, lower, upper); + } + + public Status changeRowsBoundsBySet(int[] rows, double[] lower, double[] upper) + { + return (Status)Highs_changeRowsBoundsBySet(_highs, rows.Length, rows, lower, upper); + } + + public Status changeRowsBoundsByMask(bool[] mask, double[] lower, double[] upper) + { + return (Status)Highs_changeRowsBoundsByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray(), lower, upper); + } + + public Status changeColsIntegralityByRange(int from_col, int to_col, HighsIntegrality[] integrality) + { + return (Status)Highs_changeColsIntegralityByRange(_highs, from_col, to_col, Array.ConvertAll(integrality, item => (int)item)); + } + + public Status changeCoeff(int row, int col, double value) + { + return (Status)Highs_changeCoeff(_highs, row, col, value); + } + + public Status deleteColsByRange(int from, int to) + { + return (Status)Highs_deleteColsByRange(_highs, from, to); + } + + public Status deleteColsBySet(int[] cols) + { + return (Status)Highs_deleteColsBySet(_highs, cols.Length, cols); + } + + public Status deleteColsByMask(bool[] mask) + { + return (Status)Highs_deleteColsByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray()); + } + + public Status deleteRowsByRange(int from, int to) + { + return (Status)Highs_deleteRowsByRange(_highs, from, to); + } + + public Status deleteRowsBySet(int[] rows) + { + return (Status)Highs_deleteRowsBySet(_highs, rows.Length, rows); + } + + public Status deleteRowsByMask(bool[] mask) + { + return (Status)Highs_deleteRowsByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray()); + } + + delegate int HighsGetInfoDelegate(IntPtr _highs, string infoName, out TValue output); + + private TValue GetValueOrFallback(HighsGetInfoDelegate highsGetInfoDelegate, string infoName, TValue fallback) + { + try + { + var status = (Status)highsGetInfoDelegate(_highs, infoName, out var value); + if (status != Status.kOk) + { + return fallback; + } + + return value; + } + catch + { + return fallback; + } + } + + /// + /// Gets the current solution info. + /// + /// The . + public SolutionInfo getInfo() + { + // TODO: This object does not contian the "complete" info from the C api. Add further props, if you need them. + var info = new SolutionInfo() + { + MipGap = GetValueOrFallback(Highs_getDoubleInfoValue, "mip_gap", double.NaN), + DualBound = GetValueOrFallback(Highs_getDoubleInfoValue, "mip_dual_bound", double.NaN), + ObjectiveValue = GetValueOrFallback(Highs_getDoubleInfoValue, "objective_function_value", double.NaN), + NodeCount = GetValueOrFallback(Highs_getInt64InfoValue, "mip_node_count", 0L), + IpmIterationCount = GetValueOrFallback(Highs_getIntInfoValue, "ipm_iteration_count", 0), + SimplexIterationCount = GetValueOrFallback(Highs_getIntInfoValue, "simplex_iteration_count", 0), + PdlpIterationCount = GetValueOrFallback(Highs_getIntInfoValue, "pdlp_iteration_count", 0), + }; + return info; + } + + public Status setSolution(HighsSolution solution) + { + return (Status)Highs_setSolution(_highs, solution.colvalue, solution.coldual, solution.rowvalue, solution.rowdual); + } + + public Status getBasicVariables(ref int[] basic_variables) + { + return (Status)Highs_getBasicVariables(_highs, basic_variables); + } + + public Status getBasisInverseRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) + { + return (Status)Highs_getBasisInverseRow(_highs, row, row_vector, ref row_num_nz, row_indices); + } + + public Status getBasisInverseCol(int col, double[] col_vector, ref int col_num_nz, int[] col_indices) + { + return (Status)Highs_getBasisInverseCol(_highs, col, col_vector, ref col_num_nz, col_indices); + } + + public Status getBasisSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) + { + return (Status)Highs_getBasisSolve(_highs, rhs, solution_vector, ref solution_num_nz, solution_indices); + } + + public Status getBasisTransposeSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) + { + return (Status)Highs_getBasisTransposeSolve(_highs, rhs, solution_vector, ref solution_num_nz, solution_indices); + } + + public Status getReducedRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) + { + return (Status)Highs_getReducedRow(_highs, row, row_vector, ref row_num_nz, row_indices); + } + + public Status getReducedColumn(int col, double[] col_vector, ref int col_num_nz, int[] col_indices) + { + return (Status)Highs_getReducedColumn(_highs, col, col_vector, ref col_num_nz, col_indices); + } + + /// + /// Clears the model. + /// + /// + public Status ClearModel() => (Status)Highs_clearModel(_highs); + + /// + /// Clears the solver. + /// + /// + public Status ClearSolver() => (Status)Highs_clearSolver(_highs); + + /// + /// Passes the name of a column. + /// + /// + /// + /// + public Status PassColumnName(int col, string name) => (Status)Highs_passColName(_highs, col, name); + + /// + /// Passes the name of a row. + /// + /// + /// + /// + public Status PassRowName(int row, string name) => (Status)Highs_passRowName(_highs, row, name); + + /// + /// Writes the options to file. + /// + /// + /// + public Status WriteOptions(string filename) => (Status)Highs_writeOptions(_highs, filename); + + /// + /// Writes the options deviations to file. + /// + /// + /// + public Status WriteOptionsDeviations(string filename) => (Status)Highs_writeOptionsDeviations(_highs, filename); +} diff --git a/src/interfaces/highs_csharp_api.cs b/src/interfaces/highs_csharp_api.cs index dadb15caac..3bd9f6c46a 100644 --- a/src/interfaces/highs_csharp_api.cs +++ b/src/interfaces/highs_csharp_api.cs @@ -5,1036 +5,1037 @@ // mcs -out:highscslib.dll -t:library highs_csharp_api.cs -unsafe -namespace Highs { -public enum HighsStatus +namespace Highs; { - kError = -1, - kOk, - kWarning -} - -public enum HighsMatrixFormat -{ - kColwise = 1, - kRowwise -} - -public enum HighsBasisStatus -{ - kLower = 0, - kBasic, - kUpper, - kZero, - kNonbasic -} - -public enum HighsObjectiveSense -{ - kMinimize = 1, - kMaximize = -1 -} - -public enum HighsModelStatus -{ - kNotset = 0, - kLoadError, - kModelError, - kPresolveError, - kSolveError, - kPostsolveError, - kModelEmpty, - kOptimal, - kInfeasible, - kUnboundedOrInfeasible, - kUnbounded, - kObjectiveBound, - kObjectiveTarget, - kTimeLimit, - kIterationLimit, - kUnknown, - kSolutionLimit, - kInterrupt, - kMemoryLimit -} - -public enum HighsIntegrality -{ - kContinuous = 0, - kInteger = 1, - kSemiContinuous = 2, - kSemiInteger = 3, - kImplicitInteger = 4, -} - -public class HighsModel -{ - public HighsObjectiveSense sense; - public double[] colcost; - public double offset; - public double[] collower; - public double[] colupper; - public double[] rowlower; - public double[] rowupper; - public HighsMatrixFormat a_format; - public int[] astart; - public int[] aindex; - public double[] avalue; - public int[] highs_integrality; - - public HighsModel() + public enum HighsStatus { - + kError = -1, + kOk, + kWarning } - public HighsModel(double[] colcost, double[] collower, double[] colupper, double[] rowlower, double[] rowupper, - int[] astart, int[] aindex, double[] avalue, int[] highs_integrality = null, double offset = 0, HighsMatrixFormat a_format = HighsMatrixFormat.kColwise, HighsObjectiveSense sense = HighsObjectiveSense.kMinimize) + public enum HighsMatrixFormat { - this.colcost = colcost; - this.collower = collower; - this.colupper = colupper; - this.rowlower = rowlower; - this.rowupper = rowupper; - this.astart = astart; - this.aindex = aindex; - this.avalue = avalue; - this.offset = offset; - this.a_format = a_format; - this.sense = sense; - this.highs_integrality = highs_integrality; + kColwise = 1, + kRowwise } -} - -public class HighsSolution -{ - public double[] colvalue; - public double[] coldual; - public double[] rowvalue; - public double[] rowdual; - public HighsSolution(int numcol, int numrow) + public enum HighsBasisStatus { - this.colvalue = new double[numcol]; - this.coldual = new double[numcol]; - this.rowvalue = new double[numrow]; - this.rowdual = new double[numrow]; + kLower = 0, + kBasic, + kUpper, + kZero, + kNonbasic } - public HighsSolution(double[] colvalue, double[] coldual, double[] rowvalue, double[] rowdual) + public enum HighsObjectiveSense { - this.colvalue = colvalue; - this.coldual = coldual; - this.rowvalue = rowvalue; - this.rowdual = rowdual; + kMinimize = 1, + kMaximize = -1 } -} - -public class HighsBasis -{ - public HighsBasisStatus[] colbasisstatus; - public HighsBasisStatus[] rowbasisstatus; - public HighsBasis(int numcol, int numrow) + public enum HighsModelStatus { - this.colbasisstatus = new HighsBasisStatus[numcol]; - this.rowbasisstatus = new HighsBasisStatus[numrow]; + kNotset = 0, + kLoadError, + kModelError, + kPresolveError, + kSolveError, + kPostsolveError, + kModelEmpty, + kOptimal, + kInfeasible, + kUnboundedOrInfeasible, + kUnbounded, + kObjectiveBound, + kObjectiveTarget, + kTimeLimit, + kIterationLimit, + kUnknown, + kSolutionLimit, + kInterrupt, + kMemoryLimit } - public HighsBasis(HighsBasisStatus[] colbasisstatus, HighsBasisStatus[] rowbasisstatus) + public enum HighsIntegrality { - this.colbasisstatus = colbasisstatus; - this.rowbasisstatus = rowbasisstatus; + kContinuous = 0, + kInteger = 1, + kSemiContinuous = 2, + kSemiInteger = 3, + kImplicitInteger = 4, } -} -public class HighsLpSolver : IDisposable -{ - private IntPtr highs; - - private bool _disposed; - - private const string highslibname = "highs"; - - [DllImport(highslibname)] - private static extern int Highs_call( - Int32 numcol, - Int32 numrow, - Int32 numnz, - double[] colcost, - double[] collower, - double[] colupper, - double[] rowlower, - double[] rowupper, - int[] astart, - int[] aindex, - double[] avalue, - double[] colvalue, - double[] coldual, - double[] rowvalue, - double[] rowdual, - int[] colbasisstatus, - int[] rowbasisstatus, - ref int modelstatus); - - [DllImport(highslibname)] - private static extern IntPtr Highs_create(); - - [DllImport(highslibname)] - private static extern void Highs_destroy(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_run(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_readModel(IntPtr highs, string filename); - - [DllImport(highslibname)] - private static extern int Highs_writeModel(IntPtr highs, string filename); - - [DllImport(highslibname)] - private static extern int Highs_writePresolvedModel(IntPtr highs, string filename); - - [DllImport(highslibname)] - private static extern int Highs_writeSolutionPretty(IntPtr highs, string filename); - - [DllImport(highslibname)] - private static extern int Highs_getInfinity(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_passLp( - IntPtr highs, - int numcol, - int numrow, - int numnz, - int aformat, - int sense, - double offset, - double[] colcost, - double[] collower, - double[] colupper, - double[] rowlower, - double[] rowupper, - int[] astart, - int[] aindex, - double[] avalue); - - [DllImport(highslibname)] - private static extern int Highs_passMip( - IntPtr highs, - int numcol, - int numrow, - int numnz, - int aformat, - int sense, - double offset, - double[] colcost, - double[] collower, - double[] colupper, - double[] rowlower, - double[] rowupper, - int[] astart, - int[] aindex, - double[] avalue, - int[] highs_integrality); - - [DllImport(highslibname)] - private static extern int Highs_setOptionValue(IntPtr highs, string option, string value); - - [DllImport(highslibname)] - private static extern int Highs_setBoolOptionValue(IntPtr highs, string option, int value); - - [DllImport(highslibname)] - private static extern int Highs_setIntOptionValue(IntPtr highs, string option, int value); - - [DllImport(highslibname)] - private static extern int Highs_setDoubleOptionValue(IntPtr highs, string option, double value); - - [DllImport(highslibname)] - private static extern int Highs_setStringOptionValue(IntPtr highs, string option, string value); - - [DllImport(highslibname)] - private static extern int Highs_getBoolOptionValue(IntPtr highs, string option, out int value); - - [DllImport(highslibname)] - private static extern int Highs_getIntOptionValue(IntPtr highs, string option, out int value); - - [DllImport(highslibname)] - private static extern int Highs_getDoubleOptionValue(IntPtr highs, string option, out double value); - - [DllImport(highslibname)] - private static extern int Highs_getStringOptionValue(IntPtr highs, string option, [Out] StringBuilder value); - - [DllImport(highslibname)] - private static extern int Highs_getSolution(IntPtr highs, double[] colvalue, double[] coldual, double[] rowvalue, double[] rowdual); - - [DllImport(highslibname)] - private static extern int Highs_getNumCol(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_getNumRow(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_getNumNz(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_getBasis(IntPtr highs, int[] colstatus, int[] rowstatus); - - [DllImport(highslibname)] - private static extern double Highs_getObjectiveValue(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_getIterationCount(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_getModelStatus(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_addRow(IntPtr highs, double lower, double upper, int num_new_nz, int[] indices, double[] values); - - [DllImport(highslibname)] - private static extern int Highs_addRows( - IntPtr highs, - int num_new_row, - double[] lower, - double[] upper, - int num_new_nz, - int[] starts, - int[] indices, - double[] values); - - [DllImport(highslibname)] - private static extern int Highs_addCol( - IntPtr highs, - double cost, - double lower, - double upper, - int num_new_nz, - int[] indices, - double[] values); - - [DllImport(highslibname)] - private static extern int Highs_addCols( - IntPtr highs, - int num_new_col, - double[] costs, - double[] lower, - double[] upper, - int num_new_nz, - int[] starts, - int[] indices, - double[] values); - - [DllImport(highslibname)] - private static extern int Highs_changeObjectiveSense(IntPtr highs, int sense); - - [DllImport(highslibname)] - private static extern int Highs_changeColCost(IntPtr highs, int col, double cost); - - [DllImport(highslibname)] - private static extern int Highs_changeColsCostBySet(IntPtr highs, int num_set_entries, int[] set, double[] cost); - - [DllImport(highslibname)] - private static extern int Highs_changeColsCostByMask(IntPtr highs, int[] mask, double[] cost); - - [DllImport(highslibname)] - private static extern int Highs_changeColBounds(IntPtr highs, int col, double lower, double upper); - - [DllImport(highslibname)] - private static extern int Highs_changeColsBoundsByRange(IntPtr highs, int from_col, int to_col, double[] lower, double[] upper); - - [DllImport(highslibname)] - private static extern int Highs_changeColsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); - - [DllImport(highslibname)] - private static extern int Highs_changeColsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); - - [DllImport(highslibname)] - private static extern int Highs_changeRowBounds(IntPtr highs, int row, double lower, double upper); - - [DllImport(highslibname)] - private static extern int Highs_changeRowsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); - - [DllImport(highslibname)] - private static extern int Highs_changeRowsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); - - [DllImport(highslibname)] - private static extern int Highs_changeColsIntegralityByRange(IntPtr highs, int from_col, int to_col, int[] integrality); - - [DllImport(highslibname)] - private static extern int Highs_changeCoeff(IntPtr highs, int row, int col, double value); - - [DllImport(highslibname)] - private static extern int Highs_deleteColsByRange(IntPtr highs, int from_col, int to_col); - - [DllImport(highslibname)] - private static extern int Highs_deleteColsBySet(IntPtr highs, int num_set_entries, int[] set); - - [DllImport(highslibname)] - private static extern int Highs_deleteColsByMask(IntPtr highs, int[] mask); - - [DllImport(highslibname)] - private static extern int Highs_deleteRowsByRange(IntPtr highs, int from_row, int to_row); - - [DllImport(highslibname)] - private static extern int Highs_deleteRowsBySet(IntPtr highs, int num_set_entries, int[] set); - - [DllImport(highslibname)] - private static extern int Highs_deleteRowsByMask(IntPtr highs, int[] mask); - - [DllImport(highslibname)] - private static extern int Highs_getDoubleInfoValue(IntPtr highs, string info, out double value); - - [DllImport(highslibname)] - private static extern int Highs_getIntInfoValue(IntPtr highs, string info, out int value); - - [DllImport(highslibname)] - private static extern int Highs_getInt64InfoValue(IntPtr highs, string info, out long value); - - [DllImport(highslibname)] - private static extern int Highs_setSolution(IntPtr highs, double[] col_value, double[] row_value, double[] col_dual, double[] row_dual); - - [DllImport(highslibname)] - private static extern int Highs_getColsByRange( - IntPtr highs, - int from_col, - int to_col, - ref int num_col, - double[] costs, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(highslibname)] - private static extern int Highs_getColsBySet( - IntPtr highs, - int num_set_entries, - int[] set, - ref int num_col, - double[] costs, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(highslibname)] - private static extern int Highs_getColsByMask( - IntPtr highs, - int[] mask, - ref int num_col, - double[] costs, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(highslibname)] - private static extern int Highs_getRowsByRange( - IntPtr highs, - int from_row, - int to_row, - ref int num_row, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(highslibname)] - private static extern int Highs_getRowsBySet( - IntPtr highs, - int num_set_entries, - int[] set, - ref int num_row, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(highslibname)] - private static extern int Highs_getRowsByMask( - IntPtr highs, - int[] mask, - ref int num_row, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(highslibname)] - private static extern int Highs_getBasicVariables(IntPtr highs, int[] basic_variables); - - [DllImport(highslibname)] - private static extern int Highs_getBasisInverseRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); - - [DllImport(highslibname)] - private static extern int Highs_getBasisInverseCol(IntPtr highs, int col, double[] col_vector, ref int col_num_nz, int[] col_indices); - - [DllImport(highslibname)] - private static extern int Highs_getBasisSolve( - IntPtr highs, - double[] rhs, - double[] solution_vector, - ref int solution_num_nz, - int[] solution_indices); - - [DllImport(highslibname)] - private static extern int Highs_getBasisTransposeSolve( - IntPtr highs, - double[] rhs, - double[] solution_vector, - ref int solution_nz, - int[] solution_indices); - - [DllImport(highslibname)] - private static extern int Highs_getReducedRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); - - [DllImport(highslibname)] - private static extern int Highs_getReducedColumn(IntPtr highs, int col, double[] col_vector, ref int col_num_nz, int[] col_indices); - - [DllImport(highslibname)] - private static extern int Highs_clearModel(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_clearSolver(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_passColName(IntPtr highs, int col, string name); - - [DllImport(highslibname)] - private static extern int Highs_passRowName(IntPtr highs, int row, string name); - - [DllImport(highslibname)] - private static extern int Highs_writeOptions(IntPtr highs, string filename); - - [DllImport(highslibname)] - private static extern int Highs_writeOptionsDeviations(IntPtr highs, string filename); - - public static HighsStatus call(HighsModel model, ref HighsSolution sol, ref HighsBasis bas, ref HighsModelStatus modelstatus) + public class HighsModel { - int nc = model.colcost.Length; - int nr = model.rowlower.Length; - int nnz = model.avalue.Length; - - int[] colbasstat = new int[nc]; - int[] rowbasstat = new int[nr]; - - int modelstate = 0; - - HighsStatus status = (HighsStatus)HighsLpSolver.Highs_call( - nc, - nr, - nnz, - model.colcost, - model.collower, - model.colupper, - model.rowlower, - model.rowupper, - model.astart, - model.aindex, - model.avalue, - sol.colvalue, - sol.coldual, - sol.rowvalue, - sol.rowdual, - colbasstat, - rowbasstat, - ref modelstate); - - modelstatus = (HighsModelStatus)modelstate; - - bas.colbasisstatus = colbasstat.Select(x => (HighsBasisStatus)x).ToArray(); - bas.rowbasisstatus = rowbasstat.Select(x => (HighsBasisStatus)x).ToArray(); - - return status; - } + public HighsObjectiveSense sense; + public double[] colcost; + public double offset; + public double[] collower; + public double[] colupper; + public double[] rowlower; + public double[] rowupper; + public HighsMatrixFormat a_format; + public int[] astart; + public int[] aindex; + public double[] avalue; + public int[] highs_integrality; - public HighsLpSolver() - { - this.highs = HighsLpSolver.Highs_create(); - } + public HighsModel() + { - ~HighsLpSolver() - { - this.Dispose(false); - } + } - public void Dispose() - { - this.Dispose(true); - GC.SuppressFinalize(this); + public HighsModel(double[] colcost, double[] collower, double[] colupper, double[] rowlower, double[] rowupper, + int[] astart, int[] aindex, double[] avalue, int[] highs_integrality = null, double offset = 0, HighsMatrixFormat a_format = HighsMatrixFormat.kColwise, HighsObjectiveSense sense = HighsObjectiveSense.kMinimize) + { + this.colcost = colcost; + this.collower = collower; + this.colupper = colupper; + this.rowlower = rowlower; + this.rowupper = rowupper; + this.astart = astart; + this.aindex = aindex; + this.avalue = avalue; + this.offset = offset; + this.a_format = a_format; + this.sense = sense; + this.highs_integrality = highs_integrality; + } } - protected virtual void Dispose(bool disposing) + public class HighsSolution { - if (this._disposed) + public double[] colvalue; + public double[] coldual; + public double[] rowvalue; + public double[] rowdual; + + public HighsSolution(int numcol, int numrow) { - return; + this.colvalue = new double[numcol]; + this.coldual = new double[numcol]; + this.rowvalue = new double[numrow]; + this.rowdual = new double[numrow]; } - HighsLpSolver.Highs_destroy(this.highs); - this._disposed = true; + public HighsSolution(double[] colvalue, double[] coldual, double[] rowvalue, double[] rowdual) + { + this.colvalue = colvalue; + this.coldual = coldual; + this.rowvalue = rowvalue; + this.rowdual = rowdual; + } } - public HighsStatus run() + public class HighsBasis { - return (HighsStatus)HighsLpSolver.Highs_run(this.highs); - } + public HighsBasisStatus[] colbasisstatus; + public HighsBasisStatus[] rowbasisstatus; - public HighsStatus readModel(string filename) - { - return (HighsStatus)HighsLpSolver.Highs_readModel(this.highs, filename); - } + public HighsBasis(int numcol, int numrow) + { + this.colbasisstatus = new HighsBasisStatus[numcol]; + this.rowbasisstatus = new HighsBasisStatus[numrow]; + } - public HighsStatus writeModel(string filename) - { - return (HighsStatus)HighsLpSolver.Highs_writeModel(this.highs, filename); + public HighsBasis(HighsBasisStatus[] colbasisstatus, HighsBasisStatus[] rowbasisstatus) + { + this.colbasisstatus = colbasisstatus; + this.rowbasisstatus = rowbasisstatus; + } } - public HighsStatus writePresolvedModel(string filename) - { - return (HighsStatus)HighsLpSolver.Highs_writePresolvedModel(this.highs, filename); - } + public class HighsLpSolver : IDisposable + { + private IntPtr highs; + + private bool _disposed; + + private const string highslibname = "highs"; + + [DllImport(highslibname)] + private static extern int Highs_call( + Int32 numcol, + Int32 numrow, + Int32 numnz, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue, + double[] colvalue, + double[] coldual, + double[] rowvalue, + double[] rowdual, + int[] colbasisstatus, + int[] rowbasisstatus, + ref int modelstatus); + + [DllImport(highslibname)] + private static extern IntPtr Highs_create(); + + [DllImport(highslibname)] + private static extern void Highs_destroy(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_run(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_readModel(IntPtr highs, string filename); + + [DllImport(highslibname)] + private static extern int Highs_writeModel(IntPtr highs, string filename); + + [DllImport(highslibname)] + private static extern int Highs_writePresolvedModel(IntPtr highs, string filename); + + [DllImport(highslibname)] + private static extern int Highs_writeSolutionPretty(IntPtr highs, string filename); + + [DllImport(highslibname)] + private static extern int Highs_getInfinity(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_passLp( + IntPtr highs, + int numcol, + int numrow, + int numnz, + int aformat, + int sense, + double offset, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue); + + [DllImport(highslibname)] + private static extern int Highs_passMip( + IntPtr highs, + int numcol, + int numrow, + int numnz, + int aformat, + int sense, + double offset, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue, + int[] highs_integrality); + + [DllImport(highslibname)] + private static extern int Highs_setOptionValue(IntPtr highs, string option, string value); + + [DllImport(highslibname)] + private static extern int Highs_setBoolOptionValue(IntPtr highs, string option, int value); + + [DllImport(highslibname)] + private static extern int Highs_setIntOptionValue(IntPtr highs, string option, int value); + + [DllImport(highslibname)] + private static extern int Highs_setDoubleOptionValue(IntPtr highs, string option, double value); + + [DllImport(highslibname)] + private static extern int Highs_setStringOptionValue(IntPtr highs, string option, string value); + + [DllImport(highslibname)] + private static extern int Highs_getBoolOptionValue(IntPtr highs, string option, out int value); + + [DllImport(highslibname)] + private static extern int Highs_getIntOptionValue(IntPtr highs, string option, out int value); + + [DllImport(highslibname)] + private static extern int Highs_getDoubleOptionValue(IntPtr highs, string option, out double value); + + [DllImport(highslibname)] + private static extern int Highs_getStringOptionValue(IntPtr highs, string option, [Out] StringBuilder value); + + [DllImport(highslibname)] + private static extern int Highs_getSolution(IntPtr highs, double[] colvalue, double[] coldual, double[] rowvalue, double[] rowdual); + + [DllImport(highslibname)] + private static extern int Highs_getNumCol(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_getNumRow(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_getNumNz(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_getBasis(IntPtr highs, int[] colstatus, int[] rowstatus); + + [DllImport(highslibname)] + private static extern double Highs_getObjectiveValue(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_getIterationCount(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_getModelStatus(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_addRow(IntPtr highs, double lower, double upper, int num_new_nz, int[] indices, double[] values); + + [DllImport(highslibname)] + private static extern int Highs_addRows( + IntPtr highs, + int num_new_row, + double[] lower, + double[] upper, + int num_new_nz, + int[] starts, + int[] indices, + double[] values); + + [DllImport(highslibname)] + private static extern int Highs_addCol( + IntPtr highs, + double cost, + double lower, + double upper, + int num_new_nz, + int[] indices, + double[] values); + + [DllImport(highslibname)] + private static extern int Highs_addCols( + IntPtr highs, + int num_new_col, + double[] costs, + double[] lower, + double[] upper, + int num_new_nz, + int[] starts, + int[] indices, + double[] values); + + [DllImport(highslibname)] + private static extern int Highs_changeObjectiveSense(IntPtr highs, int sense); + + [DllImport(highslibname)] + private static extern int Highs_changeColCost(IntPtr highs, int col, double cost); + + [DllImport(highslibname)] + private static extern int Highs_changeColsCostBySet(IntPtr highs, int num_set_entries, int[] set, double[] cost); + + [DllImport(highslibname)] + private static extern int Highs_changeColsCostByMask(IntPtr highs, int[] mask, double[] cost); + + [DllImport(highslibname)] + private static extern int Highs_changeColBounds(IntPtr highs, int col, double lower, double upper); + + [DllImport(highslibname)] + private static extern int Highs_changeColsBoundsByRange(IntPtr highs, int from_col, int to_col, double[] lower, double[] upper); + + [DllImport(highslibname)] + private static extern int Highs_changeColsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); + + [DllImport(highslibname)] + private static extern int Highs_changeColsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); + + [DllImport(highslibname)] + private static extern int Highs_changeRowBounds(IntPtr highs, int row, double lower, double upper); + + [DllImport(highslibname)] + private static extern int Highs_changeRowsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); + + [DllImport(highslibname)] + private static extern int Highs_changeRowsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); + + [DllImport(highslibname)] + private static extern int Highs_changeColsIntegralityByRange(IntPtr highs, int from_col, int to_col, int[] integrality); + + [DllImport(highslibname)] + private static extern int Highs_changeCoeff(IntPtr highs, int row, int col, double value); + + [DllImport(highslibname)] + private static extern int Highs_deleteColsByRange(IntPtr highs, int from_col, int to_col); + + [DllImport(highslibname)] + private static extern int Highs_deleteColsBySet(IntPtr highs, int num_set_entries, int[] set); + + [DllImport(highslibname)] + private static extern int Highs_deleteColsByMask(IntPtr highs, int[] mask); + + [DllImport(highslibname)] + private static extern int Highs_deleteRowsByRange(IntPtr highs, int from_row, int to_row); + + [DllImport(highslibname)] + private static extern int Highs_deleteRowsBySet(IntPtr highs, int num_set_entries, int[] set); + + [DllImport(highslibname)] + private static extern int Highs_deleteRowsByMask(IntPtr highs, int[] mask); + + [DllImport(highslibname)] + private static extern int Highs_getDoubleInfoValue(IntPtr highs, string info, out double value); + + [DllImport(highslibname)] + private static extern int Highs_getIntInfoValue(IntPtr highs, string info, out int value); + + [DllImport(highslibname)] + private static extern int Highs_getInt64InfoValue(IntPtr highs, string info, out long value); + + [DllImport(highslibname)] + private static extern int Highs_setSolution(IntPtr highs, double[] col_value, double[] row_value, double[] col_dual, double[] row_dual); + + [DllImport(highslibname)] + private static extern int Highs_getColsByRange( + IntPtr highs, + int from_col, + int to_col, + ref int num_col, + double[] costs, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [DllImport(highslibname)] + private static extern int Highs_getColsBySet( + IntPtr highs, + int num_set_entries, + int[] set, + ref int num_col, + double[] costs, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [DllImport(highslibname)] + private static extern int Highs_getColsByMask( + IntPtr highs, + int[] mask, + ref int num_col, + double[] costs, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [DllImport(highslibname)] + private static extern int Highs_getRowsByRange( + IntPtr highs, + int from_row, + int to_row, + ref int num_row, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [DllImport(highslibname)] + private static extern int Highs_getRowsBySet( + IntPtr highs, + int num_set_entries, + int[] set, + ref int num_row, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [DllImport(highslibname)] + private static extern int Highs_getRowsByMask( + IntPtr highs, + int[] mask, + ref int num_row, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [DllImport(highslibname)] + private static extern int Highs_getBasicVariables(IntPtr highs, int[] basic_variables); + + [DllImport(highslibname)] + private static extern int Highs_getBasisInverseRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); + + [DllImport(highslibname)] + private static extern int Highs_getBasisInverseCol(IntPtr highs, int col, double[] col_vector, ref int col_num_nz, int[] col_indices); + + [DllImport(highslibname)] + private static extern int Highs_getBasisSolve( + IntPtr highs, + double[] rhs, + double[] solution_vector, + ref int solution_num_nz, + int[] solution_indices); + + [DllImport(highslibname)] + private static extern int Highs_getBasisTransposeSolve( + IntPtr highs, + double[] rhs, + double[] solution_vector, + ref int solution_nz, + int[] solution_indices); + + [DllImport(highslibname)] + private static extern int Highs_getReducedRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); + + [DllImport(highslibname)] + private static extern int Highs_getReducedColumn(IntPtr highs, int col, double[] col_vector, ref int col_num_nz, int[] col_indices); + + [DllImport(highslibname)] + private static extern int Highs_clearModel(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_clearSolver(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_passColName(IntPtr highs, int col, string name); + + [DllImport(highslibname)] + private static extern int Highs_passRowName(IntPtr highs, int row, string name); + + [DllImport(highslibname)] + private static extern int Highs_writeOptions(IntPtr highs, string filename); + + [DllImport(highslibname)] + private static extern int Highs_writeOptionsDeviations(IntPtr highs, string filename); + + public static HighsStatus call(HighsModel model, ref HighsSolution sol, ref HighsBasis bas, ref HighsModelStatus modelstatus) + { + int nc = model.colcost.Length; + int nr = model.rowlower.Length; + int nnz = model.avalue.Length; + + int[] colbasstat = new int[nc]; + int[] rowbasstat = new int[nr]; + + int modelstate = 0; + + HighsStatus status = (HighsStatus)HighsLpSolver.Highs_call( + nc, + nr, + nnz, + model.colcost, + model.collower, + model.colupper, + model.rowlower, + model.rowupper, + model.astart, + model.aindex, + model.avalue, + sol.colvalue, + sol.coldual, + sol.rowvalue, + sol.rowdual, + colbasstat, + rowbasstat, + ref modelstate); + + modelstatus = (HighsModelStatus)modelstate; + + bas.colbasisstatus = colbasstat.Select(x => (HighsBasisStatus)x).ToArray(); + bas.rowbasisstatus = rowbasstat.Select(x => (HighsBasisStatus)x).ToArray(); + + return status; + } - public HighsStatus writeSolutionPretty(string filename) - { - return (HighsStatus)HighsLpSolver.Highs_writeSolutionPretty(this.highs, filename); - } + public HighsLpSolver() + { + this.highs = HighsLpSolver.Highs_create(); + } - public Double getInfinity() - { - return (Double)HighsLpSolver.Highs_getInfinity(this.highs); - } + ~HighsLpSolver() + { + this.Dispose(false); + } - public HighsStatus passLp(HighsModel model) - { - return (HighsStatus)HighsLpSolver.Highs_passLp( - this.highs, - model.colcost.Length, - model.rowlower.Length, - model.avalue.Length, - (int)model.a_format, - (int)model.sense, - model.offset, - model.colcost, - model.collower, - model.colupper, - model.rowlower, - model.rowupper, - model.astart, - model.aindex, - model.avalue); - } + public void Dispose() + { + this.Dispose(true); + GC.SuppressFinalize(this); + } - public HighsStatus passMip(HighsModel model) - { - return (HighsStatus)HighsLpSolver.Highs_passMip( - this.highs, - model.colcost.Length, - model.rowlower.Length, - model.avalue.Length, - (int)model.a_format, - (int)model.sense, - model.offset, - model.colcost, - model.collower, - model.colupper, - model.rowlower, - model.rowupper, - model.astart, - model.aindex, - model.avalue, - model.highs_integrality); - } + protected virtual void Dispose(bool disposing) + { + if (this._disposed) + { + return; + } - public HighsStatus setOptionValue(string option, string value) - { - return (HighsStatus)HighsLpSolver.Highs_setOptionValue(this.highs, option, value); - } + HighsLpSolver.Highs_destroy(this.highs); + this._disposed = true; + } - public HighsStatus setStringOptionValue(string option, string value) - { - return (HighsStatus)HighsLpSolver.Highs_setStringOptionValue(this.highs, option, value); - } + public HighsStatus run() + { + return (HighsStatus)HighsLpSolver.Highs_run(this.highs); + } - public HighsStatus setBoolOptionValue(string option, int value) - { - return (HighsStatus)HighsLpSolver.Highs_setBoolOptionValue(this.highs, option, value); - } + public HighsStatus readModel(string filename) + { + return (HighsStatus)HighsLpSolver.Highs_readModel(this.highs, filename); + } - public HighsStatus setDoubleOptionValue(string option, double value) - { - return (HighsStatus)HighsLpSolver.Highs_setDoubleOptionValue(this.highs, option, value); - } + public HighsStatus writeModel(string filename) + { + return (HighsStatus)HighsLpSolver.Highs_writeModel(this.highs, filename); + } - public HighsStatus setIntOptionValue(string option, int value) - { - return (HighsStatus)HighsLpSolver.Highs_setIntOptionValue(this.highs, option, value); - } + public HighsStatus writePresolvedModel(string filename) + { + return (HighsStatus)HighsLpSolver.Highs_writePresolvedModel(this.highs, filename); + } - public HighsStatus getStringOptionValue(string option, out string value) - { - var stringBuilder = new StringBuilder(); - var result = (HighsStatus)HighsLpSolver.Highs_getStringOptionValue(this.highs, option, stringBuilder); - value = stringBuilder.ToString(); - return result; - } + public HighsStatus writeSolutionPretty(string filename) + { + return (HighsStatus)HighsLpSolver.Highs_writeSolutionPretty(this.highs, filename); + } - public HighsStatus getBoolOptionValue(string option, out int value) - { - return (HighsStatus)HighsLpSolver.Highs_getBoolOptionValue(this.highs, option, out value); - } + public Double getInfinity() + { + return (Double)HighsLpSolver.Highs_getInfinity(this.highs); + } - public HighsStatus getDoubleOptionValue(string option, out double value) - { - return (HighsStatus)HighsLpSolver.Highs_getDoubleOptionValue(this.highs, option, out value); - } + public HighsStatus passLp(HighsModel model) + { + return (HighsStatus)HighsLpSolver.Highs_passLp( + this.highs, + model.colcost.Length, + model.rowlower.Length, + model.avalue.Length, + (int)model.a_format, + (int)model.sense, + model.offset, + model.colcost, + model.collower, + model.colupper, + model.rowlower, + model.rowupper, + model.astart, + model.aindex, + model.avalue); + } - public HighsStatus getIntOptionValue(string option, out int value) - { - return (HighsStatus)HighsLpSolver.Highs_getIntOptionValue(this.highs, option, out value); - } + public HighsStatus passMip(HighsModel model) + { + return (HighsStatus)HighsLpSolver.Highs_passMip( + this.highs, + model.colcost.Length, + model.rowlower.Length, + model.avalue.Length, + (int)model.a_format, + (int)model.sense, + model.offset, + model.colcost, + model.collower, + model.colupper, + model.rowlower, + model.rowupper, + model.astart, + model.aindex, + model.avalue, + model.highs_integrality); + } - public int getNumCol() - { - return HighsLpSolver.Highs_getNumCol(this.highs); - } + public HighsStatus setOptionValue(string option, string value) + { + return (HighsStatus)HighsLpSolver.Highs_setOptionValue(this.highs, option, value); + } - public int getNumRow() - { - return HighsLpSolver.Highs_getNumRow(this.highs); - } + public HighsStatus setStringOptionValue(string option, string value) + { + return (HighsStatus)HighsLpSolver.Highs_setStringOptionValue(this.highs, option, value); + } - public int getNumNz() - { - return HighsLpSolver.Highs_getNumNz(this.highs); - } + public HighsStatus setBoolOptionValue(string option, int value) + { + return (HighsStatus)HighsLpSolver.Highs_setBoolOptionValue(this.highs, option, value); + } - public HighsSolution getSolution() - { - int nc = this.getNumCol(); - int nr = this.getNumRow(); + public HighsStatus setDoubleOptionValue(string option, double value) + { + return (HighsStatus)HighsLpSolver.Highs_setDoubleOptionValue(this.highs, option, value); + } - HighsSolution sol = new HighsSolution(nc, nr); - HighsLpSolver.Highs_getSolution(this.highs, sol.colvalue, sol.coldual, sol.rowvalue, sol.rowdual); + public HighsStatus setIntOptionValue(string option, int value) + { + return (HighsStatus)HighsLpSolver.Highs_setIntOptionValue(this.highs, option, value); + } - return sol; - } + public HighsStatus getStringOptionValue(string option, out string value) + { + var stringBuilder = new StringBuilder(); + var result = (HighsStatus)HighsLpSolver.Highs_getStringOptionValue(this.highs, option, stringBuilder); + value = stringBuilder.ToString(); + return result; + } - public HighsBasis getBasis() - { - int nc = this.getNumCol(); - int nr = this.getNumRow(); + public HighsStatus getBoolOptionValue(string option, out int value) + { + return (HighsStatus)HighsLpSolver.Highs_getBoolOptionValue(this.highs, option, out value); + } - int[] colbasstat = new int[nc]; - int[] rowbasstat = new int[nr]; + public HighsStatus getDoubleOptionValue(string option, out double value) + { + return (HighsStatus)HighsLpSolver.Highs_getDoubleOptionValue(this.highs, option, out value); + } - HighsLpSolver.Highs_getBasis(this.highs, colbasstat, rowbasstat); - HighsBasis bas = new HighsBasis( - colbasstat.Select(x => (HighsBasisStatus)x).ToArray(), - rowbasstat.Select(x => (HighsBasisStatus)x).ToArray()); + public HighsStatus getIntOptionValue(string option, out int value) + { + return (HighsStatus)HighsLpSolver.Highs_getIntOptionValue(this.highs, option, out value); + } - return bas; - } + public int getNumCol() + { + return HighsLpSolver.Highs_getNumCol(this.highs); + } - public double getObjectiveValue() - { - return HighsLpSolver.Highs_getObjectiveValue(this.highs); - } + public int getNumRow() + { + return HighsLpSolver.Highs_getNumRow(this.highs); + } - public HighsModelStatus GetModelStatus() - { - return (HighsModelStatus)HighsLpSolver.Highs_getModelStatus(this.highs); - } + public int getNumNz() + { + return HighsLpSolver.Highs_getNumNz(this.highs); + } - public int getIterationCount() - { - return HighsLpSolver.Highs_getIterationCount(this.highs); - } + public HighsSolution getSolution() + { + int nc = this.getNumCol(); + int nr = this.getNumRow(); - public HighsStatus addRow(double lower, double upper, int[] indices, double[] values) - { - return (HighsStatus)HighsLpSolver.Highs_addRow(this.highs, lower, upper, indices.Length, indices, values); - } + HighsSolution sol = new HighsSolution(nc, nr); + HighsLpSolver.Highs_getSolution(this.highs, sol.colvalue, sol.coldual, sol.rowvalue, sol.rowdual); - public HighsStatus addRows(double[] lower, double[] upper, int[] starts, int[] indices, double[] values) - { - return (HighsStatus)HighsLpSolver.Highs_addRows(this.highs, lower.Length, lower, upper, indices.Length, starts, indices, values); - } + return sol; + } - public HighsStatus addCol(double cost, double lower, double upper, int[] indices, double[] values) - { - return (HighsStatus)HighsLpSolver.Highs_addCol(this.highs, cost, lower, upper, indices.Length, indices, values); - } + public HighsBasis getBasis() + { + int nc = this.getNumCol(); + int nr = this.getNumRow(); - public HighsStatus addCols(double[] costs, double[] lower, double[] upper, int[] starts, int[] indices, double[] values) - { - return (HighsStatus)HighsLpSolver.Highs_addCols( - this.highs, - costs.Length, - costs, - lower, - upper, - indices.Length, - starts, - indices, - values); - } + int[] colbasstat = new int[nc]; + int[] rowbasstat = new int[nr]; - public HighsStatus changeObjectiveSense(HighsObjectiveSense sense) - { - return (HighsStatus)HighsLpSolver.Highs_changeObjectiveSense(this.highs, (int)sense); - } + HighsLpSolver.Highs_getBasis(this.highs, colbasstat, rowbasstat); + HighsBasis bas = new HighsBasis( + colbasstat.Select(x => (HighsBasisStatus)x).ToArray(), + rowbasstat.Select(x => (HighsBasisStatus)x).ToArray()); - public HighsStatus changeColCost(int col, double cost) - { - return (HighsStatus)HighsLpSolver.Highs_changeColCost(this.highs, col, cost); - } + return bas; + } - public HighsStatus changeColsCostBySet(int[] cols, double[] costs) - { - return (HighsStatus)HighsLpSolver.Highs_changeColsCostBySet(this.highs, cols.Length, cols, costs); - } + public double getObjectiveValue() + { + return HighsLpSolver.Highs_getObjectiveValue(this.highs); + } - public HighsStatus changeColsCostByMask(bool[] mask, double[] cost) - { - return (HighsStatus)HighsLpSolver.Highs_changeColsCostByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray(), cost); - } + public HighsModelStatus GetModelStatus() + { + return (HighsModelStatus)HighsLpSolver.Highs_getModelStatus(this.highs); + } - public HighsStatus changeColBounds(int col, double lower, double upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeColBounds(this.highs, col, lower, upper); - } + public int getIterationCount() + { + return HighsLpSolver.Highs_getIterationCount(this.highs); + } - public HighsStatus changeColsBoundsByRange(int from, int to, double[] lower, double[] upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeColsBoundsByRange(this.highs, from, to, lower, upper); - } + public HighsStatus addRow(double lower, double upper, int[] indices, double[] values) + { + return (HighsStatus)HighsLpSolver.Highs_addRow(this.highs, lower, upper, indices.Length, indices, values); + } - public HighsStatus changeColsBoundsBySet(int[] cols, double[] lower, double[] upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeColsBoundsBySet(this.highs, cols.Length, cols, lower, upper); - } + public HighsStatus addRows(double[] lower, double[] upper, int[] starts, int[] indices, double[] values) + { + return (HighsStatus)HighsLpSolver.Highs_addRows(this.highs, lower.Length, lower, upper, indices.Length, starts, indices, values); + } - public HighsStatus changeColsBoundsByMask(bool[] mask, double[] lower, double[] upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeColsBoundsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray(), lower, upper); - } + public HighsStatus addCol(double cost, double lower, double upper, int[] indices, double[] values) + { + return (HighsStatus)HighsLpSolver.Highs_addCol(this.highs, cost, lower, upper, indices.Length, indices, values); + } - public HighsStatus changeRowBounds(int row, double lower, double upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeRowBounds(this.highs, row, lower, upper); - } + public HighsStatus addCols(double[] costs, double[] lower, double[] upper, int[] starts, int[] indices, double[] values) + { + return (HighsStatus)HighsLpSolver.Highs_addCols( + this.highs, + costs.Length, + costs, + lower, + upper, + indices.Length, + starts, + indices, + values); + } - public HighsStatus changeRowsBoundsBySet(int[] rows, double[] lower, double[] upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeRowsBoundsBySet(this.highs, rows.Length, rows, lower, upper); - } + public HighsStatus changeObjectiveSense(HighsObjectiveSense sense) + { + return (HighsStatus)HighsLpSolver.Highs_changeObjectiveSense(this.highs, (int)sense); + } - public HighsStatus changeRowsBoundsByMask(bool[] mask, double[] lower, double[] upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeRowsBoundsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray(), lower, upper); - } + public HighsStatus changeColCost(int col, double cost) + { + return (HighsStatus)HighsLpSolver.Highs_changeColCost(this.highs, col, cost); + } - public HighsStatus changeColsIntegralityByRange(int from_col, int to_col, HighsIntegrality[] integrality) - { - return (HighsStatus)HighsLpSolver.Highs_changeColsIntegralityByRange(this.highs, from_col, to_col, Array.ConvertAll(integrality, item => (int)item)); - } + public HighsStatus changeColsCostBySet(int[] cols, double[] costs) + { + return (HighsStatus)HighsLpSolver.Highs_changeColsCostBySet(this.highs, cols.Length, cols, costs); + } - public HighsStatus changeCoeff(int row, int col, double value) - { - return (HighsStatus)HighsLpSolver.Highs_changeCoeff(this.highs, row, col, value); - } + public HighsStatus changeColsCostByMask(bool[] mask, double[] cost) + { + return (HighsStatus)HighsLpSolver.Highs_changeColsCostByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray(), cost); + } - public HighsStatus deleteColsByRange(int from, int to) - { - return (HighsStatus)HighsLpSolver.Highs_deleteColsByRange(this.highs, from, to); - } + public HighsStatus changeColBounds(int col, double lower, double upper) + { + return (HighsStatus)HighsLpSolver.Highs_changeColBounds(this.highs, col, lower, upper); + } - public HighsStatus deleteColsBySet(int[] cols) - { - return (HighsStatus)HighsLpSolver.Highs_deleteColsBySet(this.highs, cols.Length, cols); - } + public HighsStatus changeColsBoundsByRange(int from, int to, double[] lower, double[] upper) + { + return (HighsStatus)HighsLpSolver.Highs_changeColsBoundsByRange(this.highs, from, to, lower, upper); + } - public HighsStatus deleteColsByMask(bool[] mask) - { - return (HighsStatus)HighsLpSolver.Highs_deleteColsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray()); - } + public HighsStatus changeColsBoundsBySet(int[] cols, double[] lower, double[] upper) + { + return (HighsStatus)HighsLpSolver.Highs_changeColsBoundsBySet(this.highs, cols.Length, cols, lower, upper); + } - public HighsStatus deleteRowsByRange(int from, int to) - { - return (HighsStatus)HighsLpSolver.Highs_deleteRowsByRange(this.highs, from, to); - } + public HighsStatus changeColsBoundsByMask(bool[] mask, double[] lower, double[] upper) + { + return (HighsStatus)HighsLpSolver.Highs_changeColsBoundsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray(), lower, upper); + } - public HighsStatus deleteRowsBySet(int[] rows) - { - return (HighsStatus)HighsLpSolver.Highs_deleteRowsBySet(this.highs, rows.Length, rows); - } + public HighsStatus changeRowBounds(int row, double lower, double upper) + { + return (HighsStatus)HighsLpSolver.Highs_changeRowBounds(this.highs, row, lower, upper); + } - public HighsStatus deleteRowsByMask(bool[] mask) - { - return (HighsStatus)HighsLpSolver.Highs_deleteRowsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray()); - } + public HighsStatus changeRowsBoundsBySet(int[] rows, double[] lower, double[] upper) + { + return (HighsStatus)HighsLpSolver.Highs_changeRowsBoundsBySet(this.highs, rows.Length, rows, lower, upper); + } - delegate int HighsGetInfoDelegate(IntPtr highs, string infoName, out TValue output); + public HighsStatus changeRowsBoundsByMask(bool[] mask, double[] lower, double[] upper) + { + return (HighsStatus)HighsLpSolver.Highs_changeRowsBoundsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray(), lower, upper); + } - private TValue GetValueOrFallback(HighsGetInfoDelegate highsGetInfoDelegate, string infoName, TValue fallback) - { - try + public HighsStatus changeColsIntegralityByRange(int from_col, int to_col, HighsIntegrality[] integrality) { - var status = (HighsStatus)highsGetInfoDelegate(this.highs, infoName, out var value); - if (status != HighsStatus.kOk) - { - return fallback; - } + return (HighsStatus)HighsLpSolver.Highs_changeColsIntegralityByRange(this.highs, from_col, to_col, Array.ConvertAll(integrality, item => (int)item)); + } - return value; + public HighsStatus changeCoeff(int row, int col, double value) + { + return (HighsStatus)HighsLpSolver.Highs_changeCoeff(this.highs, row, col, value); } - catch + + public HighsStatus deleteColsByRange(int from, int to) { - return fallback; + return (HighsStatus)HighsLpSolver.Highs_deleteColsByRange(this.highs, from, to); } - } - /// - /// Gets the current solution info. - /// - /// The . - public SolutionInfo getInfo() - { - // TODO: This object does not contian the "complete" info from the C api. Add further props, if you need them. - var info = new SolutionInfo() - { - MipGap = this.GetValueOrFallback(HighsLpSolver.Highs_getDoubleInfoValue, "mip_gap", double.NaN), - DualBound = this.GetValueOrFallback(HighsLpSolver.Highs_getDoubleInfoValue, "mip_dual_bound", double.NaN), - ObjectiveValue = this.GetValueOrFallback(HighsLpSolver.Highs_getDoubleInfoValue, "objective_function_value", double.NaN), - NodeCount = this.GetValueOrFallback(HighsLpSolver.Highs_getInt64InfoValue, "mip_node_count", 0L), - IpmIterationCount = this.GetValueOrFallback(HighsLpSolver.Highs_getIntInfoValue, "ipm_iteration_count", 0), - SimplexIterationCount = this.GetValueOrFallback(HighsLpSolver.Highs_getIntInfoValue, "simplex_iteration_count", 0), - PdlpIterationCount = this.GetValueOrFallback(HighsLpSolver.Highs_getIntInfoValue, "pdlp_iteration_count", 0), - }; - return info; - } + public HighsStatus deleteColsBySet(int[] cols) + { + return (HighsStatus)HighsLpSolver.Highs_deleteColsBySet(this.highs, cols.Length, cols); + } - public HighsStatus setSolution(HighsSolution solution) - { - return (HighsStatus)HighsLpSolver.Highs_setSolution(this.highs, solution.colvalue, solution.coldual, solution.rowvalue, solution.rowdual); - } + public HighsStatus deleteColsByMask(bool[] mask) + { + return (HighsStatus)HighsLpSolver.Highs_deleteColsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray()); + } - public HighsStatus getBasicVariables(ref int[] basic_variables) - { - return (HighsStatus)Highs_getBasicVariables(this.highs, basic_variables); - } + public HighsStatus deleteRowsByRange(int from, int to) + { + return (HighsStatus)HighsLpSolver.Highs_deleteRowsByRange(this.highs, from, to); + } - public HighsStatus getBasisInverseRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) - { - return (HighsStatus)Highs_getBasisInverseRow(this.highs, row, row_vector, ref row_num_nz, row_indices); - } + public HighsStatus deleteRowsBySet(int[] rows) + { + return (HighsStatus)HighsLpSolver.Highs_deleteRowsBySet(this.highs, rows.Length, rows); + } - public HighsStatus getBasisInverseCol(int col, double[] col_vector, ref int col_num_nz, int[] col_indices) - { - return (HighsStatus)Highs_getBasisInverseCol(this.highs, col, col_vector, ref col_num_nz, col_indices); - } + public HighsStatus deleteRowsByMask(bool[] mask) + { + return (HighsStatus)HighsLpSolver.Highs_deleteRowsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray()); + } - public HighsStatus getBasisSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) - { - return (HighsStatus)Highs_getBasisSolve(this.highs, rhs, solution_vector, ref solution_num_nz, solution_indices); - } + delegate int HighsGetInfoDelegate(IntPtr highs, string infoName, out TValue output); - public HighsStatus getBasisTransposeSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) - { - return (HighsStatus)Highs_getBasisTransposeSolve(this.highs, rhs, solution_vector, ref solution_num_nz, solution_indices); - } + private TValue GetValueOrFallback(HighsGetInfoDelegate highsGetInfoDelegate, string infoName, TValue fallback) + { + try + { + var status = (HighsStatus)highsGetInfoDelegate(this.highs, infoName, out var value); + if (status != HighsStatus.kOk) + { + return fallback; + } - public HighsStatus getReducedRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) - { - return (HighsStatus)Highs_getReducedRow(this.highs, row, row_vector, ref row_num_nz, row_indices); - } + return value; + } + catch + { + return fallback; + } + } - public HighsStatus getReducedColumn(int col, double[] col_vector, ref int col_num_nz, int[] col_indices) - { - return (HighsStatus)Highs_getReducedColumn(this.highs, col, col_vector, ref col_num_nz, col_indices); - } + /// + /// Gets the current solution info. + /// + /// The . + public SolutionInfo getInfo() + { + // TODO: This object does not contian the "complete" info from the C api. Add further props, if you need them. + var info = new SolutionInfo() + { + MipGap = this.GetValueOrFallback(HighsLpSolver.Highs_getDoubleInfoValue, "mip_gap", double.NaN), + DualBound = this.GetValueOrFallback(HighsLpSolver.Highs_getDoubleInfoValue, "mip_dual_bound", double.NaN), + ObjectiveValue = this.GetValueOrFallback(HighsLpSolver.Highs_getDoubleInfoValue, "objective_function_value", double.NaN), + NodeCount = this.GetValueOrFallback(HighsLpSolver.Highs_getInt64InfoValue, "mip_node_count", 0L), + IpmIterationCount = this.GetValueOrFallback(HighsLpSolver.Highs_getIntInfoValue, "ipm_iteration_count", 0), + SimplexIterationCount = this.GetValueOrFallback(HighsLpSolver.Highs_getIntInfoValue, "simplex_iteration_count", 0), + PdlpIterationCount = this.GetValueOrFallback(HighsLpSolver.Highs_getIntInfoValue, "pdlp_iteration_count", 0), + }; + return info; + } - public HighsStatus clearModel() - { - return (HighsStatus)Highs_clearModel(this.highs); - } + public HighsStatus setSolution(HighsSolution solution) + { + return (HighsStatus)HighsLpSolver.Highs_setSolution(this.highs, solution.colvalue, solution.coldual, solution.rowvalue, solution.rowdual); + } - public HighsStatus clearSolver() - { - return (HighsStatus)Highs_clearSolver(this.highs); - } + public HighsStatus getBasicVariables(ref int[] basic_variables) + { + return (HighsStatus)Highs_getBasicVariables(this.highs, basic_variables); + } - public HighsStatus passColName(int col, string name) - { - return (HighsStatus)Highs_passColName(this.highs, col, name); - } + public HighsStatus getBasisInverseRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) + { + return (HighsStatus)Highs_getBasisInverseRow(this.highs, row, row_vector, ref row_num_nz, row_indices); + } - public HighsStatus passRowName(int row, string name) - { - return (HighsStatus)Highs_passRowName(this.highs, row, name); - } + public HighsStatus getBasisInverseCol(int col, double[] col_vector, ref int col_num_nz, int[] col_indices) + { + return (HighsStatus)Highs_getBasisInverseCol(this.highs, col, col_vector, ref col_num_nz, col_indices); + } - public HighsStatus writeOptions(string filename) - { - return (HighsStatus)Highs_writeOptions(this.highs, filename); - } + public HighsStatus getBasisSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) + { + return (HighsStatus)Highs_getBasisSolve(this.highs, rhs, solution_vector, ref solution_num_nz, solution_indices); + } - public HighsStatus writeOptionsDeviations(string filename) - { - return (HighsStatus)Highs_writeOptionsDeviations(this.highs, filename); - } -} + public HighsStatus getBasisTransposeSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) + { + return (HighsStatus)Highs_getBasisTransposeSolve(this.highs, rhs, solution_vector, ref solution_num_nz, solution_indices); + } -/// -/// The solution info. -/// -public class SolutionInfo -{ - /// - /// Gets or sets the simplex iteration count. - /// - public int SimplexIterationCount { get; set; } + public HighsStatus getReducedRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) + { + return (HighsStatus)Highs_getReducedRow(this.highs, row, row_vector, ref row_num_nz, row_indices); + } - /// - /// Gets or sets the Interior Point Method (IPM) iteration count. - /// - public int IpmIterationCount { get; set; } + public HighsStatus getReducedColumn(int col, double[] col_vector, ref int col_num_nz, int[] col_indices) + { + return (HighsStatus)Highs_getReducedColumn(this.highs, col, col_vector, ref col_num_nz, col_indices); + } - /// - /// Gets or sets the PDLP iteration count. - /// - public int PdlpIterationCount { get; set; } + public HighsStatus clearModel() + { + return (HighsStatus)Highs_clearModel(this.highs); + } - /// - /// Gets or sets the MIP gap. - /// - public double MipGap { get; set; } + public HighsStatus clearSolver() + { + return (HighsStatus)Highs_clearSolver(this.highs); + } - /// - /// Gets or sets the best dual bound. - /// - public double DualBound { get; set; } + public HighsStatus passColName(int col, string name) + { + return (HighsStatus)Highs_passColName(this.highs, col, name); + } - /// - /// Gets or sets the MIP node count. - /// - public long NodeCount { get; set; } + public HighsStatus passRowName(int row, string name) + { + return (HighsStatus)Highs_passRowName(this.highs, row, name); + } + + public HighsStatus writeOptions(string filename) + { + return (HighsStatus)Highs_writeOptions(this.highs, filename); + } + + public HighsStatus writeOptionsDeviations(string filename) + { + return (HighsStatus)Highs_writeOptionsDeviations(this.highs, filename); + } + } /// - /// Gets or sets the objective value. + /// The solution info. /// - public double ObjectiveValue { get; set; } -} + public class SolutionInfo + { + /// + /// Gets or sets the simplex iteration count. + /// + public int SimplexIterationCount { get; set; } + + /// + /// Gets or sets the Interior Point Method (IPM) iteration count. + /// + public int IpmIterationCount { get; set; } + + /// + /// Gets or sets the PDLP iteration count. + /// + public int PdlpIterationCount { get; set; } + + /// + /// Gets or sets the MIP gap. + /// + public double MipGap { get; set; } + + /// + /// Gets or sets the best dual bound. + /// + public double DualBound { get; set; } + + /// + /// Gets or sets the MIP node count. + /// + public long NodeCount { get; set; } + + /// + /// Gets or sets the objective value. + /// + public double ObjectiveValue { get; set; } + } } From 00c643f2df54bb0884f7fb3826cd3ef3e49a4260 Mon Sep 17 00:00:00 2001 From: Thiago Novaes <77932135+Thiago-NovaesB@users.noreply.github.com> Date: Fri, 26 Dec 2025 15:24:02 +0100 Subject: [PATCH 2/9] merge --- src/interfaces/highs_csharp_api.cs | 1929 +++++++++++++++------------- 1 file changed, 1015 insertions(+), 914 deletions(-) diff --git a/src/interfaces/highs_csharp_api.cs b/src/interfaces/highs_csharp_api.cs index 3bd9f6c46a..a15a13c975 100644 --- a/src/interfaces/highs_csharp_api.cs +++ b/src/interfaces/highs_csharp_api.cs @@ -1,1041 +1,1142 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; // mcs -out:highscslib.dll -t:library highs_csharp_api.cs -unsafe -namespace Highs; +namespace Highs { +public enum HighsStatus { - public enum HighsStatus + kError = -1, + kOk, + kWarning +} + +public enum HighsMatrixFormat +{ + kColwise = 1, + kRowwise +} + +public enum HessianFormat +{ + kTriangular = 1, + kSquare +} + +public enum HighsBasisStatus +{ + kLower = 0, + kBasic, + kUpper, + kZero, + kNonbasic +} + +public enum HighsObjectiveSense +{ + kMinimize = 1, + kMaximize = -1 +} + +public enum HighsModelStatus +{ + kNotset = 0, + kLoadError, + kModelError, + kPresolveError, + kSolveError, + kPostsolveError, + kModelEmpty, + kOptimal, + kInfeasible, + kUnboundedOrInfeasible, + kUnbounded, + kObjectiveBound, + kObjectiveTarget, + kTimeLimit, + kIterationLimit, + kUnknown, + kSolutionLimit, + kInterrupt, + kMemoryLimit, + kHighsInterrupt +} + +public enum HighsIntegrality +{ + kContinuous = 0, + kInteger = 1, + kSemiContinuous = 2, + kSemiInteger = 3, + kImplicitInteger = 4, +} + +public class HighsModel +{ + public HighsObjectiveSense sense; + public double[] colcost; + public double offset; + public double[] collower; + public double[] colupper; + public double[] rowlower; + public double[] rowupper; + public HighsMatrixFormat a_format; + public int[] astart; + public int[] aindex; + public double[] avalue; + public int[] highs_integrality; + + public HighsModel() { - kError = -1, - kOk, - kWarning + } - public enum HighsMatrixFormat + public HighsModel(double[] colcost, double[] collower, double[] colupper, double[] rowlower, double[] rowupper, + int[] astart, int[] aindex, double[] avalue, int[] highs_integrality = null, double offset = 0, HighsMatrixFormat a_format = HighsMatrixFormat.kColwise, HighsObjectiveSense sense = HighsObjectiveSense.kMinimize) { - kColwise = 1, - kRowwise + this.colcost = colcost; + this.collower = collower; + this.colupper = colupper; + this.rowlower = rowlower; + this.rowupper = rowupper; + this.astart = astart; + this.aindex = aindex; + this.avalue = avalue; + this.offset = offset; + this.a_format = a_format; + this.sense = sense; + this.highs_integrality = highs_integrality; } +} - public enum HighsBasisStatus +public class HighsHessian +{ + public HessianFormat q_format; + public int dim; + public int[] qstart; + public int[] qindex; + public double[] qvalue; + + public HighsHessian() { - kLower = 0, - kBasic, - kUpper, - kZero, - kNonbasic + } - public enum HighsObjectiveSense + public HighsHessian(int dim, int[] qstart, int[] qindex, double[] qvalue, HessianFormat q_format = HessianFormat.kTriangular) { - kMinimize = 1, - kMaximize = -1 + this.dim = dim; + this.qstart = qstart; + this.qindex = qindex; + this.qvalue = qvalue; + this.q_format = q_format; } +} + +public class HighsSolution +{ + public double[] colvalue; + public double[] coldual; + public double[] rowvalue; + public double[] rowdual; - public enum HighsModelStatus + public HighsSolution(int numcol, int numrow) { - kNotset = 0, - kLoadError, - kModelError, - kPresolveError, - kSolveError, - kPostsolveError, - kModelEmpty, - kOptimal, - kInfeasible, - kUnboundedOrInfeasible, - kUnbounded, - kObjectiveBound, - kObjectiveTarget, - kTimeLimit, - kIterationLimit, - kUnknown, - kSolutionLimit, - kInterrupt, - kMemoryLimit + this.colvalue = new double[numcol]; + this.coldual = new double[numcol]; + this.rowvalue = new double[numrow]; + this.rowdual = new double[numrow]; } - public enum HighsIntegrality + public HighsSolution(double[] colvalue, double[] coldual, double[] rowvalue, double[] rowdual) { - kContinuous = 0, - kInteger = 1, - kSemiContinuous = 2, - kSemiInteger = 3, - kImplicitInteger = 4, + this.colvalue = colvalue; + this.coldual = coldual; + this.rowvalue = rowvalue; + this.rowdual = rowdual; } +} + +public class HighsBasis +{ + public HighsBasisStatus[] colbasisstatus; + public HighsBasisStatus[] rowbasisstatus; - public class HighsModel + public HighsBasis(int numcol, int numrow) { - public HighsObjectiveSense sense; - public double[] colcost; - public double offset; - public double[] collower; - public double[] colupper; - public double[] rowlower; - public double[] rowupper; - public HighsMatrixFormat a_format; - public int[] astart; - public int[] aindex; - public double[] avalue; - public int[] highs_integrality; + this.colbasisstatus = new HighsBasisStatus[numcol]; + this.rowbasisstatus = new HighsBasisStatus[numrow]; + } - public HighsModel() - { + public HighsBasis(HighsBasisStatus[] colbasisstatus, HighsBasisStatus[] rowbasisstatus) + { + this.colbasisstatus = colbasisstatus; + this.rowbasisstatus = rowbasisstatus; + } +} - } +public class HighsLpSolver : IDisposable +{ + private IntPtr highs; + + private bool _disposed; + + private const string highslibname = "highs"; + + [DllImport(highslibname)] + private static extern int Highs_call( + Int32 numcol, + Int32 numrow, + Int32 numnz, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue, + double[] colvalue, + double[] coldual, + double[] rowvalue, + double[] rowdual, + int[] colbasisstatus, + int[] rowbasisstatus, + ref int modelstatus); + + [DllImport(highslibname)] + private static extern IntPtr Highs_create(); + + [DllImport(highslibname)] + private static extern void Highs_destroy(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_run(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_readModel(IntPtr highs, string filename); + + [DllImport(highslibname)] + private static extern int Highs_writeModel(IntPtr highs, string filename); + + [DllImport(highslibname)] + private static extern int Highs_writePresolvedModel(IntPtr highs, string filename); + + [DllImport(highslibname)] + private static extern int Highs_writeSolutionPretty(IntPtr highs, string filename); + + [DllImport(highslibname)] + private static extern int Highs_getInfinity(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_passLp( + IntPtr highs, + int numcol, + int numrow, + int numnz, + int aformat, + int sense, + double offset, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue); + + [DllImport(highslibname)] + private static extern int Highs_passMip( + IntPtr highs, + int numcol, + int numrow, + int numnz, + int aformat, + int sense, + double offset, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue, + int[] highs_integrality); + + [DllImport(highslibname)] + private static extern int Highs_passModel( + IntPtr highs, + int numcol, + int numrow, + int numnz, + int qnumnz, + int aformat, + int qformat, + int sense, + double offset, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue, + int[] qstart, + int[] qindex, + double[] qvalue, + int[] highs_integrality); + + [DllImport(highslibname)] + private static extern int Highs_passHessian( + IntPtr highs, + int dim, + int numnz, + int q_format, + int[] qstart, + int[] qindex, + double[] qvalue); + + [DllImport(highslibname)] + private static extern int Highs_setOptionValue(IntPtr highs, string option, string value); + + [DllImport(highslibname)] + private static extern int Highs_setBoolOptionValue(IntPtr highs, string option, int value); + + [DllImport(highslibname)] + private static extern int Highs_setIntOptionValue(IntPtr highs, string option, int value); + + [DllImport(highslibname)] + private static extern int Highs_setDoubleOptionValue(IntPtr highs, string option, double value); + + [DllImport(highslibname)] + private static extern int Highs_setStringOptionValue(IntPtr highs, string option, string value); + + [DllImport(highslibname)] + private static extern int Highs_getBoolOptionValue(IntPtr highs, string option, out int value); + + [DllImport(highslibname)] + private static extern int Highs_getIntOptionValue(IntPtr highs, string option, out int value); + + [DllImport(highslibname)] + private static extern int Highs_getDoubleOptionValue(IntPtr highs, string option, out double value); + + [DllImport(highslibname)] + private static extern int Highs_getStringOptionValue(IntPtr highs, string option, [Out] StringBuilder value); + + [DllImport(highslibname)] + private static extern int Highs_getSolution(IntPtr highs, double[] colvalue, double[] coldual, double[] rowvalue, double[] rowdual); + + [DllImport(highslibname)] + private static extern int Highs_getNumCol(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_getNumRow(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_getNumNz(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_getHessianNumNz(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_getBasis(IntPtr highs, int[] colstatus, int[] rowstatus); + + [DllImport(highslibname)] + private static extern double Highs_getObjectiveValue(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_getIterationCount(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_getModelStatus(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_addRow(IntPtr highs, double lower, double upper, int num_new_nz, int[] indices, double[] values); + + [DllImport(highslibname)] + private static extern int Highs_addRows( + IntPtr highs, + int num_new_row, + double[] lower, + double[] upper, + int num_new_nz, + int[] starts, + int[] indices, + double[] values); + + [DllImport(highslibname)] + private static extern int Highs_addCol( + IntPtr highs, + double cost, + double lower, + double upper, + int num_new_nz, + int[] indices, + double[] values); + + [DllImport(highslibname)] + private static extern int Highs_addCols( + IntPtr highs, + int num_new_col, + double[] costs, + double[] lower, + double[] upper, + int num_new_nz, + int[] starts, + int[] indices, + double[] values); - public HighsModel(double[] colcost, double[] collower, double[] colupper, double[] rowlower, double[] rowupper, - int[] astart, int[] aindex, double[] avalue, int[] highs_integrality = null, double offset = 0, HighsMatrixFormat a_format = HighsMatrixFormat.kColwise, HighsObjectiveSense sense = HighsObjectiveSense.kMinimize) - { - this.colcost = colcost; - this.collower = collower; - this.colupper = colupper; - this.rowlower = rowlower; - this.rowupper = rowupper; - this.astart = astart; - this.aindex = aindex; - this.avalue = avalue; - this.offset = offset; - this.a_format = a_format; - this.sense = sense; - this.highs_integrality = highs_integrality; - } + [DllImport(highslibname)] + private static extern int Highs_changeObjectiveSense(IntPtr highs, int sense); + + [DllImport(highslibname)] + private static extern int Highs_changeColCost(IntPtr highs, int col, double cost); + + [DllImport(highslibname)] + private static extern int Highs_changeColsCostBySet(IntPtr highs, int num_set_entries, int[] set, double[] cost); + + [DllImport(highslibname)] + private static extern int Highs_changeColsCostByMask(IntPtr highs, int[] mask, double[] cost); + + [DllImport(highslibname)] + private static extern int Highs_changeColBounds(IntPtr highs, int col, double lower, double upper); + + [DllImport(highslibname)] + private static extern int Highs_changeColsBoundsByRange(IntPtr highs, int from_col, int to_col, double[] lower, double[] upper); + + [DllImport(highslibname)] + private static extern int Highs_changeColsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); + + [DllImport(highslibname)] + private static extern int Highs_changeColsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); + + [DllImport(highslibname)] + private static extern int Highs_changeRowBounds(IntPtr highs, int row, double lower, double upper); + + [DllImport(highslibname)] + private static extern int Highs_changeRowsBoundsByRange(IntPtr highs, int from_row, int to_row, double[] lower, double[] upper); + + [DllImport(highslibname)] + private static extern int Highs_changeRowsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); + + [DllImport(highslibname)] + private static extern int Highs_changeRowsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); + + [DllImport(highslibname)] + private static extern int Highs_changeColsIntegralityByRange(IntPtr highs, int from_col, int to_col, int[] integrality); + + [DllImport(highslibname)] + private static extern int Highs_changeCoeff(IntPtr highs, int row, int col, double value); + + [DllImport(highslibname)] + private static extern int Highs_deleteColsByRange(IntPtr highs, int from_col, int to_col); + + [DllImport(highslibname)] + private static extern int Highs_deleteColsBySet(IntPtr highs, int num_set_entries, int[] set); + + [DllImport(highslibname)] + private static extern int Highs_deleteColsByMask(IntPtr highs, int[] mask); + + [DllImport(highslibname)] + private static extern int Highs_deleteRowsByRange(IntPtr highs, int from_row, int to_row); + + [DllImport(highslibname)] + private static extern int Highs_deleteRowsBySet(IntPtr highs, int num_set_entries, int[] set); + + [DllImport(highslibname)] + private static extern int Highs_deleteRowsByMask(IntPtr highs, int[] mask); + + [DllImport(highslibname)] + private static extern int Highs_getDoubleInfoValue(IntPtr highs, string info, out double value); + + [DllImport(highslibname)] + private static extern int Highs_getIntInfoValue(IntPtr highs, string info, out int value); + + [DllImport(highslibname)] + private static extern int Highs_getInt64InfoValue(IntPtr highs, string info, out long value); + + [DllImport(highslibname)] + private static extern int Highs_setSolution(IntPtr highs, double[] col_value, double[] row_value, double[] col_dual, double[] row_dual); + + [DllImport(highslibname)] + private static extern int Highs_setSparseSolution(IntPtr highs, int num_entries, int[] index, double[] value); + + [DllImport(highslibname)] + private static extern int Highs_getColsByRange( + IntPtr highs, + int from_col, + int to_col, + ref int num_col, + double[] costs, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [DllImport(highslibname)] + private static extern int Highs_getColsBySet( + IntPtr highs, + int num_set_entries, + int[] set, + ref int num_col, + double[] costs, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [DllImport(highslibname)] + private static extern int Highs_getColsByMask( + IntPtr highs, + int[] mask, + ref int num_col, + double[] costs, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [DllImport(highslibname)] + private static extern int Highs_getRowsByRange( + IntPtr highs, + int from_row, + int to_row, + ref int num_row, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [DllImport(highslibname)] + private static extern int Highs_getRowsBySet( + IntPtr highs, + int num_set_entries, + int[] set, + ref int num_row, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [DllImport(highslibname)] + private static extern int Highs_getRowsByMask( + IntPtr highs, + int[] mask, + ref int num_row, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [DllImport(highslibname)] + private static extern int Highs_getBasicVariables(IntPtr highs, int[] basic_variables); + + [DllImport(highslibname)] + private static extern int Highs_getBasisInverseRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); + + [DllImport(highslibname)] + private static extern int Highs_getBasisInverseCol(IntPtr highs, int col, double[] col_vector, ref int col_num_nz, int[] col_indices); + + [DllImport(highslibname)] + private static extern int Highs_getBasisSolve( + IntPtr highs, + double[] rhs, + double[] solution_vector, + ref int solution_num_nz, + int[] solution_indices); + + [DllImport(highslibname)] + private static extern int Highs_getBasisTransposeSolve( + IntPtr highs, + double[] rhs, + double[] solution_vector, + ref int solution_nz, + int[] solution_indices); + + [DllImport(highslibname)] + private static extern int Highs_getReducedRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); + + [DllImport(highslibname)] + private static extern int Highs_getReducedColumn(IntPtr highs, int col, double[] col_vector, ref int col_num_nz, int[] col_indices); + + [DllImport(highslibname)] + private static extern int Highs_clearModel(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_clearSolver(IntPtr highs); + + [DllImport(highslibname)] + private static extern int Highs_passColName(IntPtr highs, int col, string name); + + [DllImport(highslibname)] + private static extern int Highs_passRowName(IntPtr highs, int row, string name); + + [DllImport(highslibname)] + private static extern int Highs_writeOptions(IntPtr highs, string filename); + + [DllImport(highslibname)] + private static extern int Highs_writeOptionsDeviations(IntPtr highs, string filename); + + public static HighsStatus call(HighsModel model, ref HighsSolution sol, ref HighsBasis bas, ref HighsModelStatus modelstatus) + { + int nc = model.colcost.Length; + int nr = model.rowlower.Length; + int nnz = model.avalue.Length; + + int[] colbasstat = new int[nc]; + int[] rowbasstat = new int[nr]; + + int modelstate = 0; + + HighsStatus status = (HighsStatus)HighsLpSolver.Highs_call( + nc, + nr, + nnz, + model.colcost, + model.collower, + model.colupper, + model.rowlower, + model.rowupper, + model.astart, + model.aindex, + model.avalue, + sol.colvalue, + sol.coldual, + sol.rowvalue, + sol.rowdual, + colbasstat, + rowbasstat, + ref modelstate); + + modelstatus = (HighsModelStatus)modelstate; + + bas.colbasisstatus = colbasstat.Select(x => (HighsBasisStatus)x).ToArray(); + bas.rowbasisstatus = rowbasstat.Select(x => (HighsBasisStatus)x).ToArray(); + + return status; } - public class HighsSolution + public HighsLpSolver() { - public double[] colvalue; - public double[] coldual; - public double[] rowvalue; - public double[] rowdual; + this.highs = HighsLpSolver.Highs_create(); + } - public HighsSolution(int numcol, int numrow) - { - this.colvalue = new double[numcol]; - this.coldual = new double[numcol]; - this.rowvalue = new double[numrow]; - this.rowdual = new double[numrow]; - } + ~HighsLpSolver() + { + this.Dispose(false); + } - public HighsSolution(double[] colvalue, double[] coldual, double[] rowvalue, double[] rowdual) + public void Dispose() + { + this.Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (this._disposed) { - this.colvalue = colvalue; - this.coldual = coldual; - this.rowvalue = rowvalue; - this.rowdual = rowdual; + return; } + + HighsLpSolver.Highs_destroy(this.highs); + this._disposed = true; } - public class HighsBasis + public HighsStatus run() { - public HighsBasisStatus[] colbasisstatus; - public HighsBasisStatus[] rowbasisstatus; + return (HighsStatus)HighsLpSolver.Highs_run(this.highs); + } - public HighsBasis(int numcol, int numrow) - { - this.colbasisstatus = new HighsBasisStatus[numcol]; - this.rowbasisstatus = new HighsBasisStatus[numrow]; - } + public HighsStatus readModel(string filename) + { + return (HighsStatus)HighsLpSolver.Highs_readModel(this.highs, filename); + } - public HighsBasis(HighsBasisStatus[] colbasisstatus, HighsBasisStatus[] rowbasisstatus) - { - this.colbasisstatus = colbasisstatus; - this.rowbasisstatus = rowbasisstatus; - } + public HighsStatus writeModel(string filename) + { + return (HighsStatus)HighsLpSolver.Highs_writeModel(this.highs, filename); } - public class HighsLpSolver : IDisposable - { - private IntPtr highs; - - private bool _disposed; - - private const string highslibname = "highs"; - - [DllImport(highslibname)] - private static extern int Highs_call( - Int32 numcol, - Int32 numrow, - Int32 numnz, - double[] colcost, - double[] collower, - double[] colupper, - double[] rowlower, - double[] rowupper, - int[] astart, - int[] aindex, - double[] avalue, - double[] colvalue, - double[] coldual, - double[] rowvalue, - double[] rowdual, - int[] colbasisstatus, - int[] rowbasisstatus, - ref int modelstatus); - - [DllImport(highslibname)] - private static extern IntPtr Highs_create(); - - [DllImport(highslibname)] - private static extern void Highs_destroy(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_run(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_readModel(IntPtr highs, string filename); - - [DllImport(highslibname)] - private static extern int Highs_writeModel(IntPtr highs, string filename); - - [DllImport(highslibname)] - private static extern int Highs_writePresolvedModel(IntPtr highs, string filename); - - [DllImport(highslibname)] - private static extern int Highs_writeSolutionPretty(IntPtr highs, string filename); - - [DllImport(highslibname)] - private static extern int Highs_getInfinity(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_passLp( - IntPtr highs, - int numcol, - int numrow, - int numnz, - int aformat, - int sense, - double offset, - double[] colcost, - double[] collower, - double[] colupper, - double[] rowlower, - double[] rowupper, - int[] astart, - int[] aindex, - double[] avalue); - - [DllImport(highslibname)] - private static extern int Highs_passMip( - IntPtr highs, - int numcol, - int numrow, - int numnz, - int aformat, - int sense, - double offset, - double[] colcost, - double[] collower, - double[] colupper, - double[] rowlower, - double[] rowupper, - int[] astart, - int[] aindex, - double[] avalue, - int[] highs_integrality); - - [DllImport(highslibname)] - private static extern int Highs_setOptionValue(IntPtr highs, string option, string value); - - [DllImport(highslibname)] - private static extern int Highs_setBoolOptionValue(IntPtr highs, string option, int value); - - [DllImport(highslibname)] - private static extern int Highs_setIntOptionValue(IntPtr highs, string option, int value); - - [DllImport(highslibname)] - private static extern int Highs_setDoubleOptionValue(IntPtr highs, string option, double value); - - [DllImport(highslibname)] - private static extern int Highs_setStringOptionValue(IntPtr highs, string option, string value); - - [DllImport(highslibname)] - private static extern int Highs_getBoolOptionValue(IntPtr highs, string option, out int value); - - [DllImport(highslibname)] - private static extern int Highs_getIntOptionValue(IntPtr highs, string option, out int value); - - [DllImport(highslibname)] - private static extern int Highs_getDoubleOptionValue(IntPtr highs, string option, out double value); - - [DllImport(highslibname)] - private static extern int Highs_getStringOptionValue(IntPtr highs, string option, [Out] StringBuilder value); - - [DllImport(highslibname)] - private static extern int Highs_getSolution(IntPtr highs, double[] colvalue, double[] coldual, double[] rowvalue, double[] rowdual); - - [DllImport(highslibname)] - private static extern int Highs_getNumCol(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_getNumRow(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_getNumNz(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_getBasis(IntPtr highs, int[] colstatus, int[] rowstatus); - - [DllImport(highslibname)] - private static extern double Highs_getObjectiveValue(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_getIterationCount(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_getModelStatus(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_addRow(IntPtr highs, double lower, double upper, int num_new_nz, int[] indices, double[] values); - - [DllImport(highslibname)] - private static extern int Highs_addRows( - IntPtr highs, - int num_new_row, - double[] lower, - double[] upper, - int num_new_nz, - int[] starts, - int[] indices, - double[] values); - - [DllImport(highslibname)] - private static extern int Highs_addCol( - IntPtr highs, - double cost, - double lower, - double upper, - int num_new_nz, - int[] indices, - double[] values); - - [DllImport(highslibname)] - private static extern int Highs_addCols( - IntPtr highs, - int num_new_col, - double[] costs, - double[] lower, - double[] upper, - int num_new_nz, - int[] starts, - int[] indices, - double[] values); - - [DllImport(highslibname)] - private static extern int Highs_changeObjectiveSense(IntPtr highs, int sense); - - [DllImport(highslibname)] - private static extern int Highs_changeColCost(IntPtr highs, int col, double cost); - - [DllImport(highslibname)] - private static extern int Highs_changeColsCostBySet(IntPtr highs, int num_set_entries, int[] set, double[] cost); - - [DllImport(highslibname)] - private static extern int Highs_changeColsCostByMask(IntPtr highs, int[] mask, double[] cost); - - [DllImport(highslibname)] - private static extern int Highs_changeColBounds(IntPtr highs, int col, double lower, double upper); - - [DllImport(highslibname)] - private static extern int Highs_changeColsBoundsByRange(IntPtr highs, int from_col, int to_col, double[] lower, double[] upper); - - [DllImport(highslibname)] - private static extern int Highs_changeColsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); - - [DllImport(highslibname)] - private static extern int Highs_changeColsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); - - [DllImport(highslibname)] - private static extern int Highs_changeRowBounds(IntPtr highs, int row, double lower, double upper); - - [DllImport(highslibname)] - private static extern int Highs_changeRowsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); - - [DllImport(highslibname)] - private static extern int Highs_changeRowsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); - - [DllImport(highslibname)] - private static extern int Highs_changeColsIntegralityByRange(IntPtr highs, int from_col, int to_col, int[] integrality); - - [DllImport(highslibname)] - private static extern int Highs_changeCoeff(IntPtr highs, int row, int col, double value); - - [DllImport(highslibname)] - private static extern int Highs_deleteColsByRange(IntPtr highs, int from_col, int to_col); - - [DllImport(highslibname)] - private static extern int Highs_deleteColsBySet(IntPtr highs, int num_set_entries, int[] set); - - [DllImport(highslibname)] - private static extern int Highs_deleteColsByMask(IntPtr highs, int[] mask); - - [DllImport(highslibname)] - private static extern int Highs_deleteRowsByRange(IntPtr highs, int from_row, int to_row); - - [DllImport(highslibname)] - private static extern int Highs_deleteRowsBySet(IntPtr highs, int num_set_entries, int[] set); - - [DllImport(highslibname)] - private static extern int Highs_deleteRowsByMask(IntPtr highs, int[] mask); - - [DllImport(highslibname)] - private static extern int Highs_getDoubleInfoValue(IntPtr highs, string info, out double value); - - [DllImport(highslibname)] - private static extern int Highs_getIntInfoValue(IntPtr highs, string info, out int value); - - [DllImport(highslibname)] - private static extern int Highs_getInt64InfoValue(IntPtr highs, string info, out long value); - - [DllImport(highslibname)] - private static extern int Highs_setSolution(IntPtr highs, double[] col_value, double[] row_value, double[] col_dual, double[] row_dual); - - [DllImport(highslibname)] - private static extern int Highs_getColsByRange( - IntPtr highs, - int from_col, - int to_col, - ref int num_col, - double[] costs, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(highslibname)] - private static extern int Highs_getColsBySet( - IntPtr highs, - int num_set_entries, - int[] set, - ref int num_col, - double[] costs, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(highslibname)] - private static extern int Highs_getColsByMask( - IntPtr highs, - int[] mask, - ref int num_col, - double[] costs, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(highslibname)] - private static extern int Highs_getRowsByRange( - IntPtr highs, - int from_row, - int to_row, - ref int num_row, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(highslibname)] - private static extern int Highs_getRowsBySet( - IntPtr highs, - int num_set_entries, - int[] set, - ref int num_row, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(highslibname)] - private static extern int Highs_getRowsByMask( - IntPtr highs, - int[] mask, - ref int num_row, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(highslibname)] - private static extern int Highs_getBasicVariables(IntPtr highs, int[] basic_variables); - - [DllImport(highslibname)] - private static extern int Highs_getBasisInverseRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); - - [DllImport(highslibname)] - private static extern int Highs_getBasisInverseCol(IntPtr highs, int col, double[] col_vector, ref int col_num_nz, int[] col_indices); - - [DllImport(highslibname)] - private static extern int Highs_getBasisSolve( - IntPtr highs, - double[] rhs, - double[] solution_vector, - ref int solution_num_nz, - int[] solution_indices); - - [DllImport(highslibname)] - private static extern int Highs_getBasisTransposeSolve( - IntPtr highs, - double[] rhs, - double[] solution_vector, - ref int solution_nz, - int[] solution_indices); - - [DllImport(highslibname)] - private static extern int Highs_getReducedRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); - - [DllImport(highslibname)] - private static extern int Highs_getReducedColumn(IntPtr highs, int col, double[] col_vector, ref int col_num_nz, int[] col_indices); - - [DllImport(highslibname)] - private static extern int Highs_clearModel(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_clearSolver(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_passColName(IntPtr highs, int col, string name); - - [DllImport(highslibname)] - private static extern int Highs_passRowName(IntPtr highs, int row, string name); - - [DllImport(highslibname)] - private static extern int Highs_writeOptions(IntPtr highs, string filename); - - [DllImport(highslibname)] - private static extern int Highs_writeOptionsDeviations(IntPtr highs, string filename); - - public static HighsStatus call(HighsModel model, ref HighsSolution sol, ref HighsBasis bas, ref HighsModelStatus modelstatus) - { - int nc = model.colcost.Length; - int nr = model.rowlower.Length; - int nnz = model.avalue.Length; - - int[] colbasstat = new int[nc]; - int[] rowbasstat = new int[nr]; - - int modelstate = 0; - - HighsStatus status = (HighsStatus)HighsLpSolver.Highs_call( - nc, - nr, - nnz, - model.colcost, - model.collower, - model.colupper, - model.rowlower, - model.rowupper, - model.astart, - model.aindex, - model.avalue, - sol.colvalue, - sol.coldual, - sol.rowvalue, - sol.rowdual, - colbasstat, - rowbasstat, - ref modelstate); - - modelstatus = (HighsModelStatus)modelstate; - - bas.colbasisstatus = colbasstat.Select(x => (HighsBasisStatus)x).ToArray(); - bas.rowbasisstatus = rowbasstat.Select(x => (HighsBasisStatus)x).ToArray(); - - return status; - } + public HighsStatus writePresolvedModel(string filename) + { + return (HighsStatus)HighsLpSolver.Highs_writePresolvedModel(this.highs, filename); + } - public HighsLpSolver() - { - this.highs = HighsLpSolver.Highs_create(); - } + public HighsStatus writeSolutionPretty(string filename) + { + return (HighsStatus)HighsLpSolver.Highs_writeSolutionPretty(this.highs, filename); + } - ~HighsLpSolver() - { - this.Dispose(false); - } + public Double getInfinity() + { + return (Double)HighsLpSolver.Highs_getInfinity(this.highs); + } - public void Dispose() - { - this.Dispose(true); - GC.SuppressFinalize(this); - } + public HighsStatus passLp(HighsModel model) + { + return (HighsStatus)HighsLpSolver.Highs_passLp( + this.highs, + model.colcost.Length, + model.rowlower.Length, + model.avalue.Length, + (int)model.a_format, + (int)model.sense, + model.offset, + model.colcost, + model.collower, + model.colupper, + model.rowlower, + model.rowupper, + model.astart, + model.aindex, + model.avalue); + } - protected virtual void Dispose(bool disposing) - { - if (this._disposed) - { - return; - } + public HighsStatus passMip(HighsModel model) + { + return (HighsStatus)HighsLpSolver.Highs_passMip( + this.highs, + model.colcost.Length, + model.rowlower.Length, + model.avalue.Length, + (int)model.a_format, + (int)model.sense, + model.offset, + model.colcost, + model.collower, + model.colupper, + model.rowlower, + model.rowupper, + model.astart, + model.aindex, + model.avalue, + model.highs_integrality); + } - HighsLpSolver.Highs_destroy(this.highs); - this._disposed = true; - } + public HighsStatus passHessian(HighsHessian hessian) + { + return (HighsStatus)HighsLpSolver.Highs_passHessian( + this.highs, + hessian.dim, + hessian.qvalue.Length, + (int)hessian.q_format, + hessian.qstart, + hessian.qindex, + hessian.qvalue); + } - public HighsStatus run() - { - return (HighsStatus)HighsLpSolver.Highs_run(this.highs); - } + public HighsStatus setOptionValue(string option, string value) + { + return (HighsStatus)HighsLpSolver.Highs_setOptionValue(this.highs, option, value); + } - public HighsStatus readModel(string filename) - { - return (HighsStatus)HighsLpSolver.Highs_readModel(this.highs, filename); - } + public HighsStatus setStringOptionValue(string option, string value) + { + return (HighsStatus)HighsLpSolver.Highs_setStringOptionValue(this.highs, option, value); + } - public HighsStatus writeModel(string filename) - { - return (HighsStatus)HighsLpSolver.Highs_writeModel(this.highs, filename); - } + public HighsStatus setBoolOptionValue(string option, int value) + { + return (HighsStatus)HighsLpSolver.Highs_setBoolOptionValue(this.highs, option, value); + } - public HighsStatus writePresolvedModel(string filename) - { - return (HighsStatus)HighsLpSolver.Highs_writePresolvedModel(this.highs, filename); - } + public HighsStatus setDoubleOptionValue(string option, double value) + { + return (HighsStatus)HighsLpSolver.Highs_setDoubleOptionValue(this.highs, option, value); + } - public HighsStatus writeSolutionPretty(string filename) - { - return (HighsStatus)HighsLpSolver.Highs_writeSolutionPretty(this.highs, filename); - } + public HighsStatus setIntOptionValue(string option, int value) + { + return (HighsStatus)HighsLpSolver.Highs_setIntOptionValue(this.highs, option, value); + } - public Double getInfinity() - { - return (Double)HighsLpSolver.Highs_getInfinity(this.highs); - } + public HighsStatus getStringOptionValue(string option, out string value) + { + var stringBuilder = new StringBuilder(); + var result = (HighsStatus)HighsLpSolver.Highs_getStringOptionValue(this.highs, option, stringBuilder); + value = stringBuilder.ToString(); + return result; + } - public HighsStatus passLp(HighsModel model) - { - return (HighsStatus)HighsLpSolver.Highs_passLp( - this.highs, - model.colcost.Length, - model.rowlower.Length, - model.avalue.Length, - (int)model.a_format, - (int)model.sense, - model.offset, - model.colcost, - model.collower, - model.colupper, - model.rowlower, - model.rowupper, - model.astart, - model.aindex, - model.avalue); - } + public HighsStatus getBoolOptionValue(string option, out int value) + { + return (HighsStatus)HighsLpSolver.Highs_getBoolOptionValue(this.highs, option, out value); + } - public HighsStatus passMip(HighsModel model) - { - return (HighsStatus)HighsLpSolver.Highs_passMip( - this.highs, - model.colcost.Length, - model.rowlower.Length, - model.avalue.Length, - (int)model.a_format, - (int)model.sense, - model.offset, - model.colcost, - model.collower, - model.colupper, - model.rowlower, - model.rowupper, - model.astart, - model.aindex, - model.avalue, - model.highs_integrality); - } + public HighsStatus getDoubleOptionValue(string option, out double value) + { + return (HighsStatus)HighsLpSolver.Highs_getDoubleOptionValue(this.highs, option, out value); + } - public HighsStatus setOptionValue(string option, string value) - { - return (HighsStatus)HighsLpSolver.Highs_setOptionValue(this.highs, option, value); - } + public HighsStatus getIntOptionValue(string option, out int value) + { + return (HighsStatus)HighsLpSolver.Highs_getIntOptionValue(this.highs, option, out value); + } - public HighsStatus setStringOptionValue(string option, string value) - { - return (HighsStatus)HighsLpSolver.Highs_setStringOptionValue(this.highs, option, value); - } + public int getNumCol() + { + return HighsLpSolver.Highs_getNumCol(this.highs); + } - public HighsStatus setBoolOptionValue(string option, int value) - { - return (HighsStatus)HighsLpSolver.Highs_setBoolOptionValue(this.highs, option, value); - } + public int getNumRow() + { + return HighsLpSolver.Highs_getNumRow(this.highs); + } - public HighsStatus setDoubleOptionValue(string option, double value) - { - return (HighsStatus)HighsLpSolver.Highs_setDoubleOptionValue(this.highs, option, value); - } + public int getNumNz() + { + return HighsLpSolver.Highs_getNumNz(this.highs); + } - public HighsStatus setIntOptionValue(string option, int value) - { - return (HighsStatus)HighsLpSolver.Highs_setIntOptionValue(this.highs, option, value); - } + public HighsSolution getSolution() + { + int nc = this.getNumCol(); + int nr = this.getNumRow(); - public HighsStatus getStringOptionValue(string option, out string value) - { - var stringBuilder = new StringBuilder(); - var result = (HighsStatus)HighsLpSolver.Highs_getStringOptionValue(this.highs, option, stringBuilder); - value = stringBuilder.ToString(); - return result; - } + HighsSolution sol = new HighsSolution(nc, nr); + HighsLpSolver.Highs_getSolution(this.highs, sol.colvalue, sol.coldual, sol.rowvalue, sol.rowdual); - public HighsStatus getBoolOptionValue(string option, out int value) - { - return (HighsStatus)HighsLpSolver.Highs_getBoolOptionValue(this.highs, option, out value); - } + return sol; + } - public HighsStatus getDoubleOptionValue(string option, out double value) - { - return (HighsStatus)HighsLpSolver.Highs_getDoubleOptionValue(this.highs, option, out value); - } + public HighsBasis getBasis() + { + int nc = this.getNumCol(); + int nr = this.getNumRow(); - public HighsStatus getIntOptionValue(string option, out int value) - { - return (HighsStatus)HighsLpSolver.Highs_getIntOptionValue(this.highs, option, out value); - } + int[] colbasstat = new int[nc]; + int[] rowbasstat = new int[nr]; - public int getNumCol() - { - return HighsLpSolver.Highs_getNumCol(this.highs); - } + HighsLpSolver.Highs_getBasis(this.highs, colbasstat, rowbasstat); + HighsBasis bas = new HighsBasis( + colbasstat.Select(x => (HighsBasisStatus)x).ToArray(), + rowbasstat.Select(x => (HighsBasisStatus)x).ToArray()); - public int getNumRow() - { - return HighsLpSolver.Highs_getNumRow(this.highs); - } + return bas; + } - public int getNumNz() - { - return HighsLpSolver.Highs_getNumNz(this.highs); - } + public double getObjectiveValue() + { + return HighsLpSolver.Highs_getObjectiveValue(this.highs); + } - public HighsSolution getSolution() - { - int nc = this.getNumCol(); - int nr = this.getNumRow(); + public HighsModelStatus GetModelStatus() + { + return (HighsModelStatus)HighsLpSolver.Highs_getModelStatus(this.highs); + } - HighsSolution sol = new HighsSolution(nc, nr); - HighsLpSolver.Highs_getSolution(this.highs, sol.colvalue, sol.coldual, sol.rowvalue, sol.rowdual); + public int getIterationCount() + { + return HighsLpSolver.Highs_getIterationCount(this.highs); + } - return sol; - } + public HighsStatus addRow(double lower, double upper, int[] indices, double[] values) + { + return (HighsStatus)HighsLpSolver.Highs_addRow(this.highs, lower, upper, indices.Length, indices, values); + } - public HighsBasis getBasis() - { - int nc = this.getNumCol(); - int nr = this.getNumRow(); + public HighsStatus addRows(double[] lower, double[] upper, int[] starts, int[] indices, double[] values) + { + return (HighsStatus)HighsLpSolver.Highs_addRows(this.highs, lower.Length, lower, upper, indices.Length, starts, indices, values); + } - int[] colbasstat = new int[nc]; - int[] rowbasstat = new int[nr]; + public HighsStatus addCol(double cost, double lower, double upper, int[] indices, double[] values) + { + return (HighsStatus)HighsLpSolver.Highs_addCol(this.highs, cost, lower, upper, indices.Length, indices, values); + } - HighsLpSolver.Highs_getBasis(this.highs, colbasstat, rowbasstat); - HighsBasis bas = new HighsBasis( - colbasstat.Select(x => (HighsBasisStatus)x).ToArray(), - rowbasstat.Select(x => (HighsBasisStatus)x).ToArray()); + public HighsStatus addCols(double[] costs, double[] lower, double[] upper, int[] starts, int[] indices, double[] values) + { + return (HighsStatus)HighsLpSolver.Highs_addCols( + this.highs, + costs.Length, + costs, + lower, + upper, + indices.Length, + starts, + indices, + values); + } - return bas; - } + public HighsStatus changeObjectiveSense(HighsObjectiveSense sense) + { + return (HighsStatus)HighsLpSolver.Highs_changeObjectiveSense(this.highs, (int)sense); + } - public double getObjectiveValue() - { - return HighsLpSolver.Highs_getObjectiveValue(this.highs); - } + public HighsStatus changeColCost(int col, double cost) + { + return (HighsStatus)HighsLpSolver.Highs_changeColCost(this.highs, col, cost); + } - public HighsModelStatus GetModelStatus() - { - return (HighsModelStatus)HighsLpSolver.Highs_getModelStatus(this.highs); - } + public HighsStatus changeColsCostBySet(int[] cols, double[] costs) + { + return (HighsStatus)HighsLpSolver.Highs_changeColsCostBySet(this.highs, cols.Length, cols, costs); + } - public int getIterationCount() - { - return HighsLpSolver.Highs_getIterationCount(this.highs); - } + public HighsStatus changeColsCostByMask(bool[] mask, double[] cost) + { + return (HighsStatus)HighsLpSolver.Highs_changeColsCostByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray(), cost); + } - public HighsStatus addRow(double lower, double upper, int[] indices, double[] values) - { - return (HighsStatus)HighsLpSolver.Highs_addRow(this.highs, lower, upper, indices.Length, indices, values); - } + public HighsStatus changeColBounds(int col, double lower, double upper) + { + return (HighsStatus)HighsLpSolver.Highs_changeColBounds(this.highs, col, lower, upper); + } - public HighsStatus addRows(double[] lower, double[] upper, int[] starts, int[] indices, double[] values) - { - return (HighsStatus)HighsLpSolver.Highs_addRows(this.highs, lower.Length, lower, upper, indices.Length, starts, indices, values); - } + public HighsStatus changeColsBoundsByRange(int from, int to, double[] lower, double[] upper) + { + return (HighsStatus)HighsLpSolver.Highs_changeColsBoundsByRange(this.highs, from, to, lower, upper); + } - public HighsStatus addCol(double cost, double lower, double upper, int[] indices, double[] values) - { - return (HighsStatus)HighsLpSolver.Highs_addCol(this.highs, cost, lower, upper, indices.Length, indices, values); - } + public HighsStatus changeColsBoundsBySet(int[] cols, double[] lower, double[] upper) + { + return (HighsStatus)HighsLpSolver.Highs_changeColsBoundsBySet(this.highs, cols.Length, cols, lower, upper); + } - public HighsStatus addCols(double[] costs, double[] lower, double[] upper, int[] starts, int[] indices, double[] values) - { - return (HighsStatus)HighsLpSolver.Highs_addCols( - this.highs, - costs.Length, - costs, - lower, - upper, - indices.Length, - starts, - indices, - values); - } + public HighsStatus changeColsBoundsByMask(bool[] mask, double[] lower, double[] upper) + { + return (HighsStatus)HighsLpSolver.Highs_changeColsBoundsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray(), lower, upper); + } - public HighsStatus changeObjectiveSense(HighsObjectiveSense sense) - { - return (HighsStatus)HighsLpSolver.Highs_changeObjectiveSense(this.highs, (int)sense); - } + public HighsStatus changeRowBounds(int row, double lower, double upper) + { + return (HighsStatus)HighsLpSolver.Highs_changeRowBounds(this.highs, row, lower, upper); + } - public HighsStatus changeColCost(int col, double cost) - { - return (HighsStatus)HighsLpSolver.Highs_changeColCost(this.highs, col, cost); - } + public HighsStatus changeRowsBoundsByRange(int from, int to, double[] lower, double[] upper) + { + return (HighsStatus)HighsLpSolver.Highs_changeRowsBoundsByRange(this.highs, from, to, lower, upper); + } - public HighsStatus changeColsCostBySet(int[] cols, double[] costs) - { - return (HighsStatus)HighsLpSolver.Highs_changeColsCostBySet(this.highs, cols.Length, cols, costs); - } + public HighsStatus changeRowsBoundsBySet(int[] rows, double[] lower, double[] upper) + { + return (HighsStatus)HighsLpSolver.Highs_changeRowsBoundsBySet(this.highs, rows.Length, rows, lower, upper); + } - public HighsStatus changeColsCostByMask(bool[] mask, double[] cost) - { - return (HighsStatus)HighsLpSolver.Highs_changeColsCostByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray(), cost); - } + public HighsStatus changeRowsBoundsByMask(bool[] mask, double[] lower, double[] upper) + { + return (HighsStatus)HighsLpSolver.Highs_changeRowsBoundsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray(), lower, upper); + } - public HighsStatus changeColBounds(int col, double lower, double upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeColBounds(this.highs, col, lower, upper); - } + public HighsStatus changeColsIntegralityByRange(int from_col, int to_col, HighsIntegrality[] integrality) + { + return (HighsStatus)HighsLpSolver.Highs_changeColsIntegralityByRange(this.highs, from_col, to_col, Array.ConvertAll(integrality, item => (int)item)); + } - public HighsStatus changeColsBoundsByRange(int from, int to, double[] lower, double[] upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeColsBoundsByRange(this.highs, from, to, lower, upper); - } + public HighsStatus changeCoeff(int row, int col, double value) + { + return (HighsStatus)HighsLpSolver.Highs_changeCoeff(this.highs, row, col, value); + } - public HighsStatus changeColsBoundsBySet(int[] cols, double[] lower, double[] upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeColsBoundsBySet(this.highs, cols.Length, cols, lower, upper); - } + public HighsStatus deleteColsByRange(int from, int to) + { + return (HighsStatus)HighsLpSolver.Highs_deleteColsByRange(this.highs, from, to); + } - public HighsStatus changeColsBoundsByMask(bool[] mask, double[] lower, double[] upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeColsBoundsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray(), lower, upper); - } + public HighsStatus deleteColsBySet(int[] cols) + { + return (HighsStatus)HighsLpSolver.Highs_deleteColsBySet(this.highs, cols.Length, cols); + } - public HighsStatus changeRowBounds(int row, double lower, double upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeRowBounds(this.highs, row, lower, upper); - } + public HighsStatus deleteColsByMask(bool[] mask) + { + return (HighsStatus)HighsLpSolver.Highs_deleteColsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray()); + } - public HighsStatus changeRowsBoundsBySet(int[] rows, double[] lower, double[] upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeRowsBoundsBySet(this.highs, rows.Length, rows, lower, upper); - } + public HighsStatus deleteRowsByRange(int from, int to) + { + return (HighsStatus)HighsLpSolver.Highs_deleteRowsByRange(this.highs, from, to); + } - public HighsStatus changeRowsBoundsByMask(bool[] mask, double[] lower, double[] upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeRowsBoundsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray(), lower, upper); - } + public HighsStatus deleteRowsBySet(int[] rows) + { + return (HighsStatus)HighsLpSolver.Highs_deleteRowsBySet(this.highs, rows.Length, rows); + } - public HighsStatus changeColsIntegralityByRange(int from_col, int to_col, HighsIntegrality[] integrality) - { - return (HighsStatus)HighsLpSolver.Highs_changeColsIntegralityByRange(this.highs, from_col, to_col, Array.ConvertAll(integrality, item => (int)item)); - } + public HighsStatus deleteRowsByMask(bool[] mask) + { + return (HighsStatus)HighsLpSolver.Highs_deleteRowsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray()); + } - public HighsStatus changeCoeff(int row, int col, double value) - { - return (HighsStatus)HighsLpSolver.Highs_changeCoeff(this.highs, row, col, value); - } + delegate int HighsGetInfoDelegate(IntPtr highs, string infoName, out TValue output); - public HighsStatus deleteColsByRange(int from, int to) + private TValue GetValueOrFallback(HighsGetInfoDelegate highsGetInfoDelegate, string infoName, TValue fallback) + { + try { - return (HighsStatus)HighsLpSolver.Highs_deleteColsByRange(this.highs, from, to); - } + var status = (HighsStatus)highsGetInfoDelegate(this.highs, infoName, out var value); + if (status != HighsStatus.kOk) + { + return fallback; + } - public HighsStatus deleteColsBySet(int[] cols) - { - return (HighsStatus)HighsLpSolver.Highs_deleteColsBySet(this.highs, cols.Length, cols); + return value; } - - public HighsStatus deleteColsByMask(bool[] mask) + catch { - return (HighsStatus)HighsLpSolver.Highs_deleteColsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray()); + return fallback; } + } - public HighsStatus deleteRowsByRange(int from, int to) - { - return (HighsStatus)HighsLpSolver.Highs_deleteRowsByRange(this.highs, from, to); - } + /// + /// Gets the current solution info. + /// + /// The . + public SolutionInfo getInfo() + { + // TODO: This object does not contian the "complete" info from the C api. Add further props, if you need them. + var info = new SolutionInfo() + { + MipGap = this.GetValueOrFallback(HighsLpSolver.Highs_getDoubleInfoValue, "mip_gap", double.NaN), + DualBound = this.GetValueOrFallback(HighsLpSolver.Highs_getDoubleInfoValue, "mip_dual_bound", double.NaN), + ObjectiveValue = this.GetValueOrFallback(HighsLpSolver.Highs_getDoubleInfoValue, "objective_function_value", double.NaN), + NodeCount = this.GetValueOrFallback(HighsLpSolver.Highs_getInt64InfoValue, "mip_node_count", 0L), + IpmIterationCount = this.GetValueOrFallback(HighsLpSolver.Highs_getIntInfoValue, "ipm_iteration_count", 0), + SimplexIterationCount = this.GetValueOrFallback(HighsLpSolver.Highs_getIntInfoValue, "simplex_iteration_count", 0), + PdlpIterationCount = this.GetValueOrFallback(HighsLpSolver.Highs_getIntInfoValue, "pdlp_iteration_count", 0), + }; + return info; + } - public HighsStatus deleteRowsBySet(int[] rows) - { - return (HighsStatus)HighsLpSolver.Highs_deleteRowsBySet(this.highs, rows.Length, rows); - } + public HighsStatus setSolution(HighsSolution solution) + { + return (HighsStatus)HighsLpSolver.Highs_setSolution(this.highs, solution.colvalue, solution.rowvalue, solution.coldual, solution.rowdual); + } - public HighsStatus deleteRowsByMask(bool[] mask) - { - return (HighsStatus)HighsLpSolver.Highs_deleteRowsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray()); - } + /// Set a partial primal solution by passing values for a set of variables + /// A dictionary that maps variable indices to variable values + /// The sparse solution set by this function has values for a subset of the model's variables. + /// For each entry in , the key identifies a variable by index, and + /// the value indicates the variable's value in the sparse solution. + /// A constant indicating whether the call succeeded + public HighsStatus setSparseSolution(IReadOnlyDictionary valuesByIndex) + { + return (HighsStatus)Highs_setSparseSolution(this.highs, valuesByIndex.Count, valuesByIndex.Keys.ToArray(), valuesByIndex.Values.ToArray()); + } - delegate int HighsGetInfoDelegate(IntPtr highs, string infoName, out TValue output); + public HighsStatus getBasicVariables(ref int[] basic_variables) + { + return (HighsStatus)Highs_getBasicVariables(this.highs, basic_variables); + } - private TValue GetValueOrFallback(HighsGetInfoDelegate highsGetInfoDelegate, string infoName, TValue fallback) - { - try - { - var status = (HighsStatus)highsGetInfoDelegate(this.highs, infoName, out var value); - if (status != HighsStatus.kOk) - { - return fallback; - } + public HighsStatus getBasisInverseRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) + { + return (HighsStatus)Highs_getBasisInverseRow(this.highs, row, row_vector, ref row_num_nz, row_indices); + } - return value; - } - catch - { - return fallback; - } - } + public HighsStatus getBasisInverseCol(int col, double[] col_vector, ref int col_num_nz, int[] col_indices) + { + return (HighsStatus)Highs_getBasisInverseCol(this.highs, col, col_vector, ref col_num_nz, col_indices); + } - /// - /// Gets the current solution info. - /// - /// The . - public SolutionInfo getInfo() - { - // TODO: This object does not contian the "complete" info from the C api. Add further props, if you need them. - var info = new SolutionInfo() - { - MipGap = this.GetValueOrFallback(HighsLpSolver.Highs_getDoubleInfoValue, "mip_gap", double.NaN), - DualBound = this.GetValueOrFallback(HighsLpSolver.Highs_getDoubleInfoValue, "mip_dual_bound", double.NaN), - ObjectiveValue = this.GetValueOrFallback(HighsLpSolver.Highs_getDoubleInfoValue, "objective_function_value", double.NaN), - NodeCount = this.GetValueOrFallback(HighsLpSolver.Highs_getInt64InfoValue, "mip_node_count", 0L), - IpmIterationCount = this.GetValueOrFallback(HighsLpSolver.Highs_getIntInfoValue, "ipm_iteration_count", 0), - SimplexIterationCount = this.GetValueOrFallback(HighsLpSolver.Highs_getIntInfoValue, "simplex_iteration_count", 0), - PdlpIterationCount = this.GetValueOrFallback(HighsLpSolver.Highs_getIntInfoValue, "pdlp_iteration_count", 0), - }; - return info; - } + public HighsStatus getBasisSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) + { + return (HighsStatus)Highs_getBasisSolve(this.highs, rhs, solution_vector, ref solution_num_nz, solution_indices); + } - public HighsStatus setSolution(HighsSolution solution) - { - return (HighsStatus)HighsLpSolver.Highs_setSolution(this.highs, solution.colvalue, solution.coldual, solution.rowvalue, solution.rowdual); - } + public HighsStatus getBasisTransposeSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) + { + return (HighsStatus)Highs_getBasisTransposeSolve(this.highs, rhs, solution_vector, ref solution_num_nz, solution_indices); + } - public HighsStatus getBasicVariables(ref int[] basic_variables) - { - return (HighsStatus)Highs_getBasicVariables(this.highs, basic_variables); - } + public HighsStatus getReducedRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) + { + return (HighsStatus)Highs_getReducedRow(this.highs, row, row_vector, ref row_num_nz, row_indices); + } - public HighsStatus getBasisInverseRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) - { - return (HighsStatus)Highs_getBasisInverseRow(this.highs, row, row_vector, ref row_num_nz, row_indices); - } + public HighsStatus getReducedColumn(int col, double[] col_vector, ref int col_num_nz, int[] col_indices) + { + return (HighsStatus)Highs_getReducedColumn(this.highs, col, col_vector, ref col_num_nz, col_indices); + } - public HighsStatus getBasisInverseCol(int col, double[] col_vector, ref int col_num_nz, int[] col_indices) - { - return (HighsStatus)Highs_getBasisInverseCol(this.highs, col, col_vector, ref col_num_nz, col_indices); - } + public HighsStatus clearModel() + { + return (HighsStatus)Highs_clearModel(this.highs); + } - public HighsStatus getBasisSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) - { - return (HighsStatus)Highs_getBasisSolve(this.highs, rhs, solution_vector, ref solution_num_nz, solution_indices); - } + public HighsStatus clearSolver() + { + return (HighsStatus)Highs_clearSolver(this.highs); + } - public HighsStatus getBasisTransposeSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) - { - return (HighsStatus)Highs_getBasisTransposeSolve(this.highs, rhs, solution_vector, ref solution_num_nz, solution_indices); - } + public HighsStatus passColName(int col, string name) + { + return (HighsStatus)Highs_passColName(this.highs, col, name); + } - public HighsStatus getReducedRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) - { - return (HighsStatus)Highs_getReducedRow(this.highs, row, row_vector, ref row_num_nz, row_indices); - } + public HighsStatus passRowName(int row, string name) + { + return (HighsStatus)Highs_passRowName(this.highs, row, name); + } - public HighsStatus getReducedColumn(int col, double[] col_vector, ref int col_num_nz, int[] col_indices) - { - return (HighsStatus)Highs_getReducedColumn(this.highs, col, col_vector, ref col_num_nz, col_indices); - } + public HighsStatus writeOptions(string filename) + { + return (HighsStatus)Highs_writeOptions(this.highs, filename); + } - public HighsStatus clearModel() - { - return (HighsStatus)Highs_clearModel(this.highs); - } + public HighsStatus writeOptionsDeviations(string filename) + { + return (HighsStatus)Highs_writeOptionsDeviations(this.highs, filename); + } +} - public HighsStatus clearSolver() - { - return (HighsStatus)Highs_clearSolver(this.highs); - } +/// +/// The solution info. +/// +public class SolutionInfo +{ + /// + /// Gets or sets the simplex iteration count. + /// + public int SimplexIterationCount { get; set; } - public HighsStatus passColName(int col, string name) - { - return (HighsStatus)Highs_passColName(this.highs, col, name); - } + /// + /// Gets or sets the Interior Point Method (IPM) iteration count. + /// + public int IpmIterationCount { get; set; } - public HighsStatus passRowName(int row, string name) - { - return (HighsStatus)Highs_passRowName(this.highs, row, name); - } + /// + /// Gets or sets the PDLP iteration count. + /// + public int PdlpIterationCount { get; set; } - public HighsStatus writeOptions(string filename) - { - return (HighsStatus)Highs_writeOptions(this.highs, filename); - } + /// + /// Gets or sets the MIP gap. + /// + public double MipGap { get; set; } - public HighsStatus writeOptionsDeviations(string filename) - { - return (HighsStatus)Highs_writeOptionsDeviations(this.highs, filename); - } - } + /// + /// Gets or sets the best dual bound. + /// + public double DualBound { get; set; } /// - /// The solution info. + /// Gets or sets the MIP node count. /// - public class SolutionInfo - { - /// - /// Gets or sets the simplex iteration count. - /// - public int SimplexIterationCount { get; set; } - - /// - /// Gets or sets the Interior Point Method (IPM) iteration count. - /// - public int IpmIterationCount { get; set; } - - /// - /// Gets or sets the PDLP iteration count. - /// - public int PdlpIterationCount { get; set; } - - /// - /// Gets or sets the MIP gap. - /// - public double MipGap { get; set; } - - /// - /// Gets or sets the best dual bound. - /// - public double DualBound { get; set; } - - /// - /// Gets or sets the MIP node count. - /// - public long NodeCount { get; set; } - - /// - /// Gets or sets the objective value. - /// - public double ObjectiveValue { get; set; } - } + public long NodeCount { get; set; } + + /// + /// Gets or sets the objective value. + /// + public double ObjectiveValue { get; set; } } +} \ No newline at end of file From 92fc6beb3649e2858f66673450fd2cc5850a25b7 Mon Sep 17 00:00:00 2001 From: Thiago Novaes <77932135+Thiago-NovaesB@users.noreply.github.com> Date: Fri, 26 Dec 2025 15:29:32 +0100 Subject: [PATCH 3/9] delete --- src/interfaces/Highs/Class1.cs | 7 + .../HighsInterface => Highs}/Highs.csproj | 0 .../{HighsInterface => Highs}/Highs.sln | 12 +- .../HighsInterface/Enums/BasisStatus.cs | 28 - .../HighsInterface/Enums/MatrixFormat.cs | 16 - .../HighsInterface/Enums/ModelStatus.cs | 75 -- .../HighsInterface/Enums/ObjectiveSense.cs | 16 - .../HighsInterface/Enums/Status.cs | 20 - .../HighsInterface/Enums/VariableType.cs | 28 - .../HighsInterface/Records/BasisInfo.cs | 21 - .../HighsInterface/Records/Model.cs | 57 -- .../HighsInterface/Records/Solution.cs | 26 - .../HighsInterface/Records/SolutionInfo.cs | 21 - .../HighsInterface/HighsInterface/Solver.cs | 920 ------------------ 14 files changed, 13 insertions(+), 1234 deletions(-) create mode 100644 src/interfaces/Highs/Class1.cs rename src/interfaces/{HighsInterface/HighsInterface => Highs}/Highs.csproj (100%) rename src/interfaces/{HighsInterface => Highs}/Highs.sln (58%) delete mode 100644 src/interfaces/HighsInterface/HighsInterface/Enums/BasisStatus.cs delete mode 100644 src/interfaces/HighsInterface/HighsInterface/Enums/MatrixFormat.cs delete mode 100644 src/interfaces/HighsInterface/HighsInterface/Enums/ModelStatus.cs delete mode 100644 src/interfaces/HighsInterface/HighsInterface/Enums/ObjectiveSense.cs delete mode 100644 src/interfaces/HighsInterface/HighsInterface/Enums/Status.cs delete mode 100644 src/interfaces/HighsInterface/HighsInterface/Enums/VariableType.cs delete mode 100644 src/interfaces/HighsInterface/HighsInterface/Records/BasisInfo.cs delete mode 100644 src/interfaces/HighsInterface/HighsInterface/Records/Model.cs delete mode 100644 src/interfaces/HighsInterface/HighsInterface/Records/Solution.cs delete mode 100644 src/interfaces/HighsInterface/HighsInterface/Records/SolutionInfo.cs delete mode 100644 src/interfaces/HighsInterface/HighsInterface/Solver.cs diff --git a/src/interfaces/Highs/Class1.cs b/src/interfaces/Highs/Class1.cs new file mode 100644 index 0000000000..a7a9f2c69c --- /dev/null +++ b/src/interfaces/Highs/Class1.cs @@ -0,0 +1,7 @@ +namespace Highs +{ + public class Class1 + { + + } +} diff --git a/src/interfaces/HighsInterface/HighsInterface/Highs.csproj b/src/interfaces/Highs/Highs.csproj similarity index 100% rename from src/interfaces/HighsInterface/HighsInterface/Highs.csproj rename to src/interfaces/Highs/Highs.csproj diff --git a/src/interfaces/HighsInterface/Highs.sln b/src/interfaces/Highs/Highs.sln similarity index 58% rename from src/interfaces/HighsInterface/Highs.sln rename to src/interfaces/Highs/Highs.sln index 73a7d438d9..bb1b0b4091 100644 --- a/src/interfaces/HighsInterface/Highs.sln +++ b/src/interfaces/Highs/Highs.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.14.36811.4 d17.14 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HighsInterface", "HighsInterface\HighsInterface.csproj", "{4177FE0E-0692-4E0F-987A-3C6DDC739E31}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Highs", "Highs.csproj", "{97E314AD-8C55-4E72-9FFA-6BA9EA34F55E}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -11,15 +11,15 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {4177FE0E-0692-4E0F-987A-3C6DDC739E31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {4177FE0E-0692-4E0F-987A-3C6DDC739E31}.Debug|Any CPU.Build.0 = Debug|Any CPU - {4177FE0E-0692-4E0F-987A-3C6DDC739E31}.Release|Any CPU.ActiveCfg = Release|Any CPU - {4177FE0E-0692-4E0F-987A-3C6DDC739E31}.Release|Any CPU.Build.0 = Release|Any CPU + {97E314AD-8C55-4E72-9FFA-6BA9EA34F55E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {97E314AD-8C55-4E72-9FFA-6BA9EA34F55E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {97E314AD-8C55-4E72-9FFA-6BA9EA34F55E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {97E314AD-8C55-4E72-9FFA-6BA9EA34F55E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {A40CDFF4-67AB-46EA-BC93-0D261FE12DA2} + SolutionGuid = {AEB74AA2-A995-4A88-BCB4-32F2033C8C90} EndGlobalSection EndGlobal diff --git a/src/interfaces/HighsInterface/HighsInterface/Enums/BasisStatus.cs b/src/interfaces/HighsInterface/HighsInterface/Enums/BasisStatus.cs deleted file mode 100644 index cbb2b5db6d..0000000000 --- a/src/interfaces/HighsInterface/HighsInterface/Enums/BasisStatus.cs +++ /dev/null @@ -1,28 +0,0 @@ -namespace Highs.Enums; - -/// -/// This defines the status of a variable (or slack variable for a constraint) in a basis -/// -public enum BasisStatus -{ - /// - /// The variable is nonbasic at its lower bound (or fixed value) - /// - Lower = 0, - /// - /// The variable is basic - /// - Basic, - /// - /// he variable is at its upper bound - /// - Upper, - /// - /// A free variable is nonbasic and set to zero - /// - Zero, - /// - /// The variable is nonbasic - /// - Nonbasic -} diff --git a/src/interfaces/HighsInterface/HighsInterface/Enums/MatrixFormat.cs b/src/interfaces/HighsInterface/HighsInterface/Enums/MatrixFormat.cs deleted file mode 100644 index 77f5bdb15a..0000000000 --- a/src/interfaces/HighsInterface/HighsInterface/Enums/MatrixFormat.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace Highs.Enums; - -/// -/// This defines the format of a HighsSparseMatrix -/// -public enum MatrixFormat -{ - /// - /// The matrix is stored column-wise - /// - ColumnWise = 1, - /// - /// The matrix is stored row-wise - /// - RowWise -} diff --git a/src/interfaces/HighsInterface/HighsInterface/Enums/ModelStatus.cs b/src/interfaces/HighsInterface/HighsInterface/Enums/ModelStatus.cs deleted file mode 100644 index 29736814ba..0000000000 --- a/src/interfaces/HighsInterface/HighsInterface/Enums/ModelStatus.cs +++ /dev/null @@ -1,75 +0,0 @@ -namespace Highs.Enums; - -/// -/// This defines the status of the model after a call to run -/// -public enum ModelStatus -{ - /// - /// The model status has not been set - /// - Notset = 0, - LoadError, - /// - /// There is an error in the model - /// - ModelError, - PresolveError, - /// - /// There has been an error when solving the model - /// - SolveError, - PostsolveError, - /// - /// The model is empty - /// - ModelEmpty, - /// - /// The model has been solved to optimality - /// - Optimal, - /// - /// The model is infeasible - /// - Infeasible, - /// - /// The model is unbounded or infeasible - /// - UnboundedOrInfeasible, - /// - /// The model is unbounded - /// - Unbounded, - /// - /// The bound on the model objective value has been reached - /// - ObjectiveBound, - /// - /// The target value for the model objective has been reached - /// - ObjectiveTarget, - /// - /// The run time limit has been reached - /// - TimeLimit, - /// - /// The iteration limit has been reached - /// - IterationLimit, - /// - /// The model status is unknown - /// - Unknown, - /// - /// The MIP solver has reached the limit on the number of LPs solved - /// - SolutionLimit, - /// - /// The solver has been interrupted by the user - /// - Interrupt, - /// - /// The solver has been unable to allocate sufficient memory - /// - MemoryLimit -} diff --git a/src/interfaces/HighsInterface/HighsInterface/Enums/ObjectiveSense.cs b/src/interfaces/HighsInterface/HighsInterface/Enums/ObjectiveSense.cs deleted file mode 100644 index 155741d4a3..0000000000 --- a/src/interfaces/HighsInterface/HighsInterface/Enums/ObjectiveSense.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace Highs.Enums; - -/// -/// This defines optimization sense of a HighsLp -/// -public enum ObjectiveSense -{ - /// - /// The objective is to be minimized - /// - Minimize = 1, - /// - /// The objective is to be maximized - /// - Maximize = -1 -} diff --git a/src/interfaces/HighsInterface/HighsInterface/Enums/Status.cs b/src/interfaces/HighsInterface/HighsInterface/Enums/Status.cs deleted file mode 100644 index 001f9a5de2..0000000000 --- a/src/interfaces/HighsInterface/HighsInterface/Enums/Status.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace Highs.Enums; - -/// -/// This is (part of) the return value of most HiGHS methods -/// -public enum Status -{ - /// - /// The method has exposed an error - /// - Error = -1, - /// - /// The method has completed successfully - /// - Ok, - /// - /// The method has recovered from an unusual event, or has terminated due to reaching a time or iteration limit - /// - Warning -} diff --git a/src/interfaces/HighsInterface/HighsInterface/Enums/VariableType.cs b/src/interfaces/HighsInterface/HighsInterface/Enums/VariableType.cs deleted file mode 100644 index e22f4610a5..0000000000 --- a/src/interfaces/HighsInterface/HighsInterface/Enums/VariableType.cs +++ /dev/null @@ -1,28 +0,0 @@ -namespace Highs.Enums; - -/// -/// This defines the feasible values of a variable within a model -/// -public enum VariableType -{ - /// - /// The variable can take continuous values between its bounds - /// - Continuous = 0, - /// - /// The variable must take integer values between its bounds - /// - Integer, - /// - /// The variable must be zero or take continuous values between its bounds - /// - SemiContinuous, - /// - /// The variable must be zero or take integer values between its bounds - /// - SemiInteger, - /// - /// The variable must take implicit integer values between its bounds - /// - ImplicitInteger, -} \ No newline at end of file diff --git a/src/interfaces/HighsInterface/HighsInterface/Records/BasisInfo.cs b/src/interfaces/HighsInterface/HighsInterface/Records/BasisInfo.cs deleted file mode 100644 index b6114a8aa3..0000000000 --- a/src/interfaces/HighsInterface/HighsInterface/Records/BasisInfo.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Highs.Enums; - -namespace Highs.Records; - -/// -/// This defines the basis status of the columns and rows in a basis -/// -/// The column basis status -/// The row basis status -public record BasisInfo(BasisStatus[] ColumnBasisStatus, BasisStatus[] RowBasisStatus) -{ - /// - /// The default constructor creates empty arrays - /// - /// The number of columns - /// The number of rows - public BasisInfo(int numberOfColumns, int numberOfRows) : this(new BasisStatus[numberOfColumns], - new BasisStatus[numberOfRows]) - { - } -} diff --git a/src/interfaces/HighsInterface/HighsInterface/Records/Model.cs b/src/interfaces/HighsInterface/HighsInterface/Records/Model.cs deleted file mode 100644 index 2b6d672f8a..0000000000 --- a/src/interfaces/HighsInterface/HighsInterface/Records/Model.cs +++ /dev/null @@ -1,57 +0,0 @@ -using Highs.Enums; - -namespace Highs; -/// -/// This defines a model for Highs -/// -public record Model -{ - /// - /// The objective sense - /// - public ObjectiveSense ObjectiveSense; - /// - /// The objective constant - /// - public double Offset; - /// - /// The column costs - /// - public double[] ColumnCost = []; - /// - /// The column lower bounds - /// - public double[] ColumnLower = []; - /// - /// The column upper bounds - /// - public double[] ColumnUpper = []; - /// - /// The row lower bounds - /// - public double[] RowLower = []; - /// - /// The row upper bounds - /// - public double[] RowUpper = []; - /// - /// The format of the constraint matrix. - /// - public MatrixFormat MatrixFormat; - /// - /// The starting index of each column (or row) in MatrixIndices. - /// - public int[] MatrixStart = []; - /// - /// The indices of matrix entries - /// - public int[] MatrixIndices = []; - /// - /// The values of matrix entries - /// - public double[] MatrixValues = []; - /// - /// The integrality of the variables - /// - public VariableType[] VariableTypes = []; -} diff --git a/src/interfaces/HighsInterface/HighsInterface/Records/Solution.cs b/src/interfaces/HighsInterface/HighsInterface/Records/Solution.cs deleted file mode 100644 index a1fb0143d1..0000000000 --- a/src/interfaces/HighsInterface/HighsInterface/Records/Solution.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace Highs; - -/// -/// The solution. -/// -/// The column value. -/// The column dual. -/// The row value. -/// The row dual. -public record Solution(double[] ColumnValue, - double[] ColumnDual, - double[] RowValue, - double[] RowDual) -{ - /// - /// The default constructor creates empty arrays - /// - /// The number of columns - /// The number of rows - public Solution(int numberOfColumns, int numberOfRows) : this(new double[numberOfColumns], - new double[numberOfColumns], - new double[numberOfRows], - new double[numberOfRows]) - { - } -} diff --git a/src/interfaces/HighsInterface/HighsInterface/Records/SolutionInfo.cs b/src/interfaces/HighsInterface/HighsInterface/Records/SolutionInfo.cs deleted file mode 100644 index 21996a2551..0000000000 --- a/src/interfaces/HighsInterface/HighsInterface/Records/SolutionInfo.cs +++ /dev/null @@ -1,21 +0,0 @@ -namespace Highs; - -/// -/// The solution info. -/// -/// The simplex iteration count. -/// The Interior Point Method (IPM) iteration count. -/// The PDLP iteration count. -/// The MIP gap. -/// The best dual bound. -/// The MIP node count. -/// The objective value. -public record SolutionInfo(int SimplexIterationCount, - int IpmIterationCount, - int PdlpIterationCount, - double MipGap, - double DualBound, - long NodeCount, - double ObjectiveValue) -{ -} diff --git a/src/interfaces/HighsInterface/HighsInterface/Solver.cs b/src/interfaces/HighsInterface/HighsInterface/Solver.cs deleted file mode 100644 index 30b936df30..0000000000 --- a/src/interfaces/HighsInterface/HighsInterface/Solver.cs +++ /dev/null @@ -1,920 +0,0 @@ -using System.Runtime.InteropServices; -using System.Text; -using Highs.Enums; -using Highs.Records; - -namespace Highs; - -/// -/// The Highs Solver interface. -/// -public class Solver : IDisposable -{ - /// - /// The pointer to the _highs instance. - /// - private readonly IntPtr _highs; - - /// - /// Indicates whether the instance has been disposed. - /// - private bool _disposed; - - /// - /// The name of the Highs library. - /// - private const string HighsLibName = "_highs"; - - #region Library Imports - [LibraryImport(HighsLibName)] - private static extern int Highs_call( - int numcol, - int numrow, - int numnz, - double[] colcost, - double[] collower, - double[] colupper, - double[] rowlower, - double[] rowupper, - int[] astart, - int[] aindex, - double[] avalue, - double[] colvalue, - double[] coldual, - double[] rowvalue, - double[] rowdual, - int[] colbasisstatus, - int[] rowbasisstatus, - ref int modelstatus); - - [LibraryImport(HighsLibName)] - private static extern IntPtr Highs_create(); - - [LibraryImport(HighsLibName)] - private static extern void Highs_destroy(IntPtr _highs); - - [LibraryImport(HighsLibName)] - private static extern int Highs_run(IntPtr _highs); - - [LibraryImport(HighsLibName)] - private static extern int Highs_readModel(IntPtr _highs, string filename); - - [LibraryImport(HighsLibName)] - private static extern int Highs_writeModel(IntPtr _highs, string filename); - - [LibraryImport(HighsLibName)] - private static extern int Highs_writePresolvedModel(IntPtr _highs, string filename); - - [LibraryImport(HighsLibName)] - private static extern int Highs_writeSolutionPretty(IntPtr _highs, string filename); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getInfinity(IntPtr _highs); - - [LibraryImport(HighsLibName)] - private static extern int Highs_passLp( - IntPtr _highs, - int numcol, - int numrow, - int numnz, - int aformat, - int sense, - double offset, - double[] colcost, - double[] collower, - double[] colupper, - double[] rowlower, - double[] rowupper, - int[] astart, - int[] aindex, - double[] avalue); - - [LibraryImport(HighsLibName)] - private static extern int Highs_passMip( - IntPtr _highs, - int numcol, - int numrow, - int numnz, - int aformat, - int sense, - double offset, - double[] colcost, - double[] collower, - double[] colupper, - double[] rowlower, - double[] rowupper, - int[] astart, - int[] aindex, - double[] avalue, - int[] highs_integrality); - - [LibraryImport(HighsLibName)] - private static extern int Highs_setOptionValue(IntPtr _highs, string option, string value); - - [LibraryImport(HighsLibName)] - private static extern int Highs_setBoolOptionValue(IntPtr _highs, string option, int value); - - [LibraryImport(HighsLibName)] - private static extern int Highs_setIntOptionValue(IntPtr _highs, string option, int value); - - [LibraryImport(HighsLibName)] - private static extern int Highs_setDoubleOptionValue(IntPtr _highs, string option, double value); - - [LibraryImport(HighsLibName)] - private static extern int Highs_setStringOptionValue(IntPtr _highs, string option, string value); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getBoolOptionValue(IntPtr _highs, string option, out int value); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getIntOptionValue(IntPtr _highs, string option, out int value); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getDoubleOptionValue(IntPtr _highs, string option, out double value); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getStringOptionValue(IntPtr _highs, string option, [Out] StringBuilder value); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getSolution(IntPtr _highs, double[] colvalue, double[] coldual, double[] rowvalue, double[] rowdual); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getNumCol(IntPtr _highs); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getNumRow(IntPtr _highs); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getNumNz(IntPtr _highs); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getBasis(IntPtr _highs, int[] colstatus, int[] rowstatus); - - [LibraryImport(HighsLibName)] - private static extern double Highs_getObjectiveValue(IntPtr _highs); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getIterationCount(IntPtr _highs); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getModelStatus(IntPtr _highs); - - [LibraryImport(HighsLibName)] - private static extern int Highs_addRow(IntPtr _highs, double lower, double upper, int num_new_nz, int[] indices, double[] values); - - [LibraryImport(HighsLibName)] - private static extern int Highs_addRows( - IntPtr _highs, - int num_new_row, - double[] lower, - double[] upper, - int num_new_nz, - int[] starts, - int[] indices, - double[] values); - - [LibraryImport(HighsLibName)] - private static extern int Highs_addCol( - IntPtr _highs, - double cost, - double lower, - double upper, - int num_new_nz, - int[] indices, - double[] values); - - [LibraryImport(HighsLibName)] - private static extern int Highs_addCols( - IntPtr _highs, - int num_new_col, - double[] costs, - double[] lower, - double[] upper, - int num_new_nz, - int[] starts, - int[] indices, - double[] values); - - [LibraryImport(HighsLibName)] - private static extern int Highs_changeObjectiveSense(IntPtr _highs, int sense); - - [LibraryImport(HighsLibName)] - private static extern int Highs_changeColCost(IntPtr _highs, int col, double cost); - - [LibraryImport(HighsLibName)] - private static extern int Highs_changeColsCostBySet(IntPtr _highs, int num_set_entries, int[] set, double[] cost); - - [LibraryImport(HighsLibName)] - private static extern int Highs_changeColsCostByMask(IntPtr _highs, int[] mask, double[] cost); - - [LibraryImport(HighsLibName)] - private static extern int Highs_changeColBounds(IntPtr _highs, int col, double lower, double upper); - - [LibraryImport(HighsLibName)] - private static extern int Highs_changeColsBoundsByRange(IntPtr _highs, int from_col, int to_col, double[] lower, double[] upper); - - [LibraryImport(HighsLibName)] - private static extern int Highs_changeColsBoundsBySet(IntPtr _highs, int num_set_entries, int[] set, double[] lower, double[] upper); - - [LibraryImport(HighsLibName)] - private static extern int Highs_changeColsBoundsByMask(IntPtr _highs, int[] mask, double[] lower, double[] upper); - - [LibraryImport(HighsLibName)] - private static extern int Highs_changeRowBounds(IntPtr _highs, int row, double lower, double upper); - - [LibraryImport(HighsLibName)] - private static extern int Highs_changeRowsBoundsBySet(IntPtr _highs, int num_set_entries, int[] set, double[] lower, double[] upper); - - [LibraryImport(HighsLibName)] - private static extern int Highs_changeRowsBoundsByMask(IntPtr _highs, int[] mask, double[] lower, double[] upper); - - [LibraryImport(HighsLibName)] - private static extern int Highs_changeColsIntegralityByRange(IntPtr _highs, int from_col, int to_col, int[] integrality); - - [LibraryImport(HighsLibName)] - private static extern int Highs_changeCoeff(IntPtr _highs, int row, int col, double value); - - [LibraryImport(HighsLibName)] - private static extern int Highs_deleteColsByRange(IntPtr _highs, int from_col, int to_col); - - [LibraryImport(HighsLibName)] - private static extern int Highs_deleteColsBySet(IntPtr _highs, int num_set_entries, int[] set); - - [LibraryImport(HighsLibName)] - private static extern int Highs_deleteColsByMask(IntPtr _highs, int[] mask); - - [LibraryImport(HighsLibName)] - private static extern int Highs_deleteRowsByRange(IntPtr _highs, int from_row, int to_row); - - [LibraryImport(HighsLibName)] - private static extern int Highs_deleteRowsBySet(IntPtr _highs, int num_set_entries, int[] set); - - [LibraryImport(HighsLibName)] - private static extern int Highs_deleteRowsByMask(IntPtr _highs, int[] mask); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getDoubleInfoValue(IntPtr _highs, string info, out double value); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getIntInfoValue(IntPtr _highs, string info, out int value); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getInt64InfoValue(IntPtr _highs, string info, out long value); - - [LibraryImport(HighsLibName)] - private static extern int Highs_setSolution(IntPtr _highs, double[] col_value, double[] row_value, double[] col_dual, double[] row_dual); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getColsByRange( - IntPtr _highs, - int from_col, - int to_col, - ref int num_col, - double[] costs, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getColsBySet( - IntPtr _highs, - int num_set_entries, - int[] set, - ref int num_col, - double[] costs, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getColsByMask( - IntPtr _highs, - int[] mask, - ref int num_col, - double[] costs, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getRowsByRange( - IntPtr _highs, - int from_row, - int to_row, - ref int num_row, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getRowsBySet( - IntPtr _highs, - int num_set_entries, - int[] set, - ref int num_row, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getRowsByMask( - IntPtr _highs, - int[] mask, - ref int num_row, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getBasicVariables(IntPtr _highs, int[] basic_variables); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getBasisInverseRow(IntPtr _highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getBasisInverseCol(IntPtr _highs, int col, double[] col_vector, ref int col_num_nz, int[] col_indices); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getBasisSolve( - IntPtr _highs, - double[] rhs, - double[] solution_vector, - ref int solution_num_nz, - int[] solution_indices); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getBasisTransposeSolve( - IntPtr _highs, - double[] rhs, - double[] solution_vector, - ref int solution_nz, - int[] solution_indices); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getReducedRow(IntPtr _highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); - - [LibraryImport(HighsLibName)] - private static extern int Highs_getReducedColumn(IntPtr _highs, int col, double[] col_vector, ref int col_num_nz, int[] col_indices); - - [LibraryImport(HighsLibName)] - private static extern int Highs_clearModel(IntPtr _highs); - - [LibraryImport(HighsLibName)] - private static extern int Highs_clearSolver(IntPtr _highs); - - [LibraryImport(HighsLibName)] - private static extern int Highs_passColName(IntPtr _highs, int col, string name); - - [LibraryImport(HighsLibName)] - private static extern int Highs_passRowName(IntPtr _highs, int row, string name); - - [LibraryImport(HighsLibName)] - private static extern int Highs_writeOptions(IntPtr _highs, string filename); - - [LibraryImport(HighsLibName)] - private static extern int Highs_writeOptionsDeviations(IntPtr _highs, string filename); - #endregion - - /// - /// Calls the Highs solver in a single call. - /// - /// - /// - /// - /// - /// - public static Status Call(Model model, ref Solution solution, out BasisInfo basisInfo, out ModelStatus modelStatus) - { - var numberOfColumns = model.ColumnCost.Length; - var numberOfRows = model.RowLower.Length; - var numberOfMatrixValues = model.MatrixValues.Length; - - var columnBasisStatus = new int[numberOfColumns]; - var rowBasisStatus = new int[numberOfRows]; - - var modelstate = 0; - - var status = (Status)Highs_call( - numberOfColumns, - numberOfRows, - numberOfMatrixValues, - model.ColumnCost, - model.ColumnLower, - model.ColumnUpper, - model.RowLower, - model.RowUpper, - model.MatrixStart, - model.MatrixIndices, - model.MatrixValues, - solution.ColumnValue, - solution.ColumnDual, - solution.RowValue, - solution.RowDual, - columnBasisStatus, - rowBasisStatus, - ref modelstate); - - modelStatus = (ModelStatus)modelstate; - basisInfo = new([.. columnBasisStatus.Select(x => (BasisStatus)x)], [.. rowBasisStatus.Select(x => (BasisStatus)x)]); - - return status; - } - - /// - /// The default constructor. - /// - public Solver() => _highs = Highs_create(); - - /// - /// The destructor. - /// - ~Solver() => Dispose(false); - - /// - /// Disposes the instance. - /// - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - /// - /// Disposes the instance. - /// - /// - protected virtual void Dispose(bool disposing) - { - if (_disposed) - { - return; - } - Highs_destroy(_highs); - _disposed = true; - } - - /// - /// Runs the solver. - /// - /// - public Status Run() => (Status)Highs_run(_highs); - - /// - /// Reads a model from file. - /// - /// - /// - public Status ReadModel(string filename) => (Status)Highs_readModel(_highs, filename); - - /// - /// Writes the model to file. - /// - /// - /// - public Status WriteModel(string filename) => (Status)Highs_writeModel(_highs, filename); - - /// - /// Writes the presolved model to file. - /// - /// - /// - public Status WritePresolvedModel(string filename) => (Status)Highs_writePresolvedModel(_highs, filename); - - /// - /// Writes the solution to file in a pretty format. - /// - /// - /// - public Status WriteSolutionPretty(string filename) => (Status)Highs_writeSolutionPretty(_highs, filename); - - /// - /// Gets the infinity value. - /// - /// - public double GetInfinity() => Highs_getInfinity(_highs); - - public Status passLp(HighsModel model) - { - return (Status)Highs_passLp( - _highs, - model.colcost.Length, - model.rowlower.Length, - model.avalue.Length, - (int)model.a_format, - (int)model.sense, - model.offset, - model.colcost, - model.collower, - model.colupper, - model.rowlower, - model.rowupper, - model.astart, - model.aindex, - model.avalue); - } - - public Status passMip(HighsModel model) - { - return (Status)Highs_passMip( - _highs, - model.colcost.Length, - model.rowlower.Length, - model.avalue.Length, - (int)model.a_format, - (int)model.sense, - model.offset, - model.colcost, - model.collower, - model.colupper, - model.rowlower, - model.rowupper, - model.astart, - model.aindex, - model.avalue, - model.highs_integrality); - } - - /// - /// Sets the option value. - /// - /// - /// - /// - public Status SetOptionValue(string option, string value) => (Status)Highs_setOptionValue(_highs, option, value); - - /// - /// Sets the string option value. - /// - /// - /// - /// - public Status SetStringOptionValue(string option, string value) => (Status)Highs_setStringOptionValue(_highs, option, value); - - /// - /// Sets the boolean option value. - /// - /// - /// - /// - public Status SetBoolOptionValue(string option, int value) => (Status)Highs_setBoolOptionValue(_highs, option, value); - - /// - /// Sets the double option value. - /// - /// - /// - /// - public Status SetDoubleOptionValue(string option, double value) => (Status)Highs_setDoubleOptionValue(_highs, option, value); - - /// - /// Sets the integer option value. - /// - /// - /// - /// - public Status SetIntOptionValue(string option, int value) => (Status)Highs_setIntOptionValue(_highs, option, value); - - public Status getStringOptionValue(string option, out string value) - { - var stringBuilder = new StringBuilder(); - var result = (Status)Highs_getStringOptionValue(_highs, option, stringBuilder); - value = stringBuilder.ToString(); - return result; - } - - public Status getBoolOptionValue(string option, out int value) - { - return (Status)Highs_getBoolOptionValue(_highs, option, out value); - } - - public Status getDoubleOptionValue(string option, out double value) - { - return (Status)Highs_getDoubleOptionValue(_highs, option, out value); - } - - public Status getIntOptionValue(string option, out int value) - { - return (Status)Highs_getIntOptionValue(_highs, option, out value); - } - - public int getNumCol() - { - return Highs_getNumCol(_highs); - } - - public int getNumRow() - { - return Highs_getNumRow(_highs); - } - - public int getNumNz() - { - return Highs_getNumNz(_highs); - } - - public HighsSolution getSolution() - { - int numberOfColumns = getNumCol(); - int numberOfRows = getNumRow(); - - HighsSolution sol = new HighsSolution(numberOfColumns, numberOfRows); - Highs_getSolution(_highs, sol.colvalue, sol.coldual, sol.rowvalue, sol.rowdual); - - return sol; - } - - public HighsBasis getBasis() - { - int numberOfColumns = getNumCol(); - int numberOfRows = getNumRow(); - - int[] colbasstat = new int[numberOfColumns]; - int[] rowbasstat = new int[numberOfRows]; - - Highs_getBasis(_highs, colbasstat, rowbasstat); - HighsBasis bas = new HighsBasis( - colbasstat.Select(x => (HighsBasisStatus)x).ToArray(), - rowbasstat.Select(x => (HighsBasisStatus)x).ToArray()); - - return bas; - } - - public double getObjectiveValue() - { - return Highs_getObjectiveValue(_highs); - } - - public HighsModelStatus GetModelStatus() - { - return (HighsModelStatus)Highs_getModelStatus(_highs); - } - - public int getIterationCount() - { - return Highs_getIterationCount(_highs); - } - - public Status addRow(double lower, double upper, int[] indices, double[] values) - { - return (Status)Highs_addRow(_highs, lower, upper, indices.Length, indices, values); - } - - public Status addRows(double[] lower, double[] upper, int[] starts, int[] indices, double[] values) - { - return (Status)Highs_addRows(_highs, lower.Length, lower, upper, indices.Length, starts, indices, values); - } - - public Status addCol(double cost, double lower, double upper, int[] indices, double[] values) - { - return (Status)Highs_addCol(_highs, cost, lower, upper, indices.Length, indices, values); - } - - public Status addCols(double[] costs, double[] lower, double[] upper, int[] starts, int[] indices, double[] values) - { - return (Status)Highs_addCols( - _highs, - costs.Length, - costs, - lower, - upper, - indices.Length, - starts, - indices, - values); - } - - public Status changeObjectiveSense(HighsObjectiveSense sense) - { - return (Status)Highs_changeObjectiveSense(_highs, (int)sense); - } - - public Status changeColCost(int col, double cost) - { - return (Status)Highs_changeColCost(_highs, col, cost); - } - - public Status changeColsCostBySet(int[] cols, double[] costs) - { - return (Status)Highs_changeColsCostBySet(_highs, cols.Length, cols, costs); - } - - public Status changeColsCostByMask(bool[] mask, double[] cost) - { - return (Status)Highs_changeColsCostByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray(), cost); - } - - public Status changeColBounds(int col, double lower, double upper) - { - return (Status)Highs_changeColBounds(_highs, col, lower, upper); - } - - public Status changeColsBoundsByRange(int from, int to, double[] lower, double[] upper) - { - return (Status)Highs_changeColsBoundsByRange(_highs, from, to, lower, upper); - } - - public Status changeColsBoundsBySet(int[] cols, double[] lower, double[] upper) - { - return (Status)Highs_changeColsBoundsBySet(_highs, cols.Length, cols, lower, upper); - } - - public Status changeColsBoundsByMask(bool[] mask, double[] lower, double[] upper) - { - return (Status)Highs_changeColsBoundsByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray(), lower, upper); - } - - public Status changeRowBounds(int row, double lower, double upper) - { - return (Status)Highs_changeRowBounds(_highs, row, lower, upper); - } - - public Status changeRowsBoundsBySet(int[] rows, double[] lower, double[] upper) - { - return (Status)Highs_changeRowsBoundsBySet(_highs, rows.Length, rows, lower, upper); - } - - public Status changeRowsBoundsByMask(bool[] mask, double[] lower, double[] upper) - { - return (Status)Highs_changeRowsBoundsByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray(), lower, upper); - } - - public Status changeColsIntegralityByRange(int from_col, int to_col, HighsIntegrality[] integrality) - { - return (Status)Highs_changeColsIntegralityByRange(_highs, from_col, to_col, Array.ConvertAll(integrality, item => (int)item)); - } - - public Status changeCoeff(int row, int col, double value) - { - return (Status)Highs_changeCoeff(_highs, row, col, value); - } - - public Status deleteColsByRange(int from, int to) - { - return (Status)Highs_deleteColsByRange(_highs, from, to); - } - - public Status deleteColsBySet(int[] cols) - { - return (Status)Highs_deleteColsBySet(_highs, cols.Length, cols); - } - - public Status deleteColsByMask(bool[] mask) - { - return (Status)Highs_deleteColsByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray()); - } - - public Status deleteRowsByRange(int from, int to) - { - return (Status)Highs_deleteRowsByRange(_highs, from, to); - } - - public Status deleteRowsBySet(int[] rows) - { - return (Status)Highs_deleteRowsBySet(_highs, rows.Length, rows); - } - - public Status deleteRowsByMask(bool[] mask) - { - return (Status)Highs_deleteRowsByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray()); - } - - delegate int HighsGetInfoDelegate(IntPtr _highs, string infoName, out TValue output); - - private TValue GetValueOrFallback(HighsGetInfoDelegate highsGetInfoDelegate, string infoName, TValue fallback) - { - try - { - var status = (Status)highsGetInfoDelegate(_highs, infoName, out var value); - if (status != Status.kOk) - { - return fallback; - } - - return value; - } - catch - { - return fallback; - } - } - - /// - /// Gets the current solution info. - /// - /// The . - public SolutionInfo getInfo() - { - // TODO: This object does not contian the "complete" info from the C api. Add further props, if you need them. - var info = new SolutionInfo() - { - MipGap = GetValueOrFallback(Highs_getDoubleInfoValue, "mip_gap", double.NaN), - DualBound = GetValueOrFallback(Highs_getDoubleInfoValue, "mip_dual_bound", double.NaN), - ObjectiveValue = GetValueOrFallback(Highs_getDoubleInfoValue, "objective_function_value", double.NaN), - NodeCount = GetValueOrFallback(Highs_getInt64InfoValue, "mip_node_count", 0L), - IpmIterationCount = GetValueOrFallback(Highs_getIntInfoValue, "ipm_iteration_count", 0), - SimplexIterationCount = GetValueOrFallback(Highs_getIntInfoValue, "simplex_iteration_count", 0), - PdlpIterationCount = GetValueOrFallback(Highs_getIntInfoValue, "pdlp_iteration_count", 0), - }; - return info; - } - - public Status setSolution(HighsSolution solution) - { - return (Status)Highs_setSolution(_highs, solution.colvalue, solution.coldual, solution.rowvalue, solution.rowdual); - } - - public Status getBasicVariables(ref int[] basic_variables) - { - return (Status)Highs_getBasicVariables(_highs, basic_variables); - } - - public Status getBasisInverseRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) - { - return (Status)Highs_getBasisInverseRow(_highs, row, row_vector, ref row_num_nz, row_indices); - } - - public Status getBasisInverseCol(int col, double[] col_vector, ref int col_num_nz, int[] col_indices) - { - return (Status)Highs_getBasisInverseCol(_highs, col, col_vector, ref col_num_nz, col_indices); - } - - public Status getBasisSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) - { - return (Status)Highs_getBasisSolve(_highs, rhs, solution_vector, ref solution_num_nz, solution_indices); - } - - public Status getBasisTransposeSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) - { - return (Status)Highs_getBasisTransposeSolve(_highs, rhs, solution_vector, ref solution_num_nz, solution_indices); - } - - public Status getReducedRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) - { - return (Status)Highs_getReducedRow(_highs, row, row_vector, ref row_num_nz, row_indices); - } - - public Status getReducedColumn(int col, double[] col_vector, ref int col_num_nz, int[] col_indices) - { - return (Status)Highs_getReducedColumn(_highs, col, col_vector, ref col_num_nz, col_indices); - } - - /// - /// Clears the model. - /// - /// - public Status ClearModel() => (Status)Highs_clearModel(_highs); - - /// - /// Clears the solver. - /// - /// - public Status ClearSolver() => (Status)Highs_clearSolver(_highs); - - /// - /// Passes the name of a column. - /// - /// - /// - /// - public Status PassColumnName(int col, string name) => (Status)Highs_passColName(_highs, col, name); - - /// - /// Passes the name of a row. - /// - /// - /// - /// - public Status PassRowName(int row, string name) => (Status)Highs_passRowName(_highs, row, name); - - /// - /// Writes the options to file. - /// - /// - /// - public Status WriteOptions(string filename) => (Status)Highs_writeOptions(_highs, filename); - - /// - /// Writes the options deviations to file. - /// - /// - /// - public Status WriteOptionsDeviations(string filename) => (Status)Highs_writeOptionsDeviations(_highs, filename); -} From 8da21c657505563bb2f9ee09f98062def3df7d96 Mon Sep 17 00:00:00 2001 From: Thiago Novaes <77932135+Thiago-NovaesB@users.noreply.github.com> Date: Fri, 26 Dec 2025 15:43:21 +0100 Subject: [PATCH 4/9] testing --- highs/interfaces/Highs/Enums/BasisStatus.cs | 28 + highs/interfaces/Highs/Enums/HessianFormat.cs | 16 + highs/interfaces/Highs/Enums/MatrixFormat.cs | 16 + highs/interfaces/Highs/Enums/ModelStatus.cs | 75 ++ .../interfaces/Highs/Enums/ObjectiveSense.cs | 16 + highs/interfaces/Highs/Enums/Status.cs | 20 + highs/interfaces/Highs/Enums/VariableType.cs | 28 + {src => highs}/interfaces/Highs/Highs.csproj | 0 {src => highs}/interfaces/Highs/Highs.sln | 12 +- highs/interfaces/Highs/Records/BasisInfo.cs | 21 + highs/interfaces/Highs/Records/Hessian.cs | 30 + highs/interfaces/Highs/Records/Model.cs | 58 ++ highs/interfaces/Highs/Records/Solution.cs | 26 + .../interfaces/Highs/Records/SolutionInfo.cs | 21 + highs/interfaces/Highs/Solver.cs | 920 ++++++++++++++++++ src/interfaces/Highs/Class1.cs | 7 - 16 files changed, 1281 insertions(+), 13 deletions(-) create mode 100644 highs/interfaces/Highs/Enums/BasisStatus.cs create mode 100644 highs/interfaces/Highs/Enums/HessianFormat.cs create mode 100644 highs/interfaces/Highs/Enums/MatrixFormat.cs create mode 100644 highs/interfaces/Highs/Enums/ModelStatus.cs create mode 100644 highs/interfaces/Highs/Enums/ObjectiveSense.cs create mode 100644 highs/interfaces/Highs/Enums/Status.cs create mode 100644 highs/interfaces/Highs/Enums/VariableType.cs rename {src => highs}/interfaces/Highs/Highs.csproj (100%) rename {src => highs}/interfaces/Highs/Highs.sln (66%) create mode 100644 highs/interfaces/Highs/Records/BasisInfo.cs create mode 100644 highs/interfaces/Highs/Records/Hessian.cs create mode 100644 highs/interfaces/Highs/Records/Model.cs create mode 100644 highs/interfaces/Highs/Records/Solution.cs create mode 100644 highs/interfaces/Highs/Records/SolutionInfo.cs create mode 100644 highs/interfaces/Highs/Solver.cs delete mode 100644 src/interfaces/Highs/Class1.cs diff --git a/highs/interfaces/Highs/Enums/BasisStatus.cs b/highs/interfaces/Highs/Enums/BasisStatus.cs new file mode 100644 index 0000000000..cbb2b5db6d --- /dev/null +++ b/highs/interfaces/Highs/Enums/BasisStatus.cs @@ -0,0 +1,28 @@ +namespace Highs.Enums; + +/// +/// This defines the status of a variable (or slack variable for a constraint) in a basis +/// +public enum BasisStatus +{ + /// + /// The variable is nonbasic at its lower bound (or fixed value) + /// + Lower = 0, + /// + /// The variable is basic + /// + Basic, + /// + /// he variable is at its upper bound + /// + Upper, + /// + /// A free variable is nonbasic and set to zero + /// + Zero, + /// + /// The variable is nonbasic + /// + Nonbasic +} diff --git a/highs/interfaces/Highs/Enums/HessianFormat.cs b/highs/interfaces/Highs/Enums/HessianFormat.cs new file mode 100644 index 0000000000..c54b6bb0ca --- /dev/null +++ b/highs/interfaces/Highs/Enums/HessianFormat.cs @@ -0,0 +1,16 @@ +namespace Highs.Enums; + +/// +/// The format in which the Hessian is stored +/// +public enum HessianFormat +{ + /// + /// Store the Hessian in triangular format + /// + Triangular = 1, + /// + /// Store the Hessian in square format + /// + Square +} diff --git a/highs/interfaces/Highs/Enums/MatrixFormat.cs b/highs/interfaces/Highs/Enums/MatrixFormat.cs new file mode 100644 index 0000000000..77f5bdb15a --- /dev/null +++ b/highs/interfaces/Highs/Enums/MatrixFormat.cs @@ -0,0 +1,16 @@ +namespace Highs.Enums; + +/// +/// This defines the format of a HighsSparseMatrix +/// +public enum MatrixFormat +{ + /// + /// The matrix is stored column-wise + /// + ColumnWise = 1, + /// + /// The matrix is stored row-wise + /// + RowWise +} diff --git a/highs/interfaces/Highs/Enums/ModelStatus.cs b/highs/interfaces/Highs/Enums/ModelStatus.cs new file mode 100644 index 0000000000..29736814ba --- /dev/null +++ b/highs/interfaces/Highs/Enums/ModelStatus.cs @@ -0,0 +1,75 @@ +namespace Highs.Enums; + +/// +/// This defines the status of the model after a call to run +/// +public enum ModelStatus +{ + /// + /// The model status has not been set + /// + Notset = 0, + LoadError, + /// + /// There is an error in the model + /// + ModelError, + PresolveError, + /// + /// There has been an error when solving the model + /// + SolveError, + PostsolveError, + /// + /// The model is empty + /// + ModelEmpty, + /// + /// The model has been solved to optimality + /// + Optimal, + /// + /// The model is infeasible + /// + Infeasible, + /// + /// The model is unbounded or infeasible + /// + UnboundedOrInfeasible, + /// + /// The model is unbounded + /// + Unbounded, + /// + /// The bound on the model objective value has been reached + /// + ObjectiveBound, + /// + /// The target value for the model objective has been reached + /// + ObjectiveTarget, + /// + /// The run time limit has been reached + /// + TimeLimit, + /// + /// The iteration limit has been reached + /// + IterationLimit, + /// + /// The model status is unknown + /// + Unknown, + /// + /// The MIP solver has reached the limit on the number of LPs solved + /// + SolutionLimit, + /// + /// The solver has been interrupted by the user + /// + Interrupt, + /// + /// The solver has been unable to allocate sufficient memory + /// + MemoryLimit +} diff --git a/highs/interfaces/Highs/Enums/ObjectiveSense.cs b/highs/interfaces/Highs/Enums/ObjectiveSense.cs new file mode 100644 index 0000000000..155741d4a3 --- /dev/null +++ b/highs/interfaces/Highs/Enums/ObjectiveSense.cs @@ -0,0 +1,16 @@ +namespace Highs.Enums; + +/// +/// This defines optimization sense of a HighsLp +/// +public enum ObjectiveSense +{ + /// + /// The objective is to be minimized + /// + Minimize = 1, + /// + /// The objective is to be maximized + /// + Maximize = -1 +} diff --git a/highs/interfaces/Highs/Enums/Status.cs b/highs/interfaces/Highs/Enums/Status.cs new file mode 100644 index 0000000000..001f9a5de2 --- /dev/null +++ b/highs/interfaces/Highs/Enums/Status.cs @@ -0,0 +1,20 @@ +namespace Highs.Enums; + +/// +/// This is (part of) the return value of most HiGHS methods +/// +public enum Status +{ + /// + /// The method has exposed an error + /// + Error = -1, + /// + /// The method has completed successfully + /// + Ok, + /// + /// The method has recovered from an unusual event, or has terminated due to reaching a time or iteration limit + /// + Warning +} diff --git a/highs/interfaces/Highs/Enums/VariableType.cs b/highs/interfaces/Highs/Enums/VariableType.cs new file mode 100644 index 0000000000..e22f4610a5 --- /dev/null +++ b/highs/interfaces/Highs/Enums/VariableType.cs @@ -0,0 +1,28 @@ +namespace Highs.Enums; + +/// +/// This defines the feasible values of a variable within a model +/// +public enum VariableType +{ + /// + /// The variable can take continuous values between its bounds + /// + Continuous = 0, + /// + /// The variable must take integer values between its bounds + /// + Integer, + /// + /// The variable must be zero or take continuous values between its bounds + /// + SemiContinuous, + /// + /// The variable must be zero or take integer values between its bounds + /// + SemiInteger, + /// + /// The variable must take implicit integer values between its bounds + /// + ImplicitInteger, +} \ No newline at end of file diff --git a/src/interfaces/Highs/Highs.csproj b/highs/interfaces/Highs/Highs.csproj similarity index 100% rename from src/interfaces/Highs/Highs.csproj rename to highs/interfaces/Highs/Highs.csproj diff --git a/src/interfaces/Highs/Highs.sln b/highs/interfaces/Highs/Highs.sln similarity index 66% rename from src/interfaces/Highs/Highs.sln rename to highs/interfaces/Highs/Highs.sln index bb1b0b4091..01795d0c38 100644 --- a/src/interfaces/Highs/Highs.sln +++ b/highs/interfaces/Highs/Highs.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.14.36811.4 d17.14 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Highs", "Highs.csproj", "{97E314AD-8C55-4E72-9FFA-6BA9EA34F55E}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Highs", "Highs.csproj", "{5BEA6A69-23A3-426C-A849-3B9A9A8D518A}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -11,15 +11,15 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {97E314AD-8C55-4E72-9FFA-6BA9EA34F55E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {97E314AD-8C55-4E72-9FFA-6BA9EA34F55E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {97E314AD-8C55-4E72-9FFA-6BA9EA34F55E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {97E314AD-8C55-4E72-9FFA-6BA9EA34F55E}.Release|Any CPU.Build.0 = Release|Any CPU + {5BEA6A69-23A3-426C-A849-3B9A9A8D518A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5BEA6A69-23A3-426C-A849-3B9A9A8D518A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5BEA6A69-23A3-426C-A849-3B9A9A8D518A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5BEA6A69-23A3-426C-A849-3B9A9A8D518A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {AEB74AA2-A995-4A88-BCB4-32F2033C8C90} + SolutionGuid = {F1C5253E-03B0-407C-B6C6-27C74E5FDF9F} EndGlobalSection EndGlobal diff --git a/highs/interfaces/Highs/Records/BasisInfo.cs b/highs/interfaces/Highs/Records/BasisInfo.cs new file mode 100644 index 0000000000..b6114a8aa3 --- /dev/null +++ b/highs/interfaces/Highs/Records/BasisInfo.cs @@ -0,0 +1,21 @@ +using Highs.Enums; + +namespace Highs.Records; + +/// +/// This defines the basis status of the columns and rows in a basis +/// +/// The column basis status +/// The row basis status +public record BasisInfo(BasisStatus[] ColumnBasisStatus, BasisStatus[] RowBasisStatus) +{ + /// + /// The default constructor creates empty arrays + /// + /// The number of columns + /// The number of rows + public BasisInfo(int numberOfColumns, int numberOfRows) : this(new BasisStatus[numberOfColumns], + new BasisStatus[numberOfRows]) + { + } +} diff --git a/highs/interfaces/Highs/Records/Hessian.cs b/highs/interfaces/Highs/Records/Hessian.cs new file mode 100644 index 0000000000..fcade220b9 --- /dev/null +++ b/highs/interfaces/Highs/Records/Hessian.cs @@ -0,0 +1,30 @@ +using Highs.Enums; + +namespace Highs.Records; + +/// +/// This defines the Hessian of the quadratic objective +/// +public record Hessian +{ + /// + /// Format of the Hessian + /// + public HessianFormat HessianFormat; + /// + /// Dimension of the Hessian + /// + public int Dimension; + /// + /// Start of each compressed column in the Hessian + /// + public int[] Start = []; + /// + /// Indices of the nonzeros in the Hessian + /// + public int[] Index = []; + /// + /// Values of the nonzeros in the Hessian + /// + public double[] Values = []; +} diff --git a/highs/interfaces/Highs/Records/Model.cs b/highs/interfaces/Highs/Records/Model.cs new file mode 100644 index 0000000000..9d46fa0432 --- /dev/null +++ b/highs/interfaces/Highs/Records/Model.cs @@ -0,0 +1,58 @@ +using Highs.Enums; + +namespace Highs.Records; + +/// +/// This defines a model for Highs +/// +public record Model +{ + /// + /// The objective sense + /// + public ObjectiveSense ObjectiveSense; + /// + /// The objective constant + /// + public double Offset; + /// + /// The column costs + /// + public double[] ColumnCost = []; + /// + /// The column lower bounds + /// + public double[] ColumnLower = []; + /// + /// The column upper bounds + /// + public double[] ColumnUpper = []; + /// + /// The row lower bounds + /// + public double[] RowLower = []; + /// + /// The row upper bounds + /// + public double[] RowUpper = []; + /// + /// The format of the constraint matrix. + /// + public MatrixFormat MatrixFormat; + /// + /// The starting index of each column (or row) in MatrixIndices. + /// + public int[] MatrixStart = []; + /// + /// The indices of matrix entries + /// + public int[] MatrixIndices = []; + /// + /// The values of matrix entries + /// + public double[] MatrixValues = []; + /// + /// The integrality of the variables + /// + public VariableType[] VariableTypes = []; +} diff --git a/highs/interfaces/Highs/Records/Solution.cs b/highs/interfaces/Highs/Records/Solution.cs new file mode 100644 index 0000000000..d0baefcafe --- /dev/null +++ b/highs/interfaces/Highs/Records/Solution.cs @@ -0,0 +1,26 @@ +namespace Highs.Records; + +/// +/// The solution. +/// +/// The column value. +/// The column dual. +/// The row value. +/// The row dual. +public record Solution(double[] ColumnValue, + double[] ColumnDual, + double[] RowValue, + double[] RowDual) +{ + /// + /// The default constructor creates empty arrays + /// + /// The number of columns + /// The number of rows + public Solution(int numberOfColumns, int numberOfRows) : this(new double[numberOfColumns], + new double[numberOfColumns], + new double[numberOfRows], + new double[numberOfRows]) + { + } +} diff --git a/highs/interfaces/Highs/Records/SolutionInfo.cs b/highs/interfaces/Highs/Records/SolutionInfo.cs new file mode 100644 index 0000000000..710353062e --- /dev/null +++ b/highs/interfaces/Highs/Records/SolutionInfo.cs @@ -0,0 +1,21 @@ +namespace Highs.Records; + +/// +/// The solution info. +/// +/// The simplex iteration count. +/// The Interior Point Method (IPM) iteration count. +/// The PDLP iteration count. +/// The MIP gap. +/// The best dual bound. +/// The MIP node count. +/// The objective value. +public record SolutionInfo(int SimplexIterationCount, + int IpmIterationCount, + int PdlpIterationCount, + double MipGap, + double DualBound, + long NodeCount, + double ObjectiveValue) +{ +} diff --git a/highs/interfaces/Highs/Solver.cs b/highs/interfaces/Highs/Solver.cs new file mode 100644 index 0000000000..30b936df30 --- /dev/null +++ b/highs/interfaces/Highs/Solver.cs @@ -0,0 +1,920 @@ +using System.Runtime.InteropServices; +using System.Text; +using Highs.Enums; +using Highs.Records; + +namespace Highs; + +/// +/// The Highs Solver interface. +/// +public class Solver : IDisposable +{ + /// + /// The pointer to the _highs instance. + /// + private readonly IntPtr _highs; + + /// + /// Indicates whether the instance has been disposed. + /// + private bool _disposed; + + /// + /// The name of the Highs library. + /// + private const string HighsLibName = "_highs"; + + #region Library Imports + [LibraryImport(HighsLibName)] + private static extern int Highs_call( + int numcol, + int numrow, + int numnz, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue, + double[] colvalue, + double[] coldual, + double[] rowvalue, + double[] rowdual, + int[] colbasisstatus, + int[] rowbasisstatus, + ref int modelstatus); + + [LibraryImport(HighsLibName)] + private static extern IntPtr Highs_create(); + + [LibraryImport(HighsLibName)] + private static extern void Highs_destroy(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_run(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_readModel(IntPtr _highs, string filename); + + [LibraryImport(HighsLibName)] + private static extern int Highs_writeModel(IntPtr _highs, string filename); + + [LibraryImport(HighsLibName)] + private static extern int Highs_writePresolvedModel(IntPtr _highs, string filename); + + [LibraryImport(HighsLibName)] + private static extern int Highs_writeSolutionPretty(IntPtr _highs, string filename); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getInfinity(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_passLp( + IntPtr _highs, + int numcol, + int numrow, + int numnz, + int aformat, + int sense, + double offset, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue); + + [LibraryImport(HighsLibName)] + private static extern int Highs_passMip( + IntPtr _highs, + int numcol, + int numrow, + int numnz, + int aformat, + int sense, + double offset, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue, + int[] highs_integrality); + + [LibraryImport(HighsLibName)] + private static extern int Highs_setOptionValue(IntPtr _highs, string option, string value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_setBoolOptionValue(IntPtr _highs, string option, int value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_setIntOptionValue(IntPtr _highs, string option, int value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_setDoubleOptionValue(IntPtr _highs, string option, double value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_setStringOptionValue(IntPtr _highs, string option, string value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getBoolOptionValue(IntPtr _highs, string option, out int value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getIntOptionValue(IntPtr _highs, string option, out int value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getDoubleOptionValue(IntPtr _highs, string option, out double value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getStringOptionValue(IntPtr _highs, string option, [Out] StringBuilder value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getSolution(IntPtr _highs, double[] colvalue, double[] coldual, double[] rowvalue, double[] rowdual); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getNumCol(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getNumRow(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getNumNz(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getBasis(IntPtr _highs, int[] colstatus, int[] rowstatus); + + [LibraryImport(HighsLibName)] + private static extern double Highs_getObjectiveValue(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getIterationCount(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getModelStatus(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_addRow(IntPtr _highs, double lower, double upper, int num_new_nz, int[] indices, double[] values); + + [LibraryImport(HighsLibName)] + private static extern int Highs_addRows( + IntPtr _highs, + int num_new_row, + double[] lower, + double[] upper, + int num_new_nz, + int[] starts, + int[] indices, + double[] values); + + [LibraryImport(HighsLibName)] + private static extern int Highs_addCol( + IntPtr _highs, + double cost, + double lower, + double upper, + int num_new_nz, + int[] indices, + double[] values); + + [LibraryImport(HighsLibName)] + private static extern int Highs_addCols( + IntPtr _highs, + int num_new_col, + double[] costs, + double[] lower, + double[] upper, + int num_new_nz, + int[] starts, + int[] indices, + double[] values); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeObjectiveSense(IntPtr _highs, int sense); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeColCost(IntPtr _highs, int col, double cost); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeColsCostBySet(IntPtr _highs, int num_set_entries, int[] set, double[] cost); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeColsCostByMask(IntPtr _highs, int[] mask, double[] cost); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeColBounds(IntPtr _highs, int col, double lower, double upper); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeColsBoundsByRange(IntPtr _highs, int from_col, int to_col, double[] lower, double[] upper); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeColsBoundsBySet(IntPtr _highs, int num_set_entries, int[] set, double[] lower, double[] upper); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeColsBoundsByMask(IntPtr _highs, int[] mask, double[] lower, double[] upper); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeRowBounds(IntPtr _highs, int row, double lower, double upper); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeRowsBoundsBySet(IntPtr _highs, int num_set_entries, int[] set, double[] lower, double[] upper); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeRowsBoundsByMask(IntPtr _highs, int[] mask, double[] lower, double[] upper); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeColsIntegralityByRange(IntPtr _highs, int from_col, int to_col, int[] integrality); + + [LibraryImport(HighsLibName)] + private static extern int Highs_changeCoeff(IntPtr _highs, int row, int col, double value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_deleteColsByRange(IntPtr _highs, int from_col, int to_col); + + [LibraryImport(HighsLibName)] + private static extern int Highs_deleteColsBySet(IntPtr _highs, int num_set_entries, int[] set); + + [LibraryImport(HighsLibName)] + private static extern int Highs_deleteColsByMask(IntPtr _highs, int[] mask); + + [LibraryImport(HighsLibName)] + private static extern int Highs_deleteRowsByRange(IntPtr _highs, int from_row, int to_row); + + [LibraryImport(HighsLibName)] + private static extern int Highs_deleteRowsBySet(IntPtr _highs, int num_set_entries, int[] set); + + [LibraryImport(HighsLibName)] + private static extern int Highs_deleteRowsByMask(IntPtr _highs, int[] mask); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getDoubleInfoValue(IntPtr _highs, string info, out double value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getIntInfoValue(IntPtr _highs, string info, out int value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getInt64InfoValue(IntPtr _highs, string info, out long value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_setSolution(IntPtr _highs, double[] col_value, double[] row_value, double[] col_dual, double[] row_dual); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getColsByRange( + IntPtr _highs, + int from_col, + int to_col, + ref int num_col, + double[] costs, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getColsBySet( + IntPtr _highs, + int num_set_entries, + int[] set, + ref int num_col, + double[] costs, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getColsByMask( + IntPtr _highs, + int[] mask, + ref int num_col, + double[] costs, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getRowsByRange( + IntPtr _highs, + int from_row, + int to_row, + ref int num_row, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getRowsBySet( + IntPtr _highs, + int num_set_entries, + int[] set, + ref int num_row, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getRowsByMask( + IntPtr _highs, + int[] mask, + ref int num_row, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getBasicVariables(IntPtr _highs, int[] basic_variables); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getBasisInverseRow(IntPtr _highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getBasisInverseCol(IntPtr _highs, int col, double[] col_vector, ref int col_num_nz, int[] col_indices); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getBasisSolve( + IntPtr _highs, + double[] rhs, + double[] solution_vector, + ref int solution_num_nz, + int[] solution_indices); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getBasisTransposeSolve( + IntPtr _highs, + double[] rhs, + double[] solution_vector, + ref int solution_nz, + int[] solution_indices); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getReducedRow(IntPtr _highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); + + [LibraryImport(HighsLibName)] + private static extern int Highs_getReducedColumn(IntPtr _highs, int col, double[] col_vector, ref int col_num_nz, int[] col_indices); + + [LibraryImport(HighsLibName)] + private static extern int Highs_clearModel(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_clearSolver(IntPtr _highs); + + [LibraryImport(HighsLibName)] + private static extern int Highs_passColName(IntPtr _highs, int col, string name); + + [LibraryImport(HighsLibName)] + private static extern int Highs_passRowName(IntPtr _highs, int row, string name); + + [LibraryImport(HighsLibName)] + private static extern int Highs_writeOptions(IntPtr _highs, string filename); + + [LibraryImport(HighsLibName)] + private static extern int Highs_writeOptionsDeviations(IntPtr _highs, string filename); + #endregion + + /// + /// Calls the Highs solver in a single call. + /// + /// + /// + /// + /// + /// + public static Status Call(Model model, ref Solution solution, out BasisInfo basisInfo, out ModelStatus modelStatus) + { + var numberOfColumns = model.ColumnCost.Length; + var numberOfRows = model.RowLower.Length; + var numberOfMatrixValues = model.MatrixValues.Length; + + var columnBasisStatus = new int[numberOfColumns]; + var rowBasisStatus = new int[numberOfRows]; + + var modelstate = 0; + + var status = (Status)Highs_call( + numberOfColumns, + numberOfRows, + numberOfMatrixValues, + model.ColumnCost, + model.ColumnLower, + model.ColumnUpper, + model.RowLower, + model.RowUpper, + model.MatrixStart, + model.MatrixIndices, + model.MatrixValues, + solution.ColumnValue, + solution.ColumnDual, + solution.RowValue, + solution.RowDual, + columnBasisStatus, + rowBasisStatus, + ref modelstate); + + modelStatus = (ModelStatus)modelstate; + basisInfo = new([.. columnBasisStatus.Select(x => (BasisStatus)x)], [.. rowBasisStatus.Select(x => (BasisStatus)x)]); + + return status; + } + + /// + /// The default constructor. + /// + public Solver() => _highs = Highs_create(); + + /// + /// The destructor. + /// + ~Solver() => Dispose(false); + + /// + /// Disposes the instance. + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Disposes the instance. + /// + /// + protected virtual void Dispose(bool disposing) + { + if (_disposed) + { + return; + } + Highs_destroy(_highs); + _disposed = true; + } + + /// + /// Runs the solver. + /// + /// + public Status Run() => (Status)Highs_run(_highs); + + /// + /// Reads a model from file. + /// + /// + /// + public Status ReadModel(string filename) => (Status)Highs_readModel(_highs, filename); + + /// + /// Writes the model to file. + /// + /// + /// + public Status WriteModel(string filename) => (Status)Highs_writeModel(_highs, filename); + + /// + /// Writes the presolved model to file. + /// + /// + /// + public Status WritePresolvedModel(string filename) => (Status)Highs_writePresolvedModel(_highs, filename); + + /// + /// Writes the solution to file in a pretty format. + /// + /// + /// + public Status WriteSolutionPretty(string filename) => (Status)Highs_writeSolutionPretty(_highs, filename); + + /// + /// Gets the infinity value. + /// + /// + public double GetInfinity() => Highs_getInfinity(_highs); + + public Status passLp(HighsModel model) + { + return (Status)Highs_passLp( + _highs, + model.colcost.Length, + model.rowlower.Length, + model.avalue.Length, + (int)model.a_format, + (int)model.sense, + model.offset, + model.colcost, + model.collower, + model.colupper, + model.rowlower, + model.rowupper, + model.astart, + model.aindex, + model.avalue); + } + + public Status passMip(HighsModel model) + { + return (Status)Highs_passMip( + _highs, + model.colcost.Length, + model.rowlower.Length, + model.avalue.Length, + (int)model.a_format, + (int)model.sense, + model.offset, + model.colcost, + model.collower, + model.colupper, + model.rowlower, + model.rowupper, + model.astart, + model.aindex, + model.avalue, + model.highs_integrality); + } + + /// + /// Sets the option value. + /// + /// + /// + /// + public Status SetOptionValue(string option, string value) => (Status)Highs_setOptionValue(_highs, option, value); + + /// + /// Sets the string option value. + /// + /// + /// + /// + public Status SetStringOptionValue(string option, string value) => (Status)Highs_setStringOptionValue(_highs, option, value); + + /// + /// Sets the boolean option value. + /// + /// + /// + /// + public Status SetBoolOptionValue(string option, int value) => (Status)Highs_setBoolOptionValue(_highs, option, value); + + /// + /// Sets the double option value. + /// + /// + /// + /// + public Status SetDoubleOptionValue(string option, double value) => (Status)Highs_setDoubleOptionValue(_highs, option, value); + + /// + /// Sets the integer option value. + /// + /// + /// + /// + public Status SetIntOptionValue(string option, int value) => (Status)Highs_setIntOptionValue(_highs, option, value); + + public Status getStringOptionValue(string option, out string value) + { + var stringBuilder = new StringBuilder(); + var result = (Status)Highs_getStringOptionValue(_highs, option, stringBuilder); + value = stringBuilder.ToString(); + return result; + } + + public Status getBoolOptionValue(string option, out int value) + { + return (Status)Highs_getBoolOptionValue(_highs, option, out value); + } + + public Status getDoubleOptionValue(string option, out double value) + { + return (Status)Highs_getDoubleOptionValue(_highs, option, out value); + } + + public Status getIntOptionValue(string option, out int value) + { + return (Status)Highs_getIntOptionValue(_highs, option, out value); + } + + public int getNumCol() + { + return Highs_getNumCol(_highs); + } + + public int getNumRow() + { + return Highs_getNumRow(_highs); + } + + public int getNumNz() + { + return Highs_getNumNz(_highs); + } + + public HighsSolution getSolution() + { + int numberOfColumns = getNumCol(); + int numberOfRows = getNumRow(); + + HighsSolution sol = new HighsSolution(numberOfColumns, numberOfRows); + Highs_getSolution(_highs, sol.colvalue, sol.coldual, sol.rowvalue, sol.rowdual); + + return sol; + } + + public HighsBasis getBasis() + { + int numberOfColumns = getNumCol(); + int numberOfRows = getNumRow(); + + int[] colbasstat = new int[numberOfColumns]; + int[] rowbasstat = new int[numberOfRows]; + + Highs_getBasis(_highs, colbasstat, rowbasstat); + HighsBasis bas = new HighsBasis( + colbasstat.Select(x => (HighsBasisStatus)x).ToArray(), + rowbasstat.Select(x => (HighsBasisStatus)x).ToArray()); + + return bas; + } + + public double getObjectiveValue() + { + return Highs_getObjectiveValue(_highs); + } + + public HighsModelStatus GetModelStatus() + { + return (HighsModelStatus)Highs_getModelStatus(_highs); + } + + public int getIterationCount() + { + return Highs_getIterationCount(_highs); + } + + public Status addRow(double lower, double upper, int[] indices, double[] values) + { + return (Status)Highs_addRow(_highs, lower, upper, indices.Length, indices, values); + } + + public Status addRows(double[] lower, double[] upper, int[] starts, int[] indices, double[] values) + { + return (Status)Highs_addRows(_highs, lower.Length, lower, upper, indices.Length, starts, indices, values); + } + + public Status addCol(double cost, double lower, double upper, int[] indices, double[] values) + { + return (Status)Highs_addCol(_highs, cost, lower, upper, indices.Length, indices, values); + } + + public Status addCols(double[] costs, double[] lower, double[] upper, int[] starts, int[] indices, double[] values) + { + return (Status)Highs_addCols( + _highs, + costs.Length, + costs, + lower, + upper, + indices.Length, + starts, + indices, + values); + } + + public Status changeObjectiveSense(HighsObjectiveSense sense) + { + return (Status)Highs_changeObjectiveSense(_highs, (int)sense); + } + + public Status changeColCost(int col, double cost) + { + return (Status)Highs_changeColCost(_highs, col, cost); + } + + public Status changeColsCostBySet(int[] cols, double[] costs) + { + return (Status)Highs_changeColsCostBySet(_highs, cols.Length, cols, costs); + } + + public Status changeColsCostByMask(bool[] mask, double[] cost) + { + return (Status)Highs_changeColsCostByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray(), cost); + } + + public Status changeColBounds(int col, double lower, double upper) + { + return (Status)Highs_changeColBounds(_highs, col, lower, upper); + } + + public Status changeColsBoundsByRange(int from, int to, double[] lower, double[] upper) + { + return (Status)Highs_changeColsBoundsByRange(_highs, from, to, lower, upper); + } + + public Status changeColsBoundsBySet(int[] cols, double[] lower, double[] upper) + { + return (Status)Highs_changeColsBoundsBySet(_highs, cols.Length, cols, lower, upper); + } + + public Status changeColsBoundsByMask(bool[] mask, double[] lower, double[] upper) + { + return (Status)Highs_changeColsBoundsByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray(), lower, upper); + } + + public Status changeRowBounds(int row, double lower, double upper) + { + return (Status)Highs_changeRowBounds(_highs, row, lower, upper); + } + + public Status changeRowsBoundsBySet(int[] rows, double[] lower, double[] upper) + { + return (Status)Highs_changeRowsBoundsBySet(_highs, rows.Length, rows, lower, upper); + } + + public Status changeRowsBoundsByMask(bool[] mask, double[] lower, double[] upper) + { + return (Status)Highs_changeRowsBoundsByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray(), lower, upper); + } + + public Status changeColsIntegralityByRange(int from_col, int to_col, HighsIntegrality[] integrality) + { + return (Status)Highs_changeColsIntegralityByRange(_highs, from_col, to_col, Array.ConvertAll(integrality, item => (int)item)); + } + + public Status changeCoeff(int row, int col, double value) + { + return (Status)Highs_changeCoeff(_highs, row, col, value); + } + + public Status deleteColsByRange(int from, int to) + { + return (Status)Highs_deleteColsByRange(_highs, from, to); + } + + public Status deleteColsBySet(int[] cols) + { + return (Status)Highs_deleteColsBySet(_highs, cols.Length, cols); + } + + public Status deleteColsByMask(bool[] mask) + { + return (Status)Highs_deleteColsByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray()); + } + + public Status deleteRowsByRange(int from, int to) + { + return (Status)Highs_deleteRowsByRange(_highs, from, to); + } + + public Status deleteRowsBySet(int[] rows) + { + return (Status)Highs_deleteRowsBySet(_highs, rows.Length, rows); + } + + public Status deleteRowsByMask(bool[] mask) + { + return (Status)Highs_deleteRowsByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray()); + } + + delegate int HighsGetInfoDelegate(IntPtr _highs, string infoName, out TValue output); + + private TValue GetValueOrFallback(HighsGetInfoDelegate highsGetInfoDelegate, string infoName, TValue fallback) + { + try + { + var status = (Status)highsGetInfoDelegate(_highs, infoName, out var value); + if (status != Status.kOk) + { + return fallback; + } + + return value; + } + catch + { + return fallback; + } + } + + /// + /// Gets the current solution info. + /// + /// The . + public SolutionInfo getInfo() + { + // TODO: This object does not contian the "complete" info from the C api. Add further props, if you need them. + var info = new SolutionInfo() + { + MipGap = GetValueOrFallback(Highs_getDoubleInfoValue, "mip_gap", double.NaN), + DualBound = GetValueOrFallback(Highs_getDoubleInfoValue, "mip_dual_bound", double.NaN), + ObjectiveValue = GetValueOrFallback(Highs_getDoubleInfoValue, "objective_function_value", double.NaN), + NodeCount = GetValueOrFallback(Highs_getInt64InfoValue, "mip_node_count", 0L), + IpmIterationCount = GetValueOrFallback(Highs_getIntInfoValue, "ipm_iteration_count", 0), + SimplexIterationCount = GetValueOrFallback(Highs_getIntInfoValue, "simplex_iteration_count", 0), + PdlpIterationCount = GetValueOrFallback(Highs_getIntInfoValue, "pdlp_iteration_count", 0), + }; + return info; + } + + public Status setSolution(HighsSolution solution) + { + return (Status)Highs_setSolution(_highs, solution.colvalue, solution.coldual, solution.rowvalue, solution.rowdual); + } + + public Status getBasicVariables(ref int[] basic_variables) + { + return (Status)Highs_getBasicVariables(_highs, basic_variables); + } + + public Status getBasisInverseRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) + { + return (Status)Highs_getBasisInverseRow(_highs, row, row_vector, ref row_num_nz, row_indices); + } + + public Status getBasisInverseCol(int col, double[] col_vector, ref int col_num_nz, int[] col_indices) + { + return (Status)Highs_getBasisInverseCol(_highs, col, col_vector, ref col_num_nz, col_indices); + } + + public Status getBasisSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) + { + return (Status)Highs_getBasisSolve(_highs, rhs, solution_vector, ref solution_num_nz, solution_indices); + } + + public Status getBasisTransposeSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) + { + return (Status)Highs_getBasisTransposeSolve(_highs, rhs, solution_vector, ref solution_num_nz, solution_indices); + } + + public Status getReducedRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) + { + return (Status)Highs_getReducedRow(_highs, row, row_vector, ref row_num_nz, row_indices); + } + + public Status getReducedColumn(int col, double[] col_vector, ref int col_num_nz, int[] col_indices) + { + return (Status)Highs_getReducedColumn(_highs, col, col_vector, ref col_num_nz, col_indices); + } + + /// + /// Clears the model. + /// + /// + public Status ClearModel() => (Status)Highs_clearModel(_highs); + + /// + /// Clears the solver. + /// + /// + public Status ClearSolver() => (Status)Highs_clearSolver(_highs); + + /// + /// Passes the name of a column. + /// + /// + /// + /// + public Status PassColumnName(int col, string name) => (Status)Highs_passColName(_highs, col, name); + + /// + /// Passes the name of a row. + /// + /// + /// + /// + public Status PassRowName(int row, string name) => (Status)Highs_passRowName(_highs, row, name); + + /// + /// Writes the options to file. + /// + /// + /// + public Status WriteOptions(string filename) => (Status)Highs_writeOptions(_highs, filename); + + /// + /// Writes the options deviations to file. + /// + /// + /// + public Status WriteOptionsDeviations(string filename) => (Status)Highs_writeOptionsDeviations(_highs, filename); +} diff --git a/src/interfaces/Highs/Class1.cs b/src/interfaces/Highs/Class1.cs deleted file mode 100644 index a7a9f2c69c..0000000000 --- a/src/interfaces/Highs/Class1.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Highs -{ - public class Class1 - { - - } -} From e16cf748148bdde6335305366de848ab048e7b5f Mon Sep 17 00:00:00 2001 From: Thiago Novaes <77932135+Thiago-NovaesB@users.noreply.github.com> Date: Fri, 26 Dec 2025 16:56:30 +0100 Subject: [PATCH 5/9] done --- highs/interfaces/Highs/Records/Model.cs | 120 +-- highs/interfaces/Highs/Solver.cs | 1146 ++++++++++++++++------- 2 files changed, 851 insertions(+), 415 deletions(-) diff --git a/highs/interfaces/Highs/Records/Model.cs b/highs/interfaces/Highs/Records/Model.cs index 9d46fa0432..b496f6f34c 100644 --- a/highs/interfaces/Highs/Records/Model.cs +++ b/highs/interfaces/Highs/Records/Model.cs @@ -1,58 +1,62 @@ -using Highs.Enums; - -namespace Highs.Records; - -/// -/// This defines a model for Highs -/// -public record Model -{ - /// - /// The objective sense - /// - public ObjectiveSense ObjectiveSense; - /// - /// The objective constant - /// - public double Offset; - /// - /// The column costs - /// - public double[] ColumnCost = []; - /// - /// The column lower bounds - /// - public double[] ColumnLower = []; - /// - /// The column upper bounds - /// - public double[] ColumnUpper = []; - /// - /// The row lower bounds - /// - public double[] RowLower = []; - /// - /// The row upper bounds - /// - public double[] RowUpper = []; - /// - /// The format of the constraint matrix. - /// - public MatrixFormat MatrixFormat; - /// - /// The starting index of each column (or row) in MatrixIndices. - /// - public int[] MatrixStart = []; - /// - /// The indices of matrix entries - /// - public int[] MatrixIndices = []; - /// - /// The values of matrix entries - /// - public double[] MatrixValues = []; - /// - /// The integrality of the variables - /// - public VariableType[] VariableTypes = []; -} +using Highs.Enums; + +namespace Highs.Records; + +/// +/// This defines a model for Highs +/// +public record Model +{ + /// + /// The objective sense + /// + public ObjectiveSense ObjectiveSense; + /// + /// The objective constant + /// + public double Offset; + /// + /// The column costs + /// + public double[] ColumnCost = []; + /// + /// The column lower bounds + /// + public double[] ColumnLower = []; + /// + /// The column upper bounds + /// + public double[] ColumnUpper = []; + /// + /// The row lower bounds + /// + public double[] RowLower = []; + /// + /// The row upper bounds + /// + public double[] RowUpper = []; + /// + /// The format of the constraint matrix. + /// + public MatrixFormat MatrixFormat; + /// + /// The starting index of each column (or row) in MatrixIndices. + /// + public int[] MatrixStart = []; + /// + /// The indices of matrix entries + /// + public int[] MatrixIndices = []; + /// + /// The values of matrix entries + /// + public double[] MatrixValues = []; + /// + /// The integrality of the variables + /// + public VariableType[] VariableTypes = []; + /// + /// The Hessian for quadratic objectives + /// + public Hessian Hessian = null; +} diff --git a/highs/interfaces/Highs/Solver.cs b/highs/interfaces/Highs/Solver.cs index 30b936df30..653196c42c 100644 --- a/highs/interfaces/Highs/Solver.cs +++ b/highs/interfaces/Highs/Solver.cs @@ -23,14 +23,17 @@ public class Solver : IDisposable /// /// The name of the Highs library. /// - private const string HighsLibName = "_highs"; + private const string HighsLibName = "highs"; #region Library Imports - [LibraryImport(HighsLibName)] - private static extern int Highs_call( + [DllImport(HighsLibName)] + private static extern int Highs_lpCall( int numcol, int numrow, int numnz, + int aformat, + int sense, + double offset, double[] colcost, double[] collower, double[] colupper, @@ -47,33 +50,84 @@ private static extern int Highs_call( int[] rowbasisstatus, ref int modelstatus); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] + private static extern int Highs_mipCall( + int numcol, + int numrow, + int numnz, + int aformat, + int sense, + double offset, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue, + int[] integrality, + double[] colvalue, + double[] rowvalue, + ref int modelstatus); + + [DllImport(HighsLibName)] + private static extern int Highs_qpCall( + int numcol, + int numrow, + int numnz, + int qnumnz, + int aformat, + int qformat, + int sense, + double offset, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue, + int[] qstart, + int[] qindex, + double[] qvalue, + + double[] colvalue, + double[] coldual, + double[] rowvalue, + double[] rowdual, + int[] colbasisstatus, + int[] rowbasisstatus, + ref int modelstatus); + + [DllImport(HighsLibName)] private static extern IntPtr Highs_create(); - [LibraryImport(HighsLibName)] - private static extern void Highs_destroy(IntPtr _highs); + [DllImport(HighsLibName)] + private static extern void Highs_destroy(IntPtr highs); - [LibraryImport(HighsLibName)] - private static extern int Highs_run(IntPtr _highs); + [DllImport(HighsLibName)] + private static extern int Highs_run(IntPtr highs); - [LibraryImport(HighsLibName)] - private static extern int Highs_readModel(IntPtr _highs, string filename); + [DllImport(HighsLibName)] + private static extern int Highs_readModel(IntPtr highs, string filename); - [LibraryImport(HighsLibName)] - private static extern int Highs_writeModel(IntPtr _highs, string filename); + [DllImport(HighsLibName)] + private static extern int Highs_writeModel(IntPtr highs, string filename); - [LibraryImport(HighsLibName)] - private static extern int Highs_writePresolvedModel(IntPtr _highs, string filename); + [DllImport(HighsLibName)] + private static extern int Highs_writePresolvedModel(IntPtr highs, string filename); - [LibraryImport(HighsLibName)] - private static extern int Highs_writeSolutionPretty(IntPtr _highs, string filename); + [DllImport(HighsLibName)] + private static extern int Highs_writeSolutionPretty(IntPtr highs, string filename); - [LibraryImport(HighsLibName)] - private static extern int Highs_getInfinity(IntPtr _highs); + [DllImport(HighsLibName)] + private static extern int Highs_getInfinity(IntPtr highs); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] private static extern int Highs_passLp( - IntPtr _highs, + IntPtr highs, int numcol, int numrow, int numnz, @@ -89,13 +143,34 @@ private static extern int Highs_passLp( int[] aindex, double[] avalue); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] private static extern int Highs_passMip( - IntPtr _highs, + IntPtr highs, + int numcol, + int numrow, + int numnz, + int aformat, + int sense, + double offset, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue, + int[] highs_integrality); + + [DllImport(HighsLibName)] + private static extern int Highs_passModel( + IntPtr highs, int numcol, int numrow, int numnz, + int qnumnz, int aformat, + int qformat, int sense, double offset, double[] colcost, @@ -106,65 +181,81 @@ private static extern int Highs_passMip( int[] astart, int[] aindex, double[] avalue, + int[] qstart, + int[] qindex, + double[] qvalue, int[] highs_integrality); - [LibraryImport(HighsLibName)] - private static extern int Highs_setOptionValue(IntPtr _highs, string option, string value); + [DllImport(HighsLibName)] + private static extern int Highs_passHessian( + IntPtr highs, + int dim, + int numnz, + int q_format, + int[] qstart, + int[] qindex, + double[] qvalue); + + [DllImport(HighsLibName)] + private static extern int Highs_setOptionValue(IntPtr highs, string option, string value); - [LibraryImport(HighsLibName)] - private static extern int Highs_setBoolOptionValue(IntPtr _highs, string option, int value); + [DllImport(HighsLibName)] + private static extern int Highs_setBoolOptionValue(IntPtr highs, string option, int value); - [LibraryImport(HighsLibName)] - private static extern int Highs_setIntOptionValue(IntPtr _highs, string option, int value); + [DllImport(HighsLibName)] + private static extern int Highs_setIntOptionValue(IntPtr highs, string option, int value); - [LibraryImport(HighsLibName)] - private static extern int Highs_setDoubleOptionValue(IntPtr _highs, string option, double value); + [DllImport(HighsLibName)] + private static extern int Highs_setDoubleOptionValue(IntPtr highs, string option, double value); - [LibraryImport(HighsLibName)] - private static extern int Highs_setStringOptionValue(IntPtr _highs, string option, string value); + [DllImport(HighsLibName)] + private static extern int Highs_setStringOptionValue(IntPtr highs, string option, string value); - [LibraryImport(HighsLibName)] - private static extern int Highs_getBoolOptionValue(IntPtr _highs, string option, out int value); + [DllImport(HighsLibName)] + private static extern int Highs_getBoolOptionValue(IntPtr highs, string option, out int value); - [LibraryImport(HighsLibName)] - private static extern int Highs_getIntOptionValue(IntPtr _highs, string option, out int value); + [DllImport(HighsLibName)] + private static extern int Highs_getIntOptionValue(IntPtr highs, string option, out int value); - [LibraryImport(HighsLibName)] - private static extern int Highs_getDoubleOptionValue(IntPtr _highs, string option, out double value); + [DllImport(HighsLibName)] + private static extern int Highs_getDoubleOptionValue(IntPtr highs, string option, out double value); - [LibraryImport(HighsLibName)] - private static extern int Highs_getStringOptionValue(IntPtr _highs, string option, [Out] StringBuilder value); + [DllImport(HighsLibName)] + private static extern int Highs_getStringOptionValue(IntPtr highs, string option, [Out] StringBuilder value); - [LibraryImport(HighsLibName)] - private static extern int Highs_getSolution(IntPtr _highs, double[] colvalue, double[] coldual, double[] rowvalue, double[] rowdual); + [DllImport(HighsLibName)] + private static extern int Highs_getSolution(IntPtr highs, double[] colvalue, double[] coldual, double[] rowvalue, double[] rowdual); - [LibraryImport(HighsLibName)] - private static extern int Highs_getNumCol(IntPtr _highs); + [DllImport(HighsLibName)] + private static extern int Highs_getNumCol(IntPtr highs); - [LibraryImport(HighsLibName)] - private static extern int Highs_getNumRow(IntPtr _highs); + [DllImport(HighsLibName)] + private static extern int Highs_getNumRow(IntPtr highs); - [LibraryImport(HighsLibName)] - private static extern int Highs_getNumNz(IntPtr _highs); + [DllImport(HighsLibName)] + private static extern int Highs_getNumNz(IntPtr highs); - [LibraryImport(HighsLibName)] - private static extern int Highs_getBasis(IntPtr _highs, int[] colstatus, int[] rowstatus); + [DllImport(HighsLibName)] + private static extern int Highs_getHessianNumNz(IntPtr highs); - [LibraryImport(HighsLibName)] - private static extern double Highs_getObjectiveValue(IntPtr _highs); + [DllImport(HighsLibName)] + private static extern int Highs_getBasis(IntPtr highs, int[] colstatus, int[] rowstatus); - [LibraryImport(HighsLibName)] - private static extern int Highs_getIterationCount(IntPtr _highs); + [DllImport(HighsLibName)] + private static extern double Highs_getObjectiveValue(IntPtr highs); - [LibraryImport(HighsLibName)] - private static extern int Highs_getModelStatus(IntPtr _highs); + [DllImport(HighsLibName)] + private static extern int Highs_getIterationCount(IntPtr highs); - [LibraryImport(HighsLibName)] - private static extern int Highs_addRow(IntPtr _highs, double lower, double upper, int num_new_nz, int[] indices, double[] values); + [DllImport(HighsLibName)] + private static extern int Highs_getModelStatus(IntPtr highs); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] + private static extern int Highs_addRow(IntPtr highs, double lower, double upper, int num_new_nz, int[] indices, double[] values); + + [DllImport(HighsLibName)] private static extern int Highs_addRows( - IntPtr _highs, + IntPtr highs, int num_new_row, double[] lower, double[] upper, @@ -173,9 +264,9 @@ private static extern int Highs_addRows( int[] indices, double[] values); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] private static extern int Highs_addCol( - IntPtr _highs, + IntPtr highs, double cost, double lower, double upper, @@ -183,9 +274,9 @@ private static extern int Highs_addCol( int[] indices, double[] values); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] private static extern int Highs_addCols( - IntPtr _highs, + IntPtr highs, int num_new_col, double[] costs, double[] lower, @@ -195,78 +286,84 @@ private static extern int Highs_addCols( int[] indices, double[] values); - [LibraryImport(HighsLibName)] - private static extern int Highs_changeObjectiveSense(IntPtr _highs, int sense); + [DllImport(HighsLibName)] + private static extern int Highs_changeObjectiveSense(IntPtr highs, int sense); + + [DllImport(HighsLibName)] + private static extern int Highs_changeColCost(IntPtr highs, int column, double cost); - [LibraryImport(HighsLibName)] - private static extern int Highs_changeColCost(IntPtr _highs, int col, double cost); + [DllImport(HighsLibName)] + private static extern int Highs_changeColsCostBySet(IntPtr highs, int num_set_entries, int[] set, double[] cost); - [LibraryImport(HighsLibName)] - private static extern int Highs_changeColsCostBySet(IntPtr _highs, int num_set_entries, int[] set, double[] cost); + [DllImport(HighsLibName)] + private static extern int Highs_changeColsCostByMask(IntPtr highs, int[] mask, double[] cost); - [LibraryImport(HighsLibName)] - private static extern int Highs_changeColsCostByMask(IntPtr _highs, int[] mask, double[] cost); + [DllImport(HighsLibName)] + private static extern int Highs_changeColBounds(IntPtr highs, int column, double lower, double upper); - [LibraryImport(HighsLibName)] - private static extern int Highs_changeColBounds(IntPtr _highs, int col, double lower, double upper); + [DllImport(HighsLibName)] + private static extern int Highs_changeColsBoundsByRange(IntPtr highs, int from_col, int to_col, double[] lower, double[] upper); - [LibraryImport(HighsLibName)] - private static extern int Highs_changeColsBoundsByRange(IntPtr _highs, int from_col, int to_col, double[] lower, double[] upper); + [DllImport(HighsLibName)] + private static extern int Highs_changeColsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); - [LibraryImport(HighsLibName)] - private static extern int Highs_changeColsBoundsBySet(IntPtr _highs, int num_set_entries, int[] set, double[] lower, double[] upper); + [DllImport(HighsLibName)] + private static extern int Highs_changeColsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); - [LibraryImport(HighsLibName)] - private static extern int Highs_changeColsBoundsByMask(IntPtr _highs, int[] mask, double[] lower, double[] upper); + [DllImport(HighsLibName)] + private static extern int Highs_changeRowBounds(IntPtr highs, int row, double lower, double upper); - [LibraryImport(HighsLibName)] - private static extern int Highs_changeRowBounds(IntPtr _highs, int row, double lower, double upper); + [DllImport(HighsLibName)] + private static extern int Highs_changeRowsBoundsByRange(IntPtr highs, int from_row, int to_row, double[] lower, double[] upper); - [LibraryImport(HighsLibName)] - private static extern int Highs_changeRowsBoundsBySet(IntPtr _highs, int num_set_entries, int[] set, double[] lower, double[] upper); + [DllImport(HighsLibName)] + private static extern int Highs_changeRowsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); - [LibraryImport(HighsLibName)] - private static extern int Highs_changeRowsBoundsByMask(IntPtr _highs, int[] mask, double[] lower, double[] upper); + [DllImport(HighsLibName)] + private static extern int Highs_changeRowsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); - [LibraryImport(HighsLibName)] - private static extern int Highs_changeColsIntegralityByRange(IntPtr _highs, int from_col, int to_col, int[] integrality); + [DllImport(HighsLibName)] + private static extern int Highs_changeColsIntegralityByRange(IntPtr highs, int from_col, int to_col, int[] integrality); - [LibraryImport(HighsLibName)] - private static extern int Highs_changeCoeff(IntPtr _highs, int row, int col, double value); + [DllImport(HighsLibName)] + private static extern int Highs_changeCoeff(IntPtr highs, int row, int column, double value); - [LibraryImport(HighsLibName)] - private static extern int Highs_deleteColsByRange(IntPtr _highs, int from_col, int to_col); + [DllImport(HighsLibName)] + private static extern int Highs_deleteColsByRange(IntPtr highs, int from_col, int to_col); - [LibraryImport(HighsLibName)] - private static extern int Highs_deleteColsBySet(IntPtr _highs, int num_set_entries, int[] set); + [DllImport(HighsLibName)] + private static extern int Highs_deleteColsBySet(IntPtr highs, int num_set_entries, int[] set); - [LibraryImport(HighsLibName)] - private static extern int Highs_deleteColsByMask(IntPtr _highs, int[] mask); + [DllImport(HighsLibName)] + private static extern int Highs_deleteColsByMask(IntPtr highs, int[] mask); - [LibraryImport(HighsLibName)] - private static extern int Highs_deleteRowsByRange(IntPtr _highs, int from_row, int to_row); + [DllImport(HighsLibName)] + private static extern int Highs_deleteRowsByRange(IntPtr highs, int from_row, int to_row); - [LibraryImport(HighsLibName)] - private static extern int Highs_deleteRowsBySet(IntPtr _highs, int num_set_entries, int[] set); + [DllImport(HighsLibName)] + private static extern int Highs_deleteRowsBySet(IntPtr highs, int num_set_entries, int[] set); - [LibraryImport(HighsLibName)] - private static extern int Highs_deleteRowsByMask(IntPtr _highs, int[] mask); + [DllImport(HighsLibName)] + private static extern int Highs_deleteRowsByMask(IntPtr highs, int[] mask); - [LibraryImport(HighsLibName)] - private static extern int Highs_getDoubleInfoValue(IntPtr _highs, string info, out double value); + [DllImport(HighsLibName)] + private static extern int Highs_getDoubleInfoValue(IntPtr highs, string info, out double value); - [LibraryImport(HighsLibName)] - private static extern int Highs_getIntInfoValue(IntPtr _highs, string info, out int value); + [DllImport(HighsLibName)] + private static extern int Highs_getIntInfoValue(IntPtr highs, string info, out int value); - [LibraryImport(HighsLibName)] - private static extern int Highs_getInt64InfoValue(IntPtr _highs, string info, out long value); + [DllImport(HighsLibName)] + private static extern int Highs_getInt64InfoValue(IntPtr highs, string info, out long value); - [LibraryImport(HighsLibName)] - private static extern int Highs_setSolution(IntPtr _highs, double[] col_value, double[] row_value, double[] col_dual, double[] row_dual); + [DllImport(HighsLibName)] + private static extern int Highs_setSolution(IntPtr highs, double[] col_value, double[] row_value, double[] col_dual, double[] row_dual); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] + private static extern int Highs_setSparseSolution(IntPtr highs, int num_entries, int[] index, double[] value); + + [DllImport(HighsLibName)] private static extern int Highs_getColsByRange( - IntPtr _highs, + IntPtr highs, int from_col, int to_col, ref int num_col, @@ -278,9 +375,9 @@ private static extern int Highs_getColsByRange( int[] matrix_index, double[] matrix_value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] private static extern int Highs_getColsBySet( - IntPtr _highs, + IntPtr highs, int num_set_entries, int[] set, ref int num_col, @@ -292,9 +389,9 @@ private static extern int Highs_getColsBySet( int[] matrix_index, double[] matrix_value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] private static extern int Highs_getColsByMask( - IntPtr _highs, + IntPtr highs, int[] mask, ref int num_col, double[] costs, @@ -305,9 +402,9 @@ private static extern int Highs_getColsByMask( int[] matrix_index, double[] matrix_value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] private static extern int Highs_getRowsByRange( - IntPtr _highs, + IntPtr highs, int from_row, int to_row, ref int num_row, @@ -318,9 +415,9 @@ private static extern int Highs_getRowsByRange( int[] matrix_index, double[] matrix_value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] private static extern int Highs_getRowsBySet( - IntPtr _highs, + IntPtr highs, int num_set_entries, int[] set, ref int num_row, @@ -331,9 +428,9 @@ private static extern int Highs_getRowsBySet( int[] matrix_index, double[] matrix_value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] private static extern int Highs_getRowsByMask( - IntPtr _highs, + IntPtr highs, int[] mask, ref int num_row, double[] lower, @@ -343,65 +440,65 @@ private static extern int Highs_getRowsByMask( int[] matrix_index, double[] matrix_value); - [LibraryImport(HighsLibName)] - private static extern int Highs_getBasicVariables(IntPtr _highs, int[] basic_variables); + [DllImport(HighsLibName)] + private static extern int Highs_getBasicVariables(IntPtr highs, int[] basic_variables); - [LibraryImport(HighsLibName)] - private static extern int Highs_getBasisInverseRow(IntPtr _highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); + [DllImport(HighsLibName)] + private static extern int Highs_getBasisInverseRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); - [LibraryImport(HighsLibName)] - private static extern int Highs_getBasisInverseCol(IntPtr _highs, int col, double[] col_vector, ref int col_num_nz, int[] col_indices); + [DllImport(HighsLibName)] + private static extern int Highs_getBasisInverseCol(IntPtr highs, int column, double[] col_vector, ref int col_num_nz, int[] col_indices); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] private static extern int Highs_getBasisSolve( - IntPtr _highs, + IntPtr highs, double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] private static extern int Highs_getBasisTransposeSolve( - IntPtr _highs, + IntPtr highs, double[] rhs, double[] solution_vector, ref int solution_nz, int[] solution_indices); - [LibraryImport(HighsLibName)] - private static extern int Highs_getReducedRow(IntPtr _highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); + [DllImport(HighsLibName)] + private static extern int Highs_getReducedRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); - [LibraryImport(HighsLibName)] - private static extern int Highs_getReducedColumn(IntPtr _highs, int col, double[] col_vector, ref int col_num_nz, int[] col_indices); + [DllImport(HighsLibName)] + private static extern int Highs_getReducedColumn(IntPtr highs, int column, double[] col_vector, ref int col_num_nz, int[] col_indices); - [LibraryImport(HighsLibName)] - private static extern int Highs_clearModel(IntPtr _highs); + [DllImport(HighsLibName)] + private static extern int Highs_clearModel(IntPtr highs); - [LibraryImport(HighsLibName)] - private static extern int Highs_clearSolver(IntPtr _highs); + [DllImport(HighsLibName)] + private static extern int Highs_clearSolver(IntPtr highs); - [LibraryImport(HighsLibName)] - private static extern int Highs_passColName(IntPtr _highs, int col, string name); + [DllImport(HighsLibName)] + private static extern int Highs_passColName(IntPtr highs, int column, string name); - [LibraryImport(HighsLibName)] - private static extern int Highs_passRowName(IntPtr _highs, int row, string name); + [DllImport(HighsLibName)] + private static extern int Highs_passRowName(IntPtr highs, int row, string name); - [LibraryImport(HighsLibName)] - private static extern int Highs_writeOptions(IntPtr _highs, string filename); + [DllImport(HighsLibName)] + private static extern int Highs_writeOptions(IntPtr highs, string filename); - [LibraryImport(HighsLibName)] - private static extern int Highs_writeOptionsDeviations(IntPtr _highs, string filename); + [DllImport(HighsLibName)] + private static extern int Highs_writeOptionsDeviations(IntPtr highs, string filename); #endregion /// - /// Calls the Highs solver in a single call. + /// Calls the Highs solver in a single call of a LP. /// /// /// /// /// /// - public static Status Call(Model model, ref Solution solution, out BasisInfo basisInfo, out ModelStatus modelStatus) + public static Status LpCall(Model model, ref Solution solution, out BasisInfo basisInfo, out ModelStatus modelStatus) { var numberOfColumns = model.ColumnCost.Length; var numberOfRows = model.RowLower.Length; @@ -412,10 +509,13 @@ public static Status Call(Model model, ref Solution solution, out BasisInfo basi var modelstate = 0; - var status = (Status)Highs_call( + var status = (Status)Highs_lpCall( numberOfColumns, numberOfRows, numberOfMatrixValues, + (int)model.MatrixFormat, + (int)model.ObjectiveSense, + model.Offset, model.ColumnCost, model.ColumnLower, model.ColumnUpper, @@ -433,7 +533,100 @@ public static Status Call(Model model, ref Solution solution, out BasisInfo basi ref modelstate); modelStatus = (ModelStatus)modelstate; - basisInfo = new([.. columnBasisStatus.Select(x => (BasisStatus)x)], [.. rowBasisStatus.Select(x => (BasisStatus)x)]); + basisInfo = new(Array.ConvertAll(columnBasisStatus, c => (BasisStatus)c), Array.ConvertAll(rowBasisStatus, r => (BasisStatus)r)); + + return status; + } + + /// + /// Calls the Highs solver in a single call of a Mip. + /// + /// + /// + /// + /// + public static Status MipCall(Model model, ref Solution solution, out ModelStatus modelStatus) + { + var numberOfColumns = model.ColumnCost.Length; + var numberOfRows = model.RowLower.Length; + var numberOfMatrixValues = model.MatrixValues.Length; + + var modelstate = 0; + + var status = (Status)Highs_mipCall( + numberOfColumns, + numberOfRows, + numberOfMatrixValues, + (int)model.MatrixFormat, + (int)model.ObjectiveSense, + model.Offset, + model.ColumnCost, + model.ColumnLower, + model.ColumnUpper, + model.RowLower, + model.RowUpper, + model.MatrixStart, + model.MatrixIndices, + model.MatrixValues, + Array.ConvertAll(model.VariableTypes, v => (int)v), + solution.ColumnValue, + solution.RowValue, + ref modelstate); + + modelStatus = (ModelStatus)modelstate; + + return status; + } + + /// + /// Calls the Highs solver in a single call of a QP. + /// + /// + /// + /// + /// + public static Status QPCall(Model model, ref Solution solution, out BasisInfo basisInfo, out ModelStatus modelStatus) + { + var numberOfColumns = model.ColumnCost.Length; + var numberOfRows = model.RowLower.Length; + var numberOfMatrixValues = model.MatrixValues.Length; + var numberOfHessianValues = model.Hessian.Values.Length; + + var columnBasisStatus = new int[numberOfColumns]; + var rowBasisStatus = new int[numberOfRows]; + + var modelstate = 0; + + var status = (Status)Highs_qpCall( + numberOfColumns, + numberOfRows, + numberOfMatrixValues, + numberOfHessianValues, + (int)model.MatrixFormat, + (int)model.Hessian.HessianFormat, + (int)model.ObjectiveSense, + model.Offset, + model.ColumnCost, + model.ColumnLower, + model.ColumnUpper, + model.RowLower, + model.RowUpper, + model.MatrixStart, + model.MatrixIndices, + model.MatrixValues, + model.Hessian.Start, + model.Hessian.Index, + model.Hessian.Values, + solution.ColumnValue, + solution.ColumnDual, + solution.RowValue, + solution.RowDual, + columnBasisStatus, + rowBasisStatus, + ref modelstate); + + modelStatus = (ModelStatus)modelstate; + basisInfo = new(Array.ConvertAll(columnBasisStatus, c => (BasisStatus)c), Array.ConvertAll(rowBasisStatus, r => (BasisStatus)r)); return status; } @@ -511,45 +704,55 @@ protected virtual void Dispose(bool disposing) /// public double GetInfinity() => Highs_getInfinity(_highs); - public Status passLp(HighsModel model) + /// + /// Passes the LP model to Highs. + /// + /// + /// + public Status PassLp(Model model) { return (Status)Highs_passLp( _highs, - model.colcost.Length, - model.rowlower.Length, - model.avalue.Length, - (int)model.a_format, - (int)model.sense, - model.offset, - model.colcost, - model.collower, - model.colupper, - model.rowlower, - model.rowupper, - model.astart, - model.aindex, - model.avalue); + model.ColumnCost.Length, + model.RowLower.Length, + model.MatrixValues.Length, + (int)model.MatrixFormat, + (int)model.ObjectiveSense, + model.Offset, + model.ColumnCost, + model.ColumnLower, + model.ColumnUpper, + model.RowLower, + model.RowUpper, + model.MatrixStart, + model.MatrixIndices, + model.MatrixValues); } - public Status passMip(HighsModel model) + /// + /// Passes the MIP model to Highs. + /// + /// + /// + public Status PassMip(Model model) { return (Status)Highs_passMip( _highs, - model.colcost.Length, - model.rowlower.Length, - model.avalue.Length, - (int)model.a_format, - (int)model.sense, - model.offset, - model.colcost, - model.collower, - model.colupper, - model.rowlower, - model.rowupper, - model.astart, - model.aindex, - model.avalue, - model.highs_integrality); + model.ColumnCost.Length, + model.RowLower.Length, + model.MatrixValues.Length, + (int)model.MatrixFormat, + (int)model.ObjectiveSense, + model.Offset, + model.ColumnCost, + model.ColumnLower, + model.ColumnUpper, + model.RowLower, + model.RowUpper, + model.MatrixStart, + model.MatrixIndices, + model.MatrixValues, + Array.ConvertAll(model.VariableTypes, v => (int)v)); } /// @@ -592,7 +795,13 @@ public Status passMip(HighsModel model) /// public Status SetIntOptionValue(string option, int value) => (Status)Highs_setIntOptionValue(_highs, option, value); - public Status getStringOptionValue(string option, out string value) + /// + /// Gets the string option value. + /// + /// + /// + /// + public Status GetStringOptionValue(string option, out string value) { var stringBuilder = new StringBuilder(); var result = (Status)Highs_getStringOptionValue(_highs, option, stringBuilder); @@ -600,94 +809,172 @@ public Status getStringOptionValue(string option, out string value) return result; } - public Status getBoolOptionValue(string option, out int value) - { - return (Status)Highs_getBoolOptionValue(_highs, option, out value); - } + /// + /// Gets the boolean option value. + /// + /// + /// + /// + public Status GetBoolOptionValue(string option, out int value) => (Status)Highs_getBoolOptionValue(_highs, option, out value); - public Status getDoubleOptionValue(string option, out double value) - { - return (Status)Highs_getDoubleOptionValue(_highs, option, out value); - } + /// + /// Gets the double option value. + /// + /// + /// + /// + public Status GetDoubleOptionValue(string option, out double value) => (Status)Highs_getDoubleOptionValue(_highs, option, out value); - public Status getIntOptionValue(string option, out int value) - { - return (Status)Highs_getIntOptionValue(_highs, option, out value); - } + /// + /// Gets the integer option value. + /// + /// + /// + /// + public Status GetIntOptionValue(string option, out int value) => (Status)Highs_getIntOptionValue(_highs, option, out value); - public int getNumCol() - { - return Highs_getNumCol(_highs); - } + /// + /// Gets the number of columns. + /// + /// + public int GetNumberOfColumns() => Highs_getNumCol(_highs); - public int getNumRow() - { - return Highs_getNumRow(_highs); - } + /// + /// Gets the number of rows. + /// + /// + public int GetNumberOfRows() => Highs_getNumRow(_highs); - public int getNumNz() - { - return Highs_getNumNz(_highs); - } + /// + /// Gets the number of non-zero entries. + /// + /// + public int GetNumberOfNonZeroEntries() => Highs_getNumNz(_highs); - public HighsSolution getSolution() + /// + /// Gets the solution. + /// + /// + /// + public Status GetSolution(out Solution solution) { - int numberOfColumns = getNumCol(); - int numberOfRows = getNumRow(); - - HighsSolution sol = new HighsSolution(numberOfColumns, numberOfRows); - Highs_getSolution(_highs, sol.colvalue, sol.coldual, sol.rowvalue, sol.rowdual); - - return sol; + var numberOfColumns = GetNumberOfColumns(); + var numberOfRows = GetNumberOfRows(); + + solution = new Solution(numberOfColumns, numberOfRows); + return (Status)Highs_getSolution(_highs, solution.ColumnValue, solution.ColumnDual, solution.RowValue, solution.RowDual); + } + + /// + /// Passes the Hessian to Highs. + /// + /// + /// + public Status PassHessian(Hessian hessian) + { + return (Status)Highs_passHessian(_highs, + hessian.Dimension, + hessian.Values.Length, + (int)hessian.HessianFormat, + hessian.Start, + hessian.Index, + hessian.Values); } - public HighsBasis getBasis() + /// + /// Gets the basis information. + /// + /// + public Status GetBasis(out BasisInfo basisInfo) { - int numberOfColumns = getNumCol(); - int numberOfRows = getNumRow(); - - int[] colbasstat = new int[numberOfColumns]; - int[] rowbasstat = new int[numberOfRows]; + var numberOfColumns = GetNumberOfColumns(); + var numberOfRows = GetNumberOfRows(); - Highs_getBasis(_highs, colbasstat, rowbasstat); - HighsBasis bas = new HighsBasis( - colbasstat.Select(x => (HighsBasisStatus)x).ToArray(), - rowbasstat.Select(x => (HighsBasisStatus)x).ToArray()); + var columnBasisStatus = new int[numberOfColumns]; + var rowBasisStatus = new int[numberOfRows]; - return bas; - } + var status = (Status)Highs_getBasis(_highs, columnBasisStatus, rowBasisStatus); + if (status == Status.Error) + { + basisInfo = null; + return Status.Error; + } + + basisInfo = new BasisInfo(Array.ConvertAll(columnBasisStatus, x => (BasisStatus)x), Array.ConvertAll(rowBasisStatus, x => (BasisStatus)x)); - public double getObjectiveValue() - { - return Highs_getObjectiveValue(_highs); + return status; } - public HighsModelStatus GetModelStatus() - { - return (HighsModelStatus)Highs_getModelStatus(_highs); - } + /// + /// Gets the objective value. + /// + /// + public double GetObjectiveValue() => Highs_getObjectiveValue(_highs); - public int getIterationCount() - { - return Highs_getIterationCount(_highs); - } + /// + /// Gets the model status. + /// + /// + public ModelStatus GetModelStatus() => (ModelStatus)Highs_getModelStatus(_highs); - public Status addRow(double lower, double upper, int[] indices, double[] values) + /// + /// Gets the iteration count. + /// + /// + public int GetIterationCount() => Highs_getIterationCount(_highs); + + /// + /// Adds a row to the model. + /// + /// + /// + /// + /// + /// + public Status AddRow(double lower, double upper, int[] indices, double[] values) { return (Status)Highs_addRow(_highs, lower, upper, indices.Length, indices, values); } - public Status addRows(double[] lower, double[] upper, int[] starts, int[] indices, double[] values) + /// + /// Adds multiple rows to the model. + /// + /// + /// + /// + /// + /// + /// + public Status AddRows(double[] lower, double[] upper, int[] starts, int[] indices, double[] values) { return (Status)Highs_addRows(_highs, lower.Length, lower, upper, indices.Length, starts, indices, values); } - public Status addCol(double cost, double lower, double upper, int[] indices, double[] values) + /// + /// Adds a column to the model. + /// + /// + /// + /// + /// + /// + /// + public Status AddColumn(double cost, double lower, double upper, int[] indices, double[] values) { return (Status)Highs_addCol(_highs, cost, lower, upper, indices.Length, indices, values); } - public Status addCols(double[] costs, double[] lower, double[] upper, int[] starts, int[] indices, double[] values) + /// + /// Adds multiple columns to the model. + /// + /// + /// + /// + /// + /// + /// + /// + public Status AddColumns(double[] costs, double[] lower, double[] upper, int[] starts, int[] indices, double[] values) { return (Status)Highs_addCols( _highs, @@ -701,114 +988,196 @@ public Status addCols(double[] costs, double[] lower, double[] upper, int[] star values); } - public Status changeObjectiveSense(HighsObjectiveSense sense) - { - return (Status)Highs_changeObjectiveSense(_highs, (int)sense); - } - - public Status changeColCost(int col, double cost) - { - return (Status)Highs_changeColCost(_highs, col, cost); - } - - public Status changeColsCostBySet(int[] cols, double[] costs) - { - return (Status)Highs_changeColsCostBySet(_highs, cols.Length, cols, costs); - } - - public Status changeColsCostByMask(bool[] mask, double[] cost) - { - return (Status)Highs_changeColsCostByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray(), cost); - } - - public Status changeColBounds(int col, double lower, double upper) - { - return (Status)Highs_changeColBounds(_highs, col, lower, upper); - } - - public Status changeColsBoundsByRange(int from, int to, double[] lower, double[] upper) - { - return (Status)Highs_changeColsBoundsByRange(_highs, from, to, lower, upper); - } - - public Status changeColsBoundsBySet(int[] cols, double[] lower, double[] upper) - { - return (Status)Highs_changeColsBoundsBySet(_highs, cols.Length, cols, lower, upper); - } + /// + /// Changes the objective sense. + /// + /// + /// + public Status ChangeObjectiveSense(ObjectiveSense sense) => (Status)Highs_changeObjectiveSense(_highs, (int)sense); - public Status changeColsBoundsByMask(bool[] mask, double[] lower, double[] upper) - { - return (Status)Highs_changeColsBoundsByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray(), lower, upper); - } + /// + /// Changes the cost of a column. + /// + /// + /// + /// + public Status ChangeColumnCost(int column, double cost) => (Status)Highs_changeColCost(_highs, column, cost); - public Status changeRowBounds(int row, double lower, double upper) - { - return (Status)Highs_changeRowBounds(_highs, row, lower, upper); - } + /// + /// Changes the costs of multiple columns by set. + /// + /// + /// + /// + public Status ChangeColumnsCostBySet(int[] columns, double[] costs) => (Status)Highs_changeColsCostBySet(_highs, columns.Length, columns, costs); - public Status changeRowsBoundsBySet(int[] rows, double[] lower, double[] upper) + /// + /// Changes the costs of multiple columns by mask. + /// + /// + /// + /// + public Status ChangeColumnsCostByMask(bool[] mask, double[] cost) => (Status)Highs_changeColsCostByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0), cost); + + /// + /// Changes the bounds of a column. + /// + /// + /// + /// + /// + public Status ChangeColumnBounds(int column, double lower, double upper) => (Status)Highs_changeColBounds(_highs, column, lower, upper); + + /// + /// Changes the bounds of multiple columns by range. + /// + /// + /// + /// + /// + /// + public Status ChangeColumnsBoundsByRange(int from, int to, double[] lower, double[] upper) => (Status)Highs_changeColsBoundsByRange(_highs, from, to, lower, upper); + + /// + /// Changes the bounds of multiple columns by set. + /// + /// + /// + /// + /// + public Status ChangeColumnsBoundsBySet(int[] columns, double[] lower, double[] upper) { - return (Status)Highs_changeRowsBoundsBySet(_highs, rows.Length, rows, lower, upper); + return (Status)Highs_changeColsBoundsBySet(_highs, columns.Length, columns, lower, upper); } - public Status changeRowsBoundsByMask(bool[] mask, double[] lower, double[] upper) + /// + /// + /// + /// + /// + /// + /// + public Status ChangeColumnsBoundsByMask(bool[] mask, double[] lower, double[] upper) { - return (Status)Highs_changeRowsBoundsByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray(), lower, upper); + return (Status)Highs_changeColsBoundsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0), lower, upper); } - public Status changeColsIntegralityByRange(int from_col, int to_col, HighsIntegrality[] integrality) + /// + /// Changes the bounds of a row. + /// + /// + /// + /// + /// + public Status ChangeRowBounds(int row, double lower, double upper) => (Status)Highs_changeRowBounds(_highs, row, lower, upper); + + /// + /// Changes the bounds of multiple rows by range. + /// + /// + /// + /// + /// + public Status ChangeRowsBoundsBySet(int[] rows, double[] lower, double[] upper) => (Status)Highs_changeRowsBoundsBySet(_highs, rows.Length, rows, lower, upper); + + /// + /// Changes the bounds of multiple rows by mask. + /// + /// + /// + /// + /// + public Status ChangeRowsBoundsByMask(bool[] mask, double[] lower, double[] upper) => (Status)Highs_changeRowsBoundsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0), lower, upper); + + /// + /// Changes the integrality of multiple columns by range. + /// + /// + /// + /// + /// + public Status ChangeColumnsIntegralityByRange(int from_col, int to_col, VariableType[] integrality) { return (Status)Highs_changeColsIntegralityByRange(_highs, from_col, to_col, Array.ConvertAll(integrality, item => (int)item)); } - public Status changeCoeff(int row, int col, double value) - { - return (Status)Highs_changeCoeff(_highs, row, col, value); - } - - public Status deleteColsByRange(int from, int to) - { - return (Status)Highs_deleteColsByRange(_highs, from, to); - } - - public Status deleteColsBySet(int[] cols) - { - return (Status)Highs_deleteColsBySet(_highs, cols.Length, cols); - } + /// + /// Changes a coefficient in the constraint matrix. + /// + /// + /// + /// + /// + public Status ChangeCoefficient(int row, int column, double value) => (Status)Highs_changeCoeff(_highs, row, column, value); - public Status deleteColsByMask(bool[] mask) - { - return (Status)Highs_deleteColsByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray()); - } + /// + /// Deletes multiple columns by range. + /// + /// + /// + /// + public Status DeleteColumnsByRange(int from, int to) => (Status)Highs_deleteColsByRange(_highs, from, to); - public Status deleteRowsByRange(int from, int to) - { - return (Status)Highs_deleteRowsByRange(_highs, from, to); - } + /// + /// Deletes multiple columns by set. + /// + /// + /// + public Status DeleteColumnsBySet(int[] columns) => (Status)Highs_deleteColsBySet(_highs, columns.Length, columns); - public Status deleteRowsBySet(int[] rows) - { - return (Status)Highs_deleteRowsBySet(_highs, rows.Length, rows); - } + /// + /// Deletes multiple columns by mask. + /// + /// + /// + public Status DeleteColumnsByMask(bool[] mask) => (Status)Highs_deleteColsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0)); - public Status deleteRowsByMask(bool[] mask) - { - return (Status)Highs_deleteRowsByMask(_highs, mask.Select(x => x ? 1 : 0).ToArray()); - } + /// + /// Deletes multiple rows by range. + /// + /// + /// + /// + public Status DeleteRowsByRange(int from, int to) => (Status)Highs_deleteRowsByRange(_highs, from, to); - delegate int HighsGetInfoDelegate(IntPtr _highs, string infoName, out TValue output); + /// + /// Deletes multiple rows by set. + /// + /// + /// + public Status DeleteRowsBySet(int[] rows) => (Status)Highs_deleteRowsBySet(_highs, rows.Length, rows); + /// + /// Deletes multiple rows by mask. + /// + /// + /// + public Status DeleteRowsByMask(bool[] mask) => (Status)Highs_deleteRowsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0)); + + /// + /// Delegate for getting info values. + /// + /// + /// + /// + /// + /// + delegate int HighsGetInfoDelegate(IntPtr highs, string infoName, out TValue output); + + /// + /// Gets a value or a fallback if an error occurs. + /// + /// + /// + /// + /// + /// private TValue GetValueOrFallback(HighsGetInfoDelegate highsGetInfoDelegate, string infoName, TValue fallback) { try { var status = (Status)highsGetInfoDelegate(_highs, infoName, out var value); - if (status != Status.kOk) - { - return fallback; - } - - return value; + return status != Status.Ok ? fallback : value; } catch { @@ -820,60 +1189,123 @@ private TValue GetValueOrFallback(HighsGetInfoDelegate highsGetI /// Gets the current solution info. /// /// The . - public SolutionInfo getInfo() + public SolutionInfo GetInfo() { // TODO: This object does not contian the "complete" info from the C api. Add further props, if you need them. - var info = new SolutionInfo() - { - MipGap = GetValueOrFallback(Highs_getDoubleInfoValue, "mip_gap", double.NaN), - DualBound = GetValueOrFallback(Highs_getDoubleInfoValue, "mip_dual_bound", double.NaN), - ObjectiveValue = GetValueOrFallback(Highs_getDoubleInfoValue, "objective_function_value", double.NaN), - NodeCount = GetValueOrFallback(Highs_getInt64InfoValue, "mip_node_count", 0L), - IpmIterationCount = GetValueOrFallback(Highs_getIntInfoValue, "ipm_iteration_count", 0), - SimplexIterationCount = GetValueOrFallback(Highs_getIntInfoValue, "simplex_iteration_count", 0), - PdlpIterationCount = GetValueOrFallback(Highs_getIntInfoValue, "pdlp_iteration_count", 0), - }; - return info; - } - - public Status setSolution(HighsSolution solution) - { - return (Status)Highs_setSolution(_highs, solution.colvalue, solution.coldual, solution.rowvalue, solution.rowdual); + return new SolutionInfo(GetValueOrFallback(Highs_getIntInfoValue, "simplex_iteration_count", 0), + GetValueOrFallback(Highs_getIntInfoValue, "ipm_iteration_count", 0), + GetValueOrFallback(Highs_getIntInfoValue, "pdlp_iteration_count", 0), + GetValueOrFallback(Highs_getDoubleInfoValue, "mip_gap", double.NaN), + GetValueOrFallback(Highs_getDoubleInfoValue, "mip_dual_bound", double.NaN), + GetValueOrFallback(Highs_getInt64InfoValue, "mip_node_count", 0L), + GetValueOrFallback(Highs_getDoubleInfoValue, "objective_function_value", double.NaN)); } - public Status getBasicVariables(ref int[] basic_variables) - { - return (Status)Highs_getBasicVariables(_highs, basic_variables); + /// + /// Sets the solution. + /// + /// + /// + public Status SetSolution(Solution solution) => (Status)Highs_setSolution(_highs, solution.ColumnValue, solution.ColumnDual, solution.RowValue, solution.RowDual); + + /// + /// Set a partial primal solution by passing values for a set of variables + /// + /// + /// The sparse solution set by this function has values for a subset of the model's variables. + /// For each entry in , the key identifies a variable by index, and + /// the value indicates the variable's value in the sparse solution. + /// + /// A dictionary that maps variable indices to variable values + /// + public Status SetSparseSolution(IReadOnlyDictionary valuesByIndex) + { + return (Status)Highs_setSparseSolution(_highs, valuesByIndex.Count, [.. valuesByIndex.Keys], [.. valuesByIndex.Values]); } - public Status getBasisInverseRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) + /// + /// Gets the basic variables. + /// + /// + /// + public Status GetBasicVariables(ref int[] basic_variables) => (Status)Highs_getBasicVariables(_highs, basic_variables); + + /// + /// Gets a row of the basis inverse. + /// + /// + /// + /// + /// + /// + public Status GetBasisInverseRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) { return (Status)Highs_getBasisInverseRow(_highs, row, row_vector, ref row_num_nz, row_indices); } - public Status getBasisInverseCol(int col, double[] col_vector, ref int col_num_nz, int[] col_indices) + /// + /// Gets a column of the basis inverse. + /// + /// + /// + /// + /// + /// + public Status GetBasisInverseColumn(int column, double[] column_vector, ref int column_num_nz, int[] column_indices) { - return (Status)Highs_getBasisInverseCol(_highs, col, col_vector, ref col_num_nz, col_indices); + return (Status)Highs_getBasisInverseCol(_highs, column, column_vector, ref column_num_nz, column_indices); } - public Status getBasisSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) + /// + /// Gets the basis solve. + /// + /// + /// + /// + /// + /// + public Status GetBasisSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) { return (Status)Highs_getBasisSolve(_highs, rhs, solution_vector, ref solution_num_nz, solution_indices); } - public Status getBasisTransposeSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) + /// + /// Gets the basis transpose solve. + /// + /// + /// + /// + /// + /// + public Status GetBasisTransposeSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) { return (Status)Highs_getBasisTransposeSolve(_highs, rhs, solution_vector, ref solution_num_nz, solution_indices); } - public Status getReducedRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) + /// + /// Gets a reduced row. + /// + /// + /// + /// + /// + /// + public Status GetReducedRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) { return (Status)Highs_getReducedRow(_highs, row, row_vector, ref row_num_nz, row_indices); } - public Status getReducedColumn(int col, double[] col_vector, ref int col_num_nz, int[] col_indices) + /// + /// Gets a reduced column. + /// + /// + /// + /// + /// + /// + public Status GetReducedColumn(int column, double[] column_vector, ref int column_num_nz, int[] column_indices) { - return (Status)Highs_getReducedColumn(_highs, col, col_vector, ref col_num_nz, col_indices); + return (Status)Highs_getReducedColumn(_highs, column, column_vector, ref column_num_nz, column_indices); } /// @@ -891,10 +1323,10 @@ public Status getReducedColumn(int col, double[] col_vector, ref int col_num_nz, /// /// Passes the name of a column. /// - /// + /// /// /// - public Status PassColumnName(int col, string name) => (Status)Highs_passColName(_highs, col, name); + public Status PassColumnName(int column, string name) => (Status)Highs_passColName(_highs, column, name); /// /// Passes the name of a row. From c6f62326ad89c9f18af3ec47bef83dc06a77022d Mon Sep 17 00:00:00 2001 From: Thiago Novaes <77932135+Thiago-NovaesB@users.noreply.github.com> Date: Fri, 26 Dec 2025 17:02:49 +0100 Subject: [PATCH 6/9] move --- highs/interfaces/Highs/Imports.cs | 479 +++++++++++++++++++++++ highs/interfaces/Highs/Solver.cs | 625 ++++-------------------------- 2 files changed, 556 insertions(+), 548 deletions(-) create mode 100644 highs/interfaces/Highs/Imports.cs diff --git a/highs/interfaces/Highs/Imports.cs b/highs/interfaces/Highs/Imports.cs new file mode 100644 index 0000000000..20efba6310 --- /dev/null +++ b/highs/interfaces/Highs/Imports.cs @@ -0,0 +1,479 @@ +using System.Runtime.InteropServices; +using System.Text; + +namespace Highs; + +/// +/// This contains the imports for the Highs library +/// +public static class Imports +{ + /// + /// The name of the Highs library. + /// + private const string HighsLibName = "highs"; + + #region Library Imports + [DllImport(HighsLibName)] + internal static extern int Highs_lpCall( + int numcol, + int numrow, + int numnz, + int aformat, + int sense, + double offset, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue, + double[] colvalue, + double[] coldual, + double[] rowvalue, + double[] rowdual, + int[] colbasisstatus, + int[] rowbasisstatus, + ref int modelstatus); + + [DllImport(HighsLibName)] + internal static extern int Highs_mipCall( + int numcol, + int numrow, + int numnz, + int aformat, + int sense, + double offset, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue, + int[] integrality, + double[] colvalue, + double[] rowvalue, + ref int modelstatus); + + [DllImport(HighsLibName)] + internal static extern int Highs_qpCall( + int numcol, + int numrow, + int numnz, + int qnumnz, + int aformat, + int qformat, + int sense, + double offset, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue, + int[] qstart, + int[] qindex, + double[] qvalue, + + double[] colvalue, + double[] coldual, + double[] rowvalue, + double[] rowdual, + int[] colbasisstatus, + int[] rowbasisstatus, + ref int modelstatus); + + [DllImport(HighsLibName)] + internal static extern IntPtr Highs_create(); + + [DllImport(HighsLibName)] + internal static extern void Highs_destroy(IntPtr highs); + + [DllImport(HighsLibName)] + internal static extern int Highs_run(IntPtr highs); + + [DllImport(HighsLibName)] + internal static extern int Highs_readModel(IntPtr highs, string filename); + + [DllImport(HighsLibName)] + internal static extern int Highs_writeModel(IntPtr highs, string filename); + + [DllImport(HighsLibName)] + internal static extern int Highs_writePresolvedModel(IntPtr highs, string filename); + + [DllImport(HighsLibName)] + internal static extern int Highs_writeSolutionPretty(IntPtr highs, string filename); + + [DllImport(HighsLibName)] + internal static extern int Highs_getInfinity(IntPtr highs); + + [DllImport(HighsLibName)] + internal static extern int Highs_passLp( + IntPtr highs, + int numcol, + int numrow, + int numnz, + int aformat, + int sense, + double offset, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue); + + [DllImport(HighsLibName)] + internal static extern int Highs_passMip( + IntPtr highs, + int numcol, + int numrow, + int numnz, + int aformat, + int sense, + double offset, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue, + int[] highs_integrality); + + [DllImport(HighsLibName)] + internal static extern int Highs_passModel( + IntPtr highs, + int numcol, + int numrow, + int numnz, + int qnumnz, + int aformat, + int qformat, + int sense, + double offset, + double[] colcost, + double[] collower, + double[] colupper, + double[] rowlower, + double[] rowupper, + int[] astart, + int[] aindex, + double[] avalue, + int[] qstart, + int[] qindex, + double[] qvalue, + int[] highs_integrality); + + [DllImport(HighsLibName)] + internal static extern int Highs_passHessian( + IntPtr highs, + int dim, + int numnz, + int q_format, + int[] qstart, + int[] qindex, + double[] qvalue); + + [DllImport(HighsLibName)] + internal static extern int Highs_setOptionValue(IntPtr highs, string option, string value); + + [DllImport(HighsLibName)] + internal static extern int Highs_setBoolOptionValue(IntPtr highs, string option, int value); + + [DllImport(HighsLibName)] + internal static extern int Highs_setIntOptionValue(IntPtr highs, string option, int value); + + [DllImport(HighsLibName)] + internal static extern int Highs_setDoubleOptionValue(IntPtr highs, string option, double value); + + [DllImport(HighsLibName)] + internal static extern int Highs_setStringOptionValue(IntPtr highs, string option, string value); + + [DllImport(HighsLibName)] + internal static extern int Highs_getBoolOptionValue(IntPtr highs, string option, out int value); + + [DllImport(HighsLibName)] + internal static extern int Highs_getIntOptionValue(IntPtr highs, string option, out int value); + + [DllImport(HighsLibName)] + internal static extern int Highs_getDoubleOptionValue(IntPtr highs, string option, out double value); + + [DllImport(HighsLibName)] + internal static extern int Highs_getStringOptionValue(IntPtr highs, string option, [Out] StringBuilder value); + + [DllImport(HighsLibName)] + internal static extern int Highs_getSolution(IntPtr highs, double[] colvalue, double[] coldual, double[] rowvalue, double[] rowdual); + + [DllImport(HighsLibName)] + internal static extern int Highs_getNumCol(IntPtr highs); + + [DllImport(HighsLibName)] + internal static extern int Highs_getNumRow(IntPtr highs); + + [DllImport(HighsLibName)] + internal static extern int Highs_getNumNz(IntPtr highs); + + [DllImport(HighsLibName)] + internal static extern int Highs_getHessianNumNz(IntPtr highs); + + [DllImport(HighsLibName)] + internal static extern int Highs_getBasis(IntPtr highs, int[] colstatus, int[] rowstatus); + + [DllImport(HighsLibName)] + internal static extern double Highs_getObjectiveValue(IntPtr highs); + + [DllImport(HighsLibName)] + internal static extern int Highs_getIterationCount(IntPtr highs); + + [DllImport(HighsLibName)] + internal static extern int Highs_getModelStatus(IntPtr highs); + + [DllImport(HighsLibName)] + internal static extern int Highs_addRow(IntPtr highs, double lower, double upper, int num_new_nz, int[] indices, double[] values); + + [DllImport(HighsLibName)] + internal static extern int Highs_addRows( + IntPtr highs, + int num_new_row, + double[] lower, + double[] upper, + int num_new_nz, + int[] starts, + int[] indices, + double[] values); + + [DllImport(HighsLibName)] + internal static extern int Highs_addCol( + IntPtr highs, + double cost, + double lower, + double upper, + int num_new_nz, + int[] indices, + double[] values); + + [DllImport(HighsLibName)] + internal static extern int Highs_addCols( + IntPtr highs, + int num_new_col, + double[] costs, + double[] lower, + double[] upper, + int num_new_nz, + int[] starts, + int[] indices, + double[] values); + + [DllImport(HighsLibName)] + internal static extern int Highs_changeObjectiveSense(IntPtr highs, int sense); + + [DllImport(HighsLibName)] + internal static extern int Highs_changeColCost(IntPtr highs, int column, double cost); + + [DllImport(HighsLibName)] + internal static extern int Highs_changeColsCostBySet(IntPtr highs, int num_set_entries, int[] set, double[] cost); + + [DllImport(HighsLibName)] + internal static extern int Highs_changeColsCostByMask(IntPtr highs, int[] mask, double[] cost); + + [DllImport(HighsLibName)] + internal static extern int Highs_changeColBounds(IntPtr highs, int column, double lower, double upper); + + [DllImport(HighsLibName)] + internal static extern int Highs_changeColsBoundsByRange(IntPtr highs, int from_col, int to_col, double[] lower, double[] upper); + + [DllImport(HighsLibName)] + internal static extern int Highs_changeColsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); + + [DllImport(HighsLibName)] + internal static extern int Highs_changeColsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); + + [DllImport(HighsLibName)] + internal static extern int Highs_changeRowBounds(IntPtr highs, int row, double lower, double upper); + + [DllImport(HighsLibName)] + internal static extern int Highs_changeRowsBoundsByRange(IntPtr highs, int from_row, int to_row, double[] lower, double[] upper); + + [DllImport(HighsLibName)] + internal static extern int Highs_changeRowsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); + + [DllImport(HighsLibName)] + internal static extern int Highs_changeRowsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); + + [DllImport(HighsLibName)] + internal static extern int Highs_changeColsIntegralityByRange(IntPtr highs, int from_col, int to_col, int[] integrality); + + [DllImport(HighsLibName)] + internal static extern int Highs_changeCoeff(IntPtr highs, int row, int column, double value); + + [DllImport(HighsLibName)] + internal static extern int Highs_deleteColsByRange(IntPtr highs, int from_col, int to_col); + + [DllImport(HighsLibName)] + internal static extern int Highs_deleteColsBySet(IntPtr highs, int num_set_entries, int[] set); + + [DllImport(HighsLibName)] + internal static extern int Highs_deleteColsByMask(IntPtr highs, int[] mask); + + [DllImport(HighsLibName)] + internal static extern int Highs_deleteRowsByRange(IntPtr highs, int from_row, int to_row); + + [DllImport(HighsLibName)] + internal static extern int Highs_deleteRowsBySet(IntPtr highs, int num_set_entries, int[] set); + + [DllImport(HighsLibName)] + internal static extern int Highs_deleteRowsByMask(IntPtr highs, int[] mask); + + [DllImport(HighsLibName)] + internal static extern int Highs_getDoubleInfoValue(IntPtr highs, string info, out double value); + + [DllImport(HighsLibName)] + internal static extern int Highs_getIntInfoValue(IntPtr highs, string info, out int value); + + [DllImport(HighsLibName)] + internal static extern int Highs_getInt64InfoValue(IntPtr highs, string info, out long value); + + [DllImport(HighsLibName)] + internal static extern int Highs_setSolution(IntPtr highs, double[] col_value, double[] row_value, double[] col_dual, double[] row_dual); + + [DllImport(HighsLibName)] + internal static extern int Highs_setSparseSolution(IntPtr highs, int num_entries, int[] index, double[] value); + + [DllImport(HighsLibName)] + internal static extern int Highs_getColsByRange( + IntPtr highs, + int from_col, + int to_col, + ref int num_col, + double[] costs, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [DllImport(HighsLibName)] + internal static extern int Highs_getColsBySet( + IntPtr highs, + int num_set_entries, + int[] set, + ref int num_col, + double[] costs, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [DllImport(HighsLibName)] + internal static extern int Highs_getColsByMask( + IntPtr highs, + int[] mask, + ref int num_col, + double[] costs, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [DllImport(HighsLibName)] + internal static extern int Highs_getRowsByRange( + IntPtr highs, + int from_row, + int to_row, + ref int num_row, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [DllImport(HighsLibName)] + internal static extern int Highs_getRowsBySet( + IntPtr highs, + int num_set_entries, + int[] set, + ref int num_row, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [DllImport(HighsLibName)] + internal static extern int Highs_getRowsByMask( + IntPtr highs, + int[] mask, + ref int num_row, + double[] lower, + double[] upper, + ref int num_nz, + int[] matrix_start, + int[] matrix_index, + double[] matrix_value); + + [DllImport(HighsLibName)] + internal static extern int Highs_getBasicVariables(IntPtr highs, int[] basic_variables); + + [DllImport(HighsLibName)] + internal static extern int Highs_getBasisInverseRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); + + [DllImport(HighsLibName)] + internal static extern int Highs_getBasisInverseCol(IntPtr highs, int column, double[] col_vector, ref int col_num_nz, int[] col_indices); + + [DllImport(HighsLibName)] + internal static extern int Highs_getBasisSolve( + IntPtr highs, + double[] rhs, + double[] solution_vector, + ref int solution_num_nz, + int[] solution_indices); + + [DllImport(HighsLibName)] + internal static extern int Highs_getBasisTransposeSolve( + IntPtr highs, + double[] rhs, + double[] solution_vector, + ref int solution_nz, + int[] solution_indices); + + [DllImport(HighsLibName)] + internal static extern int Highs_getReducedRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); + + [DllImport(HighsLibName)] + internal static extern int Highs_getReducedColumn(IntPtr highs, int column, double[] col_vector, ref int col_num_nz, int[] col_indices); + + [DllImport(HighsLibName)] + internal static extern int Highs_clearModel(IntPtr highs); + + [DllImport(HighsLibName)] + internal static extern int Highs_clearSolver(IntPtr highs); + + [DllImport(HighsLibName)] + internal static extern int Highs_passColName(IntPtr highs, int column, string name); + + [DllImport(HighsLibName)] + internal static extern int Highs_passRowName(IntPtr highs, int row, string name); + + [DllImport(HighsLibName)] + internal static extern int Highs_writeOptions(IntPtr highs, string filename); + + [DllImport(HighsLibName)] + internal static extern int Highs_writeOptionsDeviations(IntPtr highs, string filename); + #endregion \ No newline at end of file diff --git a/highs/interfaces/Highs/Solver.cs b/highs/interfaces/Highs/Solver.cs index 653196c42c..d90b6450ef 100644 --- a/highs/interfaces/Highs/Solver.cs +++ b/highs/interfaces/Highs/Solver.cs @@ -1,5 +1,4 @@ -using System.Runtime.InteropServices; -using System.Text; +using System.Text; using Highs.Enums; using Highs.Records; @@ -20,476 +19,6 @@ public class Solver : IDisposable /// private bool _disposed; - /// - /// The name of the Highs library. - /// - private const string HighsLibName = "highs"; - - #region Library Imports - [DllImport(HighsLibName)] - private static extern int Highs_lpCall( - int numcol, - int numrow, - int numnz, - int aformat, - int sense, - double offset, - double[] colcost, - double[] collower, - double[] colupper, - double[] rowlower, - double[] rowupper, - int[] astart, - int[] aindex, - double[] avalue, - double[] colvalue, - double[] coldual, - double[] rowvalue, - double[] rowdual, - int[] colbasisstatus, - int[] rowbasisstatus, - ref int modelstatus); - - [DllImport(HighsLibName)] - private static extern int Highs_mipCall( - int numcol, - int numrow, - int numnz, - int aformat, - int sense, - double offset, - double[] colcost, - double[] collower, - double[] colupper, - double[] rowlower, - double[] rowupper, - int[] astart, - int[] aindex, - double[] avalue, - int[] integrality, - double[] colvalue, - double[] rowvalue, - ref int modelstatus); - - [DllImport(HighsLibName)] - private static extern int Highs_qpCall( - int numcol, - int numrow, - int numnz, - int qnumnz, - int aformat, - int qformat, - int sense, - double offset, - double[] colcost, - double[] collower, - double[] colupper, - double[] rowlower, - double[] rowupper, - int[] astart, - int[] aindex, - double[] avalue, - int[] qstart, - int[] qindex, - double[] qvalue, - - double[] colvalue, - double[] coldual, - double[] rowvalue, - double[] rowdual, - int[] colbasisstatus, - int[] rowbasisstatus, - ref int modelstatus); - - [DllImport(HighsLibName)] - private static extern IntPtr Highs_create(); - - [DllImport(HighsLibName)] - private static extern void Highs_destroy(IntPtr highs); - - [DllImport(HighsLibName)] - private static extern int Highs_run(IntPtr highs); - - [DllImport(HighsLibName)] - private static extern int Highs_readModel(IntPtr highs, string filename); - - [DllImport(HighsLibName)] - private static extern int Highs_writeModel(IntPtr highs, string filename); - - [DllImport(HighsLibName)] - private static extern int Highs_writePresolvedModel(IntPtr highs, string filename); - - [DllImport(HighsLibName)] - private static extern int Highs_writeSolutionPretty(IntPtr highs, string filename); - - [DllImport(HighsLibName)] - private static extern int Highs_getInfinity(IntPtr highs); - - [DllImport(HighsLibName)] - private static extern int Highs_passLp( - IntPtr highs, - int numcol, - int numrow, - int numnz, - int aformat, - int sense, - double offset, - double[] colcost, - double[] collower, - double[] colupper, - double[] rowlower, - double[] rowupper, - int[] astart, - int[] aindex, - double[] avalue); - - [DllImport(HighsLibName)] - private static extern int Highs_passMip( - IntPtr highs, - int numcol, - int numrow, - int numnz, - int aformat, - int sense, - double offset, - double[] colcost, - double[] collower, - double[] colupper, - double[] rowlower, - double[] rowupper, - int[] astart, - int[] aindex, - double[] avalue, - int[] highs_integrality); - - [DllImport(HighsLibName)] - private static extern int Highs_passModel( - IntPtr highs, - int numcol, - int numrow, - int numnz, - int qnumnz, - int aformat, - int qformat, - int sense, - double offset, - double[] colcost, - double[] collower, - double[] colupper, - double[] rowlower, - double[] rowupper, - int[] astart, - int[] aindex, - double[] avalue, - int[] qstart, - int[] qindex, - double[] qvalue, - int[] highs_integrality); - - [DllImport(HighsLibName)] - private static extern int Highs_passHessian( - IntPtr highs, - int dim, - int numnz, - int q_format, - int[] qstart, - int[] qindex, - double[] qvalue); - - [DllImport(HighsLibName)] - private static extern int Highs_setOptionValue(IntPtr highs, string option, string value); - - [DllImport(HighsLibName)] - private static extern int Highs_setBoolOptionValue(IntPtr highs, string option, int value); - - [DllImport(HighsLibName)] - private static extern int Highs_setIntOptionValue(IntPtr highs, string option, int value); - - [DllImport(HighsLibName)] - private static extern int Highs_setDoubleOptionValue(IntPtr highs, string option, double value); - - [DllImport(HighsLibName)] - private static extern int Highs_setStringOptionValue(IntPtr highs, string option, string value); - - [DllImport(HighsLibName)] - private static extern int Highs_getBoolOptionValue(IntPtr highs, string option, out int value); - - [DllImport(HighsLibName)] - private static extern int Highs_getIntOptionValue(IntPtr highs, string option, out int value); - - [DllImport(HighsLibName)] - private static extern int Highs_getDoubleOptionValue(IntPtr highs, string option, out double value); - - [DllImport(HighsLibName)] - private static extern int Highs_getStringOptionValue(IntPtr highs, string option, [Out] StringBuilder value); - - [DllImport(HighsLibName)] - private static extern int Highs_getSolution(IntPtr highs, double[] colvalue, double[] coldual, double[] rowvalue, double[] rowdual); - - [DllImport(HighsLibName)] - private static extern int Highs_getNumCol(IntPtr highs); - - [DllImport(HighsLibName)] - private static extern int Highs_getNumRow(IntPtr highs); - - [DllImport(HighsLibName)] - private static extern int Highs_getNumNz(IntPtr highs); - - [DllImport(HighsLibName)] - private static extern int Highs_getHessianNumNz(IntPtr highs); - - [DllImport(HighsLibName)] - private static extern int Highs_getBasis(IntPtr highs, int[] colstatus, int[] rowstatus); - - [DllImport(HighsLibName)] - private static extern double Highs_getObjectiveValue(IntPtr highs); - - [DllImport(HighsLibName)] - private static extern int Highs_getIterationCount(IntPtr highs); - - [DllImport(HighsLibName)] - private static extern int Highs_getModelStatus(IntPtr highs); - - [DllImport(HighsLibName)] - private static extern int Highs_addRow(IntPtr highs, double lower, double upper, int num_new_nz, int[] indices, double[] values); - - [DllImport(HighsLibName)] - private static extern int Highs_addRows( - IntPtr highs, - int num_new_row, - double[] lower, - double[] upper, - int num_new_nz, - int[] starts, - int[] indices, - double[] values); - - [DllImport(HighsLibName)] - private static extern int Highs_addCol( - IntPtr highs, - double cost, - double lower, - double upper, - int num_new_nz, - int[] indices, - double[] values); - - [DllImport(HighsLibName)] - private static extern int Highs_addCols( - IntPtr highs, - int num_new_col, - double[] costs, - double[] lower, - double[] upper, - int num_new_nz, - int[] starts, - int[] indices, - double[] values); - - [DllImport(HighsLibName)] - private static extern int Highs_changeObjectiveSense(IntPtr highs, int sense); - - [DllImport(HighsLibName)] - private static extern int Highs_changeColCost(IntPtr highs, int column, double cost); - - [DllImport(HighsLibName)] - private static extern int Highs_changeColsCostBySet(IntPtr highs, int num_set_entries, int[] set, double[] cost); - - [DllImport(HighsLibName)] - private static extern int Highs_changeColsCostByMask(IntPtr highs, int[] mask, double[] cost); - - [DllImport(HighsLibName)] - private static extern int Highs_changeColBounds(IntPtr highs, int column, double lower, double upper); - - [DllImport(HighsLibName)] - private static extern int Highs_changeColsBoundsByRange(IntPtr highs, int from_col, int to_col, double[] lower, double[] upper); - - [DllImport(HighsLibName)] - private static extern int Highs_changeColsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); - - [DllImport(HighsLibName)] - private static extern int Highs_changeColsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); - - [DllImport(HighsLibName)] - private static extern int Highs_changeRowBounds(IntPtr highs, int row, double lower, double upper); - - [DllImport(HighsLibName)] - private static extern int Highs_changeRowsBoundsByRange(IntPtr highs, int from_row, int to_row, double[] lower, double[] upper); - - [DllImport(HighsLibName)] - private static extern int Highs_changeRowsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); - - [DllImport(HighsLibName)] - private static extern int Highs_changeRowsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); - - [DllImport(HighsLibName)] - private static extern int Highs_changeColsIntegralityByRange(IntPtr highs, int from_col, int to_col, int[] integrality); - - [DllImport(HighsLibName)] - private static extern int Highs_changeCoeff(IntPtr highs, int row, int column, double value); - - [DllImport(HighsLibName)] - private static extern int Highs_deleteColsByRange(IntPtr highs, int from_col, int to_col); - - [DllImport(HighsLibName)] - private static extern int Highs_deleteColsBySet(IntPtr highs, int num_set_entries, int[] set); - - [DllImport(HighsLibName)] - private static extern int Highs_deleteColsByMask(IntPtr highs, int[] mask); - - [DllImport(HighsLibName)] - private static extern int Highs_deleteRowsByRange(IntPtr highs, int from_row, int to_row); - - [DllImport(HighsLibName)] - private static extern int Highs_deleteRowsBySet(IntPtr highs, int num_set_entries, int[] set); - - [DllImport(HighsLibName)] - private static extern int Highs_deleteRowsByMask(IntPtr highs, int[] mask); - - [DllImport(HighsLibName)] - private static extern int Highs_getDoubleInfoValue(IntPtr highs, string info, out double value); - - [DllImport(HighsLibName)] - private static extern int Highs_getIntInfoValue(IntPtr highs, string info, out int value); - - [DllImport(HighsLibName)] - private static extern int Highs_getInt64InfoValue(IntPtr highs, string info, out long value); - - [DllImport(HighsLibName)] - private static extern int Highs_setSolution(IntPtr highs, double[] col_value, double[] row_value, double[] col_dual, double[] row_dual); - - [DllImport(HighsLibName)] - private static extern int Highs_setSparseSolution(IntPtr highs, int num_entries, int[] index, double[] value); - - [DllImport(HighsLibName)] - private static extern int Highs_getColsByRange( - IntPtr highs, - int from_col, - int to_col, - ref int num_col, - double[] costs, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(HighsLibName)] - private static extern int Highs_getColsBySet( - IntPtr highs, - int num_set_entries, - int[] set, - ref int num_col, - double[] costs, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(HighsLibName)] - private static extern int Highs_getColsByMask( - IntPtr highs, - int[] mask, - ref int num_col, - double[] costs, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(HighsLibName)] - private static extern int Highs_getRowsByRange( - IntPtr highs, - int from_row, - int to_row, - ref int num_row, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(HighsLibName)] - private static extern int Highs_getRowsBySet( - IntPtr highs, - int num_set_entries, - int[] set, - ref int num_row, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(HighsLibName)] - private static extern int Highs_getRowsByMask( - IntPtr highs, - int[] mask, - ref int num_row, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(HighsLibName)] - private static extern int Highs_getBasicVariables(IntPtr highs, int[] basic_variables); - - [DllImport(HighsLibName)] - private static extern int Highs_getBasisInverseRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); - - [DllImport(HighsLibName)] - private static extern int Highs_getBasisInverseCol(IntPtr highs, int column, double[] col_vector, ref int col_num_nz, int[] col_indices); - - [DllImport(HighsLibName)] - private static extern int Highs_getBasisSolve( - IntPtr highs, - double[] rhs, - double[] solution_vector, - ref int solution_num_nz, - int[] solution_indices); - - [DllImport(HighsLibName)] - private static extern int Highs_getBasisTransposeSolve( - IntPtr highs, - double[] rhs, - double[] solution_vector, - ref int solution_nz, - int[] solution_indices); - - [DllImport(HighsLibName)] - private static extern int Highs_getReducedRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); - - [DllImport(HighsLibName)] - private static extern int Highs_getReducedColumn(IntPtr highs, int column, double[] col_vector, ref int col_num_nz, int[] col_indices); - - [DllImport(HighsLibName)] - private static extern int Highs_clearModel(IntPtr highs); - - [DllImport(HighsLibName)] - private static extern int Highs_clearSolver(IntPtr highs); - - [DllImport(HighsLibName)] - private static extern int Highs_passColName(IntPtr highs, int column, string name); - - [DllImport(HighsLibName)] - private static extern int Highs_passRowName(IntPtr highs, int row, string name); - - [DllImport(HighsLibName)] - private static extern int Highs_writeOptions(IntPtr highs, string filename); - - [DllImport(HighsLibName)] - private static extern int Highs_writeOptionsDeviations(IntPtr highs, string filename); - #endregion - /// /// Calls the Highs solver in a single call of a LP. /// @@ -509,7 +38,7 @@ public static Status LpCall(Model model, ref Solution solution, out BasisInfo ba var modelstate = 0; - var status = (Status)Highs_lpCall( + var status = (Status)Imports.Highs_lpCall( numberOfColumns, numberOfRows, numberOfMatrixValues, @@ -553,7 +82,7 @@ public static Status MipCall(Model model, ref Solution solution, out ModelStatus var modelstate = 0; - var status = (Status)Highs_mipCall( + var status = (Status)Imports.Highs_mipCall( numberOfColumns, numberOfRows, numberOfMatrixValues, @@ -597,7 +126,7 @@ public static Status QPCall(Model model, ref Solution solution, out BasisInfo ba var modelstate = 0; - var status = (Status)Highs_qpCall( + var status = (Status)Imports.Highs_qpCall( numberOfColumns, numberOfRows, numberOfMatrixValues, @@ -634,7 +163,7 @@ public static Status QPCall(Model model, ref Solution solution, out BasisInfo ba /// /// The default constructor. /// - public Solver() => _highs = Highs_create(); + public Solver() => _highs = Imports.Highs_create(); /// /// The destructor. @@ -660,7 +189,7 @@ protected virtual void Dispose(bool disposing) { return; } - Highs_destroy(_highs); + Imports.Highs_destroy(_highs); _disposed = true; } @@ -668,41 +197,41 @@ protected virtual void Dispose(bool disposing) /// Runs the solver. /// /// - public Status Run() => (Status)Highs_run(_highs); + public Status Run() => (Status)Imports.Highs_run(_highs); /// /// Reads a model from file. /// /// /// - public Status ReadModel(string filename) => (Status)Highs_readModel(_highs, filename); + public Status ReadModel(string filename) => (Status)Imports.Highs_readModel(_highs, filename); /// /// Writes the model to file. /// /// /// - public Status WriteModel(string filename) => (Status)Highs_writeModel(_highs, filename); + public Status WriteModel(string filename) => (Status)Imports.Highs_writeModel(_highs, filename); /// /// Writes the presolved model to file. /// /// /// - public Status WritePresolvedModel(string filename) => (Status)Highs_writePresolvedModel(_highs, filename); + public Status WritePresolvedModel(string filename) => (Status)Imports.Highs_writePresolvedModel(_highs, filename); /// /// Writes the solution to file in a pretty format. /// /// /// - public Status WriteSolutionPretty(string filename) => (Status)Highs_writeSolutionPretty(_highs, filename); + public Status WriteSolutionPretty(string filename) => (Status)Imports.Highs_writeSolutionPretty(_highs, filename); /// /// Gets the infinity value. /// /// - public double GetInfinity() => Highs_getInfinity(_highs); + public double GetInfinity() => Imports.Highs_getInfinity(_highs); /// /// Passes the LP model to Highs. @@ -711,7 +240,7 @@ protected virtual void Dispose(bool disposing) /// public Status PassLp(Model model) { - return (Status)Highs_passLp( + return (Status)Imports.Highs_passLp( _highs, model.ColumnCost.Length, model.RowLower.Length, @@ -736,7 +265,7 @@ public Status PassLp(Model model) /// public Status PassMip(Model model) { - return (Status)Highs_passMip( + return (Status)Imports.Highs_passMip( _highs, model.ColumnCost.Length, model.RowLower.Length, @@ -761,7 +290,7 @@ public Status PassMip(Model model) /// /// /// - public Status SetOptionValue(string option, string value) => (Status)Highs_setOptionValue(_highs, option, value); + public Status SetOptionValue(string option, string value) => (Status)Imports.Highs_setOptionValue(_highs, option, value); /// /// Sets the string option value. @@ -769,7 +298,7 @@ public Status PassMip(Model model) /// /// /// - public Status SetStringOptionValue(string option, string value) => (Status)Highs_setStringOptionValue(_highs, option, value); + public Status SetStringOptionValue(string option, string value) => (Status)Imports.Highs_setStringOptionValue(_highs, option, value); /// /// Sets the boolean option value. @@ -777,7 +306,7 @@ public Status PassMip(Model model) /// /// /// - public Status SetBoolOptionValue(string option, int value) => (Status)Highs_setBoolOptionValue(_highs, option, value); + public Status SetBoolOptionValue(string option, int value) => (Status)Imports.Highs_setBoolOptionValue(_highs, option, value); /// /// Sets the double option value. @@ -785,7 +314,7 @@ public Status PassMip(Model model) /// /// /// - public Status SetDoubleOptionValue(string option, double value) => (Status)Highs_setDoubleOptionValue(_highs, option, value); + public Status SetDoubleOptionValue(string option, double value) => (Status)Imports.Highs_setDoubleOptionValue(_highs, option, value); /// /// Sets the integer option value. @@ -793,7 +322,7 @@ public Status PassMip(Model model) /// /// /// - public Status SetIntOptionValue(string option, int value) => (Status)Highs_setIntOptionValue(_highs, option, value); + public Status SetIntOptionValue(string option, int value) => (Status)Imports.Highs_setIntOptionValue(_highs, option, value); /// /// Gets the string option value. @@ -804,7 +333,7 @@ public Status PassMip(Model model) public Status GetStringOptionValue(string option, out string value) { var stringBuilder = new StringBuilder(); - var result = (Status)Highs_getStringOptionValue(_highs, option, stringBuilder); + var result = (Status)Imports.Highs_getStringOptionValue(_highs, option, stringBuilder); value = stringBuilder.ToString(); return result; } @@ -815,7 +344,7 @@ public Status GetStringOptionValue(string option, out string value) /// /// /// - public Status GetBoolOptionValue(string option, out int value) => (Status)Highs_getBoolOptionValue(_highs, option, out value); + public Status GetBoolOptionValue(string option, out int value) => (Status)Imports.Highs_getBoolOptionValue(_highs, option, out value); /// /// Gets the double option value. @@ -823,7 +352,7 @@ public Status GetStringOptionValue(string option, out string value) /// /// /// - public Status GetDoubleOptionValue(string option, out double value) => (Status)Highs_getDoubleOptionValue(_highs, option, out value); + public Status GetDoubleOptionValue(string option, out double value) => (Status)Imports.Highs_getDoubleOptionValue(_highs, option, out value); /// /// Gets the integer option value. @@ -831,25 +360,25 @@ public Status GetStringOptionValue(string option, out string value) /// /// /// - public Status GetIntOptionValue(string option, out int value) => (Status)Highs_getIntOptionValue(_highs, option, out value); + public Status GetIntOptionValue(string option, out int value) => (Status)Imports.Highs_getIntOptionValue(_highs, option, out value); /// /// Gets the number of columns. /// /// - public int GetNumberOfColumns() => Highs_getNumCol(_highs); + public int GetNumberOfColumns() => Imports.Highs_getNumCol(_highs); /// /// Gets the number of rows. /// /// - public int GetNumberOfRows() => Highs_getNumRow(_highs); + public int GetNumberOfRows() => Imports.Highs_getNumRow(_highs); /// /// Gets the number of non-zero entries. /// /// - public int GetNumberOfNonZeroEntries() => Highs_getNumNz(_highs); + public int GetNumberOfNonZeroEntries() => Imports.Highs_getNumNz(_highs); /// /// Gets the solution. @@ -862,7 +391,7 @@ public Status GetSolution(out Solution solution) var numberOfRows = GetNumberOfRows(); solution = new Solution(numberOfColumns, numberOfRows); - return (Status)Highs_getSolution(_highs, solution.ColumnValue, solution.ColumnDual, solution.RowValue, solution.RowDual); + return (Status)Imports.Highs_getSolution(_highs, solution.ColumnValue, solution.ColumnDual, solution.RowValue, solution.RowDual); } /// @@ -872,7 +401,7 @@ public Status GetSolution(out Solution solution) /// public Status PassHessian(Hessian hessian) { - return (Status)Highs_passHessian(_highs, + return (Status)Imports.Highs_passHessian(_highs, hessian.Dimension, hessian.Values.Length, (int)hessian.HessianFormat, @@ -893,7 +422,7 @@ public Status GetBasis(out BasisInfo basisInfo) var columnBasisStatus = new int[numberOfColumns]; var rowBasisStatus = new int[numberOfRows]; - var status = (Status)Highs_getBasis(_highs, columnBasisStatus, rowBasisStatus); + var status = (Status)Imports.Highs_getBasis(_highs, columnBasisStatus, rowBasisStatus); if (status == Status.Error) { basisInfo = null; @@ -909,19 +438,19 @@ public Status GetBasis(out BasisInfo basisInfo) /// Gets the objective value. /// /// - public double GetObjectiveValue() => Highs_getObjectiveValue(_highs); + public double GetObjectiveValue() => Imports.Highs_getObjectiveValue(_highs); /// /// Gets the model status. /// /// - public ModelStatus GetModelStatus() => (ModelStatus)Highs_getModelStatus(_highs); + public ModelStatus GetModelStatus() => (ModelStatus)Imports.Highs_getModelStatus(_highs); /// /// Gets the iteration count. /// /// - public int GetIterationCount() => Highs_getIterationCount(_highs); + public int GetIterationCount() => Imports.Highs_getIterationCount(_highs); /// /// Adds a row to the model. @@ -933,7 +462,7 @@ public Status GetBasis(out BasisInfo basisInfo) /// public Status AddRow(double lower, double upper, int[] indices, double[] values) { - return (Status)Highs_addRow(_highs, lower, upper, indices.Length, indices, values); + return (Status)Imports.Highs_addRow(_highs, lower, upper, indices.Length, indices, values); } /// @@ -947,7 +476,7 @@ public Status AddRow(double lower, double upper, int[] indices, double[] values) /// public Status AddRows(double[] lower, double[] upper, int[] starts, int[] indices, double[] values) { - return (Status)Highs_addRows(_highs, lower.Length, lower, upper, indices.Length, starts, indices, values); + return (Status)Imports.Highs_addRows(_highs, lower.Length, lower, upper, indices.Length, starts, indices, values); } /// @@ -961,7 +490,7 @@ public Status AddRows(double[] lower, double[] upper, int[] starts, int[] indice /// public Status AddColumn(double cost, double lower, double upper, int[] indices, double[] values) { - return (Status)Highs_addCol(_highs, cost, lower, upper, indices.Length, indices, values); + return (Status)Imports.Highs_addCol(_highs, cost, lower, upper, indices.Length, indices, values); } /// @@ -976,7 +505,7 @@ public Status AddColumn(double cost, double lower, double upper, int[] indices, /// public Status AddColumns(double[] costs, double[] lower, double[] upper, int[] starts, int[] indices, double[] values) { - return (Status)Highs_addCols( + return (Status)Imports.Highs_addCols( _highs, costs.Length, costs, @@ -993,7 +522,7 @@ public Status AddColumns(double[] costs, double[] lower, double[] upper, int[] s /// /// /// - public Status ChangeObjectiveSense(ObjectiveSense sense) => (Status)Highs_changeObjectiveSense(_highs, (int)sense); + public Status ChangeObjectiveSense(ObjectiveSense sense) => (Status)Imports.Highs_changeObjectiveSense(_highs, (int)sense); /// /// Changes the cost of a column. @@ -1001,7 +530,7 @@ public Status AddColumns(double[] costs, double[] lower, double[] upper, int[] s /// /// /// - public Status ChangeColumnCost(int column, double cost) => (Status)Highs_changeColCost(_highs, column, cost); + public Status ChangeColumnCost(int column, double cost) => (Status)Imports.Highs_changeColCost(_highs, column, cost); /// /// Changes the costs of multiple columns by set. @@ -1009,7 +538,7 @@ public Status AddColumns(double[] costs, double[] lower, double[] upper, int[] s /// /// /// - public Status ChangeColumnsCostBySet(int[] columns, double[] costs) => (Status)Highs_changeColsCostBySet(_highs, columns.Length, columns, costs); + public Status ChangeColumnsCostBySet(int[] columns, double[] costs) => (Status)Imports.Highs_changeColsCostBySet(_highs, columns.Length, columns, costs); /// /// Changes the costs of multiple columns by mask. @@ -1017,7 +546,7 @@ public Status AddColumns(double[] costs, double[] lower, double[] upper, int[] s /// /// /// - public Status ChangeColumnsCostByMask(bool[] mask, double[] cost) => (Status)Highs_changeColsCostByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0), cost); + public Status ChangeColumnsCostByMask(bool[] mask, double[] cost) => (Status)Imports.Highs_changeColsCostByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0), cost); /// /// Changes the bounds of a column. @@ -1026,7 +555,7 @@ public Status AddColumns(double[] costs, double[] lower, double[] upper, int[] s /// /// /// - public Status ChangeColumnBounds(int column, double lower, double upper) => (Status)Highs_changeColBounds(_highs, column, lower, upper); + public Status ChangeColumnBounds(int column, double lower, double upper) => (Status)Imports.Highs_changeColBounds(_highs, column, lower, upper); /// /// Changes the bounds of multiple columns by range. @@ -1036,7 +565,7 @@ public Status AddColumns(double[] costs, double[] lower, double[] upper, int[] s /// /// /// - public Status ChangeColumnsBoundsByRange(int from, int to, double[] lower, double[] upper) => (Status)Highs_changeColsBoundsByRange(_highs, from, to, lower, upper); + public Status ChangeColumnsBoundsByRange(int from, int to, double[] lower, double[] upper) => (Status)Imports.Highs_changeColsBoundsByRange(_highs, from, to, lower, upper); /// /// Changes the bounds of multiple columns by set. @@ -1047,7 +576,7 @@ public Status AddColumns(double[] costs, double[] lower, double[] upper, int[] s /// public Status ChangeColumnsBoundsBySet(int[] columns, double[] lower, double[] upper) { - return (Status)Highs_changeColsBoundsBySet(_highs, columns.Length, columns, lower, upper); + return (Status)Imports.Highs_changeColsBoundsBySet(_highs, columns.Length, columns, lower, upper); } /// @@ -1059,7 +588,7 @@ public Status ChangeColumnsBoundsBySet(int[] columns, double[] lower, double[] u /// public Status ChangeColumnsBoundsByMask(bool[] mask, double[] lower, double[] upper) { - return (Status)Highs_changeColsBoundsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0), lower, upper); + return (Status)Imports.Highs_changeColsBoundsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0), lower, upper); } /// @@ -1069,7 +598,7 @@ public Status ChangeColumnsBoundsByMask(bool[] mask, double[] lower, double[] up /// /// /// - public Status ChangeRowBounds(int row, double lower, double upper) => (Status)Highs_changeRowBounds(_highs, row, lower, upper); + public Status ChangeRowBounds(int row, double lower, double upper) => (Status)Imports.Highs_changeRowBounds(_highs, row, lower, upper); /// /// Changes the bounds of multiple rows by range. @@ -1078,7 +607,7 @@ public Status ChangeColumnsBoundsByMask(bool[] mask, double[] lower, double[] up /// /// /// - public Status ChangeRowsBoundsBySet(int[] rows, double[] lower, double[] upper) => (Status)Highs_changeRowsBoundsBySet(_highs, rows.Length, rows, lower, upper); + public Status ChangeRowsBoundsBySet(int[] rows, double[] lower, double[] upper) => (Status)Imports.Highs_changeRowsBoundsBySet(_highs, rows.Length, rows, lower, upper); /// /// Changes the bounds of multiple rows by mask. @@ -1087,7 +616,7 @@ public Status ChangeColumnsBoundsByMask(bool[] mask, double[] lower, double[] up /// /// /// - public Status ChangeRowsBoundsByMask(bool[] mask, double[] lower, double[] upper) => (Status)Highs_changeRowsBoundsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0), lower, upper); + public Status ChangeRowsBoundsByMask(bool[] mask, double[] lower, double[] upper) => (Status)Imports.Highs_changeRowsBoundsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0), lower, upper); /// /// Changes the integrality of multiple columns by range. @@ -1098,7 +627,7 @@ public Status ChangeColumnsBoundsByMask(bool[] mask, double[] lower, double[] up /// public Status ChangeColumnsIntegralityByRange(int from_col, int to_col, VariableType[] integrality) { - return (Status)Highs_changeColsIntegralityByRange(_highs, from_col, to_col, Array.ConvertAll(integrality, item => (int)item)); + return (Status)Imports.Highs_changeColsIntegralityByRange(_highs, from_col, to_col, Array.ConvertAll(integrality, item => (int)item)); } /// @@ -1108,7 +637,7 @@ public Status ChangeColumnsIntegralityByRange(int from_col, int to_col, Variable /// /// /// - public Status ChangeCoefficient(int row, int column, double value) => (Status)Highs_changeCoeff(_highs, row, column, value); + public Status ChangeCoefficient(int row, int column, double value) => (Status)Imports.Highs_changeCoeff(_highs, row, column, value); /// /// Deletes multiple columns by range. @@ -1116,21 +645,21 @@ public Status ChangeColumnsIntegralityByRange(int from_col, int to_col, Variable /// /// /// - public Status DeleteColumnsByRange(int from, int to) => (Status)Highs_deleteColsByRange(_highs, from, to); + public Status DeleteColumnsByRange(int from, int to) => (Status)Imports.Highs_deleteColsByRange(_highs, from, to); /// /// Deletes multiple columns by set. /// /// /// - public Status DeleteColumnsBySet(int[] columns) => (Status)Highs_deleteColsBySet(_highs, columns.Length, columns); + public Status DeleteColumnsBySet(int[] columns) => (Status)Imports.Highs_deleteColsBySet(_highs, columns.Length, columns); /// /// Deletes multiple columns by mask. /// /// /// - public Status DeleteColumnsByMask(bool[] mask) => (Status)Highs_deleteColsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0)); + public Status DeleteColumnsByMask(bool[] mask) => (Status)Imports.Highs_deleteColsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0)); /// /// Deletes multiple rows by range. @@ -1138,21 +667,21 @@ public Status ChangeColumnsIntegralityByRange(int from_col, int to_col, Variable /// /// /// - public Status DeleteRowsByRange(int from, int to) => (Status)Highs_deleteRowsByRange(_highs, from, to); + public Status DeleteRowsByRange(int from, int to) => (Status)Imports.Highs_deleteRowsByRange(_highs, from, to); /// /// Deletes multiple rows by set. /// /// /// - public Status DeleteRowsBySet(int[] rows) => (Status)Highs_deleteRowsBySet(_highs, rows.Length, rows); + public Status DeleteRowsBySet(int[] rows) => (Status)Imports.Highs_deleteRowsBySet(_highs, rows.Length, rows); /// /// Deletes multiple rows by mask. /// /// /// - public Status DeleteRowsByMask(bool[] mask) => (Status)Highs_deleteRowsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0)); + public Status DeleteRowsByMask(bool[] mask) => (Status)Imports.Highs_deleteRowsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0)); /// /// Delegate for getting info values. @@ -1192,13 +721,13 @@ private TValue GetValueOrFallback(HighsGetInfoDelegate highsGetI public SolutionInfo GetInfo() { // TODO: This object does not contian the "complete" info from the C api. Add further props, if you need them. - return new SolutionInfo(GetValueOrFallback(Highs_getIntInfoValue, "simplex_iteration_count", 0), - GetValueOrFallback(Highs_getIntInfoValue, "ipm_iteration_count", 0), - GetValueOrFallback(Highs_getIntInfoValue, "pdlp_iteration_count", 0), - GetValueOrFallback(Highs_getDoubleInfoValue, "mip_gap", double.NaN), - GetValueOrFallback(Highs_getDoubleInfoValue, "mip_dual_bound", double.NaN), - GetValueOrFallback(Highs_getInt64InfoValue, "mip_node_count", 0L), - GetValueOrFallback(Highs_getDoubleInfoValue, "objective_function_value", double.NaN)); + return new SolutionInfo(GetValueOrFallback(Imports.Highs_getIntInfoValue, "simplex_iteration_count", 0), + GetValueOrFallback(Imports.Highs_getIntInfoValue, "ipm_iteration_count", 0), + GetValueOrFallback(Imports.Highs_getIntInfoValue, "pdlp_iteration_count", 0), + GetValueOrFallback(Imports.Highs_getDoubleInfoValue, "mip_gap", double.NaN), + GetValueOrFallback(Imports.Highs_getDoubleInfoValue, "mip_dual_bound", double.NaN), + GetValueOrFallback(Imports.Highs_getInt64InfoValue, "mip_node_count", 0L), + GetValueOrFallback(Imports.Highs_getDoubleInfoValue, "objective_function_value", double.NaN)); } /// @@ -1206,7 +735,7 @@ public SolutionInfo GetInfo() /// /// /// - public Status SetSolution(Solution solution) => (Status)Highs_setSolution(_highs, solution.ColumnValue, solution.ColumnDual, solution.RowValue, solution.RowDual); + public Status SetSolution(Solution solution) => (Status)Imports.Highs_setSolution(_highs, solution.ColumnValue, solution.ColumnDual, solution.RowValue, solution.RowDual); /// /// Set a partial primal solution by passing values for a set of variables @@ -1220,7 +749,7 @@ public SolutionInfo GetInfo() /// public Status SetSparseSolution(IReadOnlyDictionary valuesByIndex) { - return (Status)Highs_setSparseSolution(_highs, valuesByIndex.Count, [.. valuesByIndex.Keys], [.. valuesByIndex.Values]); + return (Status)Imports.Highs_setSparseSolution(_highs, valuesByIndex.Count, [.. valuesByIndex.Keys], [.. valuesByIndex.Values]); } /// @@ -1228,7 +757,7 @@ public Status SetSparseSolution(IReadOnlyDictionary valuesByIndex) /// /// /// - public Status GetBasicVariables(ref int[] basic_variables) => (Status)Highs_getBasicVariables(_highs, basic_variables); + public Status GetBasicVariables(ref int[] basic_variables) => (Status)Imports.Highs_getBasicVariables(_highs, basic_variables); /// /// Gets a row of the basis inverse. @@ -1240,7 +769,7 @@ public Status SetSparseSolution(IReadOnlyDictionary valuesByIndex) /// public Status GetBasisInverseRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) { - return (Status)Highs_getBasisInverseRow(_highs, row, row_vector, ref row_num_nz, row_indices); + return (Status)Imports.Highs_getBasisInverseRow(_highs, row, row_vector, ref row_num_nz, row_indices); } /// @@ -1253,7 +782,7 @@ public Status GetBasisInverseRow(int row, double[] row_vector, ref int row_num_n /// public Status GetBasisInverseColumn(int column, double[] column_vector, ref int column_num_nz, int[] column_indices) { - return (Status)Highs_getBasisInverseCol(_highs, column, column_vector, ref column_num_nz, column_indices); + return (Status)Imports.Highs_getBasisInverseCol(_highs, column, column_vector, ref column_num_nz, column_indices); } /// @@ -1266,7 +795,7 @@ public Status GetBasisInverseColumn(int column, double[] column_vector, ref int /// public Status GetBasisSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) { - return (Status)Highs_getBasisSolve(_highs, rhs, solution_vector, ref solution_num_nz, solution_indices); + return (Status)Imports.Highs_getBasisSolve(_highs, rhs, solution_vector, ref solution_num_nz, solution_indices); } /// @@ -1279,7 +808,7 @@ public Status GetBasisSolve(double[] rhs, double[] solution_vector, ref int solu /// public Status GetBasisTransposeSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) { - return (Status)Highs_getBasisTransposeSolve(_highs, rhs, solution_vector, ref solution_num_nz, solution_indices); + return (Status)Imports.Highs_getBasisTransposeSolve(_highs, rhs, solution_vector, ref solution_num_nz, solution_indices); } /// @@ -1292,7 +821,7 @@ public Status GetBasisTransposeSolve(double[] rhs, double[] solution_vector, ref /// public Status GetReducedRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) { - return (Status)Highs_getReducedRow(_highs, row, row_vector, ref row_num_nz, row_indices); + return (Status)Imports.Highs_getReducedRow(_highs, row, row_vector, ref row_num_nz, row_indices); } /// @@ -1305,20 +834,20 @@ public Status GetReducedRow(int row, double[] row_vector, ref int row_num_nz, in /// public Status GetReducedColumn(int column, double[] column_vector, ref int column_num_nz, int[] column_indices) { - return (Status)Highs_getReducedColumn(_highs, column, column_vector, ref column_num_nz, column_indices); + return (Status)Imports.Highs_getReducedColumn(_highs, column, column_vector, ref column_num_nz, column_indices); } /// /// Clears the model. /// /// - public Status ClearModel() => (Status)Highs_clearModel(_highs); + public Status ClearModel() => (Status)Imports.Highs_clearModel(_highs); /// /// Clears the solver. /// /// - public Status ClearSolver() => (Status)Highs_clearSolver(_highs); + public Status ClearSolver() => (Status)Imports.Highs_clearSolver(_highs); /// /// Passes the name of a column. @@ -1326,7 +855,7 @@ public Status GetReducedColumn(int column, double[] column_vector, ref int colum /// /// /// - public Status PassColumnName(int column, string name) => (Status)Highs_passColName(_highs, column, name); + public Status PassColumnName(int column, string name) => (Status)Imports.Highs_passColName(_highs, column, name); /// /// Passes the name of a row. @@ -1334,19 +863,19 @@ public Status GetReducedColumn(int column, double[] column_vector, ref int colum /// /// /// - public Status PassRowName(int row, string name) => (Status)Highs_passRowName(_highs, row, name); + public Status PassRowName(int row, string name) => (Status)Imports.Highs_passRowName(_highs, row, name); /// /// Writes the options to file. /// /// /// - public Status WriteOptions(string filename) => (Status)Highs_writeOptions(_highs, filename); + public Status WriteOptions(string filename) => (Status)Imports.Highs_writeOptions(_highs, filename); /// /// Writes the options deviations to file. /// /// /// - public Status WriteOptionsDeviations(string filename) => (Status)Highs_writeOptionsDeviations(_highs, filename); + public Status WriteOptionsDeviations(string filename) => (Status)Imports.Highs_writeOptionsDeviations(_highs, filename); } From a2354286168f60756fc7d6907229e7ccd71c21c7 Mon Sep 17 00:00:00 2001 From: Thiago Novaes <77932135+Thiago-NovaesB@users.noreply.github.com> Date: Fri, 26 Dec 2025 17:03:11 +0100 Subject: [PATCH 7/9] delete --- highs/interfaces/highs_csharp_api.cs | 1142 -------------------------- 1 file changed, 1142 deletions(-) delete mode 100644 highs/interfaces/highs_csharp_api.cs diff --git a/highs/interfaces/highs_csharp_api.cs b/highs/interfaces/highs_csharp_api.cs deleted file mode 100644 index a15a13c975..0000000000 --- a/highs/interfaces/highs_csharp_api.cs +++ /dev/null @@ -1,1142 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices; -using System.Text; - -// mcs -out:highscslib.dll -t:library highs_csharp_api.cs -unsafe - -namespace Highs { -public enum HighsStatus -{ - kError = -1, - kOk, - kWarning -} - -public enum HighsMatrixFormat -{ - kColwise = 1, - kRowwise -} - -public enum HessianFormat -{ - kTriangular = 1, - kSquare -} - -public enum HighsBasisStatus -{ - kLower = 0, - kBasic, - kUpper, - kZero, - kNonbasic -} - -public enum HighsObjectiveSense -{ - kMinimize = 1, - kMaximize = -1 -} - -public enum HighsModelStatus -{ - kNotset = 0, - kLoadError, - kModelError, - kPresolveError, - kSolveError, - kPostsolveError, - kModelEmpty, - kOptimal, - kInfeasible, - kUnboundedOrInfeasible, - kUnbounded, - kObjectiveBound, - kObjectiveTarget, - kTimeLimit, - kIterationLimit, - kUnknown, - kSolutionLimit, - kInterrupt, - kMemoryLimit, - kHighsInterrupt -} - -public enum HighsIntegrality -{ - kContinuous = 0, - kInteger = 1, - kSemiContinuous = 2, - kSemiInteger = 3, - kImplicitInteger = 4, -} - -public class HighsModel -{ - public HighsObjectiveSense sense; - public double[] colcost; - public double offset; - public double[] collower; - public double[] colupper; - public double[] rowlower; - public double[] rowupper; - public HighsMatrixFormat a_format; - public int[] astart; - public int[] aindex; - public double[] avalue; - public int[] highs_integrality; - - public HighsModel() - { - - } - - public HighsModel(double[] colcost, double[] collower, double[] colupper, double[] rowlower, double[] rowupper, - int[] astart, int[] aindex, double[] avalue, int[] highs_integrality = null, double offset = 0, HighsMatrixFormat a_format = HighsMatrixFormat.kColwise, HighsObjectiveSense sense = HighsObjectiveSense.kMinimize) - { - this.colcost = colcost; - this.collower = collower; - this.colupper = colupper; - this.rowlower = rowlower; - this.rowupper = rowupper; - this.astart = astart; - this.aindex = aindex; - this.avalue = avalue; - this.offset = offset; - this.a_format = a_format; - this.sense = sense; - this.highs_integrality = highs_integrality; - } -} - -public class HighsHessian -{ - public HessianFormat q_format; - public int dim; - public int[] qstart; - public int[] qindex; - public double[] qvalue; - - public HighsHessian() - { - - } - - public HighsHessian(int dim, int[] qstart, int[] qindex, double[] qvalue, HessianFormat q_format = HessianFormat.kTriangular) - { - this.dim = dim; - this.qstart = qstart; - this.qindex = qindex; - this.qvalue = qvalue; - this.q_format = q_format; - } -} - -public class HighsSolution -{ - public double[] colvalue; - public double[] coldual; - public double[] rowvalue; - public double[] rowdual; - - public HighsSolution(int numcol, int numrow) - { - this.colvalue = new double[numcol]; - this.coldual = new double[numcol]; - this.rowvalue = new double[numrow]; - this.rowdual = new double[numrow]; - } - - public HighsSolution(double[] colvalue, double[] coldual, double[] rowvalue, double[] rowdual) - { - this.colvalue = colvalue; - this.coldual = coldual; - this.rowvalue = rowvalue; - this.rowdual = rowdual; - } -} - -public class HighsBasis -{ - public HighsBasisStatus[] colbasisstatus; - public HighsBasisStatus[] rowbasisstatus; - - public HighsBasis(int numcol, int numrow) - { - this.colbasisstatus = new HighsBasisStatus[numcol]; - this.rowbasisstatus = new HighsBasisStatus[numrow]; - } - - public HighsBasis(HighsBasisStatus[] colbasisstatus, HighsBasisStatus[] rowbasisstatus) - { - this.colbasisstatus = colbasisstatus; - this.rowbasisstatus = rowbasisstatus; - } -} - -public class HighsLpSolver : IDisposable -{ - private IntPtr highs; - - private bool _disposed; - - private const string highslibname = "highs"; - - [DllImport(highslibname)] - private static extern int Highs_call( - Int32 numcol, - Int32 numrow, - Int32 numnz, - double[] colcost, - double[] collower, - double[] colupper, - double[] rowlower, - double[] rowupper, - int[] astart, - int[] aindex, - double[] avalue, - double[] colvalue, - double[] coldual, - double[] rowvalue, - double[] rowdual, - int[] colbasisstatus, - int[] rowbasisstatus, - ref int modelstatus); - - [DllImport(highslibname)] - private static extern IntPtr Highs_create(); - - [DllImport(highslibname)] - private static extern void Highs_destroy(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_run(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_readModel(IntPtr highs, string filename); - - [DllImport(highslibname)] - private static extern int Highs_writeModel(IntPtr highs, string filename); - - [DllImport(highslibname)] - private static extern int Highs_writePresolvedModel(IntPtr highs, string filename); - - [DllImport(highslibname)] - private static extern int Highs_writeSolutionPretty(IntPtr highs, string filename); - - [DllImport(highslibname)] - private static extern int Highs_getInfinity(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_passLp( - IntPtr highs, - int numcol, - int numrow, - int numnz, - int aformat, - int sense, - double offset, - double[] colcost, - double[] collower, - double[] colupper, - double[] rowlower, - double[] rowupper, - int[] astart, - int[] aindex, - double[] avalue); - - [DllImport(highslibname)] - private static extern int Highs_passMip( - IntPtr highs, - int numcol, - int numrow, - int numnz, - int aformat, - int sense, - double offset, - double[] colcost, - double[] collower, - double[] colupper, - double[] rowlower, - double[] rowupper, - int[] astart, - int[] aindex, - double[] avalue, - int[] highs_integrality); - - [DllImport(highslibname)] - private static extern int Highs_passModel( - IntPtr highs, - int numcol, - int numrow, - int numnz, - int qnumnz, - int aformat, - int qformat, - int sense, - double offset, - double[] colcost, - double[] collower, - double[] colupper, - double[] rowlower, - double[] rowupper, - int[] astart, - int[] aindex, - double[] avalue, - int[] qstart, - int[] qindex, - double[] qvalue, - int[] highs_integrality); - - [DllImport(highslibname)] - private static extern int Highs_passHessian( - IntPtr highs, - int dim, - int numnz, - int q_format, - int[] qstart, - int[] qindex, - double[] qvalue); - - [DllImport(highslibname)] - private static extern int Highs_setOptionValue(IntPtr highs, string option, string value); - - [DllImport(highslibname)] - private static extern int Highs_setBoolOptionValue(IntPtr highs, string option, int value); - - [DllImport(highslibname)] - private static extern int Highs_setIntOptionValue(IntPtr highs, string option, int value); - - [DllImport(highslibname)] - private static extern int Highs_setDoubleOptionValue(IntPtr highs, string option, double value); - - [DllImport(highslibname)] - private static extern int Highs_setStringOptionValue(IntPtr highs, string option, string value); - - [DllImport(highslibname)] - private static extern int Highs_getBoolOptionValue(IntPtr highs, string option, out int value); - - [DllImport(highslibname)] - private static extern int Highs_getIntOptionValue(IntPtr highs, string option, out int value); - - [DllImport(highslibname)] - private static extern int Highs_getDoubleOptionValue(IntPtr highs, string option, out double value); - - [DllImport(highslibname)] - private static extern int Highs_getStringOptionValue(IntPtr highs, string option, [Out] StringBuilder value); - - [DllImport(highslibname)] - private static extern int Highs_getSolution(IntPtr highs, double[] colvalue, double[] coldual, double[] rowvalue, double[] rowdual); - - [DllImport(highslibname)] - private static extern int Highs_getNumCol(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_getNumRow(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_getNumNz(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_getHessianNumNz(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_getBasis(IntPtr highs, int[] colstatus, int[] rowstatus); - - [DllImport(highslibname)] - private static extern double Highs_getObjectiveValue(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_getIterationCount(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_getModelStatus(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_addRow(IntPtr highs, double lower, double upper, int num_new_nz, int[] indices, double[] values); - - [DllImport(highslibname)] - private static extern int Highs_addRows( - IntPtr highs, - int num_new_row, - double[] lower, - double[] upper, - int num_new_nz, - int[] starts, - int[] indices, - double[] values); - - [DllImport(highslibname)] - private static extern int Highs_addCol( - IntPtr highs, - double cost, - double lower, - double upper, - int num_new_nz, - int[] indices, - double[] values); - - [DllImport(highslibname)] - private static extern int Highs_addCols( - IntPtr highs, - int num_new_col, - double[] costs, - double[] lower, - double[] upper, - int num_new_nz, - int[] starts, - int[] indices, - double[] values); - - [DllImport(highslibname)] - private static extern int Highs_changeObjectiveSense(IntPtr highs, int sense); - - [DllImport(highslibname)] - private static extern int Highs_changeColCost(IntPtr highs, int col, double cost); - - [DllImport(highslibname)] - private static extern int Highs_changeColsCostBySet(IntPtr highs, int num_set_entries, int[] set, double[] cost); - - [DllImport(highslibname)] - private static extern int Highs_changeColsCostByMask(IntPtr highs, int[] mask, double[] cost); - - [DllImport(highslibname)] - private static extern int Highs_changeColBounds(IntPtr highs, int col, double lower, double upper); - - [DllImport(highslibname)] - private static extern int Highs_changeColsBoundsByRange(IntPtr highs, int from_col, int to_col, double[] lower, double[] upper); - - [DllImport(highslibname)] - private static extern int Highs_changeColsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); - - [DllImport(highslibname)] - private static extern int Highs_changeColsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); - - [DllImport(highslibname)] - private static extern int Highs_changeRowBounds(IntPtr highs, int row, double lower, double upper); - - [DllImport(highslibname)] - private static extern int Highs_changeRowsBoundsByRange(IntPtr highs, int from_row, int to_row, double[] lower, double[] upper); - - [DllImport(highslibname)] - private static extern int Highs_changeRowsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); - - [DllImport(highslibname)] - private static extern int Highs_changeRowsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); - - [DllImport(highslibname)] - private static extern int Highs_changeColsIntegralityByRange(IntPtr highs, int from_col, int to_col, int[] integrality); - - [DllImport(highslibname)] - private static extern int Highs_changeCoeff(IntPtr highs, int row, int col, double value); - - [DllImport(highslibname)] - private static extern int Highs_deleteColsByRange(IntPtr highs, int from_col, int to_col); - - [DllImport(highslibname)] - private static extern int Highs_deleteColsBySet(IntPtr highs, int num_set_entries, int[] set); - - [DllImport(highslibname)] - private static extern int Highs_deleteColsByMask(IntPtr highs, int[] mask); - - [DllImport(highslibname)] - private static extern int Highs_deleteRowsByRange(IntPtr highs, int from_row, int to_row); - - [DllImport(highslibname)] - private static extern int Highs_deleteRowsBySet(IntPtr highs, int num_set_entries, int[] set); - - [DllImport(highslibname)] - private static extern int Highs_deleteRowsByMask(IntPtr highs, int[] mask); - - [DllImport(highslibname)] - private static extern int Highs_getDoubleInfoValue(IntPtr highs, string info, out double value); - - [DllImport(highslibname)] - private static extern int Highs_getIntInfoValue(IntPtr highs, string info, out int value); - - [DllImport(highslibname)] - private static extern int Highs_getInt64InfoValue(IntPtr highs, string info, out long value); - - [DllImport(highslibname)] - private static extern int Highs_setSolution(IntPtr highs, double[] col_value, double[] row_value, double[] col_dual, double[] row_dual); - - [DllImport(highslibname)] - private static extern int Highs_setSparseSolution(IntPtr highs, int num_entries, int[] index, double[] value); - - [DllImport(highslibname)] - private static extern int Highs_getColsByRange( - IntPtr highs, - int from_col, - int to_col, - ref int num_col, - double[] costs, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(highslibname)] - private static extern int Highs_getColsBySet( - IntPtr highs, - int num_set_entries, - int[] set, - ref int num_col, - double[] costs, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(highslibname)] - private static extern int Highs_getColsByMask( - IntPtr highs, - int[] mask, - ref int num_col, - double[] costs, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(highslibname)] - private static extern int Highs_getRowsByRange( - IntPtr highs, - int from_row, - int to_row, - ref int num_row, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(highslibname)] - private static extern int Highs_getRowsBySet( - IntPtr highs, - int num_set_entries, - int[] set, - ref int num_row, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(highslibname)] - private static extern int Highs_getRowsByMask( - IntPtr highs, - int[] mask, - ref int num_row, - double[] lower, - double[] upper, - ref int num_nz, - int[] matrix_start, - int[] matrix_index, - double[] matrix_value); - - [DllImport(highslibname)] - private static extern int Highs_getBasicVariables(IntPtr highs, int[] basic_variables); - - [DllImport(highslibname)] - private static extern int Highs_getBasisInverseRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); - - [DllImport(highslibname)] - private static extern int Highs_getBasisInverseCol(IntPtr highs, int col, double[] col_vector, ref int col_num_nz, int[] col_indices); - - [DllImport(highslibname)] - private static extern int Highs_getBasisSolve( - IntPtr highs, - double[] rhs, - double[] solution_vector, - ref int solution_num_nz, - int[] solution_indices); - - [DllImport(highslibname)] - private static extern int Highs_getBasisTransposeSolve( - IntPtr highs, - double[] rhs, - double[] solution_vector, - ref int solution_nz, - int[] solution_indices); - - [DllImport(highslibname)] - private static extern int Highs_getReducedRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); - - [DllImport(highslibname)] - private static extern int Highs_getReducedColumn(IntPtr highs, int col, double[] col_vector, ref int col_num_nz, int[] col_indices); - - [DllImport(highslibname)] - private static extern int Highs_clearModel(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_clearSolver(IntPtr highs); - - [DllImport(highslibname)] - private static extern int Highs_passColName(IntPtr highs, int col, string name); - - [DllImport(highslibname)] - private static extern int Highs_passRowName(IntPtr highs, int row, string name); - - [DllImport(highslibname)] - private static extern int Highs_writeOptions(IntPtr highs, string filename); - - [DllImport(highslibname)] - private static extern int Highs_writeOptionsDeviations(IntPtr highs, string filename); - - public static HighsStatus call(HighsModel model, ref HighsSolution sol, ref HighsBasis bas, ref HighsModelStatus modelstatus) - { - int nc = model.colcost.Length; - int nr = model.rowlower.Length; - int nnz = model.avalue.Length; - - int[] colbasstat = new int[nc]; - int[] rowbasstat = new int[nr]; - - int modelstate = 0; - - HighsStatus status = (HighsStatus)HighsLpSolver.Highs_call( - nc, - nr, - nnz, - model.colcost, - model.collower, - model.colupper, - model.rowlower, - model.rowupper, - model.astart, - model.aindex, - model.avalue, - sol.colvalue, - sol.coldual, - sol.rowvalue, - sol.rowdual, - colbasstat, - rowbasstat, - ref modelstate); - - modelstatus = (HighsModelStatus)modelstate; - - bas.colbasisstatus = colbasstat.Select(x => (HighsBasisStatus)x).ToArray(); - bas.rowbasisstatus = rowbasstat.Select(x => (HighsBasisStatus)x).ToArray(); - - return status; - } - - public HighsLpSolver() - { - this.highs = HighsLpSolver.Highs_create(); - } - - ~HighsLpSolver() - { - this.Dispose(false); - } - - public void Dispose() - { - this.Dispose(true); - GC.SuppressFinalize(this); - } - - protected virtual void Dispose(bool disposing) - { - if (this._disposed) - { - return; - } - - HighsLpSolver.Highs_destroy(this.highs); - this._disposed = true; - } - - public HighsStatus run() - { - return (HighsStatus)HighsLpSolver.Highs_run(this.highs); - } - - public HighsStatus readModel(string filename) - { - return (HighsStatus)HighsLpSolver.Highs_readModel(this.highs, filename); - } - - public HighsStatus writeModel(string filename) - { - return (HighsStatus)HighsLpSolver.Highs_writeModel(this.highs, filename); - } - - public HighsStatus writePresolvedModel(string filename) - { - return (HighsStatus)HighsLpSolver.Highs_writePresolvedModel(this.highs, filename); - } - - public HighsStatus writeSolutionPretty(string filename) - { - return (HighsStatus)HighsLpSolver.Highs_writeSolutionPretty(this.highs, filename); - } - - public Double getInfinity() - { - return (Double)HighsLpSolver.Highs_getInfinity(this.highs); - } - - public HighsStatus passLp(HighsModel model) - { - return (HighsStatus)HighsLpSolver.Highs_passLp( - this.highs, - model.colcost.Length, - model.rowlower.Length, - model.avalue.Length, - (int)model.a_format, - (int)model.sense, - model.offset, - model.colcost, - model.collower, - model.colupper, - model.rowlower, - model.rowupper, - model.astart, - model.aindex, - model.avalue); - } - - public HighsStatus passMip(HighsModel model) - { - return (HighsStatus)HighsLpSolver.Highs_passMip( - this.highs, - model.colcost.Length, - model.rowlower.Length, - model.avalue.Length, - (int)model.a_format, - (int)model.sense, - model.offset, - model.colcost, - model.collower, - model.colupper, - model.rowlower, - model.rowupper, - model.astart, - model.aindex, - model.avalue, - model.highs_integrality); - } - - public HighsStatus passHessian(HighsHessian hessian) - { - return (HighsStatus)HighsLpSolver.Highs_passHessian( - this.highs, - hessian.dim, - hessian.qvalue.Length, - (int)hessian.q_format, - hessian.qstart, - hessian.qindex, - hessian.qvalue); - } - - public HighsStatus setOptionValue(string option, string value) - { - return (HighsStatus)HighsLpSolver.Highs_setOptionValue(this.highs, option, value); - } - - public HighsStatus setStringOptionValue(string option, string value) - { - return (HighsStatus)HighsLpSolver.Highs_setStringOptionValue(this.highs, option, value); - } - - public HighsStatus setBoolOptionValue(string option, int value) - { - return (HighsStatus)HighsLpSolver.Highs_setBoolOptionValue(this.highs, option, value); - } - - public HighsStatus setDoubleOptionValue(string option, double value) - { - return (HighsStatus)HighsLpSolver.Highs_setDoubleOptionValue(this.highs, option, value); - } - - public HighsStatus setIntOptionValue(string option, int value) - { - return (HighsStatus)HighsLpSolver.Highs_setIntOptionValue(this.highs, option, value); - } - - public HighsStatus getStringOptionValue(string option, out string value) - { - var stringBuilder = new StringBuilder(); - var result = (HighsStatus)HighsLpSolver.Highs_getStringOptionValue(this.highs, option, stringBuilder); - value = stringBuilder.ToString(); - return result; - } - - public HighsStatus getBoolOptionValue(string option, out int value) - { - return (HighsStatus)HighsLpSolver.Highs_getBoolOptionValue(this.highs, option, out value); - } - - public HighsStatus getDoubleOptionValue(string option, out double value) - { - return (HighsStatus)HighsLpSolver.Highs_getDoubleOptionValue(this.highs, option, out value); - } - - public HighsStatus getIntOptionValue(string option, out int value) - { - return (HighsStatus)HighsLpSolver.Highs_getIntOptionValue(this.highs, option, out value); - } - - public int getNumCol() - { - return HighsLpSolver.Highs_getNumCol(this.highs); - } - - public int getNumRow() - { - return HighsLpSolver.Highs_getNumRow(this.highs); - } - - public int getNumNz() - { - return HighsLpSolver.Highs_getNumNz(this.highs); - } - - public HighsSolution getSolution() - { - int nc = this.getNumCol(); - int nr = this.getNumRow(); - - HighsSolution sol = new HighsSolution(nc, nr); - HighsLpSolver.Highs_getSolution(this.highs, sol.colvalue, sol.coldual, sol.rowvalue, sol.rowdual); - - return sol; - } - - public HighsBasis getBasis() - { - int nc = this.getNumCol(); - int nr = this.getNumRow(); - - int[] colbasstat = new int[nc]; - int[] rowbasstat = new int[nr]; - - HighsLpSolver.Highs_getBasis(this.highs, colbasstat, rowbasstat); - HighsBasis bas = new HighsBasis( - colbasstat.Select(x => (HighsBasisStatus)x).ToArray(), - rowbasstat.Select(x => (HighsBasisStatus)x).ToArray()); - - return bas; - } - - public double getObjectiveValue() - { - return HighsLpSolver.Highs_getObjectiveValue(this.highs); - } - - public HighsModelStatus GetModelStatus() - { - return (HighsModelStatus)HighsLpSolver.Highs_getModelStatus(this.highs); - } - - public int getIterationCount() - { - return HighsLpSolver.Highs_getIterationCount(this.highs); - } - - public HighsStatus addRow(double lower, double upper, int[] indices, double[] values) - { - return (HighsStatus)HighsLpSolver.Highs_addRow(this.highs, lower, upper, indices.Length, indices, values); - } - - public HighsStatus addRows(double[] lower, double[] upper, int[] starts, int[] indices, double[] values) - { - return (HighsStatus)HighsLpSolver.Highs_addRows(this.highs, lower.Length, lower, upper, indices.Length, starts, indices, values); - } - - public HighsStatus addCol(double cost, double lower, double upper, int[] indices, double[] values) - { - return (HighsStatus)HighsLpSolver.Highs_addCol(this.highs, cost, lower, upper, indices.Length, indices, values); - } - - public HighsStatus addCols(double[] costs, double[] lower, double[] upper, int[] starts, int[] indices, double[] values) - { - return (HighsStatus)HighsLpSolver.Highs_addCols( - this.highs, - costs.Length, - costs, - lower, - upper, - indices.Length, - starts, - indices, - values); - } - - public HighsStatus changeObjectiveSense(HighsObjectiveSense sense) - { - return (HighsStatus)HighsLpSolver.Highs_changeObjectiveSense(this.highs, (int)sense); - } - - public HighsStatus changeColCost(int col, double cost) - { - return (HighsStatus)HighsLpSolver.Highs_changeColCost(this.highs, col, cost); - } - - public HighsStatus changeColsCostBySet(int[] cols, double[] costs) - { - return (HighsStatus)HighsLpSolver.Highs_changeColsCostBySet(this.highs, cols.Length, cols, costs); - } - - public HighsStatus changeColsCostByMask(bool[] mask, double[] cost) - { - return (HighsStatus)HighsLpSolver.Highs_changeColsCostByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray(), cost); - } - - public HighsStatus changeColBounds(int col, double lower, double upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeColBounds(this.highs, col, lower, upper); - } - - public HighsStatus changeColsBoundsByRange(int from, int to, double[] lower, double[] upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeColsBoundsByRange(this.highs, from, to, lower, upper); - } - - public HighsStatus changeColsBoundsBySet(int[] cols, double[] lower, double[] upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeColsBoundsBySet(this.highs, cols.Length, cols, lower, upper); - } - - public HighsStatus changeColsBoundsByMask(bool[] mask, double[] lower, double[] upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeColsBoundsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray(), lower, upper); - } - - public HighsStatus changeRowBounds(int row, double lower, double upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeRowBounds(this.highs, row, lower, upper); - } - - public HighsStatus changeRowsBoundsByRange(int from, int to, double[] lower, double[] upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeRowsBoundsByRange(this.highs, from, to, lower, upper); - } - - public HighsStatus changeRowsBoundsBySet(int[] rows, double[] lower, double[] upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeRowsBoundsBySet(this.highs, rows.Length, rows, lower, upper); - } - - public HighsStatus changeRowsBoundsByMask(bool[] mask, double[] lower, double[] upper) - { - return (HighsStatus)HighsLpSolver.Highs_changeRowsBoundsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray(), lower, upper); - } - - public HighsStatus changeColsIntegralityByRange(int from_col, int to_col, HighsIntegrality[] integrality) - { - return (HighsStatus)HighsLpSolver.Highs_changeColsIntegralityByRange(this.highs, from_col, to_col, Array.ConvertAll(integrality, item => (int)item)); - } - - public HighsStatus changeCoeff(int row, int col, double value) - { - return (HighsStatus)HighsLpSolver.Highs_changeCoeff(this.highs, row, col, value); - } - - public HighsStatus deleteColsByRange(int from, int to) - { - return (HighsStatus)HighsLpSolver.Highs_deleteColsByRange(this.highs, from, to); - } - - public HighsStatus deleteColsBySet(int[] cols) - { - return (HighsStatus)HighsLpSolver.Highs_deleteColsBySet(this.highs, cols.Length, cols); - } - - public HighsStatus deleteColsByMask(bool[] mask) - { - return (HighsStatus)HighsLpSolver.Highs_deleteColsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray()); - } - - public HighsStatus deleteRowsByRange(int from, int to) - { - return (HighsStatus)HighsLpSolver.Highs_deleteRowsByRange(this.highs, from, to); - } - - public HighsStatus deleteRowsBySet(int[] rows) - { - return (HighsStatus)HighsLpSolver.Highs_deleteRowsBySet(this.highs, rows.Length, rows); - } - - public HighsStatus deleteRowsByMask(bool[] mask) - { - return (HighsStatus)HighsLpSolver.Highs_deleteRowsByMask(this.highs, mask.Select(x => x ? 1 : 0).ToArray()); - } - - delegate int HighsGetInfoDelegate(IntPtr highs, string infoName, out TValue output); - - private TValue GetValueOrFallback(HighsGetInfoDelegate highsGetInfoDelegate, string infoName, TValue fallback) - { - try - { - var status = (HighsStatus)highsGetInfoDelegate(this.highs, infoName, out var value); - if (status != HighsStatus.kOk) - { - return fallback; - } - - return value; - } - catch - { - return fallback; - } - } - - /// - /// Gets the current solution info. - /// - /// The . - public SolutionInfo getInfo() - { - // TODO: This object does not contian the "complete" info from the C api. Add further props, if you need them. - var info = new SolutionInfo() - { - MipGap = this.GetValueOrFallback(HighsLpSolver.Highs_getDoubleInfoValue, "mip_gap", double.NaN), - DualBound = this.GetValueOrFallback(HighsLpSolver.Highs_getDoubleInfoValue, "mip_dual_bound", double.NaN), - ObjectiveValue = this.GetValueOrFallback(HighsLpSolver.Highs_getDoubleInfoValue, "objective_function_value", double.NaN), - NodeCount = this.GetValueOrFallback(HighsLpSolver.Highs_getInt64InfoValue, "mip_node_count", 0L), - IpmIterationCount = this.GetValueOrFallback(HighsLpSolver.Highs_getIntInfoValue, "ipm_iteration_count", 0), - SimplexIterationCount = this.GetValueOrFallback(HighsLpSolver.Highs_getIntInfoValue, "simplex_iteration_count", 0), - PdlpIterationCount = this.GetValueOrFallback(HighsLpSolver.Highs_getIntInfoValue, "pdlp_iteration_count", 0), - }; - return info; - } - - public HighsStatus setSolution(HighsSolution solution) - { - return (HighsStatus)HighsLpSolver.Highs_setSolution(this.highs, solution.colvalue, solution.rowvalue, solution.coldual, solution.rowdual); - } - - /// Set a partial primal solution by passing values for a set of variables - /// A dictionary that maps variable indices to variable values - /// The sparse solution set by this function has values for a subset of the model's variables. - /// For each entry in , the key identifies a variable by index, and - /// the value indicates the variable's value in the sparse solution. - /// A constant indicating whether the call succeeded - public HighsStatus setSparseSolution(IReadOnlyDictionary valuesByIndex) - { - return (HighsStatus)Highs_setSparseSolution(this.highs, valuesByIndex.Count, valuesByIndex.Keys.ToArray(), valuesByIndex.Values.ToArray()); - } - - public HighsStatus getBasicVariables(ref int[] basic_variables) - { - return (HighsStatus)Highs_getBasicVariables(this.highs, basic_variables); - } - - public HighsStatus getBasisInverseRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) - { - return (HighsStatus)Highs_getBasisInverseRow(this.highs, row, row_vector, ref row_num_nz, row_indices); - } - - public HighsStatus getBasisInverseCol(int col, double[] col_vector, ref int col_num_nz, int[] col_indices) - { - return (HighsStatus)Highs_getBasisInverseCol(this.highs, col, col_vector, ref col_num_nz, col_indices); - } - - public HighsStatus getBasisSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) - { - return (HighsStatus)Highs_getBasisSolve(this.highs, rhs, solution_vector, ref solution_num_nz, solution_indices); - } - - public HighsStatus getBasisTransposeSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) - { - return (HighsStatus)Highs_getBasisTransposeSolve(this.highs, rhs, solution_vector, ref solution_num_nz, solution_indices); - } - - public HighsStatus getReducedRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) - { - return (HighsStatus)Highs_getReducedRow(this.highs, row, row_vector, ref row_num_nz, row_indices); - } - - public HighsStatus getReducedColumn(int col, double[] col_vector, ref int col_num_nz, int[] col_indices) - { - return (HighsStatus)Highs_getReducedColumn(this.highs, col, col_vector, ref col_num_nz, col_indices); - } - - public HighsStatus clearModel() - { - return (HighsStatus)Highs_clearModel(this.highs); - } - - public HighsStatus clearSolver() - { - return (HighsStatus)Highs_clearSolver(this.highs); - } - - public HighsStatus passColName(int col, string name) - { - return (HighsStatus)Highs_passColName(this.highs, col, name); - } - - public HighsStatus passRowName(int row, string name) - { - return (HighsStatus)Highs_passRowName(this.highs, row, name); - } - - public HighsStatus writeOptions(string filename) - { - return (HighsStatus)Highs_writeOptions(this.highs, filename); - } - - public HighsStatus writeOptionsDeviations(string filename) - { - return (HighsStatus)Highs_writeOptionsDeviations(this.highs, filename); - } -} - -/// -/// The solution info. -/// -public class SolutionInfo -{ - /// - /// Gets or sets the simplex iteration count. - /// - public int SimplexIterationCount { get; set; } - - /// - /// Gets or sets the Interior Point Method (IPM) iteration count. - /// - public int IpmIterationCount { get; set; } - - /// - /// Gets or sets the PDLP iteration count. - /// - public int PdlpIterationCount { get; set; } - - /// - /// Gets or sets the MIP gap. - /// - public double MipGap { get; set; } - - /// - /// Gets or sets the best dual bound. - /// - public double DualBound { get; set; } - - /// - /// Gets or sets the MIP node count. - /// - public long NodeCount { get; set; } - - /// - /// Gets or sets the objective value. - /// - public double ObjectiveValue { get; set; } -} -} \ No newline at end of file From 5a0df4dbc16881ace6ed11725380036292d959d0 Mon Sep 17 00:00:00 2001 From: Thiago Novaes <77932135+Thiago-NovaesB@users.noreply.github.com> Date: Fri, 26 Dec 2025 17:10:25 +0100 Subject: [PATCH 8/9] LibraryImport --- highs/interfaces/Highs/Imports.cs | 167 +++++++++++++++--------------- 1 file changed, 84 insertions(+), 83 deletions(-) diff --git a/highs/interfaces/Highs/Imports.cs b/highs/interfaces/Highs/Imports.cs index 20efba6310..b9c2979947 100644 --- a/highs/interfaces/Highs/Imports.cs +++ b/highs/interfaces/Highs/Imports.cs @@ -14,7 +14,7 @@ public static class Imports private const string HighsLibName = "highs"; #region Library Imports - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_lpCall( int numcol, int numrow, @@ -38,7 +38,7 @@ internal static extern int Highs_lpCall( int[] rowbasisstatus, ref int modelstatus); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_mipCall( int numcol, int numrow, @@ -59,7 +59,7 @@ internal static extern int Highs_mipCall( double[] rowvalue, ref int modelstatus); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_qpCall( int numcol, int numrow, @@ -89,31 +89,31 @@ internal static extern int Highs_qpCall( int[] rowbasisstatus, ref int modelstatus); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern IntPtr Highs_create(); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern void Highs_destroy(IntPtr highs); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_run(IntPtr highs); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_readModel(IntPtr highs, string filename); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_writeModel(IntPtr highs, string filename); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_writePresolvedModel(IntPtr highs, string filename); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_writeSolutionPretty(IntPtr highs, string filename); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getInfinity(IntPtr highs); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_passLp( IntPtr highs, int numcol, @@ -131,7 +131,7 @@ internal static extern int Highs_passLp( int[] aindex, double[] avalue); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_passMip( IntPtr highs, int numcol, @@ -150,7 +150,7 @@ internal static extern int Highs_passMip( double[] avalue, int[] highs_integrality); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_passModel( IntPtr highs, int numcol, @@ -174,7 +174,7 @@ internal static extern int Highs_passModel( double[] qvalue, int[] highs_integrality); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_passHessian( IntPtr highs, int dim, @@ -184,64 +184,64 @@ internal static extern int Highs_passHessian( int[] qindex, double[] qvalue); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_setOptionValue(IntPtr highs, string option, string value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_setBoolOptionValue(IntPtr highs, string option, int value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_setIntOptionValue(IntPtr highs, string option, int value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_setDoubleOptionValue(IntPtr highs, string option, double value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_setStringOptionValue(IntPtr highs, string option, string value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getBoolOptionValue(IntPtr highs, string option, out int value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getIntOptionValue(IntPtr highs, string option, out int value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getDoubleOptionValue(IntPtr highs, string option, out double value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getStringOptionValue(IntPtr highs, string option, [Out] StringBuilder value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getSolution(IntPtr highs, double[] colvalue, double[] coldual, double[] rowvalue, double[] rowdual); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getNumCol(IntPtr highs); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getNumRow(IntPtr highs); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getNumNz(IntPtr highs); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getHessianNumNz(IntPtr highs); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getBasis(IntPtr highs, int[] colstatus, int[] rowstatus); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern double Highs_getObjectiveValue(IntPtr highs); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getIterationCount(IntPtr highs); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getModelStatus(IntPtr highs); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_addRow(IntPtr highs, double lower, double upper, int num_new_nz, int[] indices, double[] values); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_addRows( IntPtr highs, int num_new_row, @@ -252,7 +252,7 @@ internal static extern int Highs_addRows( int[] indices, double[] values); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_addCol( IntPtr highs, double cost, @@ -262,7 +262,7 @@ internal static extern int Highs_addCol( int[] indices, double[] values); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_addCols( IntPtr highs, int num_new_col, @@ -274,82 +274,82 @@ internal static extern int Highs_addCols( int[] indices, double[] values); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_changeObjectiveSense(IntPtr highs, int sense); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_changeColCost(IntPtr highs, int column, double cost); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_changeColsCostBySet(IntPtr highs, int num_set_entries, int[] set, double[] cost); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_changeColsCostByMask(IntPtr highs, int[] mask, double[] cost); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_changeColBounds(IntPtr highs, int column, double lower, double upper); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_changeColsBoundsByRange(IntPtr highs, int from_col, int to_col, double[] lower, double[] upper); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_changeColsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_changeColsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_changeRowBounds(IntPtr highs, int row, double lower, double upper); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_changeRowsBoundsByRange(IntPtr highs, int from_row, int to_row, double[] lower, double[] upper); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_changeRowsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_changeRowsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_changeColsIntegralityByRange(IntPtr highs, int from_col, int to_col, int[] integrality); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_changeCoeff(IntPtr highs, int row, int column, double value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_deleteColsByRange(IntPtr highs, int from_col, int to_col); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_deleteColsBySet(IntPtr highs, int num_set_entries, int[] set); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_deleteColsByMask(IntPtr highs, int[] mask); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_deleteRowsByRange(IntPtr highs, int from_row, int to_row); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_deleteRowsBySet(IntPtr highs, int num_set_entries, int[] set); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_deleteRowsByMask(IntPtr highs, int[] mask); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getDoubleInfoValue(IntPtr highs, string info, out double value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getIntInfoValue(IntPtr highs, string info, out int value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getInt64InfoValue(IntPtr highs, string info, out long value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_setSolution(IntPtr highs, double[] col_value, double[] row_value, double[] col_dual, double[] row_dual); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_setSparseSolution(IntPtr highs, int num_entries, int[] index, double[] value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getColsByRange( IntPtr highs, int from_col, @@ -363,7 +363,7 @@ internal static extern int Highs_getColsByRange( int[] matrix_index, double[] matrix_value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getColsBySet( IntPtr highs, int num_set_entries, @@ -377,7 +377,7 @@ internal static extern int Highs_getColsBySet( int[] matrix_index, double[] matrix_value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getColsByMask( IntPtr highs, int[] mask, @@ -390,7 +390,7 @@ internal static extern int Highs_getColsByMask( int[] matrix_index, double[] matrix_value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getRowsByRange( IntPtr highs, int from_row, @@ -403,7 +403,7 @@ internal static extern int Highs_getRowsByRange( int[] matrix_index, double[] matrix_value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getRowsBySet( IntPtr highs, int num_set_entries, @@ -416,7 +416,7 @@ internal static extern int Highs_getRowsBySet( int[] matrix_index, double[] matrix_value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getRowsByMask( IntPtr highs, int[] mask, @@ -428,16 +428,16 @@ internal static extern int Highs_getRowsByMask( int[] matrix_index, double[] matrix_value); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getBasicVariables(IntPtr highs, int[] basic_variables); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getBasisInverseRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getBasisInverseCol(IntPtr highs, int column, double[] col_vector, ref int col_num_nz, int[] col_indices); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getBasisSolve( IntPtr highs, double[] rhs, @@ -445,7 +445,7 @@ internal static extern int Highs_getBasisSolve( ref int solution_num_nz, int[] solution_indices); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getBasisTransposeSolve( IntPtr highs, double[] rhs, @@ -453,27 +453,28 @@ internal static extern int Highs_getBasisTransposeSolve( ref int solution_nz, int[] solution_indices); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getReducedRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_getReducedColumn(IntPtr highs, int column, double[] col_vector, ref int col_num_nz, int[] col_indices); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_clearModel(IntPtr highs); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_clearSolver(IntPtr highs); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_passColName(IntPtr highs, int column, string name); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_passRowName(IntPtr highs, int row, string name); - [DllImport(HighsLibName)] + [LibraryImport(HighsLibName)] internal static extern int Highs_writeOptions(IntPtr highs, string filename); - [DllImport(HighsLibName)] - internal static extern int Highs_writeOptionsDeviations(IntPtr highs, string filename); - #endregion \ No newline at end of file + [LibraryImport(HighsLibName)] + internal static extern int Highs_writeOptionsDeviations(IntPtr highs, string filename); + #endregion +} \ No newline at end of file From 49020142ef83305c00d5cfad4d6514fe53171d90 Mon Sep 17 00:00:00 2001 From: Thiago Novaes <77932135+Thiago-NovaesB@users.noreply.github.com> Date: Fri, 26 Dec 2025 17:24:31 +0100 Subject: [PATCH 9/9] done --- .../Highs/Enums/{Status.cs => HighsStatus.cs} | 40 ++-- .../Highs/{Solver.cs => HighsSolver.cs} | 182 +++++++++--------- highs/interfaces/Highs/Imports.cs | 164 ++++++++-------- .../Highs/Records/{Model.cs => HighsModel.cs} | 2 +- .../Records/{Solution.cs => HighsSolution.cs} | 52 ++--- 5 files changed, 220 insertions(+), 220 deletions(-) rename highs/interfaces/Highs/Enums/{Status.cs => HighsStatus.cs} (91%) rename highs/interfaces/Highs/{Solver.cs => HighsSolver.cs} (68%) rename highs/interfaces/Highs/Records/{Model.cs => HighsModel.cs} (94%) rename highs/interfaces/Highs/Records/{Solution.cs => HighsSolution.cs} (84%) diff --git a/highs/interfaces/Highs/Enums/Status.cs b/highs/interfaces/Highs/Enums/HighsStatus.cs similarity index 91% rename from highs/interfaces/Highs/Enums/Status.cs rename to highs/interfaces/Highs/Enums/HighsStatus.cs index 001f9a5de2..40aa091574 100644 --- a/highs/interfaces/Highs/Enums/Status.cs +++ b/highs/interfaces/Highs/Enums/HighsStatus.cs @@ -1,20 +1,20 @@ -namespace Highs.Enums; - -/// -/// This is (part of) the return value of most HiGHS methods -/// -public enum Status -{ - /// - /// The method has exposed an error - /// - Error = -1, - /// - /// The method has completed successfully - /// - Ok, - /// - /// The method has recovered from an unusual event, or has terminated due to reaching a time or iteration limit - /// - Warning -} +namespace Highs.Enums; + +/// +/// This is (part of) the return value of most HiGHS methods +/// +public enum HighsStatus +{ + /// + /// The method has exposed an error + /// + Error = -1, + /// + /// The method has completed successfully + /// + Ok, + /// + /// The method has recovered from an unusual event, or has terminated due to reaching a time or iteration limit + /// + Warning +} diff --git a/highs/interfaces/Highs/Solver.cs b/highs/interfaces/Highs/HighsSolver.cs similarity index 68% rename from highs/interfaces/Highs/Solver.cs rename to highs/interfaces/Highs/HighsSolver.cs index d90b6450ef..aee9ea8a34 100644 --- a/highs/interfaces/Highs/Solver.cs +++ b/highs/interfaces/Highs/HighsSolver.cs @@ -7,7 +7,7 @@ namespace Highs; /// /// The Highs Solver interface. /// -public class Solver : IDisposable +public class HighsSolver : IDisposable { /// /// The pointer to the _highs instance. @@ -27,7 +27,7 @@ public class Solver : IDisposable /// /// /// - public static Status LpCall(Model model, ref Solution solution, out BasisInfo basisInfo, out ModelStatus modelStatus) + public static HighsStatus LpCall(HighsModel model, ref HighsSolution solution, out BasisInfo basisInfo, out ModelStatus modelStatus) { var numberOfColumns = model.ColumnCost.Length; var numberOfRows = model.RowLower.Length; @@ -38,7 +38,7 @@ public static Status LpCall(Model model, ref Solution solution, out BasisInfo ba var modelstate = 0; - var status = (Status)Imports.Highs_lpCall( + var status = (HighsStatus)Imports.Highs_lpCall( numberOfColumns, numberOfRows, numberOfMatrixValues, @@ -74,7 +74,7 @@ public static Status LpCall(Model model, ref Solution solution, out BasisInfo ba /// /// /// - public static Status MipCall(Model model, ref Solution solution, out ModelStatus modelStatus) + public static HighsStatus MipCall(HighsModel model, ref HighsSolution solution, out ModelStatus modelStatus) { var numberOfColumns = model.ColumnCost.Length; var numberOfRows = model.RowLower.Length; @@ -82,7 +82,7 @@ public static Status MipCall(Model model, ref Solution solution, out ModelStatus var modelstate = 0; - var status = (Status)Imports.Highs_mipCall( + var status = (HighsStatus)Imports.Highs_mipCall( numberOfColumns, numberOfRows, numberOfMatrixValues, @@ -114,7 +114,7 @@ public static Status MipCall(Model model, ref Solution solution, out ModelStatus /// /// /// - public static Status QPCall(Model model, ref Solution solution, out BasisInfo basisInfo, out ModelStatus modelStatus) + public static HighsStatus QPCall(HighsModel model, ref HighsSolution solution, out BasisInfo basisInfo, out ModelStatus modelStatus) { var numberOfColumns = model.ColumnCost.Length; var numberOfRows = model.RowLower.Length; @@ -126,7 +126,7 @@ public static Status QPCall(Model model, ref Solution solution, out BasisInfo ba var modelstate = 0; - var status = (Status)Imports.Highs_qpCall( + var status = (HighsStatus)Imports.Highs_qpCall( numberOfColumns, numberOfRows, numberOfMatrixValues, @@ -163,12 +163,12 @@ public static Status QPCall(Model model, ref Solution solution, out BasisInfo ba /// /// The default constructor. /// - public Solver() => _highs = Imports.Highs_create(); + public HighsSolver() => _highs = Imports.Highs_create(); /// /// The destructor. /// - ~Solver() => Dispose(false); + ~HighsSolver() => Dispose(false); /// /// Disposes the instance. @@ -197,35 +197,35 @@ protected virtual void Dispose(bool disposing) /// Runs the solver. /// /// - public Status Run() => (Status)Imports.Highs_run(_highs); + public HighsStatus Run() => (HighsStatus)Imports.Highs_run(_highs); /// /// Reads a model from file. /// /// /// - public Status ReadModel(string filename) => (Status)Imports.Highs_readModel(_highs, filename); + public HighsStatus ReadModel(string filename) => (HighsStatus)Imports.Highs_readModel(_highs, filename); /// /// Writes the model to file. /// /// /// - public Status WriteModel(string filename) => (Status)Imports.Highs_writeModel(_highs, filename); + public HighsStatus WriteModel(string filename) => (HighsStatus)Imports.Highs_writeModel(_highs, filename); /// /// Writes the presolved model to file. /// /// /// - public Status WritePresolvedModel(string filename) => (Status)Imports.Highs_writePresolvedModel(_highs, filename); + public HighsStatus WritePresolvedModel(string filename) => (HighsStatus)Imports.Highs_writePresolvedModel(_highs, filename); /// /// Writes the solution to file in a pretty format. /// /// /// - public Status WriteSolutionPretty(string filename) => (Status)Imports.Highs_writeSolutionPretty(_highs, filename); + public HighsStatus WriteSolutionPretty(string filename) => (HighsStatus)Imports.Highs_writeSolutionPretty(_highs, filename); /// /// Gets the infinity value. @@ -238,9 +238,9 @@ protected virtual void Dispose(bool disposing) /// /// /// - public Status PassLp(Model model) + public HighsStatus PassLp(HighsModel model) { - return (Status)Imports.Highs_passLp( + return (HighsStatus)Imports.Highs_passLp( _highs, model.ColumnCost.Length, model.RowLower.Length, @@ -263,9 +263,9 @@ public Status PassLp(Model model) /// /// /// - public Status PassMip(Model model) + public HighsStatus PassMip(HighsModel model) { - return (Status)Imports.Highs_passMip( + return (HighsStatus)Imports.Highs_passMip( _highs, model.ColumnCost.Length, model.RowLower.Length, @@ -290,7 +290,7 @@ public Status PassMip(Model model) /// /// /// - public Status SetOptionValue(string option, string value) => (Status)Imports.Highs_setOptionValue(_highs, option, value); + public HighsStatus SetOptionValue(string option, string value) => (HighsStatus)Imports.Highs_setOptionValue(_highs, option, value); /// /// Sets the string option value. @@ -298,7 +298,7 @@ public Status PassMip(Model model) /// /// /// - public Status SetStringOptionValue(string option, string value) => (Status)Imports.Highs_setStringOptionValue(_highs, option, value); + public HighsStatus SetStringOptionValue(string option, string value) => (HighsStatus)Imports.Highs_setStringOptionValue(_highs, option, value); /// /// Sets the boolean option value. @@ -306,7 +306,7 @@ public Status PassMip(Model model) /// /// /// - public Status SetBoolOptionValue(string option, int value) => (Status)Imports.Highs_setBoolOptionValue(_highs, option, value); + public HighsStatus SetBoolOptionValue(string option, int value) => (HighsStatus)Imports.Highs_setBoolOptionValue(_highs, option, value); /// /// Sets the double option value. @@ -314,7 +314,7 @@ public Status PassMip(Model model) /// /// /// - public Status SetDoubleOptionValue(string option, double value) => (Status)Imports.Highs_setDoubleOptionValue(_highs, option, value); + public HighsStatus SetDoubleOptionValue(string option, double value) => (HighsStatus)Imports.Highs_setDoubleOptionValue(_highs, option, value); /// /// Sets the integer option value. @@ -322,7 +322,7 @@ public Status PassMip(Model model) /// /// /// - public Status SetIntOptionValue(string option, int value) => (Status)Imports.Highs_setIntOptionValue(_highs, option, value); + public HighsStatus SetIntOptionValue(string option, int value) => (HighsStatus)Imports.Highs_setIntOptionValue(_highs, option, value); /// /// Gets the string option value. @@ -330,10 +330,10 @@ public Status PassMip(Model model) /// /// /// - public Status GetStringOptionValue(string option, out string value) + public HighsStatus GetStringOptionValue(string option, out string value) { var stringBuilder = new StringBuilder(); - var result = (Status)Imports.Highs_getStringOptionValue(_highs, option, stringBuilder); + var result = (HighsStatus)Imports.Highs_getStringOptionValue(_highs, option, stringBuilder); value = stringBuilder.ToString(); return result; } @@ -344,7 +344,7 @@ public Status GetStringOptionValue(string option, out string value) /// /// /// - public Status GetBoolOptionValue(string option, out int value) => (Status)Imports.Highs_getBoolOptionValue(_highs, option, out value); + public HighsStatus GetBoolOptionValue(string option, out int value) => (HighsStatus)Imports.Highs_getBoolOptionValue(_highs, option, out value); /// /// Gets the double option value. @@ -352,7 +352,7 @@ public Status GetStringOptionValue(string option, out string value) /// /// /// - public Status GetDoubleOptionValue(string option, out double value) => (Status)Imports.Highs_getDoubleOptionValue(_highs, option, out value); + public HighsStatus GetDoubleOptionValue(string option, out double value) => (HighsStatus)Imports.Highs_getDoubleOptionValue(_highs, option, out value); /// /// Gets the integer option value. @@ -360,7 +360,7 @@ public Status GetStringOptionValue(string option, out string value) /// /// /// - public Status GetIntOptionValue(string option, out int value) => (Status)Imports.Highs_getIntOptionValue(_highs, option, out value); + public HighsStatus GetIntOptionValue(string option, out int value) => (HighsStatus)Imports.Highs_getIntOptionValue(_highs, option, out value); /// /// Gets the number of columns. @@ -385,13 +385,13 @@ public Status GetStringOptionValue(string option, out string value) /// /// /// - public Status GetSolution(out Solution solution) + public HighsStatus GetSolution(out HighsSolution solution) { var numberOfColumns = GetNumberOfColumns(); var numberOfRows = GetNumberOfRows(); - solution = new Solution(numberOfColumns, numberOfRows); - return (Status)Imports.Highs_getSolution(_highs, solution.ColumnValue, solution.ColumnDual, solution.RowValue, solution.RowDual); + solution = new HighsSolution(numberOfColumns, numberOfRows); + return (HighsStatus)Imports.Highs_getSolution(_highs, solution.ColumnValue, solution.ColumnDual, solution.RowValue, solution.RowDual); } /// @@ -399,9 +399,9 @@ public Status GetSolution(out Solution solution) /// /// /// - public Status PassHessian(Hessian hessian) + public HighsStatus PassHessian(Hessian hessian) { - return (Status)Imports.Highs_passHessian(_highs, + return (HighsStatus)Imports.Highs_passHessian(_highs, hessian.Dimension, hessian.Values.Length, (int)hessian.HessianFormat, @@ -414,7 +414,7 @@ public Status PassHessian(Hessian hessian) /// Gets the basis information. /// /// - public Status GetBasis(out BasisInfo basisInfo) + public HighsStatus GetBasis(out BasisInfo basisInfo) { var numberOfColumns = GetNumberOfColumns(); var numberOfRows = GetNumberOfRows(); @@ -422,11 +422,11 @@ public Status GetBasis(out BasisInfo basisInfo) var columnBasisStatus = new int[numberOfColumns]; var rowBasisStatus = new int[numberOfRows]; - var status = (Status)Imports.Highs_getBasis(_highs, columnBasisStatus, rowBasisStatus); - if (status == Status.Error) + var status = (HighsStatus)Imports.Highs_getBasis(_highs, columnBasisStatus, rowBasisStatus); + if (status == HighsStatus.Error) { basisInfo = null; - return Status.Error; + return HighsStatus.Error; } basisInfo = new BasisInfo(Array.ConvertAll(columnBasisStatus, x => (BasisStatus)x), Array.ConvertAll(rowBasisStatus, x => (BasisStatus)x)); @@ -460,9 +460,9 @@ public Status GetBasis(out BasisInfo basisInfo) /// /// /// - public Status AddRow(double lower, double upper, int[] indices, double[] values) + public HighsStatus AddRow(double lower, double upper, int[] indices, double[] values) { - return (Status)Imports.Highs_addRow(_highs, lower, upper, indices.Length, indices, values); + return (HighsStatus)Imports.Highs_addRow(_highs, lower, upper, indices.Length, indices, values); } /// @@ -474,9 +474,9 @@ public Status AddRow(double lower, double upper, int[] indices, double[] values) /// /// /// - public Status AddRows(double[] lower, double[] upper, int[] starts, int[] indices, double[] values) + public HighsStatus AddRows(double[] lower, double[] upper, int[] starts, int[] indices, double[] values) { - return (Status)Imports.Highs_addRows(_highs, lower.Length, lower, upper, indices.Length, starts, indices, values); + return (HighsStatus)Imports.Highs_addRows(_highs, lower.Length, lower, upper, indices.Length, starts, indices, values); } /// @@ -488,9 +488,9 @@ public Status AddRows(double[] lower, double[] upper, int[] starts, int[] indice /// /// /// - public Status AddColumn(double cost, double lower, double upper, int[] indices, double[] values) + public HighsStatus AddColumn(double cost, double lower, double upper, int[] indices, double[] values) { - return (Status)Imports.Highs_addCol(_highs, cost, lower, upper, indices.Length, indices, values); + return (HighsStatus)Imports.Highs_addCol(_highs, cost, lower, upper, indices.Length, indices, values); } /// @@ -503,9 +503,9 @@ public Status AddColumn(double cost, double lower, double upper, int[] indices, /// /// /// - public Status AddColumns(double[] costs, double[] lower, double[] upper, int[] starts, int[] indices, double[] values) + public HighsStatus AddColumns(double[] costs, double[] lower, double[] upper, int[] starts, int[] indices, double[] values) { - return (Status)Imports.Highs_addCols( + return (HighsStatus)Imports.Highs_addCols( _highs, costs.Length, costs, @@ -522,7 +522,7 @@ public Status AddColumns(double[] costs, double[] lower, double[] upper, int[] s /// /// /// - public Status ChangeObjectiveSense(ObjectiveSense sense) => (Status)Imports.Highs_changeObjectiveSense(_highs, (int)sense); + public HighsStatus ChangeObjectiveSense(ObjectiveSense sense) => (HighsStatus)Imports.Highs_changeObjectiveSense(_highs, (int)sense); /// /// Changes the cost of a column. @@ -530,7 +530,7 @@ public Status AddColumns(double[] costs, double[] lower, double[] upper, int[] s /// /// /// - public Status ChangeColumnCost(int column, double cost) => (Status)Imports.Highs_changeColCost(_highs, column, cost); + public HighsStatus ChangeColumnCost(int column, double cost) => (HighsStatus)Imports.Highs_changeColCost(_highs, column, cost); /// /// Changes the costs of multiple columns by set. @@ -538,7 +538,7 @@ public Status AddColumns(double[] costs, double[] lower, double[] upper, int[] s /// /// /// - public Status ChangeColumnsCostBySet(int[] columns, double[] costs) => (Status)Imports.Highs_changeColsCostBySet(_highs, columns.Length, columns, costs); + public HighsStatus ChangeColumnsCostBySet(int[] columns, double[] costs) => (HighsStatus)Imports.Highs_changeColsCostBySet(_highs, columns.Length, columns, costs); /// /// Changes the costs of multiple columns by mask. @@ -546,7 +546,7 @@ public Status AddColumns(double[] costs, double[] lower, double[] upper, int[] s /// /// /// - public Status ChangeColumnsCostByMask(bool[] mask, double[] cost) => (Status)Imports.Highs_changeColsCostByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0), cost); + public HighsStatus ChangeColumnsCostByMask(bool[] mask, double[] cost) => (HighsStatus)Imports.Highs_changeColsCostByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0), cost); /// /// Changes the bounds of a column. @@ -555,7 +555,7 @@ public Status AddColumns(double[] costs, double[] lower, double[] upper, int[] s /// /// /// - public Status ChangeColumnBounds(int column, double lower, double upper) => (Status)Imports.Highs_changeColBounds(_highs, column, lower, upper); + public HighsStatus ChangeColumnBounds(int column, double lower, double upper) => (HighsStatus)Imports.Highs_changeColBounds(_highs, column, lower, upper); /// /// Changes the bounds of multiple columns by range. @@ -565,7 +565,7 @@ public Status AddColumns(double[] costs, double[] lower, double[] upper, int[] s /// /// /// - public Status ChangeColumnsBoundsByRange(int from, int to, double[] lower, double[] upper) => (Status)Imports.Highs_changeColsBoundsByRange(_highs, from, to, lower, upper); + public HighsStatus ChangeColumnsBoundsByRange(int from, int to, double[] lower, double[] upper) => (HighsStatus)Imports.Highs_changeColsBoundsByRange(_highs, from, to, lower, upper); /// /// Changes the bounds of multiple columns by set. @@ -574,9 +574,9 @@ public Status AddColumns(double[] costs, double[] lower, double[] upper, int[] s /// /// /// - public Status ChangeColumnsBoundsBySet(int[] columns, double[] lower, double[] upper) + public HighsStatus ChangeColumnsBoundsBySet(int[] columns, double[] lower, double[] upper) { - return (Status)Imports.Highs_changeColsBoundsBySet(_highs, columns.Length, columns, lower, upper); + return (HighsStatus)Imports.Highs_changeColsBoundsBySet(_highs, columns.Length, columns, lower, upper); } /// @@ -586,9 +586,9 @@ public Status ChangeColumnsBoundsBySet(int[] columns, double[] lower, double[] u /// /// /// - public Status ChangeColumnsBoundsByMask(bool[] mask, double[] lower, double[] upper) + public HighsStatus ChangeColumnsBoundsByMask(bool[] mask, double[] lower, double[] upper) { - return (Status)Imports.Highs_changeColsBoundsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0), lower, upper); + return (HighsStatus)Imports.Highs_changeColsBoundsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0), lower, upper); } /// @@ -598,7 +598,7 @@ public Status ChangeColumnsBoundsByMask(bool[] mask, double[] lower, double[] up /// /// /// - public Status ChangeRowBounds(int row, double lower, double upper) => (Status)Imports.Highs_changeRowBounds(_highs, row, lower, upper); + public HighsStatus ChangeRowBounds(int row, double lower, double upper) => (HighsStatus)Imports.Highs_changeRowBounds(_highs, row, lower, upper); /// /// Changes the bounds of multiple rows by range. @@ -607,7 +607,7 @@ public Status ChangeColumnsBoundsByMask(bool[] mask, double[] lower, double[] up /// /// /// - public Status ChangeRowsBoundsBySet(int[] rows, double[] lower, double[] upper) => (Status)Imports.Highs_changeRowsBoundsBySet(_highs, rows.Length, rows, lower, upper); + public HighsStatus ChangeRowsBoundsBySet(int[] rows, double[] lower, double[] upper) => (HighsStatus)Imports.Highs_changeRowsBoundsBySet(_highs, rows.Length, rows, lower, upper); /// /// Changes the bounds of multiple rows by mask. @@ -616,7 +616,7 @@ public Status ChangeColumnsBoundsByMask(bool[] mask, double[] lower, double[] up /// /// /// - public Status ChangeRowsBoundsByMask(bool[] mask, double[] lower, double[] upper) => (Status)Imports.Highs_changeRowsBoundsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0), lower, upper); + public HighsStatus ChangeRowsBoundsByMask(bool[] mask, double[] lower, double[] upper) => (HighsStatus)Imports.Highs_changeRowsBoundsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0), lower, upper); /// /// Changes the integrality of multiple columns by range. @@ -625,9 +625,9 @@ public Status ChangeColumnsBoundsByMask(bool[] mask, double[] lower, double[] up /// /// /// - public Status ChangeColumnsIntegralityByRange(int from_col, int to_col, VariableType[] integrality) + public HighsStatus ChangeColumnsIntegralityByRange(int from_col, int to_col, VariableType[] integrality) { - return (Status)Imports.Highs_changeColsIntegralityByRange(_highs, from_col, to_col, Array.ConvertAll(integrality, item => (int)item)); + return (HighsStatus)Imports.Highs_changeColsIntegralityByRange(_highs, from_col, to_col, Array.ConvertAll(integrality, item => (int)item)); } /// @@ -637,7 +637,7 @@ public Status ChangeColumnsIntegralityByRange(int from_col, int to_col, Variable /// /// /// - public Status ChangeCoefficient(int row, int column, double value) => (Status)Imports.Highs_changeCoeff(_highs, row, column, value); + public HighsStatus ChangeCoefficient(int row, int column, double value) => (HighsStatus)Imports.Highs_changeCoeff(_highs, row, column, value); /// /// Deletes multiple columns by range. @@ -645,21 +645,21 @@ public Status ChangeColumnsIntegralityByRange(int from_col, int to_col, Variable /// /// /// - public Status DeleteColumnsByRange(int from, int to) => (Status)Imports.Highs_deleteColsByRange(_highs, from, to); + public HighsStatus DeleteColumnsByRange(int from, int to) => (HighsStatus)Imports.Highs_deleteColsByRange(_highs, from, to); /// /// Deletes multiple columns by set. /// /// /// - public Status DeleteColumnsBySet(int[] columns) => (Status)Imports.Highs_deleteColsBySet(_highs, columns.Length, columns); + public HighsStatus DeleteColumnsBySet(int[] columns) => (HighsStatus)Imports.Highs_deleteColsBySet(_highs, columns.Length, columns); /// /// Deletes multiple columns by mask. /// /// /// - public Status DeleteColumnsByMask(bool[] mask) => (Status)Imports.Highs_deleteColsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0)); + public HighsStatus DeleteColumnsByMask(bool[] mask) => (HighsStatus)Imports.Highs_deleteColsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0)); /// /// Deletes multiple rows by range. @@ -667,21 +667,21 @@ public Status ChangeColumnsIntegralityByRange(int from_col, int to_col, Variable /// /// /// - public Status DeleteRowsByRange(int from, int to) => (Status)Imports.Highs_deleteRowsByRange(_highs, from, to); + public HighsStatus DeleteRowsByRange(int from, int to) => (HighsStatus)Imports.Highs_deleteRowsByRange(_highs, from, to); /// /// Deletes multiple rows by set. /// /// /// - public Status DeleteRowsBySet(int[] rows) => (Status)Imports.Highs_deleteRowsBySet(_highs, rows.Length, rows); + public HighsStatus DeleteRowsBySet(int[] rows) => (HighsStatus)Imports.Highs_deleteRowsBySet(_highs, rows.Length, rows); /// /// Deletes multiple rows by mask. /// /// /// - public Status DeleteRowsByMask(bool[] mask) => (Status)Imports.Highs_deleteRowsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0)); + public HighsStatus DeleteRowsByMask(bool[] mask) => (HighsStatus)Imports.Highs_deleteRowsByMask(_highs, Array.ConvertAll(mask, x => x ? 1 : 0)); /// /// Delegate for getting info values. @@ -705,8 +705,8 @@ private TValue GetValueOrFallback(HighsGetInfoDelegate highsGetI { try { - var status = (Status)highsGetInfoDelegate(_highs, infoName, out var value); - return status != Status.Ok ? fallback : value; + var status = (HighsStatus)highsGetInfoDelegate(_highs, infoName, out var value); + return status != HighsStatus.Ok ? fallback : value; } catch { @@ -735,7 +735,7 @@ public SolutionInfo GetInfo() /// /// /// - public Status SetSolution(Solution solution) => (Status)Imports.Highs_setSolution(_highs, solution.ColumnValue, solution.ColumnDual, solution.RowValue, solution.RowDual); + public HighsStatus SetSolution(HighsSolution solution) => (HighsStatus)Imports.Highs_setSolution(_highs, solution.ColumnValue, solution.ColumnDual, solution.RowValue, solution.RowDual); /// /// Set a partial primal solution by passing values for a set of variables @@ -747,9 +747,9 @@ public SolutionInfo GetInfo() /// /// A dictionary that maps variable indices to variable values /// - public Status SetSparseSolution(IReadOnlyDictionary valuesByIndex) + public HighsStatus SetSparseSolution(IReadOnlyDictionary valuesByIndex) { - return (Status)Imports.Highs_setSparseSolution(_highs, valuesByIndex.Count, [.. valuesByIndex.Keys], [.. valuesByIndex.Values]); + return (HighsStatus)Imports.Highs_setSparseSolution(_highs, valuesByIndex.Count, [.. valuesByIndex.Keys], [.. valuesByIndex.Values]); } /// @@ -757,7 +757,7 @@ public Status SetSparseSolution(IReadOnlyDictionary valuesByIndex) /// /// /// - public Status GetBasicVariables(ref int[] basic_variables) => (Status)Imports.Highs_getBasicVariables(_highs, basic_variables); + public HighsStatus GetBasicVariables(ref int[] basic_variables) => (HighsStatus)Imports.Highs_getBasicVariables(_highs, basic_variables); /// /// Gets a row of the basis inverse. @@ -767,9 +767,9 @@ public Status SetSparseSolution(IReadOnlyDictionary valuesByIndex) /// /// /// - public Status GetBasisInverseRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) + public HighsStatus GetBasisInverseRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) { - return (Status)Imports.Highs_getBasisInverseRow(_highs, row, row_vector, ref row_num_nz, row_indices); + return (HighsStatus)Imports.Highs_getBasisInverseRow(_highs, row, row_vector, ref row_num_nz, row_indices); } /// @@ -780,9 +780,9 @@ public Status GetBasisInverseRow(int row, double[] row_vector, ref int row_num_n /// /// /// - public Status GetBasisInverseColumn(int column, double[] column_vector, ref int column_num_nz, int[] column_indices) + public HighsStatus GetBasisInverseColumn(int column, double[] column_vector, ref int column_num_nz, int[] column_indices) { - return (Status)Imports.Highs_getBasisInverseCol(_highs, column, column_vector, ref column_num_nz, column_indices); + return (HighsStatus)Imports.Highs_getBasisInverseCol(_highs, column, column_vector, ref column_num_nz, column_indices); } /// @@ -793,9 +793,9 @@ public Status GetBasisInverseColumn(int column, double[] column_vector, ref int /// /// /// - public Status GetBasisSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) + public HighsStatus GetBasisSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) { - return (Status)Imports.Highs_getBasisSolve(_highs, rhs, solution_vector, ref solution_num_nz, solution_indices); + return (HighsStatus)Imports.Highs_getBasisSolve(_highs, rhs, solution_vector, ref solution_num_nz, solution_indices); } /// @@ -806,9 +806,9 @@ public Status GetBasisSolve(double[] rhs, double[] solution_vector, ref int solu /// /// /// - public Status GetBasisTransposeSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) + public HighsStatus GetBasisTransposeSolve(double[] rhs, double[] solution_vector, ref int solution_num_nz, int[] solution_indices) { - return (Status)Imports.Highs_getBasisTransposeSolve(_highs, rhs, solution_vector, ref solution_num_nz, solution_indices); + return (HighsStatus)Imports.Highs_getBasisTransposeSolve(_highs, rhs, solution_vector, ref solution_num_nz, solution_indices); } /// @@ -819,9 +819,9 @@ public Status GetBasisTransposeSolve(double[] rhs, double[] solution_vector, ref /// /// /// - public Status GetReducedRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) + public HighsStatus GetReducedRow(int row, double[] row_vector, ref int row_num_nz, int[] row_indices) { - return (Status)Imports.Highs_getReducedRow(_highs, row, row_vector, ref row_num_nz, row_indices); + return (HighsStatus)Imports.Highs_getReducedRow(_highs, row, row_vector, ref row_num_nz, row_indices); } /// @@ -832,22 +832,22 @@ public Status GetReducedRow(int row, double[] row_vector, ref int row_num_nz, in /// /// /// - public Status GetReducedColumn(int column, double[] column_vector, ref int column_num_nz, int[] column_indices) + public HighsStatus GetReducedColumn(int column, double[] column_vector, ref int column_num_nz, int[] column_indices) { - return (Status)Imports.Highs_getReducedColumn(_highs, column, column_vector, ref column_num_nz, column_indices); + return (HighsStatus)Imports.Highs_getReducedColumn(_highs, column, column_vector, ref column_num_nz, column_indices); } /// /// Clears the model. /// /// - public Status ClearModel() => (Status)Imports.Highs_clearModel(_highs); + public HighsStatus ClearModel() => (HighsStatus)Imports.Highs_clearModel(_highs); /// /// Clears the solver. /// /// - public Status ClearSolver() => (Status)Imports.Highs_clearSolver(_highs); + public HighsStatus ClearSolver() => (HighsStatus)Imports.Highs_clearSolver(_highs); /// /// Passes the name of a column. @@ -855,7 +855,7 @@ public Status GetReducedColumn(int column, double[] column_vector, ref int colum /// /// /// - public Status PassColumnName(int column, string name) => (Status)Imports.Highs_passColName(_highs, column, name); + public HighsStatus PassColumnName(int column, string name) => (HighsStatus)Imports.Highs_passColName(_highs, column, name); /// /// Passes the name of a row. @@ -863,19 +863,19 @@ public Status GetReducedColumn(int column, double[] column_vector, ref int colum /// /// /// - public Status PassRowName(int row, string name) => (Status)Imports.Highs_passRowName(_highs, row, name); + public HighsStatus PassRowName(int row, string name) => (HighsStatus)Imports.Highs_passRowName(_highs, row, name); /// /// Writes the options to file. /// /// /// - public Status WriteOptions(string filename) => (Status)Imports.Highs_writeOptions(_highs, filename); + public HighsStatus WriteOptions(string filename) => (HighsStatus)Imports.Highs_writeOptions(_highs, filename); /// /// Writes the options deviations to file. /// /// /// - public Status WriteOptionsDeviations(string filename) => (Status)Imports.Highs_writeOptionsDeviations(_highs, filename); + public HighsStatus WriteOptionsDeviations(string filename) => (HighsStatus)Imports.Highs_writeOptionsDeviations(_highs, filename); } diff --git a/highs/interfaces/Highs/Imports.cs b/highs/interfaces/Highs/Imports.cs index b9c2979947..2e6e0ed10a 100644 --- a/highs/interfaces/Highs/Imports.cs +++ b/highs/interfaces/Highs/Imports.cs @@ -14,7 +14,7 @@ public static class Imports private const string HighsLibName = "highs"; #region Library Imports - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_lpCall( int numcol, int numrow, @@ -38,7 +38,7 @@ internal static extern int Highs_lpCall( int[] rowbasisstatus, ref int modelstatus); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_mipCall( int numcol, int numrow, @@ -59,7 +59,7 @@ internal static extern int Highs_mipCall( double[] rowvalue, ref int modelstatus); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_qpCall( int numcol, int numrow, @@ -89,31 +89,31 @@ internal static extern int Highs_qpCall( int[] rowbasisstatus, ref int modelstatus); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern IntPtr Highs_create(); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern void Highs_destroy(IntPtr highs); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_run(IntPtr highs); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_readModel(IntPtr highs, string filename); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_writeModel(IntPtr highs, string filename); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_writePresolvedModel(IntPtr highs, string filename); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_writeSolutionPretty(IntPtr highs, string filename); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getInfinity(IntPtr highs); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_passLp( IntPtr highs, int numcol, @@ -131,7 +131,7 @@ internal static extern int Highs_passLp( int[] aindex, double[] avalue); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_passMip( IntPtr highs, int numcol, @@ -150,7 +150,7 @@ internal static extern int Highs_passMip( double[] avalue, int[] highs_integrality); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_passModel( IntPtr highs, int numcol, @@ -174,7 +174,7 @@ internal static extern int Highs_passModel( double[] qvalue, int[] highs_integrality); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_passHessian( IntPtr highs, int dim, @@ -184,64 +184,64 @@ internal static extern int Highs_passHessian( int[] qindex, double[] qvalue); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_setOptionValue(IntPtr highs, string option, string value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_setBoolOptionValue(IntPtr highs, string option, int value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_setIntOptionValue(IntPtr highs, string option, int value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_setDoubleOptionValue(IntPtr highs, string option, double value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_setStringOptionValue(IntPtr highs, string option, string value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getBoolOptionValue(IntPtr highs, string option, out int value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getIntOptionValue(IntPtr highs, string option, out int value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getDoubleOptionValue(IntPtr highs, string option, out double value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getStringOptionValue(IntPtr highs, string option, [Out] StringBuilder value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getSolution(IntPtr highs, double[] colvalue, double[] coldual, double[] rowvalue, double[] rowdual); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getNumCol(IntPtr highs); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getNumRow(IntPtr highs); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getNumNz(IntPtr highs); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getHessianNumNz(IntPtr highs); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getBasis(IntPtr highs, int[] colstatus, int[] rowstatus); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern double Highs_getObjectiveValue(IntPtr highs); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getIterationCount(IntPtr highs); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getModelStatus(IntPtr highs); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_addRow(IntPtr highs, double lower, double upper, int num_new_nz, int[] indices, double[] values); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_addRows( IntPtr highs, int num_new_row, @@ -252,7 +252,7 @@ internal static extern int Highs_addRows( int[] indices, double[] values); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_addCol( IntPtr highs, double cost, @@ -262,7 +262,7 @@ internal static extern int Highs_addCol( int[] indices, double[] values); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_addCols( IntPtr highs, int num_new_col, @@ -274,82 +274,82 @@ internal static extern int Highs_addCols( int[] indices, double[] values); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_changeObjectiveSense(IntPtr highs, int sense); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_changeColCost(IntPtr highs, int column, double cost); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_changeColsCostBySet(IntPtr highs, int num_set_entries, int[] set, double[] cost); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_changeColsCostByMask(IntPtr highs, int[] mask, double[] cost); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_changeColBounds(IntPtr highs, int column, double lower, double upper); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_changeColsBoundsByRange(IntPtr highs, int from_col, int to_col, double[] lower, double[] upper); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_changeColsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_changeColsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_changeRowBounds(IntPtr highs, int row, double lower, double upper); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_changeRowsBoundsByRange(IntPtr highs, int from_row, int to_row, double[] lower, double[] upper); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_changeRowsBoundsBySet(IntPtr highs, int num_set_entries, int[] set, double[] lower, double[] upper); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_changeRowsBoundsByMask(IntPtr highs, int[] mask, double[] lower, double[] upper); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_changeColsIntegralityByRange(IntPtr highs, int from_col, int to_col, int[] integrality); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_changeCoeff(IntPtr highs, int row, int column, double value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_deleteColsByRange(IntPtr highs, int from_col, int to_col); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_deleteColsBySet(IntPtr highs, int num_set_entries, int[] set); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_deleteColsByMask(IntPtr highs, int[] mask); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_deleteRowsByRange(IntPtr highs, int from_row, int to_row); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_deleteRowsBySet(IntPtr highs, int num_set_entries, int[] set); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_deleteRowsByMask(IntPtr highs, int[] mask); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getDoubleInfoValue(IntPtr highs, string info, out double value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getIntInfoValue(IntPtr highs, string info, out int value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getInt64InfoValue(IntPtr highs, string info, out long value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_setSolution(IntPtr highs, double[] col_value, double[] row_value, double[] col_dual, double[] row_dual); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_setSparseSolution(IntPtr highs, int num_entries, int[] index, double[] value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getColsByRange( IntPtr highs, int from_col, @@ -363,7 +363,7 @@ internal static extern int Highs_getColsByRange( int[] matrix_index, double[] matrix_value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getColsBySet( IntPtr highs, int num_set_entries, @@ -377,7 +377,7 @@ internal static extern int Highs_getColsBySet( int[] matrix_index, double[] matrix_value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getColsByMask( IntPtr highs, int[] mask, @@ -390,7 +390,7 @@ internal static extern int Highs_getColsByMask( int[] matrix_index, double[] matrix_value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getRowsByRange( IntPtr highs, int from_row, @@ -403,7 +403,7 @@ internal static extern int Highs_getRowsByRange( int[] matrix_index, double[] matrix_value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getRowsBySet( IntPtr highs, int num_set_entries, @@ -416,7 +416,7 @@ internal static extern int Highs_getRowsBySet( int[] matrix_index, double[] matrix_value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getRowsByMask( IntPtr highs, int[] mask, @@ -428,16 +428,16 @@ internal static extern int Highs_getRowsByMask( int[] matrix_index, double[] matrix_value); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getBasicVariables(IntPtr highs, int[] basic_variables); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getBasisInverseRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getBasisInverseCol(IntPtr highs, int column, double[] col_vector, ref int col_num_nz, int[] col_indices); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getBasisSolve( IntPtr highs, double[] rhs, @@ -445,7 +445,7 @@ internal static extern int Highs_getBasisSolve( ref int solution_num_nz, int[] solution_indices); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getBasisTransposeSolve( IntPtr highs, double[] rhs, @@ -453,28 +453,28 @@ internal static extern int Highs_getBasisTransposeSolve( ref int solution_nz, int[] solution_indices); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getReducedRow(IntPtr highs, int row, double[] row_vector, ref int row_num_nz, int[] row_indices); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_getReducedColumn(IntPtr highs, int column, double[] col_vector, ref int col_num_nz, int[] col_indices); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_clearModel(IntPtr highs); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_clearSolver(IntPtr highs); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_passColName(IntPtr highs, int column, string name); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_passRowName(IntPtr highs, int row, string name); - [LibraryImport(HighsLibName)] + [DllImport(HighsLibName)] internal static extern int Highs_writeOptions(IntPtr highs, string filename); - [LibraryImport(HighsLibName)] - internal static extern int Highs_writeOptionsDeviations(IntPtr highs, string filename); + [DllImport(HighsLibName)] + internal static extern int Highs_writeOptionsDeviations(IntPtr highs, string filename); #endregion } \ No newline at end of file diff --git a/highs/interfaces/Highs/Records/Model.cs b/highs/interfaces/Highs/Records/HighsModel.cs similarity index 94% rename from highs/interfaces/Highs/Records/Model.cs rename to highs/interfaces/Highs/Records/HighsModel.cs index b496f6f34c..0bd2d610ee 100644 --- a/highs/interfaces/Highs/Records/Model.cs +++ b/highs/interfaces/Highs/Records/HighsModel.cs @@ -5,7 +5,7 @@ namespace Highs.Records; /// /// This defines a model for Highs /// -public record Model +public record HighsModel { /// /// The objective sense diff --git a/highs/interfaces/Highs/Records/Solution.cs b/highs/interfaces/Highs/Records/HighsSolution.cs similarity index 84% rename from highs/interfaces/Highs/Records/Solution.cs rename to highs/interfaces/Highs/Records/HighsSolution.cs index d0baefcafe..76f0ddf7e0 100644 --- a/highs/interfaces/Highs/Records/Solution.cs +++ b/highs/interfaces/Highs/Records/HighsSolution.cs @@ -1,26 +1,26 @@ -namespace Highs.Records; - -/// -/// The solution. -/// -/// The column value. -/// The column dual. -/// The row value. -/// The row dual. -public record Solution(double[] ColumnValue, - double[] ColumnDual, - double[] RowValue, - double[] RowDual) -{ - /// - /// The default constructor creates empty arrays - /// - /// The number of columns - /// The number of rows - public Solution(int numberOfColumns, int numberOfRows) : this(new double[numberOfColumns], - new double[numberOfColumns], - new double[numberOfRows], - new double[numberOfRows]) - { - } -} +namespace Highs.Records; + +/// +/// The solution. +/// +/// The column value. +/// The column dual. +/// The row value. +/// The row dual. +public record HighsSolution(double[] ColumnValue, + double[] ColumnDual, + double[] RowValue, + double[] RowDual) +{ + /// + /// The default constructor creates empty arrays + /// + /// The number of columns + /// The number of rows + public HighsSolution(int numberOfColumns, int numberOfRows) : this(new double[numberOfColumns], + new double[numberOfColumns], + new double[numberOfRows], + new double[numberOfRows]) + { + } +}