IBMDecisionOptimization / docplex-examples

These samples demonstrate how to use the DOcplex library to model and solve optimization problems.
https://ibmdecisionoptimization.github.io/
Apache License 2.0
396 stars 229 forks source link

Getting Error "'QuadExpr' object has no attribute 'number_of_terms'" #42

Closed amirvaez closed 3 years ago

amirvaez commented 3 years ago

Hi,

I am writing a code for a Large-scale quadratic optimization in Docplex and I am getting the error "'QuadExpr' object has no attribute 'number_of_terms". Does anyboady know how I can resolve this issue? My code is as follows (After defining the prmaeters and reading the data sets:

start = time.time() from docplex.mp.advmodel import AdvModel as Model

Stocastic FMO model

S1_FMO=Model('S1_FMO')

defining the the parameters and the variables

W_S1=S1_FMO.continuous_var_dict(Beamlet_Ind,name='W_S1')

defining the objective function ( Minimizing the dose to the heart)

S1_FMO.minimize(S1_FMO.sum(pi[j]H[k,j,b,o]W_S1[k,b]for (k,j,b,o) in Heart_Ind))

defining the clinical constraint ( full-volume constraints)

cts=S1_FMO.add_constraints((S1_FMO.sum(pi[j] A[k,j,b,v]W_S1[k,b] for k,j,b in Dose)-Lv)*2-1.961.96S1_FMO.sum((S1_FMO.sum( A[k,j,b,v]W_S1[k,b] for k in K for b in B))*2 Var1[j] for j in P)>=0 for v in V ) SOL=S1_FMO.solve(log_output=True) end = time.time() print("Total time:",end - start) print(SOL)

PhilippeCouronne commented 3 years ago

Hi

he code snippet above is not self-contained (Beamlet_Ind, is not defined, among others). To allow us to reproduce, could you either send a self-contained piece of code, or the Python stack you are getting?

In addition, do you confirm the problem happens with the latest version of DOcplex , that is 2.19.202 of Dec 2020? You can run the following command to get the version you ar erunning

python -c "import docplex; print(docplex.__version_info__")

Thanks in advance.

  Philippe
PhilippeCouronne commented 3 years ago

Without code, I can only make guesses, here's one with a simple method to try:

I notice you are using AdvModel, this class disables all type-checking by default. It may happen that you build an expression with unexpected arguments, and end up with a strange message, which would have been diagnosed earlier by type-checking with a clearer message. I suggest you add an extra checker='on' keyword argument when creating the AdvModel instance and see whether this changes the behavior. If so, I advise to keep type-checking on while you develop the model, and remove it only when the model is safe and you need a faster model building performance.

Code to try: create your model instance with checking enabled and see if you get a different behavior:

