Esempio n. 1
0
  public void setup(final EvolutionState state, final Parameter base) {
    super.setup(state, base);

    F_NOISE = state.parameters.getDouble(base.push(P_FNOISE), null, 0.0);
    if (F_NOISE < 0.0)
      state.output.fatal(
          "Parameter not found, or its value is below 0.0.", base.push(P_FNOISE), null);
  }
Esempio n. 2
0
  public void setup(final EvolutionState state, final Parameter base) {
    // very important, remember this
    super.setup(state, base);

    // set up our input -- don't want to use the default base, it's unsafe here
    input =
        (DoubleData)
            state.parameters.getInstanceForParameterEq(base.push(P_DATA), null, DoubleData.class);
    input.setup(state, base.push(P_DATA));
  }
Esempio n. 3
0
  public void setup(final EvolutionState state, final Parameter base) {
    super.setup(state, base);

    Parameter def = defaultBase();

    int numSelects = state.parameters.getInt(base.push(P_NUMSELECTS), def.push(P_NUMSELECTS), 1);
    if (numSelects == 0)
      state.output.fatal(
          "The number of MultiSelection sub-selection methods must be >= 1).",
          base.push(P_NUMSELECTS),
          def.push(P_NUMSELECTS));

    // make our arrays
    selects = new SelectionMethod[numSelects];

    float total = 0.0f;

    for (int x = 0; x < numSelects; x++) {
      Parameter p = base.push(P_SELECT).push("" + x);
      Parameter d = def.push(P_SELECT).push("" + x);

      selects[x] =
          (SelectionMethod) (state.parameters.getInstanceForParameter(p, d, SelectionMethod.class));
      selects[x].setup(state, p);

      // now check probability
      if (selects[x].probability < 0.0)
        state.output.error(
            "MultiSelection select #" + x + " must have a probability >= 0.0",
            p.push(P_PROB),
            d.push(P_PROB));
      else total += selects[x].probability;
    }

    state.output.exitIfErrors();

    // Now check for valid probability
    if (total <= 0.0)
      state.output.fatal("MultiSelection selects do not sum to a positive probability", base);

    if (total != 1.0) {
      state.output.message("Must normalize probabilities for " + base);
      for (int x = 0; x < numSelects; x++) selects[x].probability /= total;
    }

    // totalize
    float tmp = 0.0f;
    for (int x = 0; x < numSelects - 1; x++) // yes, it's off by one
    {
      tmp += selects[x].probability;
      selects[x].probability = tmp;
    }
    selects[numSelects - 1].probability = 1.0f;
  }
  public void setup(final EvolutionState state, final Parameter base) {
    super.setup(state, base);

    String temp;
    temp = state.parameters.getStringWithDefault(base.push(P_COMPETE_STYLE), null, "");
    if (temp.equalsIgnoreCase("single-elim-tournament")) {
      style = STYLE_SINGLE_ELIMINATION;
    } else if (temp.equalsIgnoreCase("round-robin")) {
      style = STYLE_ROUND_ROBIN;
    } else if (temp.equalsIgnoreCase("rand-1-way")) {
      style = STYLE_N_RANDOM_COMPETITORS_ONEWAY;
    } else if (temp.equalsIgnoreCase("rand-2-way")) {
      style = STYLE_N_RANDOM_COMPETITORS_TWOWAY;
    } else if (temp.equalsIgnoreCase("rand-2-ways")) {
      state.output.fatal(
          "'rand-2-ways' is no longer a valid style name: use 'rand-2-way'",
          base.push(P_COMPETE_STYLE),
          null);
    } else {
      state.output.fatal(
          "Incorrect value for parameter. Acceptable values: "
              + "single-elim-tournament, round-robin, rand-1-way, rand-2-way",
          base.push(P_COMPETE_STYLE));
    }

    if (style == STYLE_N_RANDOM_COMPETITORS_ONEWAY || style == STYLE_N_RANDOM_COMPETITORS_TWOWAY) {
      groupSize = state.parameters.getInt(base.push(P_GROUP_SIZE), null, 1);
      if (groupSize < 1) {
        state.output.fatal("Incorrect value for parameter", base.push(P_GROUP_SIZE));
      }
    }
    allowOverEvaluation = state.parameters.getBoolean(base.push(P_OVER_EVAL), null, false);
  }
Esempio n. 5
0
  /** Sets up parameters. */
  public void setup(final EvolutionState state, final Parameter base) {
    if (!(state instanceof EvolutionAgent))
      state.output.fatal("DRMStatistics requires an  EvolutionAgent", null, null);
    EvolutionAgent agent = (EvolutionAgent) state;

    super.setup(state, base);
    frequency = state.parameters.getIntWithDefault(base.push(P_FREQUENCY), null, 1);
    store_best = state.parameters.getBoolean(base.push(P_STORE_BEST), null, true);
    // use_collective = state.parameters.getBoolean(base.push(P_COLLECTIVE),null,true);

    // If we are root, set up the base filename and the logtable
    if (agent.iamroot) {
      // I'm not sure that outputting everything to the current directory is right
      basefilename =
          System.getProperty("user.dir")
              + File.separator
              + state.parameters.getFile(base.push(P_STATISTICS_FILE), null).getName();
      logtable = new Hashtable();
    } else defaultlog = -3; // Maybe can be useful for recognizing it as a non valid log

    creationtime = System.currentTimeMillis();
  }
  public void setup(final EvolutionState state, final Parameter base) {
    super.setup(state, base);

    Parameter def = defaultBase();

    // we use size distributions -- did the user specify any?
    if (!canPick())
      state.output.fatal(
          "PTC2 needs a distribution of tree sizes to pick from.  You can do this by either setting a distribution (with "
              + P_NUMSIZES
              + ") or with "
              + P_MINSIZE
              + " and "
              + P_MAXSIZE
              + ".",
          base,
          def);

    maxDepth = state.parameters.getInt(base.push(P_MAXDEPTH), def.push(P_MAXDEPTH), 1);
    if (maxDepth < 1)
      state.output.fatal("Maximum depth must be >= 1", base.push(P_MAXDEPTH), def.push(P_MAXDEPTH));
  }
  public void setup(final EvolutionState state, final Parameter base) {
    super.setup(state, base);

    Parameter def = defaultBase();

    double val = state.parameters.getDouble(base.push(P_SIZE), def.push(P_SIZE), 1.0);
    if (val < 1.0)
      state.output.fatal("Tournament size must be >= 1.", base.push(P_SIZE), def.push(P_SIZE));
    else if (val > 1 && val < 2) // pick with probability
    {
      size = 2;
      probabilityOfSelection = (val / 2);
    } else if (val != (int) val) // it's not an integer
    state.output.fatal(
          "If >= 2, Tournament size must be an integer.", base.push(P_SIZE), def.push(P_SIZE));
    else {
      size = (int) val;
      probabilityOfSelection = 1.0;
    }

    val = state.parameters.getDouble(base.push(P_SIZE2), def.push(P_SIZE2), 1.0);
    if (val < 1.0)
      state.output.fatal("Tournament size2 must be >= 1.", base.push(P_SIZE2), def.push(P_SIZE2));
    else if (val > 1 && val < 2) // pick with probability
    {
      size2 = 2;
      probabilityOfSelection2 = (val / 2);
    } else if (val != (int) val) // it's not an integer
    state.output.fatal(
          "If >= 2, Tournament size2 must be an integer.", base.push(P_SIZE2), def.push(P_SIZE2));
    else {
      size2 = (int) val;
      probabilityOfSelection2 = 1.0;
    }

    doLengthFirst =
        state.parameters.getBoolean(base.push(P_DOLENGTHFIRST), def.push(P_DOLENGTHFIRST), true);
    pickWorst = state.parameters.getBoolean(base.push(P_PICKWORST), def.push(P_PICKWORST), false);
    pickWorst2 =
        state.parameters.getBoolean(base.push(P_PICKWORST2), def.push(P_PICKWORST2), false);
  }
