How to declare int variables dynamically in java without using external LP file?

View: New views
2 Messages — Rating Filter:   Alert me  

How to declare int variables dynamically in java without using external LP file?

by rcrmca :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Can anybody please tell me how to declare int variables dynamically in java without using external LP file?

here is the example program and i want to add int x,y in Integer definition.

/*
 *
 
 P = max: 25.0 x + 40.0 y;
25.0 x  <= 100.0;
40.0 y  <= 80.0;



 *
 * Created on October 8, 2009, 2:08 PM
 */

/**
 *
 * @author  christopher
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import lpsolve.*;

public class Demo {

        public Demo() {
        }

        public int execute() throws LpSolveException {
          LpSolve lp;
          int Ncol, j, ret = 0;

          /* We will build the model row by row
             So we start with creating a model with 0 rows and 2 columns */
          Ncol = 2; /* there are two variables in the model */

          /* create space large enough for one row */
          int[] colno = new int[Ncol];
          double[] row = new double[Ncol];

          lp = LpSolve.makeLp(0, Ncol);
          if(lp.getLp() == 0)
            ret = 1; /* couldn't construct a new model... */

          if(ret == 0) {
            /* let us name our variables. Not required, but can be useful for debugging */
            lp.setColName(1, "x");
            lp.setColName(2, "y");

            lp.setAddRowmode(true);  /* makes building the model faster if it is done rows by row */

            /* construct first row (110 x + 30 y <= 4000) */
            j = 0;

            colno[j] = 1; /* first column */
            row[j++] = 25.0;

            colno[j] = 2; /* second column */
            row[j++] = 0;

            /* add the row to lpsolve */
            lp.addConstraintex(j, row, colno, LpSolve.LE, 100);
          }

          if(ret == 0) {
            /* construct second row (x + y <= 75) */
            j = 0;

            colno[j] = 1; /* first column */
            row[j++] = 0;

            colno[j] = 2; /* second column */
            row[j++] = 40.0;

            /* add the row to lpsolve */
            lp.addConstraintex(j, row, colno, LpSolve.LE, 80);
          }

          if(ret == 0) {
            lp.setAddRowmode(false); /* rowmode should be turned off again when done building the model */

            /* set the objective function (143 x + 60 y) */
            j = 0;

            colno[j] = 1; /* first column */
            row[j++] = 25.0;

            colno[j] = 2; /* second column */
            row[j++] = 40.0;

            /* set the objective in lpsolve */
            lp.setObjFnex(j, row, colno);
          }

          if(ret == 0) {
            /* set the object direction to maximize */
            lp.setMaxim();

            /* just out of curioucity, now generate the model in lp format in file christ.lp */
            lp.writeLp("christ.lp");

            /* I only want to see important messages on screen while solving */
            lp.setVerbose(LpSolve.IMPORTANT);

            /* Now let lpsolve calculate a solution */
            ret = lp.solve();
            if(ret == LpSolve.OPTIMAL)
              ret = 0;
            else
              ret = 5;
          }

          if(ret == 0) {
            /* a solution is calculated, now lets get some results */

            /* objective value */
            System.out.println("Objective value: " + lp.getObjective());

            /* variable values */
            lp.getVariables(row);
            for(j = 0; j < Ncol; j++)
              System.out.println(lp.getColName(j + 1) + ": " + row[j]);

            /* we are done now */
          }

          /* clean up such that all used memory by lpsolve is freed */
          if(lp.getLp() != 0)
            lp.deleteLp();

          return(ret);
        }

        public static void main(String[] args) {
                try {
                    //System.load("\\LpFiles\\lpsolve55j.dll");
                        new Demo().execute();
                }
                catch (LpSolveException e) {
                        e.printStackTrace();
                }
        }
}

i want this christ.LP has int declaration as,

/* Objective function */
max: +25 x +40 y;

/* Constraints */
+25 x <= 100;
+40 y <= 80;

/* Integer Definitions */
int x,y;

How can i?



Re: How to declare int variables dynamically in java without using external LP f

by peter_notebaert :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

http://lpsolve.sourceforge.net/5.5/Java/docs/api/lpsolve/LpSolve.html#setInt(int, boolean)

Peter

--- In lp_solve@..., "rcrmca" <rcrmca@...> wrote:

