Why does a decision variable domain declaration not work in Pyomo v5.7 that worked with v5.6.9 - pyomo

I have a decision variable declaration that does not work in Pyomo 5.7 that did work in 5.6.9.
The line in question is part of building a (concrete) model:
model.v = pe.Var(within = pe.RealSet) #pe = pyomo.environ
This ran fine when I had Pyomo version 5.6.9, but now gives errors described below in the provided code block.
To run with 5.7 I had to drop the within statement so I am left with:
model.v = pe.Var()
What is the reasoning for no longer supporting an explicit statement that the domain includes all reals?
When running with the original statement with Pyomo 5.7, I get this error (CPython 3.7.1 on Windows 10):
ERROR: Constructing component 'v' from data=None failed: TypeError: __new__()
takes 1 positional argument but 2 were given
Traceback (most recent call last):
File "runscript.py", line 216, in <module>
payoff_matrix = my_matrix
File "C:\Users\myusername\Documents\modelbuildscript.py", line 24, in __init__
model.v = pe.Var(within = pe.RealSet)
File "C:\EngTools\Anaconda3\2018.12\lib\site-packages\pyomo\core\base\block.py", line 543, in __setattr__
self.add_component(name, val)
File "C:\EngTools\Anaconda3\2018.12\lib\site-packages\pyomo\core\base\block.py", line 1081, in add_component
val.construct(data)
File "C:\EngTools\Anaconda3\2018.12\lib\site-packages\pyomo\core\base\var.py", line 613, in construct
self._initialize_members((None,))
File "C:\EngTools\Anaconda3\2018.12\lib\site-packages\pyomo\core\base\var.py", line 682, in_initialize_members
self.domain = self._domain_init_rule(self._parent())
TypeError: __new__() takes 1 positional argument but 2 were given

The short answer is that you should use
model.v = pe.Var(within=pe.Reals)
The slightly longer answer is that the Pyomo global sets (Reals, NonNegativeReals, Integers, PositiveIntegers, Binary, etc.) were originally defined using their own class hierarchy independent of the Set and RangeSet components. Unfortunately, that led to numerous problems (correctly detecting discrete sets, performing set operations using global sets, etc). Pyomo 5.7 switched to a new rewrite of the Set / RangeSet components that among numerous other bug fixes moved to a new paradigm where all sets in Pyomo were either Set or RangeSet components (even global sets). We put in a backwards compatibility layer that caught all the legacy uses of the RealSet class in the test suite, but your use case (while it worked) was not a documented / tested case. PR 1619 will restore this functionality (likely will be included in Pyomo 5.7.1), and adds an official deprecation warning for use of RealSet.

Related

Failed to run Cognitive-Face-Python-master test

I'm exploring the Face API. I'm using a GUI sample.
GUI link: https://github.com/Microsoft/Cognitive-Face-Python
This error occurs when I run the sample:
Traceback (most recent call last):
File "D:\Tin Central\programming\DOAN\Cognitive-Face-Python-
master\Cognitive-Face-Python-master\sample\view\__init__.py", line 101, in
OnInit
frame = MyFrame(None)
File "D:\Tin Central\programming\DOAN\Cognitive-Face-Python-
master\Cognitive-Face-Python-master\sample\view\__init__.py", line 80, in
__init__
self.book = MyLabelBook(self)
File "D:\Tin Central\programming\DOAN\Cognitive-Face-Python-
master\Cognitive-Face-Python-master\sample\view\__init__.py", line 31, in
__init__
subscription_panel = SubscriptionPanel(self)
File "D:\Tin Central\programming\DOAN\Cognitive-Face-Python-
master\Cognitive-Face-Python-master\sample\view\panel_subscription.py", line
42, in __init__
subgridsizer = wx.GridSizer(rows=2, cols=2)
TypeError: GridSizer(): arguments did not match any overloaded call:
overload 1: 'rows' is not a valid keyword argument
overload 2: 'rows' is not a valid keyword argument
overload 3: not enough arguments
overload 4: not enough arguments
You probably installed wxPython 4, which is still in beta.
You need to install wxPython 3.0.2 by going here: https://sourceforge.net/projects/wxpython/files/wxPython/3.0.2.0/.
Make sure you pick the .exe corresponding to the Python version you have (32-bit or 64-bit).
The full instructions to run the sample app are here.

