Skip to content

Commit

Permalink
Merge pull request #1902 from ERGO-Code/fix-1900a
Browse files Browse the repository at this point in the history
Fixing #1900
  • Loading branch information
jajhall authored Aug 28, 2024
2 parents f5a22b6 + 0eee142 commit 8fce625
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 10 deletions.
56 changes: 46 additions & 10 deletions examples/call_highs_from_csharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@

class Program {
static void Main(string[] args) {
double[] cc = {1, -2};
double[] cl = {0, 0};
double[] cu = {10, 10};
double[] rl = {0, 0};
double[] ru = {2, 1};
int[] astart = {0, 2};
int[] aindex = {0, 1, 0, 1};
double[] avalue = {1, 2, 1, 3};
// Illustrate the solution of a QP, after first solving just the LP
//
// minimize x_2 + (1/2)(2x_1^2 - 2x_1x_3 + 0.2x_2^2 + 2x_3^2)
//
// subject to x_1 + x_2 + x_3 >= 1; x>=0
double[] cc = {0, 1, 0};
double[] cl = {0, 0, 0};
double[] cu = {1.0e30, 1.0e30, 1.0e30};
double[] rl = {1};
double[] ru = {1.0e30};
int[] astart = {0, 3};
int[] aindex = {0, 1, 2};
double[] avalue = {1, 1, 1};
HighsObjectiveSense sense = HighsObjectiveSense.kMinimize;
double offset = 0;
HighsMatrixFormat a_format = HighsMatrixFormat.kColwise;
HighsMatrixFormat a_format = HighsMatrixFormat.kRowwise;

HighsModel model = new HighsModel(cc, cl, cu, rl, ru, astart, aindex, avalue, null, offset, a_format, sense);

Expand All @@ -31,6 +36,9 @@ static void Main(string[] args) {
Console.WriteLine("Status: " + status);
Console.WriteLine("Modelstatus: " + modelStatus);

for (int i=0; i<sol.colvalue.Length; i++) {
Console.WriteLine("Activity for col " + i + " = " + sol.colvalue[i]);
}
for (int i=0; i<sol.rowvalue.Length; i++) {
Console.WriteLine("Activity for row " + i + " = " + sol.rowvalue[i]);
}
Expand All @@ -43,5 +51,33 @@ static void Main(string[] args) {
for (int i=0; i<sol.colvalue.Length; i++) {
Console.WriteLine("x" + i + " = " + sol.colvalue[i] + " is " + bas.colbasisstatus[i]);
}
// Add the Hessian
int dim = 2;
int[] qstart = {0, 2, 3};
int[] qindex = {0, 1, 1};
double[] qvalue = {2, -1, 2};
HessianFormat q_format = HessianFormat.kTriangular;
HighsHessian hessian = new HighsHessian(dim, qstart, qindex, qvalue, q_format);
status = solver.passHessian(hessian);
status = solver.run();
sol = solver.getSolution();
modelStatus = solver.GetModelStatus();

Console.WriteLine("Status: " + status);
Console.WriteLine("Modelstatus: " + modelStatus);

for (int i=0; i<sol.colvalue.Length; i++) {
Console.WriteLine("Activity for col " + i + " = " + sol.colvalue[i]);
}
for (int i=0; i<sol.rowvalue.Length; i++) {
Console.WriteLine("Activity for row " + i + " = " + sol.rowvalue[i]);
}
for (int i=0; i<sol.coldual.Length; i++) {
Console.WriteLine("Reduced cost x[" + i + "] = " + sol.coldual[i]);
}
for (int i=0; i<sol.rowdual.Length; i++) {
Console.WriteLine("Dual value for row " + i + " = " + sol.rowdual[i]);
}

}
}
}
78 changes: 78 additions & 0 deletions src/interfaces/highs_csharp_api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public enum HighsMatrixFormat
kRowwise
}

public enum HessianFormat
{
kTriangular = 1,
kSquare
}

public enum HighsBasisStatus
{
kLower = 0,
Expand Down Expand Up @@ -104,6 +110,29 @@ public HighsModel(double[] colcost, double[] collower, double[] colupper, double
}
}

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;
Expand Down Expand Up @@ -236,6 +265,40 @@ private static extern int Highs_passMip(
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);

Expand Down Expand Up @@ -275,6 +338,9 @@ private static extern int Highs_passMip(
[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);

Expand Down Expand Up @@ -657,6 +723,18 @@ public HighsStatus passMip(HighsModel model)
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);
Expand Down

0 comments on commit 8fce625

Please sign in to comment.