Esempio n. 8
0
  /** Sets up all the GEPSymbolSet symbols, loading them from the parameter file. */
  public void setup(
      final EvolutionState state, final Parameter base, final Parameter def, GEPSpecies species) {
    // Name of file with the terminal (variable) definitions and training values
    String terminalFilename;
    // Name of file with the test data values if specified
    String testingTerminalFilename;

    // keep track of the maximum arity of any function
    maxArity = 0;

    // What's my name? Don't really use this at this time ...
    name = state.parameters.getString(base.push(P_NAME), def.push(P_NAME));
    if (name == null || name.equals(""))
      state.output.warning(
          "No name was given for this GEP symbol set...not required at this time.",
          base.push(P_NAME),
          def.push(P_NAME));

    // How many functions do I have?
    numberOfFunctions =
        state.parameters.getInt(base.push(P_FUNCTIONSIZE), def.push(P_FUNCTIONSIZE), 1);
    numberOfSymbols = numberOfFunctions;

    // How many terminals do I have? Check for a data file first ...
    // if time series problem type and using raw time series data then
    //      number of terminals will be specified in the embedding dimension value
    //      provided in the parameter file
    // else if a file specified
    //      get the 1st line of the file and count the fields in it (#terminals is number of fields
    // minus
    //      the number of chromosomes/dependent variables)
    // else
    //      use the number of terminals specified in the parameter file

    terminalFilename =
        state.parameters.getStringWithDefault(
            base.push(P_TERMINALFILENAME), def.push(P_TERMINALFILENAME), "");
    testingTerminalFilename =
        state.parameters.getStringWithDefault(
            base.push(P_TESTINGTERMINALFILENAME), def.push(P_TESTINGTERMINALFILENAME), "");
    String terminalSymbolsfromFile[] = null;
    CsvReader terminalFileCSV = null;
    CsvReader testingTerminalFileCSV = null;
    // Are we processing raw time series data?
    boolean timeseriesWithRawDataValues =
        species.problemType == GEPSpecies.PT_TIMESERIES && species.timeseriesEmbeddingDimension > 0;
    if (!terminalFilename.equals("")) {
      String defaultTerminalFileSeparator = ","; // default field separator is comma
      try {
        // allow for gzip files .... end with .gz or .gzip\
        if (terminalFilename.endsWith(".gz") || terminalFilename.endsWith(".gzip")) {
          terminalFileCSV =
              new CsvReader(
                  (InputStream) (new GZIPInputStream(new FileInputStream(terminalFilename))),
                  Charset.forName("ISO-8859-1"));
          // set terminal file name to be the one with gzip or gz removed from the end
          if (terminalFilename.endsWith(".gz"))
            terminalFilename = terminalFilename.substring(0, terminalFilename.length() - 3);
          else terminalFilename = terminalFilename.substring(0, terminalFilename.length() - 5);
        } else terminalFileCSV = new CsvReader(terminalFilename);
      } catch (FileNotFoundException e) {
        state.output.fatal(
            "The file with terminal definitions and/or values ("
                + terminalFilename
                + ") could not be found",
            base.push(P_TERMINALFILENAME),
            def.push(P_TERMINALFILENAME));
      } catch (IOException e) {
        state.output.fatal(
            "The file with terminal definitions and/or values ("
                + terminalFilename
                + ") could not be found or the expected GZIP file could nor be opened",
            base.push(P_TERMINALFILENAME),
            def.push(P_TERMINALFILENAME));
      }
      // if filename has extension .dat it is space delimited, if .csv (or anything else
      // for that matter) it is comma delimited
      // (separator can still be changed with the terminalfileseparator parameter)
      if (terminalFilename.endsWith(".dat")) defaultTerminalFileSeparator = "space";
      // if using a file for the terminals and their values then check for a non-default separator
      String terminalFileSeparator =
          state.parameters.getStringWithDefault(
              base.push(P_TERMINALFILESEPARATOR),
              def.push(P_TERMINALFILESEPARATOR),
              defaultTerminalFileSeparator);
      if (terminalFileSeparator.toLowerCase().equals("comma")) terminalFileSeparator = ",";
      else if (terminalFileSeparator == "\\t" || terminalFileSeparator.toLowerCase().equals("tab"))
        terminalFileSeparator = "\t";
      else if (terminalFileSeparator == "space") terminalFileSeparator = " ";
      terminalFileCSV.setDelimiter(terminalFileSeparator.charAt(0));
      // let's check for a testing data file at this time as well .. if no file for
      // names and training data no need to worry about this one.
      if (!testingTerminalFilename.equals("")) {
        try {
          // allow for gzip files .... end with .gz or .gzip\
          if (testingTerminalFilename.endsWith(".gz") || testingTerminalFilename.endsWith(".gzip"))
            testingTerminalFileCSV =
                new CsvReader(
                    (InputStream)
                        (new GZIPInputStream(new FileInputStream(testingTerminalFilename))),
                    Charset.forName("ISO-8859-1"));
          else testingTerminalFileCSV = new CsvReader(testingTerminalFilename);
          testingTerminalFileCSV.setDelimiter(terminalFileSeparator.charAt(0));
        } catch (FileNotFoundException e) {
          state.output.fatal(
              "The file with testing data values ("
                  + testingTerminalFilename
                  + ") could not be found",
              base.push(P_TERMINALFILENAME),
              def.push(P_TERMINALFILENAME));
        } catch (IOException e) {
          state.output.fatal(
              "The file with testing data values ("
                  + terminalFilename
                  + ") could not be found or the expected GZIP file could nor be opened",
              base.push(P_TERMINALFILENAME),
              def.push(P_TERMINALFILENAME));
        }
      }
    }

    if (timeseriesWithRawDataValues) numberOfTerminals = species.timeseriesEmbeddingDimension;
    else if (terminalFileCSV != null) {
      // get the terminal symbols for the independent and dependent variables
      try {
        terminalFileCSV.readHeaders();
        terminalSymbolsfromFile = terminalFileCSV.getHeaders();
      } catch (IOException e) {
        state.output.fatal(
            "The file with variable (terminal) definitions and values ("
                + terminalFilename
                + ") failed to read the headers"
                + e,
            base.push(P_TERMINALFILENAME),
            def.push(P_TERMINALFILENAME));
      }
      // 1 less for each dependent variable (number of chromosomes) at the end
      numberOfTerminals = terminalSymbolsfromFile.length - species.numberOfChromosomes;
      if (numberOfTerminals < 1)
        state.output.fatal(
            "The file with terminal definitions and data values ("
                + terminalFilename
                + ") has no independent variables specified in record 1",
            base.push(P_TERMINALFILENAME),
            def.push(P_TERMINALFILENAME));
      // if using a file for the terminals and their values then check for a non-default separator
    } else {
      numberOfTerminals =
          state.parameters.getInt(base.push(P_TERMINALSIZE), def.push(P_TERMINALSIZE), 1);
    }
    numberOfSymbols += numberOfTerminals;

    if (numberOfSymbols < 1)
      state.output.error(
          "The GEPSymbolSet \"" + name + "\" have at least 1 terminal symbol defined.",
          base.push(P_TERMINALSIZE),
          def.push(P_TERMINALSIZE));

    // add a special Symbol for constants if we are using them ... it will be added to the
    // end of the array of symbols!
    if (species.useConstants) {
      numberOfTerminals++; // special constant terminal
      numberOfSymbols++;
    }

    symbols = new GEPSymbol[numberOfSymbols];

    int numberOfSymbolsWithoutConstantSymbol = numberOfSymbols;
    if (species.useConstants) // add the constant terminal symbol to the end
    {
      symbols[numberOfSymbols - 1] = (GEPSymbol) (new GEPConstantTerminalSymbol());
      symbols[numberOfSymbols - 1].id = numberOfSymbols - 1;
      numberOfSymbolsWithoutConstantSymbol--;
    }

    Parameter pTerminal = base.push(P_TERMINAL);
    Parameter pdefTerminal = def.push(P_TERMINAL);
    Parameter pFunction = base.push(P_FUNCTION);
    Parameter pdefFunction = def.push(P_FUNCTION);

    // create hashtable of names of terminals and hash table with names of functions
    // so we can easily check that they are not duplicates
    Hashtable functionHT = new Hashtable();
    Hashtable terminalHT = new Hashtable();

    //      process the functions
    for (int x = 0; x < numberOfFunctions; x++) {
      Parameter pp = pFunction.push("" + x);
      Parameter ppdef = pdefFunction.push("" + x);
      String function = state.parameters.getStringWithDefault(pp, ppdef, "");
      if (function.equals("")) // no name for the function
      state.output.fatal("Invalid function specifier: '" + function + "'", pp, ppdef);
      // make sure not specifying the same function more than once
      if (functionHT.get(function) != null)
        state.output.fatal(
            "Function '" + function + "' was specified more than once in list of function symbols");
      else functionHT.put(function, function);
      GEPFunctionSymbol fs = null;
      try {
        Class classDefinition = Class.forName(LOCATION_OF_FUNCTION_CLASSES + "." + function);
        fs = (GEPFunctionSymbol) classDefinition.newInstance();
      } catch (InstantiationException e) {
        state.output.fatal(
            "Unable to create GEPFunctionSymbol class for function '" + function + "'. " + e);
      } catch (IllegalAccessException e) {
        state.output.fatal(
            "Unable to create GEPFunctionSymbol class for function '" + function + "' " + e);
      } catch (ClassNotFoundException e) {
        state.output.fatal(
            "Unable to create GEPFunctionSymbol class for function '" + function + "' " + e);
      }

      // if using a logical function must be a logical problem
      if (fs.isLogicalFunction() && (species.problemType != GEPSpecies.PT_LOGICAL))
        state.output.fatal(
            "Can only use logical functions with a logical problem type. Function "
                + function
                + " is  a logical function.",
            pp,
            ppdef);
      // if using a numerical function must be an non logical problem
      if (!fs.isLogicalFunction() && (species.problemType == GEPSpecies.PT_LOGICAL))
        state.output.fatal(
            "Can only use logical functions with a non logical problem type. Function "
                + function
                + " is a numerical function.",
            pp,
            ppdef);

      symbols[x] = (GEPSymbol) fs;
      // symbols[x].setup(state, base);
      if (fs.arity < 1) state.output.fatal("Arity must be > 0 for a GEPTerminalSymbol)", pp, ppdef);
      symbols[x].id = x;
      int weight =
          state.parameters.getInt(pp.push(P_FUNCTIONWEIGHT), ppdef.push(P_FUNCTIONWEIGHT), 1);
      if (weight < 1) {
        state.output.warning(
            "Weight for GEP Function must be > 0; defaulting to 1)",
            pp.push(P_FUNCTIONWEIGHT),
            ppdef.push(P_FUNCTIONWEIGHT));
        weight = 1;
      }
      symbols[x].weight = weight;
      if (symbols[x].arity > maxArity) maxArity = symbols[x].arity;
    }

    // process the terminals  ... defined by default for timeseries data, in the
    // CSV file if specified and not timeseries, or in the params file if neither of those.
    for (int x = numberOfFunctions;
        x < numberOfSymbolsWithoutConstantSymbol;
        x++) { // load the terminal symbols
      int index = x - numberOfFunctions;
      String terminal = "";
      if (timeseriesWithRawDataValues) {
        // terminals get default names v0, v1, v2, v3, ... vn-1
        terminal = "v" + index;
      } else if (terminalFileCSV == null) // terminals defined in param file
      {
        Parameter pp = pTerminal.push("" + index);
        Parameter ppdef = pdefTerminal.push("" + index);
        terminal = state.parameters.getStringWithDefault(pp, ppdef, "");
      } else { // terminals defined in CSV file
        terminal = terminalSymbolsfromFile[index];
      }
      if (terminal.equals("")) // no name for the terminal
      state.output.fatal("Invalid terminal specifier: '" + terminal + "' for terminal # " + index);
      // make sure not specifying the same function more than once
      if (terminalHT.get(terminal) != null)
        state.output.fatal(
            "Terminal symbol (indep var) '"
                + terminal
                + "' was specified more than once in list of terminal symbols (independent variables)");
      else terminalHT.put(terminal, terminal);
      GEPTerminalSymbol ts = new GEPTerminalSymbol(terminal, this);
      symbols[x] = (GEPSymbol) ts;
      // symbols[x].setup(state, base);
      if (ts.arity != 0) // cannot happen
      state.output.fatal("Arity must be exactly 0 for a GEPTerminalSymbol)");
      symbols[x].id = x;
      symbols[x].weight = 1; // all Terminal symbols have weight of 1
    }

    // must be at least 1 Terminal symbol in the SymbolSet.
    // If not then the user didn't specify the terminals in the param file or in the data file
    if (numberOfTerminals < 1)
      state.output.fatal(
          "Must be at least one Terminal Symbol in the set of GEPSymbols\n"
              + "Either did not specify the terminal symbols in the param file or\n"
              + "did not specify the appropriate data file with the terminals specified in the first line.");

    // collect the id's (indices) of the terminal and function symbols that
    // are in the set of symbols
    terminals = new int[numberOfTerminals];
    int terminalNum = 0;
    functions = new int[numberOfFunctions];
    int functionNum = 0;
    for (int x = 0; x < numberOfSymbols; x++) {
      if (symbols[x] instanceof GEPConstantTerminalSymbol) terminals[terminalNum++] = x;
      else if (symbols[x] instanceof GEPTerminalSymbol) terminals[terminalNum++] = x;
      else if (symbols[x] instanceof GEPFunctionSymbol) functions[functionNum++] = x;
    }

    // collect the weights for symbols and terminals and normalize and cumulate them.
    // Then we can use these arrays to pick appropriate symbols or terminals according to
    // their weights ... using the RandomChooser.PickFromDistribution
    cumulativeNormalizedSymbolWeights = new float[numberOfSymbols];
    cumulativeNormalizedTerminalWeights = new float[numberOfTerminals];
    cumulativeNormalizedFunctionWeights = new float[numberOfFunctions];
    int j = 0, k = 0;
    for (int i = 0; i < numberOfSymbols; i++) {
      float weight = (float) (symbols[i].weight);
      cumulativeNormalizedSymbolWeights[i] = weight;
      if (symbols[i] instanceof GEPTerminalSymbol
          || symbols[i] instanceof GEPConstantTerminalSymbol)
        cumulativeNormalizedTerminalWeights[j++] = weight;
      if (symbols[i] instanceof GEPFunctionSymbol)
        cumulativeNormalizedFunctionWeights[k++] = weight;
    }
    RandomChoice.organizeDistribution(cumulativeNormalizedSymbolWeights);
    RandomChoice.organizeDistribution(cumulativeNormalizedTerminalWeights);
    RandomChoice.organizeDistribution(cumulativeNormalizedFunctionWeights);

    // use the 2/3 rule if fewer functions else the 1/2 rule (don't count the constant
    // terminal here)
    if (numberOfFunctions < (numberOfTerminals - (species.useConstants ? 1 : 0)))
      probabilityOfChoosingFunction = 2.0 / 3.0;
    else probabilityOfChoosingFunction = 0.5;

    // ... and finally get the training and testing data values for the terminals and dependent
    // variable
    // and put them into the Terminal instances (creating a 'special' Terminal Symbol to
    // hold the dependent variable training and testing values)

    // If this is a time series problem AND we are using the raw time series data then
    // we named the terminals v1, v2, ..., nn where n is the number of independent
    // variables as specified in the embedding dimension (which) was used to
    // determine the number of terminals. But we have to process the time series data
    // to get the values for each terminal ... get the raw data from the CSV file
    // if specified or from the user program ... then process it into rows of data
    // representing the independent variables and the dependent variable.
    //
    //   timeseries-delay -- if 1 uses each time series value, if 2 uses every other one, etc.
    //   timeseries-embeddingdimension -- determines the number of timeseries points to use
    //        as independent variables when transforming the set of time series data. Another
    //        data point is used as the dependent variable value. So the time series 'raw' data
    //        consisting of a list of single values is processed by splitting the data into
    //        groups (rows) of size embeddingdimension+1. From the end of the time series data
    //        embeddingdimension+1 values are chosen (if delay is 1 all values are chosen, if
    //        2 every other one is chosen). The last value is the independent variable value.
    //        Then the next row is selected by moving 'delay'
    //        values from the end and chosing embeddingdimension+1 values. This is repeated
    //        until no more sets of size embeddingdimension+1 can be chosen. If this produces
    //        n sets of data then testingprediction of them are used for testing and
    //        (n - testingpredictions) are used for training.
    //
    //        So if we had the data:
    //        1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
    //        and delay was 1 and embeddingdimension was 4 then we'd process the set into
    //        the following 17 data sets. If testingpredictions was 6 then the 1st 11
    //        would be used for training and the last 6 for testing
    //        iv1 iv2 iv3 iv4  dv
    //          1   2   3   4   5
    //          2   3   4   5   6
    //          3   4   5   6   7
    //              . . .
    //         14  15  16  17  18
    //         15  16  17  18  19
    //         16  17  18  19  20
    //         17  18  19  20  21
    //        If delay was 2 then 7 sets would be formed as:
    //        iv1 iv2 iv3 iv4  dv
    //          1   3   5   7   9
    //          3   5   7   9  11
    //              . . .
    //          9  11  13  15  17
    //         11  13  15  17  19
    //         13  15  17  19  21
    //   timeseries-testingpredictions -- specifies the number of sets of data to devote to testing
    if (timeseriesWithRawDataValues) {
      GEPDependentVariable.symbol = "dependentVariable";
      double rawTimeSeriesValues[] = null;
      if (terminalFileCSV == null)
        rawTimeSeriesValues = ((GEPProblem) state.evaluator.p_problem).getTimeSeriesDataValues();
      else rawTimeSeriesValues = getRawTimeSeriesValuesFromCSVfile(state, terminalFileCSV);
      if (rawTimeSeriesValues == null)
        state.output.fatal("Unable to get time series data values from User Program or CSV file");
      Vector values[] = processRawTimeSeriesValues(state, species, rawTimeSeriesValues);
      // have an array of vectors; 1 vector for each indep variable and the dep variable(s)
      for (int i = 0; i < values.length; i++) {
        // get the values for training ... and testing (specified by timeseriesTestingPredictions)
        int sizeOfTrainingData = values[i].size() - species.timeseriesTestingPredictions;
        double v[] = new double[sizeOfTrainingData];
        double testingV[] = new double[species.timeseriesTestingPredictions];
        for (int m = 0; m < v.length; m++) v[m] = ((Double) values[i].elementAt(m)).doubleValue();
        for (int n = 0; n < testingV.length; n++)
          testingV[n] = ((Double) values[i].elementAt(n + sizeOfTrainingData)).doubleValue();
        int depVarIndex = i - values.length + species.numberOfChromosomes;
        if (depVarIndex >= 0) // last column(s) in file is(are) the dependent variable(s)
        {
          GEPDependentVariable.trainingData.setValues(v, depVarIndex);
          GEPDependentVariable.testingData.setValues(testingV, depVarIndex);
        } else {
          ((GEPTerminalSymbol) symbols[numberOfFunctions + i]).setTrainingValues(v);
          ((GEPTerminalSymbol) symbols[numberOfFunctions + i]).setTestingValues(testingV);
        }
      }
    }
    // else If there is a file with the terminals and dep variable(s) use this else ask for
    // the values from the User Program (problem).
    else if (terminalFileCSV != null) // terminals defined in CSV file
    {
      GEPDependentVariable.symbol = terminalSymbolsfromFile[terminalSymbolsfromFile.length - 1];
      // get all the values into an array of vectors (each vector holds the values for a
      // single terminal (dep or indep variable)
      Vector values[] = new Vector[terminalSymbolsfromFile.length];
      for (int i = 0; i < terminalSymbolsfromFile.length; i++) values[i] = new Vector();
      try {
        while (terminalFileCSV.readRecord()) {
          for (int i = 0; i < terminalSymbolsfromFile.length; i++)
            values[i].add(terminalFileCSV.get(i));
        }
      } catch (IOException e) {
        state.output.fatal(
            "The file with terminal definitions/values failed when reading records. " + e);
      }

      for (int i = 0; i < terminalSymbolsfromFile.length; i++) {
        double v[] = new double[values[i].size()];
        for (int m = 0; m < v.length; m++)
          try {
            v[m] = Double.parseDouble((String) values[i].elementAt(m));
          } catch (Exception e) {
            state.output.fatal(
                "Failed trying to read a training data set value. The field is supposed to be a number but was the string '"
                    + (String) values[i].elementAt(m)
                    + "'.\n"
                    + e);
          }
        int jj = terminalSymbolsfromFile.length - species.numberOfChromosomes;
        if (i >= jj) // last column(s) in file is(are) the dependent variable(s)
        GEPDependentVariable.trainingData.setValues(v, i - jj);
        else ((GEPTerminalSymbol) symbols[numberOfFunctions + i]).setTrainingValues(v);
      }
      // get the testing data as well if a file was specified
      if (testingTerminalFileCSV != null) // testing data defined in CSV file
      {
        // get all the values into an array of vectors (each vector holds the values for a
        // single terminal (dep or indep variable)
        Vector testingValues[] = new Vector[terminalSymbolsfromFile.length];
        for (int i = 0; i < terminalSymbolsfromFile.length; i++) testingValues[i] = new Vector();
        try {
          while (testingTerminalFileCSV.readRecord()) {
            for (int i = 0; i < terminalSymbolsfromFile.length; i++)
              testingValues[i].add(testingTerminalFileCSV.get(i));
          }
        } catch (IOException e) {
          state.output.fatal(
              "The file with testing data values failed when reading records. "
                  + "\nMake sure the file has the same column separators as the testing data file."
                  + "\nAlso check that it has the same as the number of columns as the testing file"
                  + e);
        }

        for (int i = 0; i < terminalSymbolsfromFile.length; i++) {
          double v[] = new double[testingValues[i].size()];
          for (int m = 0; m < v.length; m++)
            try {
              v[m] = Double.parseDouble((String) testingValues[i].elementAt(m));
            } catch (Exception e) {
              state.output.fatal(
                  "Failed trying to read a testing data set value. The field is supposed to be a number but was the string '"
                      + (String) testingValues[i].elementAt(m)
                      + "'.\n"
                      + e);
            }
          int jj = terminalSymbolsfromFile.length - species.numberOfChromosomes;
          if (i >= jj) // last column(s) in file is(are) the dependent variable(s)
          GEPDependentVariable.testingData.setValues(v, i - jj);
          else ((GEPTerminalSymbol) symbols[numberOfFunctions + i]).setTestingValues(v);
        }
      }
    }
    // else terminals were defined in the param file and no CSV file
    // defined so .... ask User Problem for the values, training and testing (if there are any)
    else {
      GEPDependentVariable.symbol = "dependentVariable";
      GEPProblem prob = (GEPProblem) state.evaluator.p_problem;
      double vals[] = null;
      for (int i = numberOfFunctions; i < numberOfSymbolsWithoutConstantSymbol; i++) {
        GEPTerminalSymbol ts = (GEPTerminalSymbol) symbols[i];
        vals = prob.getDataValues(ts.symbol);
        if (vals == null)
          state.output.fatal(
              "Expecting user problem (GEPProblem/ProblemForm) to supply training data values for terminal symbol '"
                  + ts
                  + "'.");
        ts.setTrainingValues(vals);
        vals = prob.getTestingDataValues(ts.symbol);
        if (vals != null) // don't have to supply testing data
        ts.setTestingValues(vals);
      }
      // if just one dep var then ask user by requesting with getdataValues("dependentVariable")
      // and if more than one dep var (more than 1 chromosome) then ask for dep variables
      // with getDataValues("dependentVariable0"), getDataValues("dependentVariable1"), ...
      for (int i = 0; i < species.numberOfChromosomes; i++) {
        String depVarSym = GEPDependentVariable.symbol;
        if (species.numberOfChromosomes > 1) depVarSym = depVarSym + i;
        vals = prob.getDataValues(depVarSym);
        if (vals == null)
          state.output.fatal(
              "Expecting user problem (GEPProblem/ProblemForm) to supply training data values for dependent variable '"
                  + depVarSym
                  + "'.");
        GEPDependentVariable.trainingData.setValues(vals, i);
        vals = prob.getTestingDataValues(depVarSym);
        if (vals != null) // don't have to supply testing data
        GEPDependentVariable.testingData.setValues(vals, i);
      }
    }

    // Some checking of data values to ensure they meet the requirements for the various problem
    // types.
    // For all problem types need to make sure all indep vars and the dep var have the same number
    // of values!
    int numValues = GEPDependentVariable.trainingData.values[0].length;
    for (int i = numberOfFunctions; i < numberOfSymbolsWithoutConstantSymbol; i++)
      if (((GEPTerminalSymbol) symbols[i]).trainingValues.length != numValues)
        state.output.fatal(
            "Must have same number of values for all independent variables and the dependent variable."
                + "/nNumber of values for Dependent Variable is: "
                + numValues
                + "/nNumber of values for Independent Variable '"
                + symbols[i].symbol
                + "' is: "
                + ((GEPTerminalSymbol) symbols[i]).trainingValues.length);
    // For Classification and logical problems all dependent variable values must be either 0 or 1
    if (species.problemType == GEPSpecies.PT_CLASSIFICATION
        || species.problemType == GEPSpecies.PT_LOGICAL) {
      double dvVals[] = GEPDependentVariable.trainingData.values[0];
      for (int i = 0; i < numValues; i++)
        if (dvVals[i] != 0.0 && dvVals[i] != 1.0)
          state.output.fatal(
              "For classification/logical problems all dependent variable values must be either 1 or 0.\nFound value "
                  + dvVals[i]
                  + " at index "
                  + i
                  + "in the values.");
    }
    // For Logical problems all independent variable values must be 0 or 1
    if (species.problemType == GEPSpecies.PT_LOGICAL) { // for each indep variable symbol
      for (int i = numberOfFunctions; i < numberOfSymbolsWithoutConstantSymbol; i++) {
        double ivVals[] = ((GEPTerminalSymbol) symbols[i]).trainingValues;
        for (int m = 0; m < numValues; m++)
          if (ivVals[m] != 0.0 && ivVals[m] != 1.0)
            state.output.fatal(
                "For logical problems all independent variable values must be either 1 or 0.\nFound value "
                    + ivVals[m]
                    + " at index '"
                    + m
                    + "' in the variable '"
                    + ((GEPTerminalSymbol) symbols[i]).symbol
                    + "'.");
      }
    }
    state.output.exitIfErrors();
  }
