Abstract model data import from numpy array / list

497 views
Skip to first unread message

john...@gmail.com

unread,
Aug 3, 2016, 8:48:33 AM8/3/16
to Pyomo Forum
Hey!

I have created an abstract model and a data file.

lets assume 2 parameters:

model.Hours = RangeSet(1,5)
model.Price= Param(model.Hours)
model
.weight= Param(model.Hours)


The .data file looks like:

param Price:=
1 2
2 3
3 19
4 2
5 4


param weight:=
1 44
2 44
3 45
4 46
5 44

In a second example, some of the parameter are the result of a previous simulation within Python and the data are stored in a numpy array - in this example lets assume the parameter "Price" is available within Python as a list:

Price = [2,3,19,2,4]

My question: I dont want to copy the result of the simulation into the .dat file. Is there a "better" possibility?

//thank you for your help




Gabe Hackebeil

unread,
Aug 3, 2016, 9:02:50 AM8/3/16
to pyomo...@googlegroups.com
If you add the keyword "mutable=True" to the Param declaration, it will allow to update the value of the Param on the concrete instance. For instance, starting from a ConcreteModel (which is what gets returned from create_instance),

model = ConcreteModel()
model.p = Param(mutable=True)
model.q = Param([1,2,3], mutable=True)

# ... Add constraints

# update parameter values

model.p.set_value(2.0)
model.q.store_values({1:1.0, 2:2.1})
model.q[3].set_value(5.1)

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

john...@gmail.com

unread,
Aug 3, 2016, 10:24:36 AM8/3/16
to Pyomo Forum
Thank you Gabe for your answer!

Unfortunatelly I am new to Pyomo and i did not get it :).
If we stay at the simple example that i posted:

I leave the old param-values for "Price" in the "data.dat" file.
Do I understand it correct:
Then I load variable (list)  Price = [2,3,19,2,3,4]



instance
= model.create_instance("data.dat")
opt
= SolverFactory("gurobi")
Price = [2,3,19,2,3,4]

But how do I "overwrite" the model.Price param at this point with the list "Price"?

results = opt.solve(instance)

Gabriel Hackebeil

unread,
Aug 4, 2016, 10:05:44 AM8/4/16
to pyomo...@googlegroups.com
Here is the same thing starting from an AbstractModel:

# define the model
model = AbstractModel()
model.Price_index = Set()
model.Price = Param(model.Price_index, mutable=True)
# …

# create the instance
instance = model.create_instance(“data.dat”)

# do the first solve
# ...

# update the mutable Param values
new_price = {0: 2, 1:3}
for key, val in new_price.items():
    instance.Price[key].set_value(val)

# do another solve
...

Gabe

john...@gmail.com

unread,
Aug 4, 2016, 12:25:08 PM8/4/16
to Pyomo Forum
Thank you very much Gabe!

As soon as i change the parameter to: model.Price = Param(model.Price_index, mutable=True)

I get the following error message (even if I dont update the mutable Param values):

Implicit conversion of Pyomo NumericValue type `Price[48]' to a float is
disabled. This error is often the result of using Pyomo components as
arguments to one of the Python built-in math module functions when
defining expressions. Avoid this error by using Pyomo-provided math
functions.

In the data.dat file Price looks like:

param Price :=
1 23.026130503
2 12.235828145
..
..
48 37.768450126
;


Again- thank you very much :)!

Gabriel Hackebeil

unread,
Aug 4, 2016, 12:48:58 PM8/4/16
to pyomo...@googlegroups.com
Can you show me the line of code that causes this error. If you are using the pyomo command, you might have to add something like “-c” to get the full traceback.

Gabe

john...@gmail.com

unread,
Aug 5, 2016, 2:24:15 AM8/5/16
to Pyomo Forum
Hey Gabe - Thank you.

This is what i get:d


Traceback (most recent call last):

 
File "<ipython-input-32-42b92a2abec4>", line 1, in <module>
    runfile
('D:/,,,/test.py', wdir='D:/.../')

 
File "C:\...\Continuum\Anaconda21\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
    execfile
(filename, namespace)

 
File "C:\...\Continuum\Anaconda21\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
   
exec(compile(scripttext, filename, 'exec'), glob, loc)

 
File "D:/.../Test.py", line 91, in <module>
    plt
.step(x,demand,label='Price',linewidth=1, color = "r")

 
File "C:\...\Continuum\Anaconda21\lib\site-packages\matplotlib\pyplot.py", line 3360, in step
    ret
= ax.step(x, y, *args, **kwargs)

 
File "C:\...\Continuum\Anaconda21\lib\site-packages\matplotlib\__init__.py", line 1811, in inner
   
return func(ax, *args, **kwargs)

 
File "C:\...\Continuum\Anaconda21\lib\site-packages\matplotlib\axes\_axes.py", line 1861, in step
   
return self.plot(x, y, *args, **kwargs)

 
File "C:\...\Continuum\Anaconda21\lib\site-packages\matplotlib\__init__.py", line 1811, in inner
   
return func(ax, *args, **kwargs)

 
File "C:\...\Continuum\Anaconda21\lib\site-packages\matplotlib\axes\_axes.py", line 1425, in plot
   
self.add_line(line)

 
File "C:\...\Continuum\Anaconda21\lib\site-packages\matplotlib\axes\_base.py", line 1708, in add_line
   
self._update_line_limits(line)

 
File "C:\...\Continuum\Anaconda21\lib\site-packages\matplotlib\axes\_base.py", line 1730, in _update_line_limits
    path
= line.get_path()

 
File "C:\...\Continuum\Anaconda21\lib\site-packages\matplotlib\lines.py", line 925, in get_path
   
self.recache()

 
File "C:\...\Continuum\Anaconda21\lib\site-packages\matplotlib\lines.py", line 621, in recache
    y
= np.asarray(yconv, np.float_)

 
File "C:\...\Continuum\Anaconda21\lib\site-packages\numpy\core\numeric.py", line 474, in asarray
   
return array(a, dtype, copy=False, order=order)

 
File "C:\...\Continuum\Anaconda21\lib\site-packages\pyomo\core\base\numvalue.py", line 373, in __float__
    functions
.""" % (self.cname(),))

TypeError: Implicit conversion of Pyomo NumericValue type `Price[48]' to a float is

john...@gmail.com

unread,
Aug 5, 2016, 5:55:39 AM8/5/16
to Pyomo Forum
Ad: I guess there are some problems with plt.step () function. But now i get the following error message:



 
File "C:\...\Continuum\Anaconda21\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
    execfile
(filename, namespace)


 
File "C:\Users\...\Anaconda21\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
   
exec(compile(scripttext, filename, 'exec'), glob, loc)

 
File "D:/,,,/Test.py", line 151, in <module>
    instance
.Price[key].set_value(val)

AttributeError: '_ParamData' object has no attribute 'set_value'




Gabriel Hackebeil

unread,
Aug 5, 2016, 6:37:22 PM8/5/16
to pyomo...@googlegroups.com
Try just directly setting the .value attribute for parameters then.

As for the other error, you need to convert all Pyomo types to their value before storing them in an array.

x = np.array([value(instance.x[i]) for i in instance.x])

NumPy arrays are capable of storing Pyomo types (I think it implicitly chooses the dtype=object storage setting), but that doesn’t provide any useful functionality for plotting because you want the numeric values (not the Pyomo types, which, confusingly, have a base class called NumericValue).

Gabe

Reply all
Reply to author
Forward
0 new messages