0

I am new to CPLEX Python API. I wish to solve a Linear Programming problem in python which I have already done in the CPLEX OPL IDE by taking a .mod and .dat files as inputs. I want to use it in python since I wish to vary my inputs continuously. My mod file for the problem is given below. Can someone help me on how to use this for the python API.

int n = ...; 
int m = ...; 

int c = ...; 
int s = ...; 

range v = 1..n;
range p = 1..m;

int c_req[v] = ...;
int s_req[v] = ...;

int trust[v][v] = ...;


// decision variables

dvar boolean assign[p][v]; 

// expressions

dexpr int used[pi in p] = max(vi in v) assign[pi][v]; // used[i] = 1     iff pi is used
dexpr int totalUsed = sum(pi in p) used[pi];

execute {
  cplex.tilim = 60; // Time limit 60 seconds
}

// model

minimize totalUsed;

subject to {
  forall(pi in p) 
    c_cap:
    sum(vi in v) c_req[vi] * assign[pi][vi] <= c;

  forall(pi in p)
    s_cap:
    sum(vi in v) s_req[vi] * assign[pi][vi] <= s;

  forall(vi in v)
    v_all:
    sum(pi in p) assign[pi][vi] == 1;

  forall(pi in p, v1 in v, v2 in v) if (v1 < v2) if (trust[v1][v2] ==  0)
    trust_constraint:
    assign[p][v1] + assign[p][v2] <= 1;
}

1 Answer 1

0

you could write

subprocess.check_call(["C:/CPLEXStudio127/opl/bin/x64_win64/oplrun", "diet.mod", "diet.dat"]) 

in order to call OPL from python. And you would generate diet.dat beforehand.

Full example at https://www.ibm.com/developerworks/community/forums/html/threadTopic?id=0b6cacbe-4dda-4da9-9282-f527c3464f47

Then you do not have to migrate your model from OPL to Python.

You may also translate your model to Python and then I recommend DOCPLEX : https://developer.ibm.com/docloud/documentation/optimization-modeling/modeling-for-python/

regards

Sign up to request clarification or add additional context in comments.

1 Comment

If you generate the diet.dat, please make sure the file handle is flushed and closed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.