I am trying to solve a bilevel problem using Pyomo in Python. However, when I try to run the code, I am getting the following error:
"Implicit conversion of Pyomo NumericValue type `mon' 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 Pyomo's documentation there is no reference to Pyomo-provided functions. I want to know how I can modify the penultimate line of code shown so that model.rn[i,j] meet the integer requirement?
The following is my code:
import random
import matplotlib.pyplot as plt
import numpy as np
from pyomo.environ import *
from pyomo.bilevel import *
from pyomo.bilevel.components import SubModel
from pyomo.opt import SolverFactory
capacity =[150,80, 65]
model = ConcreteModel()
model.sub = SubModel()
model.M=RangeSet(1,3)
model.N=RangeSet(1,12)
model.f= Param(model.M,model.N,within=NonNegativeIntegers,initialize=20)
model.v= Param(model.M,model.N,within=NonNegativeIntegers)
model.sub.x = Param(within=Binary)
model.r= Var(model.M,model.N,within=PercentFraction)
model.rp= Var(model.M,model.N,within=NonNegativeReals,bounds=(0, 10))
model.rn = Var(model.M, model.N, within=NonNegativeIntegers)
model.un= Var(model.M,model.N,within=NonNegativeIntegers)
for j in range(1,13):
model.v[1,j] = capacity[0]-model.f[1,j]
model.v[2,j] = capacity[1]-model.f[2,j]
model.v[3,j] = capacity[2]-model.f[3,j]
for j in range(1,13):
for i in range(1,4):
model.rn[i,j]=floor(model.v[i,j]*model.r[i,j])
model.un[i,j]=model.v[i,j]-model.rn[i,j]
That's tricky to do. As far as I know, it may only work on the values of that pyomo object as model.r is a pyomo object. It is not a problem on a parameter but the variable.
You may want to write out constraints that models the python 'floor' function instead.
Related
There is a function called dea(x, y, *args) in library(Benchmarking) which returns useful objects. I've described 3 key ones below:
crs = dea(mydata_matrix_x, my_data_matrix_y, RTS="IN", ORIENTATION= "in") # both matrixes have N rows
efficiency(crs) # a 'numeric' type object which looks like a 1xN vector
peers(crs) # A matrix: Nx2 (looks to me like a pandas dataframe when run in .ipynb file with R kernel)
lambda(crs) # A matrix: Nx2 of type dbl (also looks like a dataframe)
Now I would like to programatically vary my_data_matrix_x. This matrix represents my inputs. At first it will be a Nx10 matrix. However I intend to drop each column sequentially and run dea() on the Nx9 matrix, then graph the efficiency(crs) scores that come out. The issue is I have no idea how to achieve this in R (amongst other things) and would rather circumvent the issue by writing all my code in Python and importing this dea() function somehow from an R script
I believe the best solution available to me will be to read and write from files:
from Benchmarking_script.r import dea
def test_inputs(data, input):
INPUTS = ['input 1', 'input2', 'input3', 'input4,' 'input5']
OUTPUTS = ['output1', 'output2']
data_inputs = data.drop(f"{input}", axis=1)
data_outputs = data[OUTPUTS]
data_inputs.to_csv("my_inputs.csv")
data_outputs.to_csv("my_outputs.csv")
run Benchmarking.dea(data_inputs, data_outputs, RTS="crs", ORIENTATION="in")
clearly this last line won't work: I am interested to hear flexible (and simple!) ways to run this dea() function idiomatically as if it was a native Python function
Related SO questions
The closest answer on SO I've found has been Importing any function from an R package into python
When adapting the code I've written
import pandas as pd
data = pd.read_csv("path/to_data.csv")
import rpy2
import rpy2.robjects as robjects
import rpy2.robjects.packages as rpackages
from rpy2.robjects.vectors import StrVector
from rpy2.robjects.packages import importr
utils = rpackages.importr('utils')
utils.chooseCRANmirror(ind=1)
packnames = ('Benchmarking')
utils.install_packages(StrVector(packnames))
Benchmarking = importr('Benchmarking')
crs = Benchmarking.dea(data['Age'], data['CO2'], RTS='crs', ORIENTATION='in')
--------------------------------------------------------------
NotImplementedError: Conversion 'py2rpy' not defined for objects of type '<class 'pandas.core.series.Series'>'
So importing the function natively as a Python file hasn't worked
The second approach is the way to go. You need to use a converter context so python and r variables would be converted automatically. Specifically, try pandas2ri submodule shipped with rpy2. Something like this:
from rpy2.robjects import pandas2ri
with pandas2ri:
crs = Benchmarking.dea(data['Age'], data['CO2'], RTS='crs', ORIENTATION='in')
If this doesn't work, update your post with the error.
I'm faced with an Hessenberg index-2 DAE, I'm trying to solve it using the python module gekko.
After a few days of trial and error, I think I'm not too far from a code that works. But I've just realized that maybe gekko is not able to handle complex numbers?
Here is a minimal working example:
import numpy as np
from gekko import GEKKO
# Define the simulation and its parameters
g = GEKKO()
g.options.IMODE = 7
g.options.NODES = 1
# define the time array
n_steps = 100
Time = np.linspace(0, 2 * np.pi, n_steps)
g.time = Time
# Initialise the variables
x = g.Var(0.0)
# Write the model's equations
g.Equation(x.dt() == 1.0j * x)
# solve the equations
g.solve(disp = False)
print(x.value)
If I try to run this code, I expect to find the standard complex exponential.
But instead, I get the following error:
File "gekko.py", line 2185, in solve
raise Exception(response)
Exception: #error: Model Expression
*** Error in syntax of function string: Missing operator
Position: 9
$v1-(((1j)*(v1)))
Could you confirm that gekko cannot handle complex numbers? And maybe suggest another python DAE solver that does?
Thank you so much!
Gekko does not natively handle complex numbers. The automatic differentiation and gradient-based solvers haven't been programmed with that in mind. As you discussed in the comments, there are workarounds to solve complex number problems by splitting the variable. There are additional suggestions at Application of complex numbers in Linear Programming?
I am trying to use scipy Optimize fsolve function to find the zero of a function defined using a COM object.
First, I create the COM object:
import win32com.client
os = win32com.client.Dispatch("PX32.OpenServer.1")
This COM object works as an interface to a program where I input variables, then make calculations, and obtain results, using the setvalue, docommand and getvalue methods/functions.
If I define a function like:
def fn(x):
os.setvalue("INPUT_VAR_STRING", x)
os.docommand("COMMAND_STRING")
return float(os.getvalue("RESULT_STRING"))
By changing the input value (passed into fn through x), I get different output values:
fn(100)
> 18.139818203
fn(190)
> -40.93182830
But, if I try to use fsolve to find the root of the fn function, I get an error:
from scipy.optimize import fsolve
fsolve(fn,150)
> TypeError
> (...)
> TypeError: Internal error - the buffer length is not the sequence length!
What does this mean? How can I try to solve this error that has appeared?
I found the problem. According to http://kitchingroup.cheme.cmu.edu/pycse/pycse.html,
12.11 Using an external solver with Aspen
(...)
flashT = float(flashT) # COM objects do not understand numpy types
That was the main thing. I had to change
os.setvalue("INPUT_VAR_STRING", x) for
os.setvalue("INPUT_VAR_STRING", float(x)) to get the expected results.
I'm fairly new to programming, but this problem happens in python and in excel as well.
I'm using the following formulas for the RC transfer function
s/(s+1) for High Pass
1/(s+1) for Low Pass
with s = jwRC
below is the code I used in python
from pylab import *
from numpy import *
from cmath import *
"""
Generating a transfer function for RC filters.
Importing modules for complex math and plotting.
"""
f = arange(1, 5000, 1)
w = 2.0j*pi*f
R=100
C=1E-5
hp_tf = (w*R*C)/(w*R*C+1) # High Pass Transfer function
lp_tf = 1/(w*R*C+1) # Low Pass Transfer function
plot(f, hp_tf) # plot high pass transfer function
plot(f, lp_tf, '-r') # plot low pass transfer function
xscale('log')
I can't post images yet so I can't show the plot. But the issue here is the cutoff frequency is different for each one. They should cross at y=0.707, but they actually cross at about 0.5.
I figure my formula or method is wrong somewhere, but I can't find the mistake can anyone help me out?
Also, on a related note, I tried to convert to dB scale and I get the following error:
TypeError: only length-1 arrays can be converted to Python scalars
I'm using the following
debl=20*log(hp_tf)
This is a classical example why you should avoid pylab and more generally imports of the form
from module import *
unless you know exactly what it does, since it hopelessly clutters the name space.
Using,
import matplotlib.pyplot as plt
import numpy as np
and then calling np.log and plt.plot etc. will solve your problem.
Furether explanations
What's happening here is that,
from pylab import *
defines a log function from numpy that operate on arrays (the one you want).
However, the later import,
from cmath import *
overwrites it with a version that only accepts scalars, hence your error.
In a project using SciPy and NumPy, should I use scipy.pi, numpy.pi, or math.pi?
>>> import math
>>> import numpy as np
>>> import scipy
>>> math.pi == np.pi == scipy.pi
True
So it doesn't matter, they are all the same value.
The only reason all three modules provide a pi value is so if you are using just one of the three modules, you can conveniently have access to pi without having to import another module. They're not providing different values for pi.
One thing to note is that not all libraries will use the same meaning for pi, of course, so it never hurts to know what you're using. For example, the symbolic math library Sympy's representation of pi is not the same as math and numpy:
import math
import numpy
import scipy
import sympy
print(math.pi == numpy.pi)
> True
print(math.pi == scipy.pi)
> True
print(math.pi == sympy.pi)
> False
If we look its source code, scipy.pi is precisely math.pi; in fact, it's defined as
import math as _math
pi = _math.pi
In their source codes, math.pi is defined to be equal to 3.14159265358979323846 and numpy.pi is defined to be equal to 3.141592653589793238462643383279502884; both are well above the 15 digit accuracy of a float in Python, so it doesn't matter which one you use.
That said, if you're not already using numpy or scipy, importing them just for np.pi or scipy.pi would add unnecessary dependency while math is a Python standard library, so there's not dependency issues. For example, for pi in tensorflow code in python, one could use tf.constant(math.pi).