Pyomo accessing solver status - pyomo

I am solving a MINLP with Pyomo and Scip as an Solver.
After the problem is solved I automatically get this output:
SCIP Status : problem is solved [optimal solution found]
Solving Time (sec) : 13.00
Solving Nodes : 1845
Primal Bound : +5.21506487017157e+03 (26 solutions)
Dual Bound : +5.21506487017157e+03
Gap : 0.00 %
I want to save the values of this output. With the solving time it works like this:
results = opt.solve(instance)
Time = results.solver.time
However if I try the same with the Gap and the Nodes get an error:
AttributeError: Unknown attribute `gap' for object with type <class 'pyomo.opt.results.solver.SolverInformation'>
I have also tried using:
GAP = results.solution.gap
Which results in an output that says undefined.
Is there any other way to acess those two values.

Related

Is it possible to output error on training data through the Experimenter module?

I am trying to build a learning curve which compares training and testing accuracy versus training set size in WEKA. The testing accuracy portion versus training set size is easily done (through LearningRateProducer), but what I can't figure out is how to get training accuracy results through the experimenter module in an automated way. Here is an example of the output I'm looking for. This result is from the simple CLI module after running IBk.
=== Error on training data ===
Correctly Classified Instances 4175 100 %
Incorrectly Classified Instances 0 0 %
Kappa statistic 1
Mean absolute error 0.0005
Root mean squared error 0.0012
Relative absolute error 0.717 %
Root relative squared error 0.6913 %
Total Number of Instances 4175
I could do this through simple CLI, but I have many experiments that I need to generate a learning curve for and I would prefer a less manual way. An experiment module solution would be most desirable.
Thanks,
B
I was able to get this information by installing the groovy console and using the following script:
data = (new weka.core.converters.ConverterUtils.DataSource("/Path/To/Arff")).getDataSet()
data.setClassIndex(data.numAttributes() - 1)
data.randomize(new Random(1))
classifier = new weka.classifiers.trees.J48()
println "|train|\t%acc_{train}\t%acc_{test}"
stepSize = data.numInstances() / 10
for (int i = stepSize; i < data.numInstances(); i += stepSize ) {
subset = new weka.core.Instances(data, 1, i)
classifier.buildClassifier(subset)
evaluationObject = new weka.classifiers.evaluation.Evaluation(subset)
evaluationObject.evaluateModel(classifier, subset)
testSubset = new weka.core.Instances(data, i + 1, data.numInstances() - (i + 1))
evaluationObjectTest = new weka.classifiers.evaluation.Evaluation(subset)
evaluationObjectTest.evaluateModel(classifier, testSubset)
credit to Eibe Frank: https://weka.8497.n7.nabble.com/How-to-generating-learning-curve-for-training-set-td41654.html
The solution is comparable to Experimenter. You can directly call classifiers through groovy code and batch them however you need.

How to fix RuntimeError & Segmentation fault while running demo codes in auto-07p

I was running demo codes in AUTO-07p, which is mainly based on fortran but also following python syntax, but I was getting same error when I run most of demo codes. Since they're not written by me, but written by experts and has been released for a long time, so there must NOT an error. Maybe I don't have to modify the codes.
So when I run codes containing either bifurcation analysis part or plotting, I kept getting an error message RuntimeError: maximum recursion depth exceeded while calling a Python object.
So, I tried to change recursion limit to 10^7, but then I got the error message "Segmentation fault (core dumped)
I'm using ubuntu 16.04 LTS
demo('bvp')
bvp =run('bvp')
branchpoints = bvp("BP")
for solution in branchpoints:
bp = load(solution,ISW=-1,NTST=50)
# Compute forwards
print "Solution label",bp["LAB"],"forwards"
fw = run(bp)
# Compute backwards
print "Solution lavel", bp["LAB"], "backwards"
bw = run(bp,DS='-')
both = fw + bw
merged = merge(both)
bvp = bvp + merged
bvp=relavel(bvp)
save(bvp, 'bvp')
plot(bvp)
wait()
The error message says (excluding seemingly unnecessary lines)
/usr/local/lib/python2.7/dist-packages/numpy/core/numeric.pyc in flatnonzero(a)
924
925
-->926 return np.nonzero(np.ravel(a))[0]
927
928
... last 1 frames repeated, from the frame below ...
/usr/local/lib/python2.7/dist-packages/numpy/core/numeric.pyc in flatnonzero(a)
924
925
-->926 return np.nonzero(np.ravel(a))[0]
927
928
RuntimeError: maximum recursion depth exceeded while calling a Python object
So it seems the code is stuck in an infinite loop, but that flatnonzero function is simply indexing nonzero entries... I'm not sure what should I do

How to import a 3D mesh which is generated by Gmsh to Fipy?

I'm working on a coupled DEM-CFD model. I'm using Fipy for its CFD part and my geometry is cylinder. How can I import generated mesh in Gmsh to Fipy?
I need to generate a cylindrical geometry and discretised it by generating triangular mesh in 3D in Fipy. I tried to find a command in the help of Fipy to generate a 3D triangular mesh in cylindrical geometry, but I couldn’t find anything relevant for 3D. I have generated the Mesh in Gmsh software which is accepted by Fipy based on its help. I have the mesh file now, but I cannot import it to Fipy.
Whenever I tried to import it to the model, I faced with this error Gmsh version must be >= 2.0. However, my Gmsh version was 4.
I used Gmsh V2.5 as well but the error was the same. I searched the error and I see some people were struggling with that as well. They recommended to put the Gmsh.exe file in script folder to solve the problem. I have done that but I’ve got another error. The new error is
Windows Error: [Error 32] The process cannot access the file because it is being used by another process
when the command was mesh = fp.Gmsh3D("Cylinder.msh").
I searched this error as well and someone suggested to mention geometry file after mesh, mesh = fp.Gmsh3D("Cylinder.msh, Cylinder.geo").
Now, I’m getting this
GmshException: Gmsh hasn't produced any cells! Check your Gmsh code.
I tried to make the circular mesh which is a 2D triangular mesh in a circle (it is in Fipy help), but I got this
GmshException: Gmsh hasn't produced any cells! Check your Gmsh code
The python scritp, cylinder.msh, cylinder.geo and Gmsh.exe files are in the same folder.
Error:
File "C:\Users\jpethati\AppData\Local\Continuum\anaconda2\lib\site-packages\fipy\meshes\gmshMesh.py", line 809, in read
raise GmshException(errStr)
GmshException: Gmsh hasn't produced any cells! Check your Gmsh code.
Gmsh output:
Info : Running 'gmsh c:\users\jpethati\appdata\local\temp\tmpsw2tzg.geo -3 -nopopup -format msh -o c:\users\jpethati\appdata\local\temp\tmp6gbww9.msh' [Gmsh 4.2.2, 1 node, max. 1 thread]
Info : Started on Fri Mar 29 10:25:25 2019
Info : Reading 'c:\users\jpethati\appdata\local\temp\tmpsw2tzg.geo'...
Info : Done reading 'c:\users\jpethati\appdata\local\temp\tmpsw2tzg.geo'
Info : Meshing 1D...
Info : Done meshing 1D (0 s)
Info : Meshing 2D...
Info : Done meshing 2D (0 s)
Info : Meshing 3D...
Info : Done meshing 3D (0 s)
Info : 0 vertices 0 elements
Info : Writing 'c:\users\jpethati\appdata\local\temp\tmp6gbww9.msh'...
Info : Done writing 'c:\users\jpethati\appdata\local\temp\tmp6gbww9.msh'
Info : Stopped on Fri Mar 29 10:25:25 2019
import fipy as fp
First try
mesh = fp.Gmsh3D("Cylinder.msh")
Second try
mesh = fp.Gmsh3D("Cylinder.msh, Cylinder.geo")
FiPy 3.3 resolved some issues with Gmsh on Windows. Please upgrade and let us know if you're still having problems.

Tensorflow RNN slice error

I am attempting to create a multilayered RNN using LSTMs in tensorflow. I am using Tensorflow version 0.9.0 and python 2.7 on Ubuntu 14.04.
However, I keep getting the following error:
tensorflow.python.framework.errors.InvalidArgumentError: Expected begin[1] in [0, 2000], but got 4000
when I use
rnn_cell.MultiRNNCell([cell]*num_layers)
if num_layers is greater than 1.
My code:
size = 1000
config.forget_bias = 1
and config.num_layers = 3
cell = rnn_cell.LSTMCell(size,forget_bias=config.forget_bias)
cell_layers = rnn_cell.MultiRNNCell([cell]*config.num_layers)
I would also like to be able to switch to using GRU cells but this gives me the same error:
Expected begin[1] in [0, 1000], but got 2000
I have tried explicitly setting
num_proj = 1000
which also did not help.
Is this something to do with my use of concatenated states? As I have attempted to set
state_is_tuple=True
which gives:
`ValueError: Some cells return tuples of states, but the flag state_is_tuple is not set. State sizes are: [LSTMStateTuple(c=1000, h=1000), LSTMStateTuple(c=1000, h=1000), LSTMStateTuple(c=1000, h=1000)]`
Any help would be much appreciated!
I'm not sure why this worked but, I added in a dropout wrapper. i.e.
if Training:
cell = rnn_cell.DropoutWrapper(cell,output_keep_prob=config.keep_prob)
And now it works.
This works for both LSTM and GRU cells.
This problem is occurring because you have increased layer of your GRU cell but your initial vector is not doubled. If your initial_vector size is [batch_size,50].
Then initial_vector = tf.concat(1,[initial_vector]*num_layers)
Now input this to decoder as initial vector.

Reaching limitations creating a gallery of plots

In a script I'm using, the code generates a figure where a number of subplots are generated. Usually it creates a rectangular grid of plots, but for it's current use, the horizontal parameter only has 1 value, and the vertical parameter has considerably more values than it has had previously. This is causing my program to crash while running, because (presumably) the vertical dimension is too large. The code that's causing the issue is:
#can't get past the first line here
self.fig1 = plt.figure('Title',figsize=(4.6*numXparams,2.3*numYparams))
self.gs = gridspec.GridSpec(numYparams,numXparams)
self.gs.update(left=0.03, right=0.97, top=0.9, bottom=0.1, wspace=0.5, hspace=0.5)
and then later in a nested for loop running over both params:
ax = plt.subplot(self.gs[par0, par1])
The error I'm getting is:
X Error of failed request: badAlloc (insufficient resources for operation)
Major opcode of failed request: 53 (X_CreatePixmap)
Serial number of failed request: 295
Current serial number in output stream: 296
My vertical parameter currently has 251 values in it, so I can see how 251*2.3 inches could lead to trouble. I added in the 2.3*numYparams because the plots were overlapping, but I don't know how to create the figure any smaller without changing how the plots are arranged in the figure. It is important for these plots to stay in a vertically oriented column.
There are a couple of errors in your code. Fixing them allowed me to generate the figure you are asking for.
# I needed the figsize keyword here
self.fig1 = plt.figure(figsize=(4.6*numXparams,2.3*numYparams))
# You had x and y switched around here
self.gs = gridspec.GridSpec(numYparams,numXparams)
self.gs.update(left=0.03, right=0.97, top=0.9, bottom=0.1, wspace=0.5, hspace=0.5)
# I ran this loop
for i in range(numYparams):
ax = fig1.add_subplot(gs[i, 0]) # note the y coord in the gridspec comes first
ax.text(0.5,0.5,i) # just an identifier
fig1.savefig('column.png',dpi=50) # had to drop the dpi, because you can't have a png that tall!
and this is the top and bottom of the output figure:
Admittedly, there was a lot of space above the first and below the last subplot, but you can fix that by playing with the figure dimensions or gs.update