IBMDecisionOptimization / doopl-examples

IBM OPL Connector for Python
Apache License 2.0
6 stars 9 forks source link

when I would run my opl model with python I got this exception #5

Closed MehdiGAIED closed 4 years ago

MehdiGAIED commented 4 years ago

code:

from doopl.factory import * import os from os.path import dirname, abspath, join DATADIR=join(dirname(abspath(file)),'data') mod=join(DATADIR,"test.mod") dat=join(DATADIR,"test.dat") gen_dir=os.path.join(DATADIR,"generated")

if not os.path.isdir(gen_dir): os.makedirs(gen_dir)

Create an OPL model from a .mod file

with create_opl_model(model=mod, data=dat) as opl:

tuple can be a list of tuples, a pandas dataframe...

#opl.set_input("TupleSet1", tuples)

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

print(opl)
print(opl.objective_value)
print(opl.get_table("InVolumeThroughHubonTestRes"))
print(opl.get_table("OutVolumeThroughHubonTestRes"))
print(opl.report)                

error return _opl.IloEnv__createOplModel(self, modelSource)

IloException

vlkong commented 4 years ago

Hi,

Can you please be more precise in your question ? I don't understand what is wrong, I don't have datasets to reproduce.

Thanks for providing your data, as well as a full log of your error.

MehdiGAIED commented 4 years ago

I had an optimization problem that I wrote it with OPL , and now I 'm trying to run it with python .When I executed the code I had an IloException. you find here my opl code:

Mehdi Gaied ICT engineering Student-ENIT mehdi.gaied@etudiant-enit.utm.tn mehdi.gaied@etudiant-enit.utm.tn +21655700787

Le jeu. 11 juin 2020 à 15:44, Viu-Long Kong (江偉綸) notifications@github.com a écrit :

Hi,

Can you please be more precise in your question ? I don't understand what is wrong, I don't have datasets to reproduce.

Thanks for providing your data, as well as a full log of your error.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/IBMDecisionOptimization/doopl-examples/issues/5#issuecomment-642658042, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALGS5HSEFO4VLYJONVSAK33RWDNUBANCNFSM4N3M4UTA .

/*****

//les variables

int n=30;// nombre de users int napl=16; //nombre d'ap LIFI int napw=1;// nombre d'ap wifi int nap=napl+napw; //nombre d'ap total float Pdl=0.8;// proportion de wifi en downlink int T=10*n; // TDMA****T selon l'article est égal k(=10 dans l'article') fois le nombre de users donc T=20 range temps=1..T; //intervalle d'entier associé au temps d'association float debit_phy[0..n-1][0..nap-1];// le débit physique {accesspt} APwifi={<0,<7.5,7.5,1.5>,2>}; {user} users={<i,<rand(15),rand(15),rand(3)>>|i in 1..n}; {accesspt} APlifi={<4i+j-4,<3.75i,3.75*j,0>,1>|i,j in 1..4};//AP lifi en grille {accesspt} AP=APlifi union APwifi; //les aps totals float D[u in 0..n-1][ap in 0 ..nap-1]=sqrt(pow(item(users,u).position.x-item(AP,ap).position.x,2)+pow(item(users,u).position.y-item(AP,ap).position.y,2)+pow(item(users,u).position.z-item(AP,ap).position.z,2));//matrice de distance

execute calcul_debit {// dépend de la distance

var debit ;

for (var u = 0; u <= n-1; u++) { for (var ap = 0; ap <= nap-1; ap++)

  {
  if(D[u][ap] <=1)      
    debit =800;
else  if( 1<D[u][ap] && 2>=D[u][ap])
     debit=600;
 else  if (D[u][ap]<=4 && D[u][ap]>2)
      debit=400;
 else
     debit=0.00001;

debit_phy[u][ap]=debit;    
   }

} }

/****/ //déclarer les variables de décisions

dvar boolean y[AP][users][temps];// la sortie de l'algorithme est une matrice binaire à 3 dimensions //qui donne les associations entre AP et utilisateur qui auront lieu

//model

     maximize 

sum(u in 0..n-1) sum(ap in 0..nap-1) sum(t in temps ) y[item(AP,ap)][item(users,u)][t]log(debit_phy[u][ap](t/T));//debit_atteint=debit_phy.t/T

//les contraintes à satisfaire

subject to {

//un utilisateur ne peut pas etre associé à plus q'un AP forall(u in 0..n-1) ( sum(ap in 0..nap-1) sum(t in temps) y[item(AP,ap)][item(users,u)][t])==1;

//le temps d'allocation d'un AP lifi ne peut pas dépasser l'intervalle de TDMA T ***cette contrainte doit etre valable pour tous les APs lifi forall(ap in 0..napl-1)

 (sum(u in 0..n-1) sum(t in temps)y[item(APlifi,ap)][item(users,u)][t]*(t/T))<=1;

// le temps l'allocation d'un APwifi ne peut pas dépasser l'intervalle de TDMA T ****cette contrainte doit etre valable pour tous les APswifi forall(ap in 0..napw-1) (sum(u in 0..n-1) sum(t in temps) y[item(APwifi,ap)][item(users,u)][t](t/T))<=Pdl;

}