>
> Can anybody please tell me how to declare int variables dynamically in java without using external LP file?
>
> here is the example program and i want to add int x,y in Integer definition.
>
> /*
>  *
>  
>  P = max: 25.0 x + 40.0 y;
> 25.0 x  <= 100.0;
> 40.0 y  <= 80.0;
>
>
>
>  *
>  * Created on October 8, 2009, 2:08 PM
>  */
>
> /**
>  *
>  * @author  christopher
>  */
> import java.io.BufferedReader;
> import java.io.IOException;
> import java.io.InputStreamReader;
>
> import lpsolve.*;
>
> public class Demo {
>
> public Demo() {
> }
>
> public int execute() throws LpSolveException {
>           LpSolve lp;
>           int Ncol, j, ret = 0;
>
>           /* We will build the model row by row
>              So we start with creating a model with 0 rows and 2 columns */
>           Ncol = 2; /* there are two variables in the model */
>
>           /* create space large enough for one row */
>           int[] colno = new int[Ncol];
>           double[] row = new double[Ncol];
>
>           lp = LpSolve.makeLp(0, Ncol);
>           if(lp.getLp() == 0)
>             ret = 1; /* couldn't construct a new model... */
>
>           if(ret == 0) {
>             /* let us name our variables. Not required, but can be useful for debugging */
>             lp.setColName(1, "x");
>             lp.setColName(2, "y");
>
>             lp.setAddRowmode(true);  /* makes building the model faster if it is done rows by row */
>
>             /* construct first row (110 x + 30 y <= 4000) */
>             j = 0;
>
>             colno[j] = 1; /* first column */
>             row[j++] = 25.0;
>
>             colno[j] = 2; /* second column */
>             row[j++] = 0;
>
>             /* add the row to lpsolve */
>             lp.addConstraintex(j, row, colno, LpSolve.LE, 100);
>           }
>
>           if(ret == 0) {
>             /* construct second row (x + y <= 75) */
>             j = 0;
>
>             colno[j] = 1; /* first column */
>             row[j++] = 0;
>
>             colno[j] = 2; /* second column */
>             row[j++] = 40.0;
>
>             /* add the row to lpsolve */
>             lp.addConstraintex(j, row, colno, LpSolve.LE, 80);
>           }
>
>           if(ret == 0) {
>             lp.setAddRowmode(false); /* rowmode should be turned off again when done building the model */
>
>             /* set the objective function (143 x + 60 y) */
>             j = 0;
>
>             colno[j] = 1; /* first column */
>             row[j++] = 25.0;
>
>             colno[j] = 2; /* second column */
>             row[j++] = 40.0;
>
>             /* set the objective in lpsolve */
>             lp.setObjFnex(j, row, colno);
>           }
>
>           if(ret == 0) {
>             /* set the object direction to maximize */
>             lp.setMaxim();
>
>             /* just out of curioucity, now generate the model in lp format in file christ.lp */
>             lp.writeLp("christ.lp");
>
>             /* I only want to see important messages on screen while solving */
>             lp.setVerbose(LpSolve.IMPORTANT);
>
>             /* Now let lpsolve calculate a solution */
>             ret = lp.solve();
>             if(ret == LpSolve.OPTIMAL)
>               ret = 0;
>             else
>               ret = 5;
>           }
>
>           if(ret == 0) {
>             /* a solution is calculated, now lets get some results */
>
>             /* objective value */
>             System.out.println("Objective value: " + lp.getObjective());
>
>             /* variable values */
>             lp.getVariables(row);
>             for(j = 0; j < Ncol; j++)
>               System.out.println(lp.getColName(j + 1) + ": " + row[j]);
>
>             /* we are done now */
>           }
>
>           /* clean up such that all used memory by lpsolve is freed */
>           if(lp.getLp() != 0)
>             lp.deleteLp();
>
>           return(ret);
>         }
>
> public static void main(String[] args) {
> try {
>                     //System.load("\\LpFiles\\lpsolve55j.dll");
> new Demo().execute();
> }
> catch (LpSolveException e) {
> e.printStackTrace();
> }
> }
> }
>
> i want this christ.LP has int declaration as,
>
> /* Objective function */
> max: +25 x +40 y;
>
> /* Constraints */
> +25 x <= 100;
> +40 y <= 80;
>
> /* Integer Definitions */
> int x,y;
>
> How can i?
>