Skip to main content

Solve string data

Answered

Comments

5 comments

  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Mohamadreza,

    Good practice when building a model is to separate the logical model from the data model.

    We demonstrate this in our python examples where we build and solve the classic diet problem: diet.py

    We then show how to separate the data and model into different python files: diet2.py and dietmodel.py

    We also show how to populate the data using a SQLite database: diet3.py

    Could you please review these and let us know if you need any clarification?

    - Riley

     

    0
  • Mohamadreza Amini
    Gurobi-versary
    First Question
    First Comment

    Thank you Riley for your response.

    Unfortunately, the files linked did not provide me with the right solution. The sample I have is given below:

    import gurobipy as gp

    # Define the model
    model = gp.Model()

    # Define the problem data as a string
    data_string = """ Maximize
     3 x + 2 y
    Subject To
     Constraint1: 2 x + y <= 10
     Constraint2: x + 3 y <= 12
    Bounds
     x >= 0
     y >= 0
    End
    """

    # Pass the problem data string to the model using the read() method
    model.read(datastring=data_string)

    # Optimize the model
    model.optimize()

    # Print the optimal solution
    print("Optimal solution:")
    for v in model.getVars():
      print(v.varName, v.x)
    print("Objective value:", model.objVal)
    When the model
    Maximize
    3 x + 2 y
    Subject To
    Constraint1: 2 x + y <= 10
    Constraint2: x + 3 y <= 12
    Bounds
    x >= 0
    y >= 0
    End
    is read from a file such as model.lp, the code works perfectly and prints the results. However, in the code snippet I have provided you with, I want to do exactly the same; I have a string of my model which in a real application is selected from my database and then passed to be solved, etc. The format is as given. I would not like to use model methods, etc. but pass a string to the read method. 
    I hope it clarifies my point.
     
    thanks
    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Mohamadreza,

    The functionality you are after does not exist, there is only one way to read a problem in LP file format, and that is to read from a file.

    However we can create a function that does what you want, by writing the string to a temporary file then using gurobipy to read the file, as an intermediate step:

    import gurobipy as gp
    from tempfile import NamedTemporaryFile

    def read_from_string(string):
      with NamedTemporaryFile(mode="w", suffix=".lp") as f:
            f.write(string)
            f.flush()
            return gp.read(f.name)

    You can then use it like so:

    prob = """
    Maximize
      .01 Pennies + .05 Nickels + .1 Dimes + .25 Quarters + 1 Dollars
    Subject To
      Copper: .06 Pennies + 3.8 Nickels + 2.1 Dimes + 5.2 Quarters + 7.2 Dollars -
         Cu = 0
      Nickel: 1.2 Nickels + .2 Dimes + .5 Quarters + .2 Dollars -
         Ni = 0
      Zinc: 2.4 Pennies + .5 Dollars - Zi = 0
      Manganese: .3 Dollars - Mn = 0
    Bounds
      Cu <= 1000
      Ni <= 50
      Zi <= 50
      Mn <= 50
    Integers
      Pennies Nickels Dimes Quarters Dollars
    End
    """

    m = read_from_string(prob)
    m.optimize()

    - Riley

    0
  • Mohamadreza Amini
    Gurobi-versary
    First Question
    First Comment

    Ah all right. Since I need to solve huge models, it will take a lot more time to first write and then read from a file. Therefore, I was wondering if there is another approach.

    Thank you for your response.

    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Mohamadreza,

    It might be worth investigating whether your database can store files.  A compressed MPS file (with extension .mps.bz2) can be directly read by Gurobi, and would be much smaller than the equivalent string.

    - Riley

    0

Please sign in to leave a comment.