coin-or / OS

Optimization Services
Other
1 stars 4 forks source link

Error in gradient calculations #55

Closed svigerske closed 5 years ago

svigerske commented 5 years ago

Issue created by migration from Trac.

Original creator: @andreaslundell

Original creation time: 2015-01-10 11:52:54

Version:

I may have found a bug in the gradient calculations in OSInstance.

A minimal working example (utilizing the MINLP problem available at http://www.gamsworld.org/minlp/minlplib2/data/osil/synthes1.osil)


FileUtil fileUtil = new FileUtil(); OSiLReader osilreader = new OSiLReader(); OSInstance *instance = NULL;

std::string filename = "synthes1.osil"; std::string osil = fileUtil->getFileAsChar(filename.c_str());

instance = osilreader->readOSiL(osil);

instance->getJacobianSparsityPattern();

std::vector point(6); point[0] = 0.0; point[1] = 0.0; point[2] = 1.0; point[3] = 0.0; point[4] = 0.0; point[5] = 0.0;

SparseVector *sp; sp = instance->calculateConstraintFunctionGradient(&point.at(0), 0, true);

std::cout << "Printing model:" << std::endl;

std::cout << instance->printModel() << std::endl;

std::cout << "Now printing the gradient for constraint 0" << std::endl;

for (int i = 0; i < sp->number; i++) { std::cout << " index: " << sp->indexes[i] << " gradient value: " << sp->values[i] << std::endl; }


When running the code above, the output is:


Printing model:

Objectives: min -7x_2 + 5x_3 + 6x_4 + 8x_5 + --((10 x_0) + (-(18 ln( (x_1 + 1))) - ( 19.2 * ln( (-x_1 + x_0 + 1)))))

Constraints: e2 0 <= -.8x_2 + ((.96 ln( (-x_1 + x_0 + 1))) + (.8 ln( (x_1 + 1)))) e3 -2 <= -1x_2 + -2x_5 + ((1.2 ln( (-x_1 + x_0 + 1))) + ln( (x_1 + 1))) e4 -1x_0 + 1x_1 <= 0 e5 1x_1 + -2x_3 <= 0 e6 1x_0 + -1x_1 + -2x_4 <= 0 e7 1x_3 + 1*x_4 <= 1

Variables: x1: x_0 Type = C Lower Bound = 0 Upper Bound = 2 x2: x_1 Type = C Lower Bound = 0 Upper Bound = 2 x3: x_2 Type = C Lower Bound = 0 Upper Bound = 1 b4: x_3 Type = B Lower Bound = 0 Upper Bound = 1 b5: x_4 Type = B Lower Bound = 0 Upper Bound = 1 b6: x_5 Type = B Lower Bound = 0 Upper Bound = 1

Now printing the gradient for constraint 0 index: 2 gradient value: -0.8 index: 0 gradient value: -0.16 index: 1 gradient value: 0.96


The gradient for the first constraint is

nabla_(x_0) = 0.96/(1+x_0-x1) nabla(x_1) = -0.96/(1+x_0-x_1) + 0.8/(1+x1) nabla(x_2) = -0.8

so it seems the variable indexes for variables x_0 and x_1 are in the wrong order. Or am I missing something?

I am using the Windows binaries (1.7.4), which are already a year old, so it might be that this has been fixed already in a newer release.

svigerske commented 5 years ago

Comment by @h-i-gassmann created at 2015-03-05 21:00:40

Fixed in release 2.9.1

svigerske commented 5 years ago

Comment by @h-i-gassmann created at 2015-03-05 21:00:40

Resolution: fixed