I know that some examples from the Pyomo book can be run from Anaconda company prompt, eg. by the command “runef -m ReferenceModel.py” for the farmer example.
I would like to run the examples within the Spyder IDE. Spyder doesn’t recognise any of the code. For example, I get the following error message ‘from pyomo.core import *’ used; unable to detect undefined names
How can I run the examples within Spyder? I am not sure if adding a line
pyomo solve my_model.ph my_data.dat —-solver=‘glpk’ at the end of the script would work
Assuming you've set up your abstract model, you can instantiate it with data using:
data = DataPortal()
data.load(filename="my_data.dat", model=my_model)
Then you can solve in Spyder and present results with the following:
from pyomo.opt import SolverFactory
opt = pyomo.environ.SolverFactory('glpk')
instance = model.create_instance(data)
opt.solve(instance)
instance.display()
References:
(1) https://www.osti.gov/servlets/purl/1376827
(2) https://pyomo.readthedocs.io/en/stable/working_models.html
Related
I am trying to run a parametric sweep in OpenModelica using OMPython. Let's assume that I have a Modelica model my_model.mo belonging to the library my_library. The model has two parameters: a and b.
I successfully managed to run a single parametric run by using the following code:
from OMPython import OMCSessionZMQ
omc = OMCSessionZMQ()
omc.sendExpression('loadModel(my_library)')
omc.sendExpression('simulate(my_library.my_model, simflags="-overrideFile=parameter_sweep.txt", stopTime=86400)')
where the file parameter_sweep.txt is:
a=5
b=6
Now the question is: how can I run multiple parametric runs? I could add one more line to the code where a new txt file (parameter_sweep1.txt) with a new set of values for the parameters is used:
from OMPython import OMCSessionZMQ
omc = OMCSessionZMQ()
omc.sendExpression('loadModel(my_library)')
omc.sendExpression('simulate(my_library.my_model, simflags="-overrideFile=parameter_sweep.txt", stopTime=86400)')
omc.sendExpression('simulate(my_library.my_model, simflags="-overrideFile=parameter_sweep1.txt", stopTime=86400)')
However, I am afraid that in this way there is the need to recompile. Is there a way to do multiple parametric runs and avoid re-compilation?
Use the buildModel command instead of simulate Then start the process manually in Python using a library such as subprocess. The command is simply something like:
["./my_library.my_model", "-overrideFile=parameter_sweep.txt"]
(If you use Windows, I believe you need to update your PATH environment variable as well, in order to find the used DLLs. If you use Linux, it just works.)
I need to write an optimization file for Gurobi (Python) that is a modified version of a classic TSP. I tried to run the example file from their website:
examples.gurobi.com/traveling-salesman-problem/
I always get the following error:
TypeError: object of type 'NoneType' has no len()
What do I need to change?
Thx
Full code: https://www.dropbox.com/s/ewisx805b3o2wq5/beispiel_opt.py?dl=0
I can confirm the error with the example code from Gurobi's website. At the first look the problem seems to be inside the subtour function, that returns None if sum(lengths) == n and the missing check for if tour is None inside the subtourlim function.
Instead of providing a fix for the specific code, I first checked the examples that Gurobi installs inside the specific installation directory:
Mac: /Library/gurobi810/mac64/examples/python/
Linux: /opt/gurobi800/linux64/examples/python/
Windows: c:\gurobi800\win64\examples\python\
And surprisingly the tsp.py from there runs without any errors. Note also that the two mentioned functions are revised. So I guess the example from the website is just a old version of the code.
The following flags were defined in a misc_fun.py file to include machine and directories info:
import tensorflow as tf
flags = tf.app.flags
FLAGS = flags.FLAGS
# definitions
flags.DEFINE_string(
'DEFAULT_IN',
'~/PycharmProjects/myNN/Data/',
"""Default input folder.""")
...
It worked fine in TensorFlow 1.0 - 1.4 versions (with Pycharm). After updating to TensorFlow 1.5.-rc0, the following error occurred:
Usage:
from misc_fun import FLAGS
FLAGS.DEFAULT_IN = FLAGS.DEFAULT_DOWNLOAD # change default input folder
Error:
UnparsedFlagAccessError: Trying to access flag --DEFAULT_DOWNLOAD before flags were parsed.
However print(FLAGS) worked fine, which gives:
misc_fun:
--DEFAULT_DOWNLOAD: default download folder for large datasets.
(default: '/home/username/Downloads/Data/')
--DEFAULT_IN: default input folder.
(default: '~/PycharmProjects/myNN/Data/')
...
I tried FLAGS = flags.FLAGS(sys.argv), resulting in the following error:
UnrecognizedFlagError: Unknown command line flag 'f'
Although there is a workaround using the class object, I wonder what could be the problem here.
I have tried adding the following line below.
tf.app.flags.DEFINE_string('f', '', 'kernel')
This solution is different from others in that it is simple and easy to try. You just need to add this into your code, and it doesn't change your system. Please let me know if this solution helps solve other people's problems.
The reference for this solution is from a Chinese website: https://blog.csdn.net/qq_39956625/article/details/80500291
With 1.5.0-rc0 the Tensorflow maintainers have switched tf.app.flags to the flags module from abseil. Unfortunately, it is not 100% API compatible to the previous implementation. I worked around your problem with something like
remaining_args = FLAGS([sys.argv[0]] + [flag for flag in sys.argv if flag.startswith("--")])
assert(remaining_args == [sys.argv[0]])
before accessing the FLAGS object the first time.
Alternatively you can use FLAGS(sys.argv, known_only=True) to parse all related flags (the ones defined using tf.app.flags.DEFINE_xxx). This will release any other args that are not known. Useful if you have some command line arguments that are not related to TF.
This question concerns Matlab 2014b, Python 3.4 and Mac OS 10.10.
I have the following Python file tmp.py:
from statsmodels.tsa.arima_process import ArmaProcess
import numpy as np
def generate_AR_time_series():
arparams = np.array([-0.8])
maparams = np.array([])
ar = np.r_[1, -arparams]
ma = np.r_[1, maparams]
arma_process = ArmaProcess(ar, ma)
return arma_process.generate_sample(100)
I want to call generate_AR_time_series from Matlab so I used:
py.tmp.generate_AR_time_series()
which gave a vague error message
Undefined variable "py" or class "py.tmp.generate_AR_time_series".
To look into the problem further, I tried
tmp = py.eval('__import__(''tmp'')', struct);
which gave me a detailed but still obscured error message:
Python Error:
dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/special/_ufuncs.so, 2): Symbol
not found: __gfortran_stop_numeric_f08
Referenced from: /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/special/_ufuncs.so
Expected in: /Applications/MATLAB_R2014b.app/sys/os/maci64/libgfortran.3.dylib
in /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/special/_ufuncs.so
I can call the function within Python just fine, so I guess the problem is with Matlab. From the detailed message, it seems that the problem lies in something is expected in the Matlab installation path, but of course Matlab installation path does not contain those things since they are third-party libraries for Python.
How to solve this problem?
Edit 1:
libgfortran.3.dylib can be found in a lot of places:
/Applications/MATLAB_R2014a.app/sys/os/maci64/libgfortran.3.dylib
/Applications/MATLAB_R2014b.app/sys/os/maci64/libgfortran.3.dylib
/opt/local/lib/gcc48/libgfortran.3.dylib
/opt/local/lib/gcc49/libgfortran.3.dylib
/opt/local/lib/libgcc/libgfortran.3.dylib
/Users/wdg/Documents/MATLAB/mcode/nativelibs/macosx/bin/libgfortran.3.dylib
Try:
setenv('DYLD_LIBRARY_PATH', '/usr/local/bin/');
For me, using the setenv approach from within MATLAB did not work. Also, MATLAB modifies the DYLD_LIBRARY_PATH variable during startup to include necessary libraries.
First, you have to make sure which version of gfortran scipy was linked against: in Terminal.app, enter otool -L /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/special/_ufuncs.so and look for 'libgfortran' in the output.
It worked for me to copy $(MATLABROOT)/bin/.matlab7rc.sh to my home directory and change the line LDPATH_PREFIX='' in the mac section (around line 195 in my case) to LDPATH_PREFIX='/opt/local/lib/gcc49', or whatever path to libgfortran you found above.
This ensures that /opt/local/lib/gcc49/libgfortran.3.dylib is found before the MATLAB version, but leaves other paths intact.
I'm trying to use Pyomo to find the optimal values of a Python model on OSX. I got the script from https://github.com/shoibalc/recem, and installed Pyomo and COIN-OR following the instructions to the extent that I could, changing a few things that were outdated or didn't seem to work on OSX.
The code that is causing problems is below.
import pyomo
from pyomo.opt.base import *
from pyomo.opt.parallel import SolverManagerFactory
from DICE2007 import createDICE2007
from DICEutils import DICE_results_writer
global start_time
start_time = time.time()
dice = createDICE2007()
dice.doc = 'OPTIMAL SCENARIO'
opt = SolverFactory('ipopt',solver_io='nl')
tee = False
options = """
halt_on_ampl_error=yes"""
solver_manager = SolverManagerFactory('serial')
print '[%8.2f] create model %s OPTIMAL SCENARIO\n' %(time.time()-start_time,dice.name)
instance = dice.create()
print '[%8.2f] created instance\n' %(time.time()-start_time)
results = solver_manager.solve(instance, opt=opt, tee=tee, options=options, suffixes=['dual','rc'])
This crashes on the last ("results") line, with the following error message:
The SolverFactory was unable to create the solver "ipopt"
and returned an UnknownSolver object. This error is raised at the point
where the UnknownSolver object was used as if it were valid (by calling
method "solve").
The original solver was created with the following parameters:
solver_io: nl
type: ipopt
_args: ()
options: {}
_options_str: []
I'm very new to all this, but thought that maybe Pyomo can't access the ipopt file it needs, which I think for me is located in the COIN-OR binaries I downloaded. I tried adding the relevant-seeming files to my PYTHONPATH and also importing them into the script, which didn't help. Any ideas what I should try next to either make this work, or to amend the script to something that would work?
A colleague of mine had the same issue and he managed to solve it by generating the solver object with the route to the IPOPT AMPL executable:
opt = SolverFactory('/route/to/ipopt',solver_io='nl')