# Re: Null Value when using PenaltyMethod

Home 21090308 Forums Re: Null Value when using PenaltyMethod

#2007
frogman119
Member

Hi,

please find below the objective function and the constraints. I have cut out some passages (…). Please apologize the unprofessional way of coding. The array “variables” contains doubles read from a string handed over to the library. I chose the “ConjugateGradient.class” as optimization algorithm in the penalty method. For some problems the routine works fine, for some I get the null value for xmin and the code throws a null-pointer exception when running the last line “xmin = optim.search(1000, xmin);”.

Thanks a lot for your help.
rgds
Roger

….
public double [] variables = new double [500];
public int finalcounter;
….

final int fdimension = ((numberofvariables-6)/3) + 1;

//here goes the function to be maximized
RealScalarFunction f = new RealScalarFunction() {

public int dimension4Domain() {
return fdimension;
}
public int dimension4Range() {
return 1;
}
@Override
public double evaluate(double… x)
{
double result = 0.0;
if ((variables[0] 0))
{
result = result + (variables[1] * (x[0] – variables[4]));
int numbertocount1 = fdimension-1;
for (int i=0, j=0; j<numbertocount1; i+=3, j+=1)
{
result += (variables[1] * variables[i+8] * (x[j+1] – variables[i+6]));
result += (variables[2] * (x[j+1] – variables[i+6]));
result -= (1 + (variables[2] * (x[j+1] – variables[i+6])))* Math.log(1 + (variables[2] * (x[j+1] – variables[i+6])));
}
}
if ((variables[0] > 1.6) && (variables[0] < 2.5))
{
result = result + (variables[1] * (x[0] – variables[4]));
int numbertocount1 = fdimension-1;
for (int i=0, j=0; j<numbertocount1; i+=3, j+=1)
{
result += (variables[1] * variables[i+8] * (x[j+1] – variables[i+6]));
result += (variables[2] * (x[j+1] – variables[i+6]));
result -= (1 + (variables[2] * (x[j+1] – variables[i+6])))* Math.log(1 + (variables[2] * (x[j+1] – variables[i+6])));
}
}
if (variables[0] > 2.7)
{
double g = 4 * variables[3];
double partresult = 0.0;
result = result + (g * (x[0] – variables[4]));
int numbertocount1 = fdimension-1;
for (int i=0, j=0; j<numbertocount1; i+=3, j+=1)
{
result += (g * variables[i+8] * (x[j+1] – variables[i+6]));
partresult += (x[j+1] – variables[i+6]);
}
result = result – (2 * partresult * partresult);
}
if ((variables[0] -1.5))
{
result = result + (variables[1] * (x[0] – variables[4]));
int numbertocount1 = fdimension-1;
for (int i=0, j=0; j<numbertocount1; i+=3, j+=1)
{
result += (variables[1] * variables[i+8] * (x[j+1] – variables[i+6]));
result += (variables[2] * (x[j+1] – variables[i+6]));
result -= (1 + (variables[2] * (x[j+1] – variables[i+6])))* Math.log(1 + (variables[2] * (x[j+1] – variables[i+6])));
}
}
if ((variables[0] -2.5))
{
result = result + (variables[1] * (x[0] – variables[4]));
int numbertocount1 = fdimension-1;
for (int i=0, j=0; j<numbertocount1; i+=3, j+=1)
{
result += (variables[1] * variables[i+8] * (x[j+1] – variables[i+6]));
result += (variables[2] * (x[j+1] – variables[i+6]));
result -= (1 + (variables[2] * (x[j+1] – variables[i+6])))* Math.log(1 + (variables[2] * (x[j+1] – variables[i+6])));
}
}
if (variables[0] < -2.7)
{
double g = 4 * variables[3];
double partresult = 0.0;
result = result + (g * (x[0] – variables[4]));
int numbertocount1 = fdimension-1;
for (int i=0, j=0; j<numbertocount1; i+=3, j+=1)
{
result += (g * variables[i+8] * (x[j+1] – variables[i+6]));
partresult += (x[j+1] – variables[i+6]);
}
result = result – (2 * partresult * partresult);
}
return -result;
}
};
//here go the constraints to be satisfied
RealScalarFunction[] test = new RealScalarFunction[fdimension];
if (variables[0] < 0)
{
finalcounter = 0;
for (int i = 0; i<fdimension; i++)
{
finalcounter=i;
test[finalcounter] = new RealScalarFunction(){
//non-negativity constraint
@Override
public int dimension4Domain() {
return fdimension;
}
public int dimension4Range() {
return 1;
}
@Override
public double evaluate(double… x)
{
return (-1 * x[finalcounter]);
}
};
}
}
if (variables[0] > 0)
{
finalcounter = 0;
for (int i = 0; i<fdimension; i++)
{
finalcounter=i;
test[finalcounter] = new RealScalarFunction(){
//limitation on the negative values which can be realized
@Override
public int dimension4Domain() {
return fdimension;
}
public int dimension4Range() {
return 1;
}
@Override
public double evaluate(double… x)
{
return (-100000000 – (1 * x[finalcounter]));
}
};
}
}
RealScalarFunction c16 = new RealScalarFunction() {
//this is a balance sheet constraint
@Override
public int dimension4Domain() {
return fdimension;
}
@Override
public int dimension4Range() {
return 1;
}
@Override
public double evaluate(double… x)
{
double result = 0.0;
result = variables[5] – x[0];
for (int i=1, j = 7; i<fdimension; i++, j+=3)
{
result -= (x * variables[j]);
}
return result;
}
};
//Problem formulation
Problem ConstrainedProblem = new Problem(f, new EqualityConstraints(c16),new InequalityConstraints(test));
PenaltyFunction a = new Courant(new EqualityConstraints(c16));
PenaltyFunction b = new Fletcher (new InequalityConstraints(test));
SumOfPenalties sp = new SumOfPenalties (a,b);
PenaltyMethod optim = new PenaltyMethod(ConstrainedProblem, sp, 1e30, 1e-15, ConjugateGradient.class);
double [] inputarray = new double[fdimension];
inputarray[0]=variables[4];
for (int i=1, j=6; i<fdimension; i++, j+=3)
{
inputarray
=variables[j];
}
Vector xmin = new DenseVector(inputarray);
xmin = optim.search(1000, xmin);
…