coin-or / SHOT

A solver for mixed-integer nonlinear optimization problems
https://shotsolver.dev
Eclipse Public License 2.0
117 stars 25 forks source link

constant not initialized in Simplifications.h:convertSquareToUnivariteQuadraticExpression() #126

Closed svigerske closed 3 years ago

svigerske commented 3 years ago

With current master, I get this warning:

Model/Simplifications.h:1061:42: warning: ‘constant’ may be used uninitialized in this function [-Wmaybe-uninitialized]
 1061 |         std::make_shared<LinearTerm>(2.0 * constant * variableCoefficient, variable), constant * constant);
      |                                      ~~~~^~~~~~~~~~

I wouldn't know for sure how to fix this. The code does

    double constant;

    if(sum->children[0]->getType() == E_NonlinearExpressionTypes::Constant)
    {
        constant = std::dynamic_pointer_cast<ExpressionConstant>(sum->children[0])->constant;
        ...
    }
    else if(sum->children[1]->getType() == E_NonlinearExpressionTypes::Constant)
    {
        constant = std::dynamic_pointer_cast<ExpressionConstant>(sum->children[1])->constant;
        ...
    }

    resultingExpression = std::make_tuple(
        std::make_shared<QuadraticTerm>(variableCoefficient * variableCoefficient, variable, variable),
        std::make_shared<LinearTerm>(2.0 * constant * variableCoefficient, variable), constant * constant);

So should the last be called also if neither term of the sum is a constant, maybe with constant=0?

Or is there a

else
   return resultingExpression;

missing before?

andreaslundell commented 3 years ago

The constant should be initialized to zero on line 1018. What this does is expands (c x + d)^2 to c^2 x^2 +2 c d x + d^2.

What kind of fancy compiler do you use to get these kinds of warnings? :-)

svigerske commented 3 years ago

It's called the GNU compiler collection :stuck_out_tongue: Maybe you need -Wall to get these.