Esempio n. 9
0
  public void setup(final EvolutionState state, final Parameter base) {
    super.setup(state, base);

    Parameter def = defaultBase();

    // create the arrays
    minGenes = new double[genomeSize];
    maxGenes = new double[genomeSize];

    // LOADING GLOBAL MIN/MAX GENES
    double minGene =
        state.parameters.getDoubleWithDefault(base.push(P_MINGENE), def.push(P_MINGENE), 0);
    double maxGene = state.parameters.getDouble(base.push(P_MAXGENE), def.push(P_MAXGENE), minGene);
    if (maxGene < minGene)
      state.output.fatal(
          "FloatVectorSpecies must have a default min-gene which is <= the default max-gene",
          base.push(P_MAXGENE),
          def.push(P_MAXGENE));

    for (int x = 0; x < genomeSize; x++) {
      minGenes[x] = minGene;
      maxGenes[x] = maxGene;
    }

    // LOADING SEGMENTS

    // Set number of segments to 0 by default
    int numSegments = 0;
    // Now check to see if segments of genes (genes having the same min and
    // max values) exist
    if (state.parameters.exists(base.push(P_NUM_SEGMENTS), def.push(P_NUM_SEGMENTS))) {
      numSegments =
          state.parameters.getIntWithDefault(
              base.push(P_NUM_SEGMENTS), def.push(P_NUM_SEGMENTS), 0);

      if (numSegments == 0)
        state.output.warning(
            "The number of genome segments has been defined to be equal to 0.\n"
                + "Hence, no genome segments will be defined.",
            base.push(P_NUM_SEGMENTS),
            def.push(P_NUM_SEGMENTS));
      else if (numSegments < 0)
        state.output.fatal(
            "Invalid number of genome segments: "
                + numSegments
                + "\nIt must be a nonnegative value.",
            base.push(P_NUM_SEGMENTS),
            def.push(P_NUM_SEGMENTS));

      // read the type of segment definition using the default start value
      String segmentType =
          state.parameters.getStringWithDefault(
              base.push(P_SEGMENT_TYPE), def.push(P_SEGMENT_TYPE), P_SEGMENT_START);

      if (segmentType.equalsIgnoreCase(P_SEGMENT_START))
        initializeGenomeSegmentsByStartIndices(state, base, def, numSegments, minGene, maxGene);
      else if (segmentType.equalsIgnoreCase(P_SEGMENT_END))
        initializeGenomeSegmentsByEndIndices(state, base, def, numSegments, minGene, maxGene);
      else
        state.output.fatal(
            "Invalid specification of genome segment type: "
                + segmentType
                + "\nThe "
                + P_SEGMENT_TYPE
                + " parameter must have the value of "
                + P_SEGMENT_START
                + " or "
                + P_SEGMENT_END,
            base.push(P_SEGMENT_TYPE),
            def.push(P_SEGMENT_TYPE));
    }

    // LOADING PER-GENE VALUES

    boolean foundStuff = false;
    boolean warnedMin = false;
    boolean warnedMax = false;
    for (int x = 0; x < genomeSize; x++) {
      if (!state.parameters.exists(
          base.push(P_MINGENE).push("" + x), def.push(P_MINGENE).push("" + x))) {
        if (foundStuff && !warnedMin) {
          state.output.warning(
              "FloatVectorSpecies has missing min-gene values for some genes.\n"
                  + "The first one is gene #"
                  + x
                  + ".",
              base.push(P_MINGENE).push("" + x),
              def.push(P_MINGENE).push("" + x));
          warnedMin = true;
        }
      } else {
        minGenes[x] =
            state.parameters.getDoubleWithDefault(
                base.push(P_MINGENE).push("" + x), def.push(P_MINGENE).push("" + x), minGene);
        foundStuff = true;
      }

      if (!state.parameters.exists(
          base.push(P_MAXGENE).push("" + x), def.push(P_MAXGENE).push("" + x))) {
        if (foundStuff && !warnedMax) {
          state.output.warning(
              "FloatVectorSpecies has missing max-gene values for some genes.\n"
                  + "The first one is gene #"
                  + x
                  + ".",
              base.push(P_MAXGENE).push("" + x),
              def.push(P_MAXGENE).push("" + x));
          warnedMax = true;
        }
      } else {
        maxGenes[x] =
            state.parameters.getDoubleWithDefault(
                base.push(P_MAXGENE).push("" + x), def.push(P_MAXGENE).push("" + x), maxGene);
        foundStuff = true;
      }
    }

    // VERIFY
    for (int x = 0; x < genomeSize; x++) {
      if (maxGenes[x] != maxGenes[x]) // uh oh, NaN
      state.output.fatal("FloatVectorSpecies found that max-gene[" + x + "] is NaN");

      if (minGenes[x] != minGenes[x]) // uh oh, NaN
      state.output.fatal("FloatVectorSpecies found that min-gene[" + x + "] is NaN");

      if (maxGenes[x] < minGenes[x])
        state.output.fatal(
            "FloatVectorSpecies must have a min-gene["
                + x
                + "] which is <= the max-gene["
                + x
                + "]");

      // check to see if these longs are within the data type of the particular individual
      if (!inNumericalTypeRange(minGenes[x]))
        state.output.fatal(
            "This FloatvectorSpecies has a prototype of the kind: "
                + i_prototype.getClass().getName()
                + ", but doesn't have a min-gene["
                + x
                + "] value within the range of this prototype's genome's data types");
      if (!inNumericalTypeRange(maxGenes[x]))
        state.output.fatal(
            "This FloatvectorSpecies has a prototype of the kind: "
                + i_prototype.getClass().getName()
                + ", but doesn't have a max-gene["
                + x
                + "] value within the range of this prototype's genome's data types");
    }

    /// OTHER SETUP

    String mtype =
        state.parameters.getStringWithDefault(base.push(P_MUTATIONTYPE), null, V_RESET_MUTATION);
    mutationType = C_RESET_MUTATION;
    if (mtype == null)
      state.output.warning(
          "No mutation type given for VectorSpecies, assuming 'reset' mutation",
          base.push(P_MUTATIONTYPE),
          def.push(P_MUTATIONTYPE));
    else if (mtype.equalsIgnoreCase(V_RESET_MUTATION)) mutationType = C_RESET_MUTATION; // redundant
    else if (mtype.equalsIgnoreCase(V_GAUSS_MUTATION)) mutationType = C_GAUSS_MUTATION;
    else
      state.output.fatal(
          "FloatVectorSpecies given a bad mutation type: " + mtype,
          base.push(P_MUTATIONTYPE),
          def.push(P_MUTATIONTYPE));

    if (mutationType == C_GAUSS_MUTATION) {
      double gaussMutationStdev =
          state.parameters.getDouble(base.push(P_STDEV), def.push(P_STDEV), 0);
      if (gaussMutationStdev <= 0)
        state.output.fatal(
            "If it's going to use gaussian mutation, FloatvectorSpecies must have a strictly positive standard deviation",
            base.push(P_STDEV),
            def.push(P_STDEV));

      gaussMutationStdevs = new double[genomeSize];
      double defaultRange = maxGene - minGene;
      double defaultStdev = gaussMutationStdev;
      double defaultStdevOverRange = defaultStdev / defaultRange;
      for (int x = 0; x < genomeSize; x++) {
        gaussMutationStdevs[x] = defaultStdevOverRange * (maxGene(x) - minGene((x)));
      }

      outOfRangeRetries =
          state.parameters.getIntWithDefault(
              base.push(P_OUTOFBOUNDS_RETRIES), def.push(P_OUTOFBOUNDS_RETRIES), outOfRangeRetries);
      if (outOfRangeRetries < 0) {
        state.output.fatal(
            "If it's going to use gaussian mutation, FloatvectorSpecies must have a positive number of out-of-bounds retries or 0 (for don't give up)",
            base.push(P_OUTOFBOUNDS_RETRIES),
            def.push(P_OUTOFBOUNDS_RETRIES));
      }
    }
    /*
    //Debugging
    for(int i = 0; i < minGenes.length; i++)
    System.out.println("Min: " + minGenes[i] + ", Max: " + maxGenes[i]);
    */
  }
 public void setup(final EvolutionState state, final Parameter base) {
   super.setup(state, base);
   Parameter def = defaultBase();
   tossSecondParent = state.parameters.getBoolean(base.push(P_TOSS), def.push(P_TOSS), false);
 }