execute Display{

  for (var u in users ){

      for (var ap in AP){

             for(var t=1 ;t<=T; t++){

               if(y[ap][u][t]==1){

                       writeln("l'utilisateur",u," est associé au point d'accés ",ap, " pendant ",t," intervalles de T")  ;                              
                                                       }              

                                       }

                                           }

                                    }

   }
MehdiGAIED commented 4 years ago

full log: runfile('C:/Users/Client/Desktop/essai.py', wdir='C:/Users/Client/Desktop') Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/Client/Desktop/essai.py', wdir='C:/Users/Client/Desktop')

File "D:\anaconda\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace)

File "D:\anaconda\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Client/Desktop/essai.py", line 26, in with create_opl_model(model=mod, data=dat) as opl:

File "D:\anaconda\lib\contextlib.py", line 112, in enter return next(self.gen)

File "D:\anaconda\lib\site-packages\doopl\factory.py", line 41, in create_opl_model opl = _env._createOplModel(_modelSource)

File "D:\anaconda\lib\site-packages\doopl\internal\opl12100\opl.py", line 96, in _createOplModel return _opl.IloEnv__createOplModel(self, modelSource)

IloException

MehdiGAIED commented 4 years ago

You need more details?

vlkong commented 4 years ago

Yes. What is the IloException message ?

Doopl never stops saying just IloException. It always display the error message like File XXX Not found etc...

vlkong commented 4 years ago

It works for me:

[P37] C:\work\misc\doopl\issue_5_iloexception>python test.py
Found incumbent of value -209.109518 after 0.02 sec. (9.62 ticks)
Tried aggregator 1 time.
MIP Presolve eliminated 0 rows and 1800 columns.
Reduced MIP has 47 rows, 151200 columns, and 302400 nonzeros.
Reduced MIP has 151200 binaries, 0 generals, 0 SOSs, and 0 indicators.
Presolve time = 0.28 sec. (277.40 ticks)
Tried aggregator 1 time.
Reduced MIP has 47 rows, 151200 columns, and 302400 nonzeros.
Reduced MIP has 151200 binaries, 0 generals, 0 SOSs, and 0 indicators.
Presolve time = 0.38 sec. (319.80 ticks)
Probing time = 0.17 sec. (76.82 ticks)
Clique table members: 132.
MIP emphasis: balance optimality and feasibility.
MIP search method: dynamic search.
Parallel mode: deterministic, using up to 12 threads.
Root relaxation solution time = 0.64 sec. (611.67 ticks)

        Nodes                                         Cuts/
   Node  Left     Objective  IInf  Best Integer    Best Bound    ItCnt     Gap

*     0+    0                         -209.1095    49535.1556              ---
*     0+    0                         -169.4992    49535.1556              ---
*     0+    0                         -162.2468    49535.1556              ---
*     0+    0                         -140.2626    49535.1556              ---
*     0+    0                         -132.8290    49535.1556              ---
*     0+    0                         -127.0091    49535.1556              ---
*     0+    0                         -118.9053    49535.1556              ---
*     0+    0                         -112.7161    49535.1556              ---
      0     0       56.2815    23     -112.7161       56.2815        0  149.93%
*     0+    0                           55.5354       56.2815             1.34%
      0     0       55.9450    30       55.5354      Cuts: 38      996    0.74%
      0     0       55.9440    32       55.5354      Cuts: 10     1096    0.74%
*     0+    0                           55.8835       55.9440             0.11%
*     0+    0                           55.9335       55.9440             0.02%
*     0+    0                           55.9365       55.9440             0.01%
*     0+    0                           55.9393       55.9440             0.01%

Cover cuts applied:  8
Flow cuts applied:  4
Mixed integer rounding cuts applied:  3
Zero-half cuts applied:  12
Gomory fractional cuts applied:  1

Root node processing (before b&c):
  Real time             =    5.33 sec. (4179.29 ticks)
Parallel b&c, 12 threads:
  Real time             =    0.00 sec. (0.00 ticks)
  Sync time (average)   =    0.00 sec.
  Wait time (average)   =    0.00 sec.
                          ------------