S1_FMO=Model('S1_FMO', checker='on)

Hope this helps

          Philippe.
PhilippeCouronne commented 3 years ago

OK, I think I understand what happened: as you are using AdvModel, type-checking is disabled by default (this will be changed in the next version) so you don't get any clear message when you pass unexpected arguments to methods. In your case, defining a batch of quadratic constraints, you should use Model.add_quadratic_constraints and not Model.add_constraints, which expects linear constraints.

In short, use Model.add_quadratic_constraints to define a collection of quadratic constraints, and also enable type-checking when developing a Model with AdvModel

amirvaez commented 3 years ago

Hi phillippe,

I did this before and got an error like what I have typed below. I am using CPLEX 12.10. It is really frustrated and I am deciding to shift everything to Matlab since docpelx has a lot of discrepencies. Please let me know if there is a way to resolve this issue!

AdvModel does not have object add_quadratic_constraints

Amirhossein Vaeztehrani, Ph.D. candidate,

Department of Management Sciences,

University of Waterloo


From: PhilippeCouronne notifications@github.com Sent: Thursday, January 28, 2021 12:12 PM To: IBMDecisionOptimization/docplex-examples docplex-examples@noreply.github.com Cc: Amir Vaeztehrani amir.vaeztehrani@uwaterloo.ca; Author author@noreply.github.com Subject: Re: [IBMDecisionOptimization/docplex-examples] Getting Error "'QuadExpr' object has no attribute 'number_of_terms'" (#42)

OK, I think I understand what happened: as you are using AdvModel, type-checking is disabled by default (this will be changed in the next version) so you don't get any clear message when you pass unexpected arguments to methods. In your case, defining a batch of quadratic constraints, you should use Model.add_quadratic_constraints and not Model.add_constraints, which expects linear constraints.

In short, use Model.add_quadratic_constraints to define a collection of quadratic constraints, and also enable type-checking when developing a Model with AdvModel

- You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/IBMDecisionOptimization/docplex-examples/issues/42#issuecomment-769236325, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALCI4DE7K5KYGRAOL23A373S4GLJFANCNFSM4WLBQYRQ.

PhilippeCouronne commented 3 years ago

Hi,

AdvModel is a subclass of docplex.mp.Model hence has access to Model.add_quadratic_constraints()

The following code demonstrates how to add quadratic constraints to an adv model:

mdl  = AdvModel(name='q3', checker='on')
mdl.environment.print_information()
x, y = mdl.continuous_var_list(keys=['x', 'y'])
x2 = x ** 2
y2 = y ** 2
xy = x * y
qc1 = (x2 <= 3)
qc2 = (xy >= 1)
qc3 = (y2 <= 3)
qcs = [qc1, qc2, qc3]
mdl.add_quadratic_constraints(qcs)
mdl.print_information()

The output is:

* Python version 3.7.7, located at: C:\python\anaconda2020.02\envs\docplex_dev37\python.exe
* docplex is present, version is 2.19.0
* CPLEX library is present, version is 20.1.0.0, located at: C:\OPTIM\cplex_distrib\cplex2001R1\python\3.7\x64_win64
* pandas is present, version is 1.0.3
* numpy is present, version is 1.18.1
Model: q3
 - number of variables: 2
   - binary=0, integer=0, continuous=2
 - number of constraints: 3
   - linear=0, quadratic=3
 - parameters: defaults
 - objective: none
 - problem type is: QCP
amirvaez commented 3 years ago

This is the error I am getting. It does not still work for me.

[cid:1054ab26-2892-4af1-a2c9-18121f38446a]

Amirhossein Vaeztehrani, Ph.D. candidate,

Department of Management Sciences,

University of Waterloo


From: PhilippeCouronne notifications@github.com Sent: Thursday, January 28, 2021 12:35 PM To: IBMDecisionOptimization/docplex-examples docplex-examples@noreply.github.com Cc: Amir Vaeztehrani amir.vaeztehrani@uwaterloo.ca; Author author@noreply.github.com Subject: Re: [IBMDecisionOptimization/docplex-examples] Getting Error "'QuadExpr' object has no attribute 'number_of_terms'" (#42)

Hi,

AdvModel is a subclass of docplex.mp.Model hence has access to Model.add_quadratic_constraints()

The following code demonstrates how to add quadratic constraints to an adv model:

mdl = AdvModel('on') mdl.environment.print_information() x, y = mdl.continuous_var_list(keys=['x', 'y']) x2 = x 2 y2 = y 2 xy = x * y qc1 = (x2 <= 3) qc2 = (xy >= 1) qc3 = (y2 <= 3) qcs = [qc1, qc2, qc3] mdl.add_quadratic_constraints(qcs) mdl.print_information()

The output is:

PhilippeCouronne commented 3 years ago

I can see no image or text.

amirvaez commented 3 years ago

I mean this one

AttributeError: 'AdvModel' object has no attribute 'add_quadratic_constraints'

Amirhossein Vaeztehrani, Ph.D. candidate,

Department of Management Sciences,

University of Waterloo


From: PhilippeCouronne notifications@github.com Sent: Thursday, January 28, 2021 12:52 PM To: IBMDecisionOptimization/docplex-examples docplex-examples@noreply.github.com Cc: Amir Vaeztehrani amir.vaeztehrani@uwaterloo.ca; Author author@noreply.github.com Subject: Re: [IBMDecisionOptimization/docplex-examples] Getting Error "'QuadExpr' object has no attribute 'number_of_terms'" (#42)

I can see no image or text.

- You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/IBMDecisionOptimization/docplex-examples/issues/42#issuecomment-769262783, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALCI4DBE2WMOBXXI6GOYK2DS4GP5RANCNFSM4WLBQYRQ.

PhilippeCouronne commented 3 years ago

I don't get it. The code I posted above works on docplex 2.19, latest version from pypi. Which version of docplex do you have and what's the output of the above code snippet?

amirvaez commented 3 years ago

I am using docplex 2.14 and it is not working for me yet. The output of the whole code is a set of optimal values for variables and the objective function.

Amirhossein Vaeztehrani, Ph.D. candidate,

Department of Management Sciences,

University of Waterloo


From: PhilippeCouronne notifications@github.com Sent: Thursday, January 28, 2021 1:13 PM To: IBMDecisionOptimization/docplex-examples docplex-examples@noreply.github.com Cc: Amir Vaeztehrani amir.vaeztehrani@uwaterloo.ca; Author author@noreply.github.com Subject: Re: [IBMDecisionOptimization/docplex-examples] Getting Error "'QuadExpr' object has no attribute 'number_of_terms'" (#42)

I don't get it. The code I posted above works on docplex 2.19, latest version from pypi. Which version of docplex do you have and what's the output of the above code snippet?

- You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/IBMDecisionOptimization/docplex-examples/issues/42#issuecomment-769277012, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALCI4DDORPRAAPV2E6BKVU3S4GSN7ANCNFSM4WLBQYRQ.

PhilippeCouronne commented 3 years ago

OK, I strongly suggest you update to the latest version, that is 2.19. The command to run is

pip install --upgrade docplex

Once this is done, execute the DOcplex check list by running:

python -m docplex.mp.check_list

in an environment where docplex code is accessible by the PYTHONPATH. This command checks all modules and paths relative to DOcplex, and reports any issues. This is equivalent to calling python and running

from docplex.mp.check_list import * run_docplex_check_list()

As for the code output, I was of course referring to the output of the code snippet I posted in this comment: https://github.com/IBMDecisionOptimization/docplex-examples/issues/42#issuecomment-769252335. Once you have updated to the latest version, run the code example to see what happens when adding quadratic constraints.

amirvaez commented 3 years ago

I wrote this code in my anaconda prompt and I am getting the following error.

ERROR: Cannot uninstall 'docplex'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall. Also in docplex checklist, I am getting

I am getting the same error for the code snippet

AttributeError: 'AdvModel' object has no attribute 'add_quadratic_constraints'

Amirhossein Vaeztehrani, Ph.D. candidate,

Department of Management Sciences,

University of Waterloo


From: PhilippeCouronne notifications@github.com Sent: Thursday, January 28, 2021 4:54 PM To: IBMDecisionOptimization/docplex-examples docplex-examples@noreply.github.com Cc: Amir Vaeztehrani amir.vaeztehrani@uwaterloo.ca; Author author@noreply.github.com Subject: Re: [IBMDecisionOptimization/docplex-examples] Getting Error "'QuadExpr' object has no attribute 'number_of_terms'" (#42)

OK, I strongly suggest you update to the latest version, that is 2.19. The command to run is

pip install --upgrade docplex

Once this is done, execute the DOcplex check list by running:

python -m docplex.mp.check_list

in an environment where docplex code is accessible by the PYTHONPATH. This command checks all modules and paths relative to DOcplex, and reports any issues. This is equivalent to calling python and running

from docplex.mp.check_list import * run_docplex_check_list()

As for the code output, I was of course referring to the output of the code snippet I posted in this comment:

42 (comment)https://github.com/IBMDecisionOptimization/docplex-examples/issues/42#issuecomment-769252335.

Once you have updated to the latest version, run the code example to see what happens when adding quadratic constraints.

- You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/IBMDecisionOptimization/docplex-examples/issues/42#issuecomment-769424378, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALCI4DARFKLLHP7QTYASPITS4HMJHANCNFSM4WLBQYRQ.

PhilippeCouronne commented 3 years ago

Hello,

The errors you are getting when upgrading docplex are due to an old version of 'pip'. I will explain in a later section below how to fix this and manage the upgrade to 2.19.

This said, upgrading is not mandatory. Method Model.add_quadratic_constraints was introduced in version 2.16 and this is why you don't have it. To post a batch of quadratic constraints with 2.15, you had to do it in a for loop, using Model.add_constraint, as in the following code, which should work "as is" in your version

mdl  = AdvModel(name='q3', checker='on')
mdl.environment.print_information()
x, y = mdl.continuous_var_list(keys=['x', 'y'])
x2 = x ** 2
y2 = y ** 2
xy = x * y
qc1 = (x2 <= 3)
qc2 = (xy >= 1)
qc3 = (y2 <= 3)
qcs = [qc1, qc2, qc3]
for qc in qcs:
    mdl.add_constraint(qc)
mdl.print_information()

Now, if you want to upgrade to 2.19 (which I recommend, to benefit from bug fixes and new features), you have to deal with the ancient version of pip. Run the following command, which reinstalls the latest docplex, whatever the ancient version is:

pip install --ignore-installed docplex

At the end of the day, you should be able to handle quadratic constraints, with or without updating docplex.

Let me know if this works.

    Philippe.
amirvaez commented 3 years ago

Thank you Phillipe for your detailed response. I really appreciate it. I really prefer to upgrade my docplex. Although I run pip install --ignore-installed docplex both generally and in the directory of my python path, I am still getting this checklist in my Jupyter notebook.

while running the above commend I got his in my terminal

ERROR: spyder 4.1.4 requires pyqt5<5.13; python_version >= "3", which is not installed. ERROR: spyder 4.1.4 requires pyqtwebengine<5.13; python_version >= "3", which is not installed. Installing collected packages: certifi, idna, urllib3, chardet, requests, six, docplex Successfully installed certifi-2020.12.5 chardet-4.0.0 docplex-2.19.202 idna-2.10 requests-2.25.1 six-1.15.0 urllib3-1.26.3

Which I guess means that i am still using docpelx 2.15 in note book eventhough I have upgraded it. Is there any way to fix this?


From: PhilippeCouronne notifications@github.com Sent: Monday, February 1, 2021 5:43 AM To: IBMDecisionOptimization/docplex-examples docplex-examples@noreply.github.com Cc: Amir Vaeztehrani amir.vaeztehrani@uwaterloo.ca; Author author@noreply.github.com Subject: Re: [IBMDecisionOptimization/docplex-examples] Getting Error "'QuadExpr' object has no attribute 'number_of_terms'" (#42)

Hello,

The errors you are getting when upgrading docplex are due to an old version of 'pip'. I will explain in a later section below how to fix this and manage the upgrade to 2.19.

This said, upgrading is not mandatory. Method Model.add_quadratic_constraints was introduced in version 2.16 and this is why you don't have it. To post a batch of quadratic constraints with 2.15, you had to do it in a for loop, using Model.add_constraint, as in the following code, which should work "as is" in your version

mdl = AdvModel(name='q3', checker='on') mdl.environment.print_information() x, y = mdl.continuous_var_list(keys=['x', 'y']) x2 = x 2 y2 = y 2 xy = x * y qc1 = (x2 <= 3) qc2 = (xy >= 1) qc3 = (y2 <= 3) qcs = [qc1, qc2, qc3] for qc in qcs: mdl.add_constraint(qc) mdl.print_information()

Now, if you want to upgrade to 2.19 (which I recommend, to benefit from bug fixes and new features), you have to deal with the ancient version of pip. Run the following command, which reinstalls the latest docplex, whatever the ancient version is:

pip install --ignore-installed docplex

At the end of the day, you should be able to handle quadratic constraints, with or without updating docplex.

Let me know if this works.

Philippe.

- You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/IBMDecisionOptimization/docplex-examples/issues/42#issuecomment-770759866, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALCI4DGXOL3OURGK2KOD3JDS42AWJANCNFSM4WLBQYRQ.

vlkong commented 3 years ago

Hello,

You have this error because either your spyder is not configured to use the python interpreter you used to pip install docplex.

Please make sure that your spyder is using the right environment. For reference, the Spyder user manual has this entry: https://docs.spyder-ide.org/current/faq.html#using-existing-environment

Best regards

PhilippeCouronne commented 3 years ago

I'll let Viu handle discussions about Spyder, wwith which I'm not familiar. First, I suggest you check that docplex 2.19 is indeed installed by running pip freeze and see which version of DOcplex you see.

Once this is OK, the next issue is to change the kernel you are running in your notebook; to answer this, we'd need to know whether you are running a plain Python, a conda env, or a virtual env. If you are running a conda env in jupyter, then you have to install docplex 2.19 in that environment, or maybe create a fresh one, in which to install docplex, in order not to change the default conda installation?

amirvaez commented 3 years ago

When I run pip freeze, I got docplex==2.19.202 as the version which is installed. I am using jupyter 3.7 which is the latest compatiable version with CPLEX. Once I renew my jupyter, I cannot use docplex anymore. I do not know how to upgrade docplex in jupyter though.

Amirhossein Vaeztehrani, Ph.D. candidate,

Department of Management Sciences,

University of Waterloo


From: PhilippeCouronne notifications@github.com Sent: Monday, February 1, 2021 12:02 PM To: IBMDecisionOptimization/docplex-examples docplex-examples@noreply.github.com Cc: Amir Vaeztehrani amir.vaeztehrani@uwaterloo.ca; Author author@noreply.github.com Subject: Re: [IBMDecisionOptimization/docplex-examples] Getting Error "'QuadExpr' object has no attribute 'number_of_terms'" (#42)

I'll let Viu handle discussions about Spyder, wwith which I'm not familiar. First, I suggest you check that docplex 2.19 is indeed installed by running pip freeze and see which version of DOcplex you see.

Once this is OK, the next issue is to change the kernel you are running in your notebook; to answer this, we'd need to know whether you are running a plain Python, a conda env, or a virtual env. If you are running a conda env in jupyter, then you have to install docplex 2.19 in that environment, or maybe create a fresh one, in which to install docplex, in order not to change the default conda installation?

- You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/IBMDecisionOptimization/docplex-examples/issues/42#issuecomment-771005125, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALCI4DF2N7AVVATDTNMMBLLS43NB5ANCNFSM4WLBQYRQ.

vlkong commented 3 years ago

Jupyter has a wonderful feature called shell escape.

Create a new notebook in your jupyter. Create a new cell with:

!pip install --ignore-installed docplex

It will run and display what it is doing. After this is done, you can just restart your kernels and you should be good to go. If the pip install in the cell does not work, please provide a copy of the output.

PhilippeCouronne commented 3 years ago

@amirvaez Do you confirm the issue is now closed on your side?

amirvaez commented 3 years ago

Hi Phillipe,

With yout college's guide, I could upgrade docplex, however now I am getting another error which is probably due to some mistakes 8n the defining constraint. I could not find enough time to resolve it. Once I find a way to resolve it, I will keep you updated.

Thank you for your help,

Amirhossein Vaeztehrani, Ph.D. candidate,

Department of Management Sciences,

University of Waterloo


From: PhilippeCouronne notifications@github.com Sent: Thursday, February 4, 2021 4:01 AM To: IBMDecisionOptimization/docplex-examples docplex-examples@noreply.github.com Cc: Amir Vaeztehrani amir.vaeztehrani@uwaterloo.ca; Mention mention@noreply.github.com Subject: Re: [IBMDecisionOptimization/docplex-examples] Getting Error "'QuadExpr' object has no attribute 'number_of_terms'" (#42)

@amirvaezhttps://github.com/amirvaez Do you confirm the issue is now closed on your side?

- You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/IBMDecisionOptimization/docplex-examples/issues/42#issuecomment-773146275, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALCI4DGR3CAYM3QII6OEQ3TS5JO6RANCNFSM4WLBQYRQ.

PhilippeCouronne commented 3 years ago

Hi Amir For our records, we'd like to know why you chose to use class AdvModel instead of the regular Model class? Thanks. Philippe.

amirvaez commented 3 years ago

Hi,

I saw a tutorial in ipynb format on quadratic programming in my CPLEX package which had used advmodel. This is why I am using it.

Amirhossein Vaeztehrani, Ph.D. candidate,

Department of Management Sciences,

University of Waterloo


From: PhilippeCouronne notifications@github.com Sent: Friday, February 5, 2021 3:52 AM To: IBMDecisionOptimization/docplex-examples docplex-examples@noreply.github.com Cc: Amir Vaeztehrani amir.vaeztehrani@uwaterloo.ca; Mention mention@noreply.github.com Subject: Re: [IBMDecisionOptimization/docplex-examples] Getting Error "'QuadExpr' object has no attribute 'number_of_terms'" (#42)

Hi Amir For our records, we'd like to know why you'd need to use class AdvModel instead of the regular Model class? Thanks. Philippe.

- You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/IBMDecisionOptimization/docplex-examples/issues/42#issuecomment-773890914, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALCI4DAMRTS7W6HHCPTJXOLS5OWVVANCNFSM4WLBQYRQ.

PhilippeCouronne commented 3 years ago

HI Amir,

Class AdvModel is likely to be deprecated in the future, you might try to switch imports to the regular class docplex.mp.model.Model class. This one-line change will guarantee that your code works well with future versions.

amirvaez commented 3 years ago

Hi Philippe,

Thanks for reaching me out about this. I have changed the class and I could get some results for the convex cases! However, I do not have any idea if CPLEX has a solver for non-convex problems or not. I could not find anything online. BTW, I really appreciate your support.

Amirhossein Vaeztehrani, Ph.D. candidate,

Department of Management Sciences,

University of Waterloo


From: PhilippeCouronne notifications@github.com Sent: Monday, February 8, 2021 10:55 AM To: IBMDecisionOptimization/docplex-examples docplex-examples@noreply.github.com Cc: Amir Vaeztehrani amir.vaeztehrani@uwaterloo.ca; Mention mention@noreply.github.com Subject: Re: [IBMDecisionOptimization/docplex-examples] Getting Error "'QuadExpr' object has no attribute 'number_of_terms'" (#42)

HI Amir,

Class AdvModel is likely to be deprecated in the future, you might try to switch imports to the regular class docplex.mp.model.Model class. This one-line change will guarantee that your code works well with future versions.

- You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/IBMDecisionOptimization/docplex-examples/issues/42#issuecomment-775247827, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALCI4DB3FPUCH7IOTXNTEHLS6ACOZANCNFSM4WLBQYRQ.

PhilippeCouronne commented 3 years ago

Hi Amir,

For non-convex QPs, set the optimalitytarget parameter to 2 (first-order); CPLEX will switch to another algorithm, but cannot guarantee global optimality, only local. This fixes the "5002" error about non-convex problems. In docplex:

mdl.parameters.optimalitytarget = 2

This is explained in detail here: https://www.ibm.com/support/knowledgecenter/SSSA5P_20.1.0/ilog.odms.cplex.help/CPLEX/UsrMan/topics/cont_optim/qp/02_convexity_defn.html

 Philippe.
amirvaez commented 3 years ago

Thank you very much Philippe,

I will try this for my non-convex cases to see if it reaches to a reasonable solution. I have another question! is there any way to start with an initial solution in QP? I mean starting from a warm start solution instead of starting from scrath?

Thank you in advance,

Amirhossein Vaeztehrani, Ph.D. candidate,

Department of Management Sciences,

University of Waterloo


From: PhilippeCouronne notifications@github.com Sent: Tuesday, February 9, 2021 3:46 AM To: IBMDecisionOptimization/docplex-examples docplex-examples@noreply.github.com Cc: Amir Vaeztehrani amir.vaeztehrani@uwaterloo.ca; Mention mention@noreply.github.com Subject: Re: [IBMDecisionOptimization/docplex-examples] Getting Error "'QuadExpr' object has no attribute 'number_of_terms'" (#42)

Hi Amir,

For non-convex QPs, set the optimalitytarget parameter to 2 (first-order); CPLEX will switch to another algorithm, but cannot guarantee global optimality, only local. This fixes the "5002" error about non-convex problems. In docplex:

mdl.parameters.optimalitytarget = 2

This is explained in detail here: https://www.ibm.com/support/knowledgecenter/SSSA5P_20.1.0/ilog.odms.cplex.help/CPLEX/UsrMan/topics/cont_optim/qp/02_convexity_defn.html

Philippe.

- You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/IBMDecisionOptimization/docplex-examples/issues/42#issuecomment-775770931, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALCI4DD3NMPFRIIZLC5BAL3S6DY53ANCNFSM4WLBQYRQ.

amirvaez commented 3 years ago

I have used this syntax and I am still getting the following error

Version identifier: 12.10.0.0 | 2019-11-26 | 843d4de2ae CPXPARAM_Read_DataCheck 1 CPXPARAM_RandomSeed 201903125 CPXPARAM_OptimalityTarget 2 CPLEX Error 5002: 'q6' is not convex. Presolve time = 0.50 sec. (731.29 ticks) Error: Model is non-convex Total time: 4837.257754564285 None

Apparently, by this command, the error 5002 is not resolved! I am pretty sure that this problem has a solution ( the optimal solution for relaxed quadratic constraint is feasible for the problem.) However, I do not know if it is possible I enter this solution as an initial solution to the problem!

Amirhossein Vaeztehrani, Ph.D. candidate,

Department of Management Sciences,

University of Waterloo


From: PhilippeCouronne notifications@github.com Sent: Tuesday, February 9, 2021 3:46 AM To: IBMDecisionOptimization/docplex-examples docplex-examples@noreply.github.com Cc: Amir Vaeztehrani amir.vaeztehrani@uwaterloo.ca; Mention mention@noreply.github.com Subject: Re: [IBMDecisionOptimization/docplex-examples] Getting Error "'QuadExpr' object has no attribute 'number_of_terms'" (#42)

Hi Amir,

For non-convex QPs, set the optimalitytarget parameter to 2 (first-order); CPLEX will switch to another algorithm, but cannot guarantee global optimality, only local. This fixes the "5002" error about non-convex problems. In docplex:

mdl.parameters.optimalitytarget = 2

This is explained in detail here: https://www.ibm.com/support/knowledgecenter/SSSA5P_20.1.0/ilog.odms.cplex.help/CPLEX/UsrMan/topics/cont_optim/qp/02_convexity_defn.html

Philippe.

- You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/IBMDecisionOptimization/docplex-examples/issues/42#issuecomment-775770931, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALCI4DD3NMPFRIIZLC5BAL3S6DY53ANCNFSM4WLBQYRQ.

PhilippeCouronne commented 3 years ago

Hi Amir,

The setting i gave (optimalitytarget==2) wiorks only for QP models (that is quadratic objective) but not for QCPs. CPLEX cannot solve non-convex models with quadratic constraints: that's the meaning of the "Model is non-convex" message. I'm afraid there is no workaround for this (except maybe linearize your quadratic constraints, but that might well be not possible)

As for passing an initial solution to a QP, this is possible for MIQP/MIQCP problems , that is quadratic problems with integers variables, using the mip starts feature. Build a solution object from a dictionary of (variable, value) pairs, and pass it to the model using Model.add_mip_start. See the code snippet below for an example

mdl =Model(name='miqcp_mipstart')
ix, iy = mdl.integer_var_list(keys=['ii', 'jj'], ub = 5)
qc1 = mdl.add(ix ** 2 + iy **2 <= 10)
mdl.add( ix + iy >= 3)
mdl.minimize(ix**2 + 3 *  iy**2)
assert ('MIQCP' == mdl.problem_type)
s1 = mdl.new_solution({ix: 2, iy: 2})
mdl.print_information()
mdl.add_mip_start(s1)

sol = mdl.solve(log_output=True)
assert sol
assert abs(sol.objective_value -7) <= 1e-3

In the output log below, you'll see the mip start solution was used as a starting point, with objective 16.

Model: miqcp_mipstart
 - number of variables: 2
   - binary=0, integer=2, continuous=0
 - number of constraints: 2
   - linear=1, quadratic=1
 - parameters: defaults
 - objective: minimize quadratic
 - problem type is: MIQCP
Version identifier: 20.1.0.0 | 2020-11-10 | 9bedb6d68
CPXPARAM_Read_DataCheck                          1
1 of 1 MIP starts provided solutions.
MIP start 'm1' defined initial solution with objective 16.0000.
Tried aggregator 1 time.
MIQCP Presolve eliminated 1 rows and 1 columns.
Reduced MIQCP has 7 rows, 9 columns, and 15 nonzeros.
Reduced MIQCP has 0 binaries, 2 generals, 0 SOSs, and 0 indicators.
Reduced MIQCP has 2 quadratic constraints.
Presolve time = 0.00 sec. (0.01 ticks)
MIP emphasis: balance optimality and feasibility.
MIP search method: dynamic search.
Parallel mode: deterministic, using up to 12 threads.
Root relaxation solution time = 0.00 sec. (0.00 ticks)

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

*     0+    0                           16.0000        0.0000           100.00%
      0     0        0.0000     0       16.0000        0.0000        0  100.00%
      0     0        0.0000     0       16.0000       Cone: 2        1  100.00%
      0     0        6.0000     0       16.0000       Cone: 3        4  100.00%
      0     0        6.0000     0       16.0000       Cone: 4        5  100.00%
      0     0        6.0000     3       16.0000       Cone: 5        6  100.00%
      0     0        6.5000     0       16.0000       Cone: 6        7  100.00%
      0     0        6.7500     3       16.0000       Cone: 7        8  100.00%
      0     0        6.7500     3       16.0000      Cone: 10       13  100.00%
      0     0        6.7500     3       16.0000        6.7500       13   57.81%
*     0+    0                            9.0000        6.7500            25.00%
*     0+    0                            7.0000        6.7500             3.57%
      0     0        cutoff              7.0000                     16    0.00%
Elapsed time = 0.02 sec. (0.70 ticks, tree = 0.01 MB, solutions = 3)

Mixed integer rounding cuts applied:  1
Cone linearizations applied:  2

Root node processing (before b&c):
  Real time             =    0.02 sec. (0.70 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) =    0.02 sec. (0.70 ticks)
solution for: miqcp_mipstart
objective: 7
ii = 2
jj = 1

Hope this helps

 Philippe