Menu

#48 Customize new problem with extending MOEA current problem

Any Version
open
nobody
None
5
2015-07-30
2015-07-27
Anonymous
No

Hi I am a new user of MOEA. I have a linear optimization problem with one decision variable, three objectives and two constraints. I am trying to construct this problem with MOEA but failed.

My problem's detail is like this:
Objective 1: Maximize sum[EPDO(i)*X(i)]; Objective 2: Maximize sum[SCD(i)*X(i)]; Objective 3:Maximize sum[ASV(i)*X(i)]. Where: EPDO(i),SCD(i) and ASV(i) are parameters which I want to read from Excel. X(i) is a decision variable. i from 0 to 300. And constraints are sum[X(i)]=5000; and any X(i)>0 and<1000;

I don't know this kind of problem should be customized and constructed based on the implementation of problem, abstract problem or any other listed problems within MOEA since I felt that any of the listed examples in MOEA is not applicable to my problem.

Looking forward to your reply.

Discussion

  • Anonymous

    Anonymous - 2015-07-27

    In general, you should extend AbstractProblem. There are only two methods you need to implement: newSolution and evaluate. An example of a simple problem with 1 decision variable, 2 objectives, and 1 constraint is shown below:

    public class SchafferWithConstraint extends AbstractProblem {
    
        public SchafferWithConstraint() {
            super(1, 2, 1);
        }
    
        @Override
        public void evaluate(Solution solution) {
            double x = EncodingUtils.getReal(solution.getVariable(0));
    
            solution.setObjective(0, Math.pow(x, 2.0));
            solution.setObjective(1, Math.pow(x - 2.0, 2.0));
    
            // set constraint to 0 when satisfied, otherwise set to non-zero value
            solution.setConstraint(0, x > 1.0 ? 0 : x - 1.0);
        }
    
        @Override
        public Solution newSolution() {
            Solution solution = new Solution(1, 2, 1);
            solution.setVariable(0, EncodingUtils.newReal(-10.0, 10.0));
            return solution;
        }
    
    }
    
     
    • Anonymous

      Anonymous - 2015-07-27

      Hi, thank you for your quick response.
      Based on the material you provided, I constructed the codes for my problem "NgbrPro", but it seems incorrect. Could you please give me some advises? Looking forward to your reply soon.

      // Construct NgbrPro within MOEA Abstract Problem
      public class NgbrPro extends AbstractProblem {

      public NgbrPro(){
          super(1,3,1);
      }
      
      //main function
      public static void main(String[] args) {
      
          // Read and Input Parameters from Excel
          Read();
          InputP();
      
          // Call MOEA Executor and Algorithm "NSGAII" to solve problem"NgbrPro"
          NondominatedPopulation result = new Executor()
          .withProblemClass(NgbrPro.class)
          .withAlgorithm("NSGAII")
          .withMaxEvaluations(50000)
          .distributeOnAllCores()
          .run();
      }
      
      @Override
      public void evaluate(Solution solution){
      
          //define one decision variable x
          int [] x= EncodingUtils.getPermutation(solution.getVariable(0));
      
          //define three objectives
          double E=0;
          double SC=0;
          double SV=0;
          for (int i=0; i<results.size();i++){
              E+= EPDO[i]*x[i];
              SC+=SCD[i]*x[i];
              SV+=ASV[i]*x[i];
          }
          //negate the objectives since this problem is maximization
              E=-E;
              SC=-SC;
              SV=-SV;
      
          solution.setObjective(0, E);
          solution.setObjective(1, SC);
          solution.setObjective(2, SV);
      
          //set constraint to 0 when satisfied, otherwise set to non-zero value
          int P=0;
          for(int i=0; i<results.size();i++){
              P+=x[i];
          }
          solution.setConstraint(0, P > 5000 ? 0 : P - 5000);
      }
      
      @Override
      public Solution newSolution(){
          Solution solution = new Solution(1,3,1);
          solution.setVariable(0, EncodingUtils.newInt(0, 1000));
          return solution;
      }
      
      static List<Parameter> results = new ArrayList<Parameter>();
      static final int num=388;
      

      // define Parameters
      static double [] neighbor;
      // Parameter 2: EPDO/km
      static double [] EPDO;
      // Parameter 3: School Density
      static double [] SCD;
      //Parameter 4: Average Speed violation
      static double [] ASV;

      // Read Parameters from Excel to Java List
      public static void Read(){
      try {
      //Find the location of the file
      FileInputStream file = new FileInputStream(new File("E:"+File.separator+"ua2015"+File.separator+"PRC Project"+File.separator+"04. Optimization and Scheduling"+File.separator+"01. NGHBR Model"+File.separator+"Java Parameter Input.xlsx"));

              //Get the workbook instance for XLSX file
              XSSFWorkbook workbook = new XSSFWorkbook(file);
      
              //Get first sheet from the workbook
              XSSFSheet sheet = workbook.getSheetAt(0);
      
              //Iterate through each rows from first sheet
              Iterator<Row> rowIterator = sheet.iterator();
              while(rowIterator.hasNext()){
                  Row row =rowIterator.next();
      
                  //For each row, iterate through each columns
                  Iterator<Cell> cellIterator = row.cellIterator();
                  while (cellIterator.hasNext()){
                      Cell cell = cellIterator.next();
      
                      //Check data type in excel cell and read cell and add that into Parameter list
                      switch(cell.getCellType()){
                      case Cell.CELL_TYPE_NUMERIC:
                          //System.out.print(cell.getNumericCellValue()+"\n");
                          results.add(new Parameter(String.valueOf(cell.getNumericCellValue())));
                          break;
                      case Cell.CELL_TYPE_STRING:
                          //System.out.print(cell.getStringCellValue()+"\n");
                          results.add(new Parameter(cell.getStringCellValue()));
                          break;
                      }
                  }
              }
              file.close();
              workbook.close();
              //for(Parameter data : results) {
              //System.out.println(data.getValue());
      
              //}
          }catch (FileNotFoundException e){
              e.printStackTrace();
          }catch (IOException e){
              e.printStackTrace();
          }
      

      }

      //Input Excel value to Parameters
      public static void InputP() {
      int i=0;
      int j=4;
      neighbor=new double[num];
      EPDO=new double [num];
      SCD=new double [num];
      ASV=new double [num];
      for (i=0,j=4;j<results.size();i++,j=j+4){
      neighbor[i]=Double.valueOf(results.get(j).getValue());
      EPDO[i]=Double.valueOf(results.get(j+1).getValue());
      SCD[i]=Double.valueOf(results.get(j+2).getValue());
      ASV[i]=Double.valueOf(results.get(j+3).getValue());

              }
              }
          }
      
       

      Last edit: Anonymous 2015-07-27
  • Anonymous

    Anonymous - 2015-07-27

    One issue I see is its defining a single variable, an integer

    solution.setVariable(0, EncodingUtils.newInt(0, 1000));
    

    but then trying to read that as a permutation

    int [] x= EncodingUtils.getPermutation(solution.getVariable(0));
    

    Everything else from the MOEA aspect appears to be correct.

     
    • Anonymous

      Anonymous - 2015-07-28

      Hi, how to define a vector variable int[] with bounded constraints in MOEA?
      When set Variable using solution.setVarialbe(0,Variable), I cannot find the definition of setting int[] with bounded constraints using EncodingUtils function.

       

      Last edit: Anonymous 2015-07-28
  • Anonymous

    Anonymous - 2015-07-28

    You'll want to set individual variables for each value:

    Solution solution = new Solution(20, 3, 1);
    
    for (int i = 0; i < 20; i++) {
        solution.setVariable(i, EncodingUtils.newInt(0, 1000));
    }
    
     
  • Anonymous

    Anonymous - 2015-07-28

    Hi, thank you for reply.

    Then question comes to modify "evaluate" method. Since the 20 variables have been set in Override of "Solution" method, I just defined the objective with the calling of solution.getVariable(i) as follows and showed problematic:

    for (int i=0; i<results.size;i++){
    E+= excelrw.EPDO[i]*solution.getVariable(i);}

    Do I still need to define int [] x= EncodingUtils.getInt(??) before? But I don't know what should I input in the EncodingUtils.getInt().

     
  • Anonymous

    Anonymous - 2015-07-29

    You can either get all values at once:

     int[] values = EncodingUtils.getInt(solution);
    

    Or read each value individually:

      for (int i = 0; i < solution.getNumberOfVariables(); i++) {
          E += excelrw.EDPO[i] * EncodingUtils.getInt(solution.getVariable(i));
      }
    
     
  • Anonymous

    Anonymous - 2015-07-30

    Hi My problem was solved! Thank you very much.

    But why even when the constraints were satisfied, showing zero, with the increasing number of iterations, the solutions were changes slightly every iteration (I mean the value of objectives and variables). Why there is no an exact solution?

     
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.