Total (root+branch&cut) =    5.33 sec. (4179.29 ticks)
l'utilisateur <1 <1 6 2>> est associé au point d'accés  <2 <3.75 7.5 0> 1> pendant 150 intervalles de T
l'utilisateur <2 <9 12 1>> est associé au point d'accés  <7 <7.5 11.25 0> 1> pendant 150 intervalles de T
l'utilisateur <3 <1 13 0>> est associé au point d'accés  <3 <3.75 11.25 0> 1> pendant 150 intervalles de T
l'utilisateur <4 <14 0 0>> est associé au point d'accés  <13 <15 3.75 0> 1> pendant 300 intervalles de T
l'utilisateur <5 <10 5 2>> est associé au point d'accés  <5 <7.5 3.75 0> 1> pendant 150 intervalles de T
l'utilisateur <6 <0 13 2>> est associé au point d'accés  <16 <15 15 0> 1> pendant 300 intervalles de T
l'utilisateur <7 <11 10 2>> est associé au point d'accés  <11 <11.25 11.25 0> 1> pendant 150 intervalles de T
l'utilisateur <8 <9 14 2>> est associé au point d'accés  <12 <11.25 15 0> 1> pendant 151 intervalles de T
l'utilisateur <9 <9 10 1>> est associé au point d'accés  <6 <7.5 7.5 0> 1> pendant 150 intervalles de T
l'utilisateur <10 <11 8 1>> est associé au point d'accés  <10 <11.25 7.5 0> 1> pendant 150 intervalles de T
l'utilisateur <11 <4 12 2>> est associé au point d'accés  <3 <3.75 11.25 0> 1> pendant 150 intervalles de T
l'utilisateur <12 <10 12 2>> est associé au point d'accés  <12 <11.25 15 0> 1> pendant 149 intervalles de T
l'utilisateur <13 <12 7 0>> est associé au point d'accés  <10 <11.25 7.5 0> 1> pendant 150 intervalles de T
l'utilisateur <14 <2 5 1>> est associé au point d'accés  <1 <3.75 3.75 0> 1> pendant 150 intervalles de T
l'utilisateur <15 <10 7 0>> est associé au point d'accés  <9 <11.25 3.75 0> 1> pendant 150 intervalles de T
l'utilisateur <16 <14 6 2>> est associé au point d'accés  <14 <15 7.5 0> 1> pendant 150 intervalles de T
l'utilisateur <17 <6 12 1>> est associé au point d'accés  <8 <7.5 15 0> 1> pendant 149 intervalles de T
l'utilisateur <18 <1 14 2>> est associé au point d'accés  <4 <3.75 15 0> 1> pendant 150 intervalles de T
l'utilisateur <19 <1 0 1>> est associé au point d'accés  <8 <7.5 15 0> 1> pendant 151 intervalles de T
l'utilisateur <20 <5 10 1>> est associé au point d'accés  <6 <7.5 7.5 0> 1> pendant 150 intervalles de T
l'utilisateur <21 <11 9 2>> est associé au point d'accés  <0 <7.5 7.5 1.5> 2> pendant 240 intervalles de T
l'utilisateur <22 <5 12 2>> est associé au point d'accés  <7 <7.5 11.25 0> 1> pendant 150 intervalles de T
l'utilisateur <23 <10 5 2>> est associé au point d'accés  <5 <7.5 3.75 0> 1> pendant 150 intervalles de T
l'utilisateur <24 <12 4 2>> est associé au point d'accés  <9 <11.25 3.75 0> 1> pendant 150 intervalles de T
l'utilisateur <25 <14 9 2>> est associé au point d'accés  <14 <15 7.5 0> 1> pendant 150 intervalles de T
l'utilisateur <26 <1 7 2>> est associé au point d'accés  <2 <3.75 7.5 0> 1> pendant 150 intervalles de T
l'utilisateur <27 <14 10 0>> est associé au point d'accés  <15 <15 11.25 0> 1> pendant 300 intervalles de T
l'utilisateur <28 <3 2 1>> est associé au point d'accés  <1 <3.75 3.75 0> 1> pendant 150 intervalles de T
l'utilisateur <29 <11 11 2>> est associé au point d'accés  <11 <11.25 11.25 0> 1> pendant 150 intervalles de T
l'utilisateur <30 <3 14 0>> est associé au point d'accés  <4 <3.75 15 0> 1> pendant 150 intervalles de T
oplmodel:
     mod file: C:\work\misc\doopl\issue_5_iloexception\data\test.mod
     0 .dat files
     0 python structures

55.93934628398623

with test.py as:

from doopl.factory import *
import os
from os.path import dirname, abspath, join

DATADIR=join(dirname(abspath(__file__)),'data')
mod=join(DATADIR,"test.mod")
dat=join(DATADIR,"test.dat")
gen_dir=os.path.join(DATADIR,"generated")

if not os.path.isdir(gen_dir):
    os.makedirs(gen_dir)

with create_opl_model(model=mod) as opl:
    opl.run()

    print(opl)
    print(opl.objective_value)
    print(opl.get_table("InVolumeThroughHubonTestRes"))
    print(opl.get_table("OutVolumeThroughHubonTestRes"))
    print(opl.report) 
