Using baron solver in Pyomo - pyomo

I tried to solve an optimization problem given in pyomo examples using baron in Ubuntu (Linux).
Example problem:
from pyomo.environ import *
model = ConcreteModel()
model.x = Var(initialize=1.5)
model.y = Var(initialize=1.5)
def rosenbrock(model):
return (1.0-model.x)**2 \
+ 100.0*(model.y - model.x**2)**2
model.obj = Objective(rule=rosenbrock, sense=minimize)
SolverFactory('baron').solve(model, tee=True)
model.pprint()
When I solve the above problem using Baron the following exception is thrown:
BARON version 19.3.24. Built: LNX-64 Sun Mar 24 16:49:54 EDT 2019
BARON is a product of The Optimization Firm.
For information on BARON, see https://minlp.com/about-baron
Exception in thread Thread-23:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in
__bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File
"/home/user/.local/lib/python2.7/site-packages/pyutilib/subprocess/processmngr.py",
line 367, in _merged_reader
char = s.data.decode(encoding)
File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe4 in position 0:
unexpected end of data
When I solve the same problem using ipopt, the problem is solved as expected.
Please let me know how to fix this issue. Let me know if you need any further details.
Thanks in advance
Sakthi

Related

Pyglet - TypeError: expected string or buffer

I'm trying to use pyglet instead of pygame, 'cause it supports several screens.
this is a sample code that I run:
import pyglet
display = pyglet.canvas.get_display()
screens = display.get_screens()
window = pyglet.window.Window(fullscreen=True, screen=screens[1])
pyglet.app.run()
and I get this error:
Traceback (most recent call last): File
"/home/pi/netcomShopTV/idk.py", line 5, in
window = pyglet.window.Window() File "/usr/local/lib/python2.7/dist-packages/pyglet/init.py", line 359,
in getattr
import(import_name) File "/usr/local/lib/python2.7/dist-packages/pyglet/window/init.py",
line 1890, in
gl._create_shadow_window() File "/usr/local/lib/python2.7/dist-packages/pyglet/gl/init.py", line
209, in _create_shadow_window
_shadow_window = Window(width=1, height=1, visible=False) File "/usr/local/lib/python2.7/dist-packages/pyglet/window/xlib/init.py",
line 171, in init
super(XlibWindow, self).init(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/pyglet/window/init.py",
line 642, in init
self._create() File "/usr/local/lib/python2.7/dist-packages/pyglet/window/xlib/init.py",
line 265, in _create
self.context.set_vsync(self._vsync) # XXX ? File "/usr/local/lib/python2.7/dist-packages/pyglet/gl/xlib.py", line 265,
in set_vsync
warnings.warn(e) TypeError: expected string or buffer
Pyglet Version: 1.4.8
I searched in internet, couldn't find anything to solve this problem.
It seems this bug was introduced with this recent change. You should definitely raise it on pyglet github issue tracker.
Meanwhile, try installing the version prior to 1.4.8. (I though suspect this may just lead to crashing on failed sync as opposed to trying to warn you and then crashing :)).
As #alecxe mentioned, it was a bug. After I opened a ticket on github, I got the solution:
This is an exception for a Raspberry Pi specific issue. It's supposed
to raise a warning, and pass without crashing. If possible, could you
try editing line 265 in
/usr/local/lib/python2.7/dist-packages/pyglet/gl/xlib.py, and
changing:
warnings.warn(e) to warnings.warn(e.message)

gcloud components update fails with UnicodeDecodeError in MacOS

gcloud components update fails with UnicodeDecodeError in MacOS. Its not clear what would be causing this issue in MacOS and it says Hotfix for UnicodeDecodeError issue affecting logging. How do I get past this issue?
This is my current configuration
gcloud -v
Google Cloud SDK 190.0.0
beta 2017.09.15
bq 2.0.29
core 2018.02.16
gsutil 4.28
Updates are available for some Cloud SDK components. To install them,
please run:
$ gcloud components update
This is the error I get
Container Engine
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 859, in emit
msg = self.format(record)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 732, in format
return fmt.format(record)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 474, in format
s = self._fmt % record.__dict__
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 6: ordinal not in range(128)
Logged from file log.py, line 184
▪ Added container/use_v1_api_client property as an alias of
container/use_v1_api. container/use_v1_api is still supported.
Kubernetes Engine
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 859, in emit
msg = self.format(record)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 732, in format
return fmt.format(record)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 474, in format
s = self._fmt % record.__dict__
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 6: ordinal not in range(128)
Logged from file log.py, line 184
▪ Added --enable-pod-security-policy flag to enable PodSecurityPolicy
enforcement in clusters: gcloud <alpha|beta> container clusters
<create|update> --enable-pod-security-policy
Stackdriver Debugger
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 859, in emit
msg = self.format(record)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 732, in format
return fmt.format(record)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 474, in format
s = self._fmt % record.__dict__
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 6: ordinal not in range(128)
Logged from file log.py, line 184
▪ Changed gcloud beta source upload command to use the .gcloudignore
file if present. If not present and .gitignore is, .gitignore will
still be used. See gcloud topic gcloudignore to learn more.
190.0.1 (2018-02-22)
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 859, in emit
msg = self.format(record)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 732, in format
return fmt.format(record)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 474, in format
s = self._fmt % record.__dict__
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 6: ordinal not in range(128)
Logged from file log.py, line 184
▪ Hotfix for UnicodeDecodeError issue affecting logging.
I can see your Python version is Ok (2.7).
You may need to run gcloud init before trying to update.
If the issue persists or you already ran the aforementioned command, it would be faster to reinstall gcloud. Current version was released today (198.0.0).

