GEKKO MINLP: "Exception: This steady-state IMODE only allows scalar values." in a cross entropy minimization problem

294 views
Skip to first unread message

Anna Lungarska

unread,
Sep 17, 2018, 10:17:47 AM9/17/18
to apmonitor
Hello,

I would greatly appreciate some help with the following cross entropy minimization problem.
In a few words (more details are provided later on), I am trying to solve this:


subject to:

I am attaching my code and the input files.

When I launch my code I get the following error :


Traceback (most recent call last):
 
File "<stdin>", line 1, in <module>
 
File "/usr/local/lib/python2.7/dist-packages/gekko/gekko.py", line 601, in solve
   
self._write_csv()
 
File "/usr/local/lib/python2.7/dist-packages/gekko/gk_write_files.py", line 148, in _write_csv
   
raise Exception('This steady-state IMODE only allows scalar values.')
Exception: This steady-state IMODE only allows scalar values.

I am using Ubuntu 16.04. Your suggestions are more than welcome!

Some explications on the model :

We want to allocate a number ( C, index j) of crops on plots (I, index i). We have a matrix IxC with the plots and the probabilities of presence of the crops (estimated previously using a Multinomial Logit).
We know the surface of each plot (s_i) and the total surface of the crop j in the region (S_j). Our initial estimates of the probabilities of presence, or priors, may result in greater or lower total area for the crops
and for this reason we are using the cross entropy minimization to adjust the priors (and obtain the posteriors) with respect to the total surface of the crops in the region. Another constraint to our problem is that
we would like to have binary posteriors based on the continuous priors (that's why we try to solve this as a MINLP).


CE_min_GEKKO.py
count_204.txt
priors_204.txt
reference_204.txt

John Hedengren

unread,
Sep 17, 2018, 12:02:07 PM9/17/18
to apmo...@googlegroups.com

Anna,

 

You need to declare parameter and variable arrays with a single value given to each such as:

 

#define parameter

priors_p = [m.Param(value=i) for i in priors]   

cultu = np.array(references["cultu"])

ref_p = [m.Param(value=i) for i in cultu]

count_p = [m.Param(value=i) for i in counts]

cult_p = [m.Param(value=i) for i in cultures]

 

#initialize variables

posts_v = [m.Var(value=i) for i in priors]

 

Even with these changes, there is still a problem with the Equations.

 

You can see the model that GEKKO produces by opening the text file at print(m.path). This model is compiled into byte-code before giving it to an MINLP solver.

 

Best regards,

 

John Hedengren

--
--
APMonitor user's group e-mail list.
- To post a message, send email to apmo...@googlegroups.com
- To unsubscribe, send email to apmonitor+...@googlegroups.com
- Visit this group at http://groups.google.com/group/apmonitor

---
You received this message because you are subscribed to the Google Groups "apmonitor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to apmonitor+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anna Lungarska

unread,
Sep 18, 2018, 11:12:43 AM9/18/18
to apmonitor
Thank you professor Hedengren for the help!

In deed, the problem is now stuck elsewhere. After launching the program and some calculation time, I get the following error:
Traceback (most recent call last):
 
File "<stdin>", line 1, in <module>

 
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile
    execfile
(filename, namespace)
 
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 81, in execfile
    builtins
.execfile(filename, *where)
 
File "/sda2/Dropbox/CE_R/CE_min_GEKKO.py", line 76, in <module>
    m
.solve(disp=True) # Solve

 
File "/usr/local/lib/python2.7/dist-packages/gekko/gekko.py", line 601, in solve
   
self._write_csv()

 
File "/usr/local/lib/python2.7/dist-packages/gekko/gk_write_files.py", line 165, in _write_csv
    vp
.VALUE = np.ones(length)*vp.VALUE
TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('S32') dtype('S32') dtype('S32')

I've checked the model in the text file and it seems to be containing all of the specified parameters and control variables.
I am not sure that the array structure is respected while the equations apply per row and per column of the priors matrix.

I am attaching the updated code and the model text file.

Should I try to recreate equations as they are automatically generated in a GAMS.lst or there is a simpler way to use np.array for the parameters and CV in
GEKKO?

Thank you once again!
gk_model2.apm
CE_min_GEKKO.py

John Hedengren

unread,
Sep 18, 2018, 12:21:05 PM9/18/18
to APM Google Groups
Anna,

You cannot do matrix math in GEKKO so you need to use different list comprehension forms to do the summations and multiplications. I've attached an updated example. You previous model had bad equations (see Equations section of apm file). The new version fixes those.

The current error is with the list "cultures" that is a string and this causes a problem with initializing cult_p:

cult_p = [m.Param(value=cult) for cult in cultures[1:]]

As you start to solve your problem, I recommend testing your problem on a much smaller data set until you've worked out the issues with the syntax. You don't want to wait a couple minutes to see if they problem gives a good solution as you are building your problem.

Best regards,

John Hedengren
--
Best regards,

John Hedengren
APMonitor Optimization Suite
CE_min_GEKKO.py
Reply all
Reply to author
Forward
0 new messages