Menu

#57 Problem setting the decision variables bounds(lower,upper)

Any Version
open
nobody
None
5
2015-12-21
2015-12-20
Anonymous
No

Hi, Every One

Due of being a new user of Java as well MOEA, please can anyone provide me with details of how to extend the RealVariable class or the EncodingUtils.newReal to set the decision variables bounds (lower & upper) to a variables declared somewhere else in my program out side the newSolution method as shown bellow

This is part of my code

@Override
public void evaluate (Solution solution) {
.....
.....
// finding the smallest and largest numbers in an array
largest = points [0] [0] ;
smallest = points [0] [0] ;
for (int i = 0; i < 40; ++i)
{
for (int j = 0; j < 3; ++j)
{
if(points[i] [j] > largest)
largest = points[i] [j];
else if (points[i] [j] < smallest)
smallest = points[i] [j];
}
}
............
............
// setting the decision variables bounds to the lowest and highest values of the array
@Override
public Solution newSolution ()
{
Solution solution = new Solution(numberOfVariables,
numberOfObjectives);
for(int i=0; i< numberOfVariables; i++)
{
solution.setVariable(i, new RealVariable(smallest, largest));
}
return solution;
}

Thank you

Discussion

  • Anonymous

    Anonymous - 2015-12-21

    Here is an example. The lower and upper bound are stored in a static array that you can modify.

    import java.util.Arrays;
    
    import org.moeaframework.Executor;
    import org.moeaframework.core.NondominatedPopulation;
    import org.moeaframework.core.PRNG;
    import org.moeaframework.core.Solution;
    import org.moeaframework.core.variable.EncodingUtils;
    import org.moeaframework.core.variable.RealVariable;
    import org.moeaframework.problem.AbstractProblem;
    
    public class Example4 {
    
    static int nvars = 11;
    static double[] lowerBounds = new double[nvars];
    static double[] upperBounds = new double[nvars];
    
    static {
        Arrays.fill(lowerBounds, 0.0);
        Arrays.fill(upperBounds, 1.0);
    }
    
    public class ExtendedRealVariable extends RealVariable {
    
        private double[] lowerBounds;
    
        private double[] upperBounds;
    
        private int index;
    
        public ExtendedRealVariable(double[] lowerBounds, double[] upperBounds, int index) {
            super(-Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
            this.lowerBounds = lowerBounds;
            this.upperBounds = upperBounds;
            this.index = index;
        }
    
        @Override
        public double getLowerBound() {
            return lowerBounds[index];
        }
    
        @Override
        public double getUpperBound() {
            return upperBounds[index];
        }
    
        @Override
        public RealVariable copy() {
            RealVariable copy = new ExtendedRealVariable(lowerBounds, upperBounds, index);
            copy.setValue(getValue());
            return copy;
        }
    
        @Override
        public void randomize() {
            setValue(PRNG.nextDouble(getLowerBound(), getUpperBound()));
        }
    
    }
    
    public static class MyDTLZ2 extends AbstractProblem {
    
        public MyDTLZ2() {
            super(nvars, 2);
        }
    
        @Override
        public Solution newSolution() {
            Solution solution = new Solution(getNumberOfVariables(), 
                    getNumberOfObjectives());
    
            for (int i = 0; i < getNumberOfVariables(); i++) {
                solution.setVariable(i, new ExtendedRealVariable(lowerBounds, upperBounds, i));
            }
    
            return solution;
        }
    
        @Override
        public void evaluate(Solution solution) {
            double[] x = EncodingUtils.getReal(solution);
            double[] f = new double[numberOfObjectives];
    
            int k = numberOfVariables - numberOfObjectives + 1;
    
            double g = 0.0;
            for (int i = numberOfVariables - k; i < numberOfVariables; i++) {
                g += Math.pow(x[i] - 0.5, 2.0);
            }
    
            for (int i = 0; i < numberOfObjectives; i++) {
                f[i] = 1.0 + g;
    
                for (int j = 0; j < numberOfObjectives - i - 1; j++) {
                    f[i] *= Math.cos(0.5 * Math.PI * x[j]);
                }
    
                if (i != 0) {
                    f[i] *= Math.sin(0.5 * Math.PI * x[numberOfObjectives - i - 1]);
                }
            }
    
            solution.setObjectives(f);
        }
    
    }
    
    public static void main(String[] args) {
        //configure and run the DTLZ2 function
        NondominatedPopulation result = new Executor()
                .withProblemClass(MyDTLZ2.class)
                .withAlgorithm("NSGAII")
                .withMaxEvaluations(10000)
                .run();
    
        //display the results
        System.out.format("Objective1  Objective2%n");
    
        for (Solution solution : result) {
            System.out.format("%.4f      %.4f%n",
                    solution.getObjective(0),
                    solution.getObjective(1));
        }
    }
    
    }
    
     
    • Anonymous

      Anonymous - 2015-12-21

      Hi, thank you very much for your help

      When I runThe code I have got 2 errors as follows:

      1- with the code part

      @Override
      public void randomize() {
      setValue(PRNG.nextDouble(getLowerBound(), getUpperBound()));
      }

      I got this error message
      ((method randomize() of type Example4.ExtendedRealVariable must override or implement a supertype method))

      2- withe the statement

      solution.setVariable(i, new ExtendedRealVariable(lowerBounds, upperBounds, i));

      I got this error message
      ((No enclosing instance of type Example4 is accessible. Must qualify the allocation with an enclosing instance of type Example4 (e.g. x.new A() where x is an instance of Example4))

      So can you please tell me what I should do to correct these errors

      Thank you

       
  • Anonymous

    Anonymous - 2015-12-21

    To fix the first issue, upgrade to version 2.7 of the software.

    For the second error, make the ExtendedRealVariable class static:

      public static class ExtendedRealVariable ...
    
     
    • Anonymous

      Anonymous - 2015-12-21

      Thank you again

      for the second error I have made the ExtendedRealVariable class static as you mentioned and it solved the issue.

      for the first error I have removed the @Override annotation befote the (public void randomize()) method, and now the code is running and giving results. So is removing the @Override correct or not???

      Thanks

       
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.