Object value is zero

Hi everyone! I changed the initial values of my object. However, I still get zero as the output for the object values from the solver. How can I solve this problem? The following code contains pieces of my code:

void setInitValues(double xt, double yt)
{
// Set initial values for variables
ampl::Variable xTarget = ampl->getVariable(“x2”);
ampl::Variable yTarget = ampl->getVariable(“y2”);
xTarget.setValue(xt);
yTarget.setValue(yt);

std::cout << "set initial values in AMPL" << std::endl;
// Print the initial values to confirm they are set correctly
std::cout << "Initial value of x target: " << xTarget.value() << std::endl;
std::cout << "Initial value of y target: " << yTarget.value() << std::endl;

ampl -> solve();
double xt = ampl->getVariable("x2").value();
double yt = ampl->getVariable("y2").value();
ampl::Objective obj_value = ampl -> getObjective("Obj_value");
double objValue = obj_value.value();
std::cout << "Objective is: " << objValue << std::endl;

}

How can I fix this problem, and what is causing it? Thank you for your help.

1 Like

Hi @ae.naderi,

Thank you for reaching out. What is the output from the solver? Even though you are setting the initial value, the solver will change the values of the variables and may be setting them back to zero. What do you get if you remove ampl -> solve();?