Merve Ozcetin
Merve Ozcetin

Reputation: 59

Calling a CPLEX (.mod) file which is linked with Excel

I want to call a .mod CPLEX file from Python. In the next link, there are instructions on how to call CPLEX within Python:

How to run a .mod file (CPLEX) using python?

Code

But, in my case, the .mod file uses data read from Excel. In this case, do I need to use:

import pandas

Is it a correct or good approach to do something like this (calling a CPLEX (.mod) file which is linked with Excel)?

In the links, buses are defined as a bus structure that has nbSeats and cost features.

But what if there is no such structure in the problem, and there are only separate variables and parameters, then what should we use instead of opl.set_input()?

For example, if there are only variables or parameters like nbKids defined in the codes, how we can pass it from Python to a CPLEX .mod file?

Upvotes: 1

Views: 347

Answers (1)

Alex Fleischer
Alex Fleischer

Reputation: 10037

as you can see in https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoocalloplwithdataindatfile.py

you can use a .dat file with doopl to read / write data with OPL from python:

from doopl.factory import *


# Create an OPL model from a .mod file
with create_opl_model(model="zootupleset.mod",data="zootupleset.dat") as opl:
    

    # Generate the problem and solve it.
    opl.run()

    # Get the names of post processing tables
    print("Table names are: "+ str(opl.output_table_names))

    # Get all the post processing tables as dataframes.
    for name, table in iteritems(opl.report):
        print("Table : " + name)
    for t in table.itertuples(index=False):
            print(t)

    # nicer display
    for t in table.itertuples(index=False):
        print(t[0]," buses ",t[1], "seats")

and then in the .dat you may use SheetRead to connect with an Excel spreadsheet.

See https://github.com/AlexFleischerParis/zooopl/blob/master/zooexcel.dat

SheetConnection s("zoo.xlsx");

params from SheetRead(s,"params!A2");
buses from SheetRead(s,"buses!A2:B3");
results to SheetWrite(s,"buses!E2:F3");

Upvotes: 2

Related Questions