Getting TypeError with speech_recognition module in Python

I want to convert speech to text in real time using the module SpeechRecognition 3.4.6 I've installed everything and now I am trying a simple code from example, here's the code:
import speech_recognition as sr
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
# recognize speech using Sphinx
try:
print("Sphinx thinks you said " + r.recognize_sphinx(audio))
except sr.UnknownValueError:
print("Sphinx could not understand audio")
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))
I am getting error at line audio = r.listen(source), the error traceback is:
Traceback (most recent call last):
File "sr.py", line 4, in <module>
audio = r.listen(source) # listen for the first phrase and extract it into audio data
File "/usr/local/lib/python2.7/dist- packages/speech_recognition/__init__.py", line 493, in listen
buffer = source.stream.read(source.CHUNK)
File "/usr/local/lib/python2.7/dist-packages/speech_recognition/__init__.py", line 139, in read
return self.pyaudio_stream.read(size, exception_on_overflow = False)
File "/usr/local/lib/python2.7/dist-packages/pyaudio.py", line 608, in read
return pa.read_stream(self._stream, num_frames, exception_on_overflow)
TypeError: function takes exactly 2 arguments (3 given)
You need to install pyaudio 0.2.9, it seems you have older version
it seems that the version of python is too old.

UnicodeDecodeError: in python 2.7

I am working with the VirusTotal api, exactly with this:
https://www.virustotal.com/es/documentation/public-api/#scanning-files
This is the part of my scritp where im having problems:
def scanAFile(fileToScan):
host = "www.virustotal.com"
selector = "https://www.virustotal.com/vtapi/v2/file/scan"
fields = [("apikey", myPublicKey)]
file_to_send = open(fileToScan, "rb").read()
files = [("file", fileToScan, file_to_send)]
json = postfile.post_multipart(host, selector, fields, files)
return simplejson.loads(json)
With some files there is not any error and it runs fine, but the next error is occurring when trying to scan some files, for example this error is for a jpg file:
Traceback (most recent call last):
File "F:/devPy/myProjects/script_vt.py", line 138, in <module>
scanMyFile()
File "F:/devPy/myProjects/script_vt.py", line 75, in scanQueue
jsonScan = scanAFile(fileToScan)
File "F:/devPy/myProjects/script_vt.py", line 37, in scanAFile
json = postfile.post_multipart(host, selector, fields, files)
File "F:\devPy\myProjects\script_vt.py", line 10, in post_multipart
content_type, body = encode_multipart_formdata(fields, files)
File "F:\devPy\myProjects\script_vt.py", line 42, in encode_multipart_formdata
body = CRLF.join(L)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)
I should indicate I work with pycharm under windows, could this cause the encoding error?
Any idea how to solve it? I got stack and couldnt find any solution on the net.

python-2.7 bug in inspect()?

I want to parallelize execution of a for loop on the quadcore processor of my computer's CPU. I am using pp (Python-Parallel) - rather than joblib.Parallel for reasons considered here.
But I am getting an error:
Traceback (most recent call last):
File "batching.py", line 60, in cleave_out_bad_data
job1 = job_server.submit(cleave_out, (data_dir,dirlist,), (endswithdat,))
File "/homes/ad6813/.local/lib/python2.7/site-packages/pp.py", line 459, in submit
sfunc = self.__dumpsfunc((func, ) + depfuncs, modules)
File "/homes/ad6813/.local/lib/python2.7/site-packages/pp.py", line 637, in __dumpsfunc
sources = [self.__get_source(func) for func in funcs]
File "/homes/ad6813/.local/lib/python2.7/site-packages/pp.py", line 704, in __get_source
sourcelines = inspect.getsourcelines(func)[0]
File "/usr/lib/python2.7/inspect.py", line 690, in getsourcelines
lines, lnum = findsource(object)
File "/usr/lib/python2.7/inspect.py", line 529, in findsource
raise IOError('source code not available')
IOError: source code not available
It looks like the reason is a python-2.7 bug.
Has anyone come across this and solved it?
Here is my code:
def clean_dir(data_dir, dirlist):
job_server = pp.Server()
job1 = job_server.submit(clean, (data_dir,dirlist,), (endswith,))
def clean(data_dir, dirlist):
[good_or_bad(file, data_dir) for file in dirlist if endswith(file)]
Inspired by here, the way I fixed a similar problem is to save the code and function in one file like "test.py" and call this file with python, rather than input the function line by line in python shell. I works for me.