Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing #1900 #1902

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading