I am working on a C++ program that uses the Ipopt library for optimization. In my program, I have a class called IpoptOptimizer, which has properties startPoint and a. I initialize these properties using the initialize function, where I assign values to them. However, when I try to access these properties within Ipopt functions, such as eval_f, they appear to be empty.
Here's a more complete code with additional information:
ipoptOptimizer.h :
#pragma once
#include "IpTNLP.hpp"
#include "IpIpoptApplication.hpp"
using namespace Ipopt;
class ipoptOptimizer :
public TNLP
{
public:
std::vector<double> startPoint;
int a;
void initilize();
void startOptimization();
virtual bool eval_f(
Ipopt::Index n,
const Ipopt::Number* x,
bool new_x,
Ipopt::Number& obj_value
);
// And other ipopt functions, like get_bounds_info and so on
};
ipoptOptimizer.cpp
#include "ipoptOptimizer.h"
void ipoptOptimizer::initilize()
{
a = 10;
startPoint.push_back(1);
startPoint.push_back(2);
startPoint.push_back(3);
startPoint.push_back(4);
}
void ipoptOptimizer::startOptimization()
{
SmartPtr<TNLP> mynlp = new ipoptOptimizer();
SmartPtr<IpoptApplication> app = IpoptApplicationFactory();
app->Options()->SetNumericValue("tol", 3.82e-6);
app->Options()->SetStringValue("mu_strategy", "adaptive");
app->Options()->SetStringValue("output_file", "ipopt.out");
ApplicationReturnStatus status;
status = app->Initialize();
if (status != Solve_Succeeded)
std::cout << std::endl << std::endl << "*** Error during initialization!" << std::endl;
status = app->OptimizeTNLP(mynlp);
if (status != Solve_Succeeded)
std::cout << std::endl << std::endl << "*** Error during optimizing!" << std::endl;
}
bool ipoptOptimizer::eval_f(Index n, const Number* x, bool new_x, Number& obj_value)
{
std::cout << a << std::endl;
std::cout << startPoint.size();
return true;
}
// And other ipopt functions, like get_bounds_info and so on
and Main function:
#include "ipoptOptimizer.h"
int main()
{
ipoptOptimizer optimizer;
optimizer.initilize();
optimizer.startOptimization();
return 0;
}
The issue I am facing is that when I run the startOptimization function, the output of eval_f shows a as 0 and startPoint with a size of 0. This indicates that the properties are not being properly accessed or retained within the Ipopt functions.
I've verified that the properties are properly initialized in the initialize function and have valid values. However, when I try to access them within the eval_f function, or other functions of ipopt, they seem to be empty. I suspect there might be a scoping or initialization issue that I'm missing.
Can someone please guide me on how to resolve this issue? Any suggestions or insights would be greatly appreciated.
Thank you in advance!
I'm not familiar with this library but I think the problem is that you have two separate ipoptOptimizer
objects. There is one created in main
ipoptOptimizer optimizer;
and there is one created here
SmartPtr<TNLP> mynlp = new ipoptOptimizer();
Only the first object has the initilize
method called. This is why the properties you set don't seem to stick, you are setting the properties on one object, but testing another.
The code needs some work to remove one of these two objects. The quick fix seems to be to add the line
SmartPtr<TNLP> mynlp = new ipoptOptimizer();
mynlp->initilize(); // new line
An alternative would be to convert initilize
to a constructor, that way it is called automatically.
Then replace the entire contains of main
with the entire contents of startOptimization
(and delete startOptimization
).
But as I said, I'm not familiar with this library.