Example #1
0
 /**
  * Get an instance by a relative path.
  *
  * @param relPath
  * @return
  */
 public static PropertyFilePath getFilePathFromResource(String relPath) {
   String fName = ReflectPackage.getResourcePathFromCP(relPath);
   if (fName == null) {
     return null;
   } else {
     return new PropertyFilePath(fName);
   }
 }
  private void initProblemDefinition() {
    this.m_Mocco.m_JPanelParameters.removeAll();
    this.m_ProblemChooser = new JComboBox();
    JComponent tmpC = new JPanel();
    tmpC.setLayout(new BorderLayout());

    Class[] altern = null;
    try {
      altern =
          ReflectPackage.getAssignableClassesInPackage(
              "eva2.server.go.problems",
              Class.forName("eva2.server.go.problems.InterfaceMultiObjectiveDeNovoProblem"),
              true,
              true);
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    this.m_ProblemChooser.setModel(new DefaultComboBoxModel(altern));

    String objectName = (this.m_Mocco.m_State.m_OriginalProblem.getClass().getName());
    this.m_ProblemChooser.getModel().setSelectedItem(objectName);
    this.m_ProblemChooser.addActionListener(problemChanged);
    JPanel tmpP = new JPanel();
    tmpP.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 0;
    tmpP.add(
        this.makeHelpText(
            "Choose and parameterize the optimization problem to solve by means of MOCCO. "
                + "Please note that it is not necessary to include MOSO converters yet and that only problems complying "
                + "with the InterfaceMultiObjectiveDeNovoProblem can be optimized using the MOCCO approach."),
        gbc);
    gbc.gridx = 0;
    gbc.gridy = 1;
    tmpP.add(this.m_ProblemChooser, gbc);
    this.m_Mocco.m_JPanelParameters.setLayout(new BorderLayout());
    this.m_Mocco.m_JPanelParameters.add(tmpP, BorderLayout.NORTH);
    JParaPanel paraPanel = new JParaPanel(this.m_Mocco.m_State.m_OriginalProblem, "MyGUI");
    this.m_Mocco.m_JPanelParameters.add(paraPanel.makePanel(), BorderLayout.CENTER);
  }
Example #3
0
  /**
   * Prints formatted help output for a specific class.
   *
   * <p>If the class is an interface or abstract class additional information on assignable
   * subclasses will be shown.
   *
   * @param clazz The class to show help for.
   */
  private static void printHelpFor(Class<?> clazz) {
    System.out.println(clazz.getName() + "\n");
    if (clazz.isAnnotationPresent(Description.class)) {
      Description description = clazz.getAnnotation(Description.class);
      System.out.printf("%s\n\n", description.value());
    }

    /** In case we have an Abstract class or Interface show available sub types. */
    if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) {
      Class<?>[] subTypes =
          ReflectPackage.getAssignableClassesInPackage(
              clazz.getPackage().getName(), clazz, true, true);
      if (subTypes.length > 0) {
        System.out.printf("Available types for %s\n\n", clazz.getName());
        for (Class<?> type : subTypes) {
          if (Modifier.isAbstract(type.getModifiers())) {
            continue;
          }
          Description description = clazz.getAnnotation(Description.class);
          System.out.printf(
              "\t\033[1m%s\033[0m (%s)\n",
              type.getName(), StringTools.cutClassName(type.getName()));
          if (description != null) {
            System.out.printf("\t\t%s", description.value());
          } else {
            System.out.println("\t\tNo description available.");
          }
        }
        System.out.println();
      }
    }

    /** Get available parameters for this class and list them with their description. */
    ParameterGenerator generator = new ParameterGenerator(clazz, false);
    generator.generate();
    Map<String, List<Parameter>> paramList = generator.getParameterList();

    List<Parameter> parameters = paramList.get(clazz.getName());
    if (parameters.size() > 0) {
      System.out.println("Options:");

      for (Parameter key : parameters) {
        Class<?> type = key.getType();
        String typeDefinition;
        if (type.isEnum()) {
          Enum[] enumConstants = (Enum[]) type.getEnumConstants();
          typeDefinition = "{";
          for (int i = 0; i < enumConstants.length; i++) {
            typeDefinition += enumConstants[i].name();
            if (i != enumConstants.length - 1) {
              typeDefinition += ",";
            }
          }
          typeDefinition += "}";
        } else {
          typeDefinition = key.getType().getName();
        }
        System.out.printf("\t\033[1m--%s\033[0m \033[4m%s\033[0m\n", key.getName(), typeDefinition);
        System.out.printf("\t\t%s\n", key.getDescription());
      }
    }
    System.out.print("\n\n");
  }
  @Override
  public void optimize() {
    //  System.out.println("opt. called");
    AbstractEAIndividual indy;
    //      if ((this.indyhash == null) || (this.indyhash.size() <1)) initialize();

    for (int i = 0; i < this.population.size(); i++) {
      indy = this.population.get(i);
      if (!indy.hasData(gradientKey)) {
        // System.out.println("new indy to hash");
        //        Hashtable history = new Hashtable();
        int[] lock = new int[((InterfaceDataTypeDouble) indy).getDoubleData().length];
        double[] wstepsize = new double[((InterfaceDataTypeDouble) indy).getDoubleData().length];
        for (int li = 0; li < lock.length; li++) {
          lock[li] = 0;
        }
        for (int li = 0; li < lock.length; li++) {
          wstepsize[li] = 1.0;
        }
        double fitness = 0;
        indy.putData(lockKey, lock);
        indy.putData(lastFitnessKey, fitness);
        indy.putData(stepSizeKey, globalinitstepsize);
        indy.putData(wStepSizeKey, wstepsize);
        //        indyhash.put(indy, history);
      } else {
        // System.out.println("indy already in hash");
      }
    }
    // System.out.println("hashtable built");
    for (int i = 0; i < this.population.size(); i++) {

      indy = this.population.get(i);
      double[][] range = ((InterfaceDataTypeDouble) indy).getDoubleRange();
      double[] params = ((InterfaceDataTypeDouble) indy).getDoubleData();
      indy.putData(oldParamsKey, params);

      int[] lock = (int[]) indy.getData(lockKey);
      double indystepsize = (Double) indy.getData(stepSizeKey);

      if ((this.optimizationProblem instanceof InterfaceFirstOrderDerivableProblem)
          && (indy instanceof InterfaceDataTypeDouble)) {
        for (int iterations = 0; iterations < this.iterations; iterations++) {

          double[] oldgradient =
              indy.hasData(gradientKey) ? (double[]) indy.getData(gradientKey) : null;
          double[] wstepsize = (double[]) indy.getData(wStepSizeKey);
          double[] oldchange = null;

          double[] gradient =
              ((InterfaceFirstOrderDerivableProblem) optimizationProblem)
                  .getFirstOrderGradients(params);
          if ((oldgradient != null) && (wstepsize != null)) { // LOCAL adaption
            for (int li = 0; li < wstepsize.length; li++) {
              double prod = gradient[li] * oldgradient[li];
              if (prod < 0) {
                wstepsize[li] = wDecreaseStepSize * wstepsize[li];
              } else if (prod > 0) {
                wstepsize[li] = wIncreaseStepSize * wstepsize[li];
              }
              wstepsize[li] = (wstepsize[li] < localminstepsize) ? localminstepsize : wstepsize[li];
              wstepsize[li] = (wstepsize[li] > localmaxstepsize) ? localmaxstepsize : wstepsize[li];
            }
          }
          double[] newparams = new double[params.length];
          indy.putData(gradientKey, gradient);
          double[] change = new double[params.length];
          if (indy.hasData(changesKey)) {
            oldchange = (double[]) indy.getData(changesKey);
          }
          boolean dograddesc = (this.momentumterm) && (oldchange != null);

          for (int j = 0; j < newparams.length; j++) {
            if (lock[j] == 0) {
              double tempstepsize = 1;
              if (this.localStepSizeAdaption) {
                tempstepsize *= wstepsize[j];
              }
              if (this.globalStepSizeAdaption) {
                tempstepsize *= indystepsize;
              }
              double wchange =
                  signum(tempstepsize * gradient[j])
                      * Math.min(
                          maximumabsolutechange,
                          Math.abs(tempstepsize * gradient[j])); // indystepsize * gradient[j];
              if (this.manhattan) {
                wchange = this.signum(wchange) * tempstepsize;
              }
              if (dograddesc) {
                wchange += this.momentumweigth * oldchange[j];
              }
              newparams[j] = params[j] - wchange;
              if (newparams[j] < range[j][0]) {
                newparams[j] = range[j][0];
              }
              if (newparams[j] > range[j][1]) {
                newparams[j] = range[j][1];
              }
              //              for (int g = 0; g < newparams.length; g++) {
              //                System.out.println("Param " + g +": " + newparams[g]);
              //              }
              change[j] += wchange;
            } else {
              lock[j]--;
            }
          }
          params = newparams;

          indy.putData(changesKey, change);
        } // end loop iterations

        ((InterfaceDataTypeDouble) indy).setDoubleGenotype(params);

      } // end if ((this.problem instanceof InterfaceFirstOrderDerivableProblem) && (indy instanceof
      // InterfaceDataTypeDouble)) {
      else {
        String msg =
            "Warning, problem of type InterfaceFirstOrderDerivableProblem and template of type InterfaceDataTypeDouble is required for "
                + this.getClass();
        EVAERROR.errorMsgOnce(msg);
        Class<?>[] clsArr =
            ReflectPackage.getAssignableClasses(
                InterfaceFirstOrderDerivableProblem.class.getName(), true, true);
        msg += " (available: ";
        for (Class<?> cls : clsArr) {
          msg = msg + " " + cls.getSimpleName();
        }
        msg += ")";
        throw new RuntimeException(msg);
      }
    } // for loop population size

    this.optimizationProblem.evaluate(this.population);
    population.incrGeneration();

    if (this.recovery) {
      for (int i = 0; i < this.population.size(); i++) {
        indy = this.population.get(i);
        if (indy.getFitness()[0] > recoverythreshold) {

          ((InterfaceDataTypeDouble) indy)
              .setDoublePhenotype((double[]) indy.getData(oldParamsKey));
          double[] changes = (double[]) indy.getData(changesKey);
          int[] lock = (int[]) indy.getData(lockKey);

          int indexmaxchange = 0;
          double maxchangeval = Double.NEGATIVE_INFINITY;
          for (int j = 0; j < changes.length; j++) {
            if ((changes[j] > maxchangeval) && (lock[j] == 0)) {
              indexmaxchange = j;
              maxchangeval = changes[j];
            }
          }
          lock[indexmaxchange] = recoverylocksteps;
          indy.putData(lockKey, lock);
        } else {
        }
      }
      this.optimizationProblem.evaluate(this.population);
      population.incrGeneration();
    }

    if (this.globalStepSizeAdaption) {

      // System.out.println("gsa main");
      for (int i = 0; i < this.population.size(); i++) {
        indy = this.population.get(i);

        if (indy.getData(lastFitnessKey) != null) {
          double lastfit = (Double) indy.getData(lastFitnessKey);
          double indystepsize = (Double) indy.getData(stepSizeKey);

          if (lastfit < indy.getFitness()[0]) { // GLOBAL adaption
            indystepsize *= wDecreaseStepSize;
          } else {
            indystepsize *= wIncreaseStepSize;
          }
          indystepsize = (indystepsize > globalmaxstepsize) ? globalmaxstepsize : indystepsize;
          indystepsize = (indystepsize < globalminstepsize) ? globalminstepsize : indystepsize;
          indy.putData(stepSizeKey, indystepsize);
        }

        indy.putData(lastFitnessKey, indy.getFitness()[0]);
      }
    }

    this.firePropertyChangedEvent(Population.NEXT_GENERATION_PERFORMED);
  }