MehdiGAIED commented 4 years ago

I'm using anconda spyder and do not work with me should I use python directly?

vlkong commented 4 years ago

given your stack, you don't have any issues with spyder.

You still did not tell me what the IloException message is. File XXX not found ? Something else? Without this information, I cannot help.

Please read my test.py above carefully and compare this to your code (or try my version). I believe your code is wrong since you are passing create_opl_model() a dat file that does not exist.

MehdiGAIED commented 4 years ago

I tried ur code and I got this: NameError Traceback (most recent call last)

in 3 from os.path import dirname, abspath, join 4 ----> 5 DATADIR=join(dirname(abspath(__file__)),'data') 6 mod=join(DATADIR,"test.mod") 7 dat=join(DATADIR,"test.dat") NameError: name '__file__' is not defined
vlkong commented 4 years ago

This is your code, please have a look at your first post :-) The only thing I changed was create_opl_model(model=mod)

Are you running the code in a python interactive ? __file__ is not defined in python interactive, you need to put the code in a script.

If you have code that does not work, please create a github project with the code to reproduce. Or at least, verbatim your code using ``` blocks, like this: ```

# verbatim code that will not be interpreted as markdown
DATADIR=join(dirname(abspath(__file__)),'data')

```

And please be precise in expressing the context of your error (is it in a script? is it in a python interactive).

Otherwise, I'm spending my time guessing what you want to say instead of helping you with your issue. Thanks.

MehdiGAIED commented 4 years ago

Yes sur i'm using script (anaconda spyder ) and i verbatim ur code but i still have the some problem erreur

MehdiGAIED commented 4 years ago

this is my script: code

vlkong commented 4 years ago

So the code above run without theNameError: name 'file' is not defined, because you have some IOExceptions from OPL. So you are not running your script in the same conditions than a few post back.

Is your data in a data directory ?

Please create a github project with your spyder project, including data and script so that I can reproduce with just a git clone.

MehdiGAIED commented 4 years ago

I defined all my data and the data file is empty. this is my project directory:

essai.zip

MehdiGAIED commented 4 years ago

I mean I defined all my data on the essai.mod and essai.dat is empty as shown

MehdiGAIED commented 4 years ago

I create a github project: https://github.com/mehdi2019gaied/opl-project

vlkong commented 4 years ago

Your project cannot work. There is no 'data/' directory.

Your essai.py is basically looking for data/essai.mod. However, there is no data/ directory.

Please use:

mod="test.mod"

instead of

mod=join(DATADIR, "test.mod")
MehdiGAIED commented 4 years ago

thanks sur it works now ! I execute it on anaconda prompt . sur I want to do post processing after resolving the problem .so How can I get my decision variable value on python and how I can do post processing with python . for exmple : I want to use the values of the matrix Y (decision variable of my opl problem )to do some instructions on python.

vlkong commented 4 years ago

Please have a look at this sample:

https://github.com/IBMDecisionOptimization/doopl-examples/blob/master/examples/Iterators.py

It shows you how to iterate on some output tuples in your model.

MehdiGAIED commented 4 years ago

thanks ! but I got it the some result .there is no data frame with the the solution

vlkong commented 4 years ago

Please upload your full program here: project https://github.com/mehdi2019gaied/opl-project

In your project, there are only empty .mod and .dat

I cannot help if you don't provide an example showing what is not working.

Thanks

MehdiGAIED commented 4 years ago

Done Sur !

vlkong commented 4 years ago

Your python code wants to iterate on a tupleset named solution:

    list = opl.get_table("solution")
    for t in list.itertuples(index=False):
        print(t)

My understanding of your model is that your solution is in array y and you are interested with cells which value is 1.

In your test.mod, before the execute Display block, define your solution tupleset and populate it:

tuple SolutionEntry {
   int userid;
   int ida;
   int t;
}

{SolutionEntry} solution = {<u.idu, ap.ida, t> | ap in AP, u in users, t in 1..T : y[ap][u][t]==1 };
MehdiGAIED commented 4 years ago

it works .thank u very much Sur! Can I also use my output y and all my input data and sets to create a graph with python ?

vlkong commented 4 years ago

Yes, of course. Once you get the data frame, you can easily graph your data with matplotlib.

Please have a look at https://matplotlib.org/tutorials/index.html

MehdiGAIED commented 4 years ago

I will try and if i get problem i return to u .

btw , u can make more than one argument on opl.get table ?

vlkong commented 4 years ago

What do you mean by more than one argument ? opl.get_table() accepts only only one arguments that is the name of the table you want. You can call opl.get_table() for as many table as you need.

MehdiGAIED commented 4 years ago

yup yup Sur I did it as u say and it works !