Esempio n. 11
0
  public void setup(final EvolutionState state, final Parameter base) {
    Parameter def = defaultBase();

    int size;

    // do we load from a file?
    loadInds = state.parameters.getFile(base.push(P_FILE), null);

    // what species do we use?

    species =
        (Species)
            state.parameters.getInstanceForParameter(
                base.push(P_SPECIES), def.push(P_SPECIES), Species.class);
    species.setup(state, base.push(P_SPECIES));

    // how big should our subpopulation be?

    size = state.parameters.getInt(base.push(P_SUBPOPSIZE), def.push(P_SUBPOPSIZE), 1);
    if (size <= 0)
      state.output.fatal(
          "Subpopulation size must be an integer >= 1.\n",
          base.push(P_SUBPOPSIZE),
          def.push(P_SUBPOPSIZE));

    // How often do we retry if we find a duplicate?
    numDuplicateRetries = state.parameters.getInt(base.push(P_RETRIES), def.push(P_RETRIES), 0);
    if (numDuplicateRetries < 0)
      state.output.fatal(
          "The number of retries for duplicates must be an integer >= 0.\n",
          base.push(P_RETRIES),
          def.push(P_RETRIES));

    individuals = new Individual[size];
  }
Esempio n. 12
0
  private void initializeGenomeSegmentsByEndIndices(
      final EvolutionState state,
      final Parameter base,
      final Parameter def,
      int numSegments,
      double minGene,
      double maxGene) {
    boolean warnedMin = false;
    boolean warnedMax = false;
    double currentSegmentMinGeneValue = Double.MAX_VALUE;
    double currentSegmentMaxGeneValue = Double.MIN_VALUE;

    int previousSegmentEnd = -1;
    int currentSegmentEnd = 0;
    // iterate over segments and set genes values for each segment
    for (int i = 0; i < numSegments; i++) {
      // check if the segment data exist
      if (state.parameters.exists(
          base.push(P_SEGMENT).push("" + i).push(P_SEGMENT_END),
          def.push(P_SEGMENT).push("" + i).push(P_SEGMENT_END))) {
        // Read the index of the end gene specifying current segment
        currentSegmentEnd =
            state.parameters.getInt(
                base.push(P_SEGMENT).push("" + i).push(P_SEGMENT_END),
                def.push(P_SEGMENT).push("" + i).push(P_SEGMENT_END));

      } else {
        state.output.fatal(
            "Genome segment "
                + i
                + " has not been defined!"
                + "\nYou must specify end indices for "
                + numSegments
                + " segment(s)",
            base.push(P_SEGMENT).push("" + i).push(P_SEGMENT_END),
            base.push(P_SEGMENT).push("" + i).push(P_SEGMENT_END));
      }

      // check if the end index is valid
      if (currentSegmentEnd <= previousSegmentEnd || currentSegmentEnd >= genomeSize)
        state.output.fatal(
            "Invalid end index value for segment "
                + i
                + ": "
                + currentSegmentEnd
                + "\nThe value must be greater than "
                + previousSegmentEnd
                + " and smaller than "
                + genomeSize);

      // check if the index of the final segment is equal to the genomeSize
      if (i == numSegments - 1 && currentSegmentEnd != (genomeSize - 1))
        state.output.fatal(
            "Invalid end index value for the last segment "
                + i
                + ": "
                + currentSegmentEnd
                + "\nThe value must be equal to the index of the last gene in the genome:  "
                + (genomeSize - 1));

      // get min and max values of genes in this segment
      if (!state.parameters.exists(
          base.push(P_SEGMENT).push("" + i).push(P_MINGENE),
          base.push(P_SEGMENT).push("" + i).push(P_MINGENE))) {
        if (!warnedMin) {
          state.output.warning(
              "IntegerVectorSpecies has missing min-gene values for some segments.\n"
                  + "The first segment is #"
                  + i
                  + ".",
              base.push(P_SEGMENT).push("" + i),
              base.push(P_SEGMENT).push("" + i));
          warnedMin = true;
        }

        // the min-gene value has not been defined for this segment so assume the global min value
        currentSegmentMinGeneValue = minGene;
      } else // get the min value for this segment
      {
        currentSegmentMinGeneValue =
            state.parameters.getDoubleWithDefault(
                base.push(P_SEGMENT).push("" + i).push(P_MINGENE),
                base.push(P_SEGMENT).push("" + i).push(P_MINGENE),
                minGene);

        // check if the value is in range
        if (!inNumericalTypeRange(currentSegmentMinGeneValue))
          state.output.error(
              "This IntegerVectorSpecies has a prototype of the kind: "
                  + i_prototype.getClass().getName()
                  + ", but doesn't have a min-gene "
                  + " value for segment "
                  + i
                  + " within the range of this prototype's genome's data types",
              base.push(P_SEGMENT).push("" + i).push(P_MINGENE),
              base.push(P_SEGMENT).push("" + i).push(P_MINGENE));
      }

      if (!state.parameters.exists(
          base.push(P_SEGMENT).push("" + i).push(P_MAXGENE),
          base.push(P_SEGMENT).push("" + i).push(P_MAXGENE))) {
        if (!warnedMax) {
          state.output.warning(
              "IntegerVectorSpecies has missing max-gene values for some segments.\n"
                  + "The first segment is #"
                  + i
                  + ".",
              base.push(P_SEGMENT).push("" + i),
              base.push(P_SEGMENT).push("" + i));
          warnedMax = true;
        }

        // the max-gen value has not been defined for this segment so assume the global max value
        currentSegmentMaxGeneValue = maxGene;

      } else // get the max value for this segment
      {
        currentSegmentMaxGeneValue =
            state.parameters.getDoubleWithDefault(
                base.push(P_SEGMENT).push("" + i).push(P_MAXGENE),
                base.push(P_SEGMENT).push("" + i).push(P_MAXGENE),
                maxGene);

        // check if the value is in range
        if (!inNumericalTypeRange(currentSegmentMaxGeneValue))
          state.output.fatal(
              "This IntegerVectorSpecies has a prototype of the kind: "
                  + i_prototype.getClass().getName()
                  + ", but doesn't have a max-gene "
                  + " value for segment "
                  + i
                  + " within the range of this prototype's genome's data types",
              base.push(P_SEGMENT).push("" + i).push(P_MAXGENE),
              base.push(P_SEGMENT).push("" + i).push(P_MAXGENE));
      }

      // check is min is smaller than or equal to max
      if (currentSegmentMaxGeneValue < currentSegmentMinGeneValue)
        state.output.fatal(
            "IntegerVectorSpecies must have a min-gene value for segment "
                + i
                + " which is <= the max-gene value",
            base.push(P_SEGMENT).push("" + i).push(P_MAXGENE),
            base.push(P_SEGMENT).push("" + i).push(P_MAXGENE));

      // and assign min and max values for all genes in this segment
      for (int j = previousSegmentEnd + 1; j <= currentSegmentEnd; j++) {
        minGenes[j] = currentSegmentMinGeneValue;
        maxGenes[j] = currentSegmentMaxGeneValue;
      }

      previousSegmentEnd = currentSegmentEnd;
    }
  }