Using pyfmi with multiprocessing for simulation of Modelica FMUs

I am trying to simulate multiple Modelica FMUs in parallel using python/pyfmi and multiprocessing. However I am not able to return any pyfmi FMI objects from the subprocesses once the FMUs are initialized. It seems that pyfmi FMI objects (e.g. pyfmi.fmi.FMUModelCS2 or pyfmi.fmi.FMUState2) are not pickable. I also tried dill to pickle, which doesn't work for me eather. With dill the objects are picklable though, meaning no error, but somehow corrupted if I try to reload them afterwards. Does anyone have an idea of how to solve this issue? Thanks!
The problem is that pyfmi.fmiFMUModelCS2 is a Cython class dependent on external libraries which makes it unpickable. So it is not possible unfortunately.
If you want to use multiprocessing the only way forward that I see is that you first create the processes and then load the FMUs into the separate processes. In this way you do not need to pickle the classes.
I faced a similar problem when I created EstimationPy. I ended up creating a wrapper for running parallel simulation of the same FMU using multiple processes.
I suggest you to look at the implementation here
https://github.com/lbl-srg/EstimationPy/blob/master/estimationpy/fmu_utils/fmu_pool.py
And to the example http://lbl-srg.github.io/EstimationPy/modules/examples/first_order.html#run-multiple-simulations
The pathos module allows multiprocessing with a similar interface as the multiprocessing but relies on dill instead of pickle for serialisation.
The Pool method works for parallel execution of model.simulate, provided that results are handled in memory:
n_core = 2
n_simulation = 10
# ====
import pyfmi
model = pyfmi.load_fmu(path_fmu)
def worker(*args):
model.reset()
print "================> %d" % args[0]
return model.simulate(options=dict(result_handling="memory"))["y"]
from pathos.multiprocessing import Pool
pool = Pool(n_core)
out = pool.map(worker, range(n_simulation))
pool.close()
pool.join()
Note in the above snippet that it is necessary to handle results in memory : options=dict(result_handling="memory").
The default is to use temporary files which works for when the amount of simulations is small.
However, the longer the queue, the higher the chance to get something like
Exception in thread Thread-27:
Traceback (most recent call last):
File "/home/USER/anaconda2/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/home/USER/anaconda2/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/home/USER/anaconda2/lib/python2.7/site-packages/multiprocess/pool.py", line 389, in _handle_results
task = get()
File "/home/USER/anaconda2/lib/python2.7/site-packages/dill/dill.py", line 260, in loads
return load(file)
File "/home/USER/anaconda2/lib/python2.7/site-packages/dill/dill.py", line 250, in load
obj = pik.load()
File "/home/USER/anaconda2/lib/python2.7/pickle.py", line 864, in load
dispatch[key](self)
File "/home/USER/anaconda2/lib/python2.7/pickle.py", line 1139, in load_reduce
value = func(*args)
TypeError: __init__() takes exactly 2 arguments (1 given)
which I fail to grasp.

Can't import wx (wxPython Phoenix) into my script

I'm taking a course in Python, and the current assignment is to convert a previous assignment written in Python 2 (which used wxPython) to Python 3 (which needs Phoenix). I successfully installed Phoenix, and in the Py3 shell I can now import wx just fine. However, if I try to run my actually script, it immediately gets this error:
Traceback (most recent call last):
File "C:\Python27\transferdrillPy3.py", line 10, in
class windowClass(wx.Frame):
NameError: name 'wx' is not defined
What's up with that?
I tried going through my code and deleting every single "wx.", and now it works. I guess Phoenix doesn't need that.

