Skip to main content

Calling jar file through Anylogic

Answered

Comments

10 comments

  • Riley Clement
    Gurobi Staff Gurobi Staff

    HI Saeed,

    Anylogic bundles it's own JRE.  Somewhere in the depths of the Anylogic installation folder on your machine you will find java executables.  If you run the following in a command line

    java -version

    with their java executable it will print to screen and tell you what version it is.

    When you install Gurobi you will find java libraries inside the lib folder which is inside the Gurobi installation directory.

    See ¸How do I install Gurobi Optimizer? if you need to.  I would verify you can run Gurobi from the command line first, before attempting to call it from Anylogic.

    but it says there is no solver! I don't understand what this means

    I also don't understand what this means - there is some detail missing here.  Can you post the full message and explain when/how/where it occurs?

    - Riley

     

    0
  • Saeed Abdolmaleki
    First Question
    Gurobi-versary
    First Comment

    Hi Dr. Riley,

     

    Thanks for your swift reply. Sure, let me share my code error on Anylogic while I was attempting to solve a very simple Linear Problem via a function module, and I execute the function body embedded code via a button. :

     

    My Optimization model :

    import gurobi.*;

    public class SimpleLP {
        public static void main(String[] args) {
            try {
          
                GRBEnv env = new GRBEnv("lp_problem.log");
                GRBModel model = new GRBModel(env);

             
                GRBVar x1 = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x1");
                GRBVar x2 = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x2");
                GRBVar x3 = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x3");

              
                GRBLinExpr objective = new GRBLinExpr();
                objective.addTerm(3.0, x1);
                objective.addTerm(5.0, x2);
                objective.addTerm(2.0, x3);
                model.setObjective(objective, GRB.MAXIMIZE);

           
                GRBLinExpr constraint1 = new GRBLinExpr();
                constraint1.addTerm(2.0, x1);
                constraint1.addTerm(1.0, x2);
                constraint1.addTerm(1.0, x3);
                model.addConstr(constraint1, GRB.LESS_EQUAL, 20.0, "c1");

               
                GRBLinExpr constraint2 = new GRBLinExpr();
                constraint2.addTerm(4.0, x1);
                constraint2.addTerm(3.0, x2);
                constraint2.addTerm(1.0, x3);
                model.addConstr(constraint2, GRB.LESS_EQUAL, 42.0, "c2");

        
                GRBLinExpr constraint3 = new GRBLinExpr();
                constraint3.addTerm(3.0, x1);
                constraint3.addTerm(2.0, x2);
                constraint3.addTerm(1.0, x3);
                model.addConstr(constraint3, GRB.LESS_EQUAL, 30.0, "c3");

               
                model.optimize();

                
                System.out.println("Objective Value: " + model.get(GRB.DoubleAttr.ObjVal));
                System.out.println("x1: " + x1.get(GRB.DoubleAttr.X));
                System.out.println("x2: " + x2.get(GRB.DoubleAttr.X));
                System.out.println("x3: " + x3.get(GRB.DoubleAttr.X));

               
                model.dispose();
                env.dispose();
            } catch (GRBException e) {
                e.printStackTrace();
            }
        }
    }


    And my Error:


    java.lang.Error: Unresolved compilation problems:
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "import", delete this token
    Illegal modifier for the local class SimpleLP; only abstract or final is permitted
    The method main cannot be declared static; static methods can only be declared in a static or top level type
    GRBEnv cannot be resolved to a type
    GRBEnv cannot be resolved to a type
    GRBModel cannot be resolved to a type
    GRBModel cannot be resolved to a type
    GRBVar cannot be resolved to a type
    GRB cannot be resolved to a variable
    GRB cannot be resolved to a variable
    GRBVar cannot be resolved to a type
    GRB cannot be resolved to a variable
    GRB cannot be resolved to a variable
    GRBVar cannot be resolved to a type
    GRB cannot be resolved to a variable
    GRB cannot be resolved to a variable
    GRBLinExpr cannot be resolved to a type
    GRBLinExpr cannot be resolved to a type
    GRB cannot be resolved to a variable
    GRBLinExpr cannot be resolved to a type
    GRBLinExpr cannot be resolved to a type
    GRB cannot be resolved to a variable
    GRBLinExpr cannot be resolved to a type
    GRBLinExpr cannot be resolved to a type
    GRB cannot be resolved to a variable
    GRBLinExpr cannot be resolved to a type
    GRBLinExpr cannot be resolved to a type
    GRB cannot be resolved to a variable
    GRB cannot be resolved to a variable
    GRB cannot be resolved to a variable
    GRB cannot be resolved to a variable
    GRB cannot be resolved to a variable
    GRBException cannot be resolved to a type

     

    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Saeed,

    I've uploaded a simple example to a public repo to demonstrate calling Gurobi from Anylogic.
    https://github.com/venaturum/Gurobi_Anylogic_Example

    Note that for Gurobi v11+ you will need to use
    import com.gurobi.gurobi.*;

    not

    import gurobi.*;

    I also don't think it makes sense to use a static main method in this context.

    Here is a copy of the README from the repo...

    Before running you should verify you can compile and run a simple java example outside of Anylogic, from your command line. E.g. navigate to the examples/java directory of your Gurobi installation and run

    javac -classpath .:/Library/gurobi1103/macos_universal2/lib/gurobi.jar Diet.java
    
    java -cp .:/Library/gurobi1103/macos_universal2/lib/gurobi.jar Diet
    
    The structure of the project is as follows:

    The MIP1 class is based off the Mip1 java example, which can be found here: https://docs.gurobi.com/projects/examples/en/current/examples/mip1.html

    The static main method has been modified to be a public method.

     

    The Main agent contains a simple button which instantiates a new MIP1 object and runs it

     

    In the project settings you need to add the gurobi.jar file as a dependency. The jar file is inside the lib directory of the Gurobi installation folder. It's location on your machine may be different.

    Expected result:

    - Riley

     

    0
  • Saeed Abdolmaleki
    First Question
    Gurobi-versary
    First Comment

    Thank you for letting me know how to implement java based Gurobi in Anylogic with the concrete given example. It works!

    0
  • Yohanes Kristianto Nugroho
    First Comment

    Thanks for the example. How to transfer the console outputs x,y,z to variables in the simulation canvas?

    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Yohanes,

    You will want to return the solution values from the function.  You can either do this as an array of values, or create your own solution class and return an instance.

    Then in the code that calls the function, capture the returned object and use it to update the values of variables in the canvas.

    - Riley

    0
  • Yohanes Kristianto Nugroho
    First Comment

    Thank you. I made a function and return it to a variable on a canvas. But a message appears as follow:

    Cannot make a static reference to the non static field output.

    Can you help me to solve the problem ?

    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Did you declare your function as static?

    0
  • Yohanes Kristianto Nugroho
    First Comment

    Yes, the function returns nothing:

    please take a look the code from Gurobi:

    /* Copyright 2024, Gurobi Optimization, LLC */

    /* This example formulates and solves the following simple MIP model:

         maximize    x +   y + 2 z
         subject to  x + 2 y + 3 z <= 4
                     x +   y       >= 1
                     x, y, z binary
    */

    import com.gurobi.gurobi.*;

    public class Mip1 {
    public static double run() {

    double output =0.0;

      try {

          // Create empty environment, set options, and start
          GRBEnv env = new GRBEnv(true);
          env.set("logFile", "mip1.log");
          env.start();

          // Create empty model
          GRBModel model = new GRBModel(env);

          // Create variables
          GRBVar x = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "x");
          GRBVar y = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "y");
          GRBVar z = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "z");

          // Set objective: maximize x + y + 2 z
          GRBLinExpr expr = new GRBLinExpr();
          expr.addTerm(1.0, x); expr.addTerm(1.0, y); expr.addTerm(2.0, z);
          model.setObjective(expr, GRB.MAXIMIZE);

          // Add constraint: x + 2 y + 3 z <= 4
          expr = new GRBLinExpr();
          expr.addTerm(1.0, x); expr.addTerm(2.0, y); expr.addTerm(3.0, z);
          model.addConstr(expr, GRB.LESS_EQUAL, 4.0, "c0");

          // Add constraint: x + y >= 1
          expr = new GRBLinExpr();
          expr.addTerm(1.0, x); expr.addTerm(1.0, y);
          model.addConstr(expr, GRB.GREATER_EQUAL, 1.0, "c1");

          // Optimize model
          model.optimize();

          System.out.println(x.get(GRB.StringAttr.VarName)
                             + " " +x.get(GRB.DoubleAttr.X));
          System.out.println(y.get(GRB.StringAttr.VarName)
                             + " " +y.get(GRB.DoubleAttr.X));
          System.out.println(z.get(GRB.StringAttr.VarName)
                             + " " +z.get(GRB.DoubleAttr.X));

          System.out.println("Obj: " + model.get(GRB.DoubleAttr.ObjVal));

          // Dispose of model and environment
          model.dispose();
          env.dispose();

        } catch (GRBException e) {
          System.out.println("Error code: " + e.getErrorCode() + ". " +
                             e.getMessage());
        }

    finally {

    System.out.println("Thank you for using the program!");

    }

    return output;

    }
    }

    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Don't declare it as static...

    0

Please sign in to leave a comment.