Esempio n. 13
0
  /** Read additional parameters for the spatially-embedded subpopulation. */
  public void setup(final EvolutionState state, final Parameter base) {
    super.setup(state, base);

    // by default, the space is toroidal
    toroidal = state.parameters.getBoolean(base.push(P_TOROIDAL), null, true);
  }
Esempio n. 14
0
  public void setup(final EvolutionState state, final Parameter base) {
    Parameter def = defaultBase();

    terminalProbability =
        state.parameters.getDoubleWithMax(
            base.push(P_TERMINAL_PROBABILITY), def.push(P_TERMINAL_PROBABILITY), 0.0, 1.0);
    if (terminalProbability == -1.0)
      state.output.fatal(
          "Invalid terminal probability for KozaNodeSelector ",
          base.push(P_TERMINAL_PROBABILITY),
          def.push(P_TERMINAL_PROBABILITY));

    nonterminalProbability =
        state.parameters.getDoubleWithMax(
            base.push(P_NONTERMINAL_PROBABILITY), def.push(P_NONTERMINAL_PROBABILITY), 0.0, 1.0);
    if (nonterminalProbability == -1.0)
      state.output.fatal(
          "Invalid nonterminal probability for KozaNodeSelector ",
          base.push(P_NONTERMINAL_PROBABILITY),
          def.push(P_NONTERMINAL_PROBABILITY));

    rootProbability =
        state.parameters.getDoubleWithMax(
            base.push(P_ROOT_PROBABILITY), def.push(P_ROOT_PROBABILITY), 0.0, 1.0);

    if (rootProbability == -1.0)
      state.output.fatal(
          "Invalid root probability for KozaNodeSelector ",
          base.push(P_ROOT_PROBABILITY),
          def.push(P_ROOT_PROBABILITY));

    if (rootProbability + terminalProbability + nonterminalProbability > 1.0f)
      state.output.fatal(
          "The terminal, nonterminal, and root for KozaNodeSelector"
              + base
              + " may not sum to more than 1.0. ("
              + terminalProbability
              + " "
              + nonterminalProbability
              + " "
              + rootProbability
              + ")",
          base);

    reset();
  }
  public void setup(final EvolutionState state, final Parameter base) {
    super.setup(state, base);

    Parameter def = defaultBase();
    Parameter p = base.push(P_NODESELECTOR).push("0");
    Parameter d = def.push(P_NODESELECTOR).push("0");

    nodeselect1 =
        (GPNodeSelector) (state.parameters.getInstanceForParameter(p, d, GPNodeSelector.class));
    nodeselect1.setup(state, p);

    p = base.push(P_NODESELECTOR).push("1");
    d = def.push(P_NODESELECTOR).push("1");

    if (state.parameters.exists(p, d) && state.parameters.getString(p, d).equals(V_SAME))
      // can't just copy it this time; the selectors
      // use internal caches.  So we have to clone it no matter what
      nodeselect2 = (GPNodeSelector) (nodeselect1.clone());
    else {
      nodeselect2 =
          (GPNodeSelector) (state.parameters.getInstanceForParameter(p, d, GPNodeSelector.class));
      nodeselect2.setup(state, p);
    }

    numTries = state.parameters.getInt(base.push(P_NUM_TRIES), def.push(P_NUM_TRIES), 1);
    if (numTries == 0)
      state.output.fatal(
          "GPCrossover Pipeline has an invalid number of tries (it must be >= 1).",
          base.push(P_NUM_TRIES),
          def.push(P_NUM_TRIES));

    maxDepth = state.parameters.getInt(base.push(P_MAXDEPTH), def.push(P_MAXDEPTH), 1);
    if (maxDepth == 0)
      state.output.fatal(
          "GPCrossover Pipeline has an invalid maximum depth (it must be >= 1).",
          base.push(P_MAXDEPTH),
          def.push(P_MAXDEPTH));

    maxSize = NO_SIZE_LIMIT;
    if (state.parameters.exists(base.push(P_MAXSIZE), def.push(P_MAXSIZE))) {
      maxSize = state.parameters.getInt(base.push(P_MAXSIZE), def.push(P_MAXSIZE), 1);
      if (maxSize < 1) state.output.fatal("Maximum tree size, if defined, must be >= 1");
    }

    tree1 = TREE_UNFIXED;
    if (state.parameters.exists(base.push(P_TREE).push("" + 0), def.push(P_TREE).push("" + 0))) {
      tree1 =
          state.parameters.getInt(base.push(P_TREE).push("" + 0), def.push(P_TREE).push("" + 0), 0);
      if (tree1 == -1) state.output.fatal("Tree fixed value, if defined, must be >= 0");
    }

    tree2 = TREE_UNFIXED;
    if (state.parameters.exists(base.push(P_TREE).push("" + 1), def.push(P_TREE).push("" + 1))) {
      tree2 =
          state.parameters.getInt(base.push(P_TREE).push("" + 1), def.push(P_TREE).push("" + 1), 0);
      if (tree2 == -1) state.output.fatal("Tree fixed value, if defined, must be >= 0");
    }
    tossSecondParent = state.parameters.getBoolean(base.push(P_TOSS), def.push(P_TOSS), false);

    GPInitializer initializer = ((GPInitializer) state.initializer);
    fs = initializer.treeConstraints[0].functionset;

    templateTree = (GPNode) (((GPNode[]) fs.nodesByName.get("+"))[0]).lightClone();
    GPNode left = (GPNode) (((GPNode[]) fs.nodesByName.get("*"))[0]).lightClone();
    GPNode right = (GPNode) (((GPNode[]) fs.nodesByName.get("*"))[0]).lightClone();
    // RegERC ercNode1 = (RegERC)((((GPNode[])fs.nodesByName.get("ERC"))[0]).lightClone());
    //
    // generate TR node that will be replated by random tree
    // GPNode TRNode = new GPNode();
    // ercNode.constraints = 6;
    // ercNode.children = new GPNode[0];

    templateTree.children = new GPNode[2];
    templateTree.children[0] = left;
    templateTree.children[1] = right;

    left.children = new GPNode[2];
    left.children[0] = null;
    left.children[1] = null;

    right.children = new GPNode[2];
    right.children[0] = null;
    right.children[1] = null;
  }
