0

Does anyone know how to import a matrix from an csv or xlsx file into the data file of a CPlex project in the CPlex studio?

I am using CPlex studio on Linux, and I am trying to solve a linear program. I have an inequality constraint in the form of Ax<=b. I have A and b as .csv or xlsx files. I need to load A and b into the .dat file of my CPlex studio project. Following an online tutorial, I am using the SheetConnection Data_A("A.xlsx"); command. But the program would not compile and I get the following error, "Sheet data not supported on this platform". I used the .csv file instead, yet I get the same error.

Does anyone know how to import data in the form of a matrix into CPlex project, besides the SheetConnection command?

Kind regards, and I look forward to hearing from you.

Ahmad

1 Answers1

0

You can use CSVRead:

nbKids=300;

// New feature in CPLEX 20.10

CSVConnection csvinput("zoobusesinput.csv?firstIsHeader=true", "");
buses from CSVRead(csvinput, "");

CSVConnection csvoutput("zoobusesoutput.csv?outputOnly=true&append=false&outputSeparator=;", "");
result to CSVPublish(csvoutput, "");

or if you use a to

o old CPLEX version you can [call some python to do the job][2]

// Read from an Excel spreadsheet without SheetRead
// which means you can use this on non Windows platform

execute
    {

    function read_excel(filename,sheetname,skiprows,nrows,cols,datfilename,resname)
    {
        var quote="\"";
        
        var python=new IloOplOutputFile("c:\\temp\\readexcel.py");
        
        python.writeln("import pandas as pd");
        python.writeln("import xlrd");
        python.writeln("df=pd.read_excel('"+filename+"'"+",sheet_name = '"+sheetname+"'"+
        ",skiprows = "+skiprows+  ",nrows= "+nrows+ ","
        +"header=None,usecols = '"+cols+"')");
        python.writeln("print(df)");
        
        
        
        python.writeln("res = open(",quote,datfilename,quote,",",quote,"w",quote,")");
        python.writeln("res.write(",quote,resname,"=[",quote,")");
        python.writeln("res.write(",quote,"\\","n",quote,")");
        python.writeln("for i, row in enumerate(df.values):");
       
        python.writeln("   res.write(",quote,"[",quote,")");
        
        python.writeln("   for j in row:");
       
        python.writeln("      if (j==j):");
        python.writeln("         res.write(str(j))");
        python.writeln("         res.write(\",\")");
       
        python.writeln("   res.write(\"],\")    ");
        python.writeln("   res.write(",quote,"\\","n",quote,")");
        python.writeln("res.write(\"];\")");
        python.writeln("res.close()");
        python.close();
       
        python.close();
        
        IloOplExec("C:\\Python36\\python.exe c:\\temp\\readexcel.py",true);
        
    }
    read_excel("c:\\\\temp\\\\read2Darray.xls","Sheet1",0,2,"B:D","c:\\\\temp\\\\resexcel","res");
}    
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15