Using external functions through ASL in pyomo, solving with ipopt #761
-
Hi, I'm planning to write a corresponding ASL structural adapter function for a C++ function I've written as an external function. Then I compile it into a .dll file, call it via ExternalFunction in Pyomo and solve it with IPOPT. I'm now experiencing the following error message from IPOPT.It may seem a bit confusing.
Here is the C++ function. double Srk::CalcMassEntropyLiquidMixture(
std::vector<double> &molefraction,
double &temperature,
double &pressure) Here's the adapter function I write that corresponds to the ASL structure. real calc_mass_entropy_liquid_mixture(arglist *al) {
int n = al->n;
std::vector<double> molefraction = {al->ra, al->ra + n - 3};
double temperature = al->ra[n-2];
double pressure = al->ra[n-1];
real entropy;
entropy = Srk::CalcMassEntropyLiquidMixture(molefraction, temperature, pressure);
return entropy; // return answer
}
void funcadd(AmplExports *ae){
int t = FUNCADD_REAL_VALUED;
addfunc("calc_mass_entropy_liquid_mixture", (rfunc)calc_mass_entropy_liquid_mixture, t, -1, NULL);
} Here's the pyomo part. def create_model():
m = pyo.ConcreteModel()
m.name = 'Example 1: Eason'
m.z = pyo.Var(range(3), domain=pyo.Reals, initialize=2.)
m.x = pyo.Var(range(3), initialize={0: 0.3, 1: 0.3, 2: 0.4})
m.temperature = pyo.Var(initialize=260.0)
m.pressure = pyo.Var(initialize=1823850)
m.ext_fcn = pyo.ExternalFunction(library='myfunctions.dll', function='calc_mass_entropy_liquid_mixture') #Here calls the imported function
m.obj = pyo.Objective(expr=(m.z[0]-1.0)**2 + (m.z[0]-m.z[1])**2 + (m.z[2]-1.0)**2 + (m.x[0]-1.0)**4 + (m.x[1]-1.0)**6)
m.c1 = pyo.Constraint(expr=100 + m.ext_fcn(m.x[0], m.x[1], m.x[2], m.temperature, m.pressure) == 66) #it becomes a part of the constraints here
m.c2 = pyo.Constraint(expr=m.z[2]**4 * m.z[1]**2 + m.z[1] == 8+pyo.sqrt(2.0))
return m
model = create_model()
solver = pyo.SolverFactory('ipopt')
try:
result = solver.solve(model, tee=True)
print("Solver status:", result.solver.status)
print("Solver termination condition:", result.solver.termination_condition)
except Exception as e:
print("Error during solving:", e) Interestingly, when I replace m.c1 = pyo.Constraint(expr=100 + m.ext_fcn(m.x[0], m.x[1], m.x[2], m.temperature, m.pressure) == 66) with m.c1 = pyo.Constraint(expr=100 + m.ext_fcn(m.x, m.temperature, m.pressure) == 66) in pyomo, the error is
I have no idea how to fix it, so I ask for help here. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
There is a good advise already:
So what does the Ipopt log say? |
Beta Was this translation helpful? Give feedback.
-
I've solved the problem. Compiling the .dll library with gcc did the trick! I used to compile it with Developer Command Prompt for VS, but it failed. |
Beta Was this translation helpful? Give feedback.
I've solved the problem. Compiling the .dll library with gcc did the trick! I used to compile it with Developer Command Prompt for VS, but it failed.