Esempio n. 16
0
  /** This must be called <i>after</i> the GPTypes have been set up. */
  public final void setup(final EvolutionState state, final Parameter base) {
    // What's my name?
    name = state.parameters.getString(base.push(P_NAME), null);
    if (name == null)
      state.output.fatal("No name was given for this node constraints.", base.push(P_NAME));

    // Register me
    GPNodeConstraints old_constraints =
        (GPNodeConstraints)
            (((GPInitializer) state.initializer).nodeConstraintRepository.put(name, this));
    if (old_constraints != null)
      state.output.fatal(
          "The GP node constraint \"" + name + "\" has been defined multiple times.",
          base.push(P_NAME));

    // What's my return type?
    String s = state.parameters.getString(base.push(P_RETURNS), null);
    if (s == null)
      state.output.fatal(
          "No return type given for the GPNodeConstraints " + name, base.push(P_RETURNS));
    returntype = GPType.typeFor(s, state);

    // Load probability of selection

    if (state.parameters.exists(base.push(P_PROBABILITY), null)) {
      float f = state.parameters.getFloat(base.push(P_PROBABILITY), null, 0);
      if (f < 0)
        state.output.fatal(
            "The probability of selection is < 0, which is not valid.",
            base.push(P_PROBABILITY),
            null);
      probabilityOfSelection = f;
    } else probabilityOfSelection = DEFAULT_PROBABILITY;

    // How many child types do I have?

    int x = state.parameters.getInt(base.push(P_SIZE), null, 0);
    if (x < 0)
      state.output.fatal(
          "The number of children types for the GPNodeConstraints " + name + " must be >= 0.",
          base.push(P_SIZE));

    childtypes = new GPType[x];

    Parameter p = base.push(P_CHILD);

    // Load my children
    for (x = 0; x < childtypes.length; x++) {
      s = state.parameters.getString(p.push("" + x), null);
      if (s == null)
        state.output.fatal(
            "Type #" + x + " is not defined for the GPNodeConstraints " + name + ".",
            base.push("" + x));
      childtypes[x] = GPType.typeFor(s, state);
    }
    // ...because I promised when I called typeFor(...)
    state.output.exitIfErrors();
  }
  public void setup(final EvolutionState state, final Parameter base) {
    super.setup(state, base);

    Parameter def = defaultBase();
    Parameter p = base.push(P_NODESELECTOR).push("0");
    Parameter d = def.push(P_NODESELECTOR).push("0");

    nodeselect1 =
        (GPNodeSelector) (state.parameters.getInstanceForParameter(p, d, GPNodeSelector.class));
    nodeselect1.setup(state, p);

    p = base.push(P_NODESELECTOR).push("1");
    d = def.push(P_NODESELECTOR).push("1");

    if (state.parameters.exists(p, d) && state.parameters.getString(p, d).equals(V_SAME))
      // can't just copy it this time; the selectors
      // use internal caches.  So we have to clone it no matter what
      nodeselect2 = (GPNodeSelector) (nodeselect1.clone());
    else {
      nodeselect2 =
          (GPNodeSelector) (state.parameters.getInstanceForParameter(p, d, GPNodeSelector.class));
      nodeselect2.setup(state, p);
    }

    numTries = state.parameters.getInt(base.push(P_NUM_TRIES), def.push(P_NUM_TRIES), 1);
    if (numTries == 0)
      state.output.fatal(
          "GPCrossover Pipeline has an invalid number of tries (it must be >= 1).",
          base.push(P_NUM_TRIES),
          def.push(P_NUM_TRIES));

    maxDepth = state.parameters.getInt(base.push(P_MAXDEPTH), def.push(P_MAXDEPTH), 1);
    if (maxDepth == 0)
      state.output.fatal(
          "GPCrossover Pipeline has an invalid maximum depth (it must be >= 1).",
          base.push(P_MAXDEPTH),
          def.push(P_MAXDEPTH));

    maxSize = NO_SIZE_LIMIT;
    if (state.parameters.exists(base.push(P_MAXSIZE), def.push(P_MAXSIZE))) {
      maxSize = state.parameters.getInt(base.push(P_MAXSIZE), def.push(P_MAXSIZE), 1);
      if (maxSize < 1) state.output.fatal("Maximum tree size, if defined, must be >= 1");
    }

    tree1 = TREE_UNFIXED;
    if (state.parameters.exists(base.push(P_TREE).push("" + 0), def.push(P_TREE).push("" + 0))) {
      tree1 =
          state.parameters.getInt(base.push(P_TREE).push("" + 0), def.push(P_TREE).push("" + 0), 0);
      if (tree1 == -1) state.output.fatal("Tree fixed value, if defined, must be >= 0");
    }

    tree2 = TREE_UNFIXED;
    if (state.parameters.exists(base.push(P_TREE).push("" + 1), def.push(P_TREE).push("" + 1))) {
      tree2 =
          state.parameters.getInt(base.push(P_TREE).push("" + 1), def.push(P_TREE).push("" + 1), 0);
      if (tree2 == -1) state.output.fatal("Tree fixed value, if defined, must be >= 0");
    }
    tossSecondParent = state.parameters.getBoolean(base.push(P_TOSS), def.push(P_TOSS), false);
  }