0

I am trying to solve the following equation:

A+2B+3C+E+2D+3F >=16

A, B, C, D, E, F >=1

While A, B, C, D, E, F belongs to integer.

My code in C# works, just the problem is the answers are in double.

Is there a way to constraint the variables as integers?

double[] c = new double[] { 1, 2, 3, 1, 2, 3 };
double[,] a = new double[,]
{
{ 1, 2, 3, 1, 2, 3 },
{ 1, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0 },
{ 0, 0, 0, 0, 1, 0 },
{ 0, 0, 0, 0, 0, 1 }
};
double[] lowerBounds = new double[] { 1, 1, 1, 1, 1, 1 };
double[] upperBounds = new double[] { double.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity };
double[] s = new double[] { 1, 1, 1, 1, 1, 1 };
double[] x;
double[] al = new double[] { 16, 1, 1, 1, 1, 1, 1, 1 };
double[] au = new double[] { System.Double.PositiveInfinity, System.Double.PositiveInfinity, System.Double.PositiveInfinity, System.Double.PositiveInfinity, System.Double.PositiveInfinity, System.Double.PositiveInfinity, System.Double.PositiveInfinity };
alglib.minlpstate state;
alglib.minlpreport rep;
alglib.minlpcreate(6, out state);
alglib.minlpsetcost(state, c);
alglib.minlpsetbc(state, lowerBounds, upperBounds);
alglib.minlpsetlc2dense(state, a, al, au, 7);
alglib.minlpsetscale(state, s);
alglib.minlpoptimize(state);
alglib.minlpresults(state, out x, out rep);
System.Diagnostics.Debug.WriteLine("{0}", alglib.ap.format(x, 3)); // EXPECTED: [0,1]

I tried to look through the ALGLIB documentary, but I find no where it mentions constraint variables as integer. I hope there is a way to do it, or maybe I should not use minlp, maybe there is a package for inter linear programming.

EDIT: I can convert the double to integer for the result, but then the equation will not be satisfied. Since the answer is {1.860,1.329,1.161,1.860,1.329,1.161}, if I convert them to integer {2, 1, 1, 2, 1, 1}, then it will not satisfied A+2B+3C+E+2D+3F >=16.

Henry Yu
  • 1
  • 1
  • This is fairly typical of math libraries in C# because of the lack of polymorphic number types and operators. You can always convert your result into an array of integral values after you've performed all of your desired processing with the library in question. That said, you haven't explained why doubles are not appropriate for you. – Aluan Haddad Jun 14 '23 at 06:46
  • I can convert the double to integer for the result, but then the equation will not be satisfied. Since the answer is {1.860,1.329,1.161,1.860,1.329,1.161}, if I convert them to integer {2, 1, 1, 2, 1, 1}, then it will not satisfied A+2B+3C+E+2D+3F >=16. – Henry Yu Jun 14 '23 at 07:17
  • Oh dear, I didn't realize the library was doing that, not having used it myself. Sorry if I wasted your time – Aluan Haddad Jun 14 '23 at 07:17

0 Answers0