cifar10: TypeError: range() takes at least 2 arguments (1 given)

When I build the file: cifar10_train.py, it occurs that:
...
File ".../cifar10.py", line 271, in loss
indices = tf.reshape(tf.range(FLAGS.batch_size), [FLAGS.batch_size, 1])
TypeError: range() takes at least 2 arguments (1 given)
The trouble occurs in the file cifar10.py.
It seems that you are using TensorFlow version 0.5 and a more recent version of the cifar10_train.py script. The signature of tf.range() was changed to accept a single argument (like the Python range() built-in function) after version 0.5 was released.
I'd always recommend upgrading to the latest version of TensorFlow, since the runtime has had many performance and stability improvements since the initial release.
If that doesn't work, the following equivalent code is taken from the original release:
indices = tf.reshape(tf.range(0, FLAGS.batch_size, 1), [FLAGS.batch_size, 1])
I alter the command as follows:
#indices = tf.reshape(tf.range(FLAGS.batch_size), [FLAGS.batch_size, 1])
indices = tf.reshape(range(FLAGS.batch_size), [FLAGS.batch_size, 1])
Then, the code works well.

Simple reading/writing from/to a USB HID device in Python?

I've got a fairly simple USB HID device that I've been trying to figure out how to read from and write to using Python. I've been able to read from it using PyWinUSB, but the problem comes in when I try to write to it. Trying to write to it makes things explode.
For example:
device = hid.HidDeviceFilter(vendor_id = 0x0003, product_id = 0x1001).get_devices()[0]
This works fine. Then for reading raw data, which is all that I care about right now (I'll work with that once I can figure out how to write to the cursed thing):
def readData(data):
print(data)
return None
This works fine (in fact, it was quite exciting when I got to see it work). So I would assign the data handler like so:
device.set_raw_data_handler(readData)
And every time I hit a button, it's fine. The data comes through as you would expect. Which is great!
The problem comes when I want to write to the device.
Following the sample simple_send file as a template (which was probably not the best choice), I would do the following:
report = device.find_output_reports()[0]
Which would return a report object with a dictionary holding 4 entries. Is that correct? Do you write to a device using the output_reports object? Trying to do so by setting the report value to ANYTHING:
report[<key>] = "pneumonoultramicroscopicvolcanoconiosis"
report.send()
This would keep returning some obnoxious error that I can't interpret:
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
report.send()
File "C:\Python27\lib\site-packages\pywinusb-0.3.1-py2.7.egg\pywinusb\hid\core.py", line 1446, in send
self.__prepare_raw_data()
File "C:\Python27\lib\site-packages\pywinusb-0.3.1-py2.7.egg\pywinusb\hid\core.py", line 1401, in __prepare_raw_data
byref(self.__raw_data), self.__raw_report_size) )
File "C:\Python27\lib\site-packages\pywinusb-0.3.1-py2.7.egg\pywinusb\hid\winapi.py", line 382, in __init__
raise helpers.HIDError("hidP error: %s" % self.error_message_dict[error_code])
HIDError: hidP error: data index not found
I'm using Windows 7. I've managed to find (finally) a reference for the HID DLL exported functions, and I don't HAVE to (or, for that matter even really WANT to) use the PyWinUSB library. I just want to make this work, and it didn't seem like it would be that tough, but it has been.
Can someone tell me what it is I've been doing wrong here?
Thanks.
Also, I tried tracing the error call, and made it so far before the program just closed which was kind of disheartening.
i made it work with this
buffer= [0xFF]*33 # 33 = report size + 1 byte (report id)
buffer[0]=0x0 # report id
buffer[1]=0xFE
buffer[2]=0x00
buffer[3]=0xFF
out_report.set_raw_data(buffer)
out_report.send()
dev.close()
For me worked only this:
report.send([0x70, ..., 0x73 ])
The function call sequence with set_raw_data([0x70, ..., 0x73) and subsequent send() didn't work for me.