Exemplo n.º 1
0
  /**
   * The function calculates for each derivation (d) that belongs to "individuals", the
   * entropy-based fitness function of the rule set associated to d, over the isokinetic curves
   * sample set "features".
   *
   * @param individuals The derivations with the rule set associated to each one.
   * @param features The sample set of isokinetic curves.
   */
  private Map<Derivation, Double> fitness(
      Collection<Derivation> individuals, DoubleMatrix2D features, boolean applyEnt)
      throws GggpExceptionImpl {

    Map<Derivation, Double> res = null;
    Collection<AuxFeatureFitnessImpl> auxFit;
    Iterator<AuxFeatureFitnessImpl> itAux;
    AuxFeatureFitnessImpl auxFitE;
    Iterator<Derivation> it;
    Derivation d;
    Collection<Rules> cr;
    Iterator<Rules> itr;
    Rules r;
    int normalClass;
    int injuryClass;
    double notSuccess;
    boolean evaluation;
    double success;
    double ebff;
    double efc;
    double H;
    double auxFitness;

    if ((individuals != null) && (features != null)) {

      auxFit = new LinkedList<AuxFeatureFitnessImpl>();

      auxFitE = new AuxFeatureFitnessImpl();

      auxFit = auxFitE.mapFeaturesWithLabel(this.getFeatures());

      res = new ConcurrentHashMap<Derivation, Double>();

      it = individuals.iterator();

      while (it.hasNext()) {

        d = it.next();

        cr = parseRules(d, "real");

        itAux = auxFit.iterator();

        success = 0;
        H = 0;

        /**
         * To each isokinetic curve auxFitE, all the DB's rules are applied. Only the rules that
         * returns TRUE are considered, and depending of the class (normal or injury) associated to
         * the rule and to auxFitE, one of two variables are incremented ("normalClass" = normal
         * class, and "injuryClass" = normal class). At the end, the class with more hits is
         * returned.
         */
        while (itAux.hasNext()) {

          auxFitE = itAux.next();

          itr = cr.iterator();

          normalClass = 0;
          injuryClass = 0;
          notSuccess = 0;

          /** Applies all the rules over the isokinetic curve auxFiteE */
          while (itr.hasNext()) {

            r = itr.next();

            evaluation = r.evaluatesRule(auxFitE.getFeatures(), r.getAntecedent());

            if (evaluation) {

              if (((r.getConsecuent()).toUpperCase()).equals("NORMAL")) {

                if (auxFitE.getLabel().equals(new Double(1))) {

                  /** The rule misclassify the isokinetic curve. */
                  notSuccess++;
                }

                normalClass++;

              } else {

                if (auxFitE.getLabel().equals(new Double(0))) {

                  /** The rule misclassify the isokinetic curve. */
                  notSuccess++;
                }

                injuryClass++;
              }
            }
          }

          if (applyEnt) {

            if (cr.size() != 0) {

              /**
               * The entropy function component of the rule set over the isokinetic curve auxFitE.
               */
              efc = notSuccess / (2 * cr.size());

            } else {
              efc = 0;
            }

            /**
             * The entropy funtion of the distribution of the rule set's non success results, over
             * the sample of isokinect curves.
             */
            if (efc != 0) {

              H = H - efc * Math.log(efc);
            }
          }

          /**
           * Classification of the result, that is: if the rules made a right prognosis, increment
           * success, otherwise it does nothing.
           */
          if (auxFitE.getLabel().equals(new Double(0))) {

            if (normalClass > injuryClass) {

              /** The rule set classify properly the isokinetic curve. */
              success++;
            }

          } else {

            if (injuryClass > normalClass) {

              /** The rule set classify properly the isokinetic curve. */
              success++;
            }
          }
        }

        if (applyEnt) {

          auxFitness = (success - H);

        } else {
          auxFitness = success;
        }

        if (auxFitness != 0) {

          /**
           * The entropy-based fitness function of the rule set over the isokinetic curves sample
           * set.
           */
          ebff = auxFitness / features.rows();

        } else {
          ebff = 0;
        }

        res.put(d, ebff);
      }
    }

    return res;
  }
Exemplo n.º 2
0
  /**
   * @param individual The derivation to be tested.
   * @param d The features used to test the derivation.
   * @return A list with the test results.
   * @throws GggpExceptionImpl
   */
  public List<Integer> test(Derivation individual, DoubleMatrix2D d) throws GggpExceptionImpl {

    List<Integer> res = null;
    Collection<AuxFeatureFitnessImpl> auxFit;
    Iterator<AuxFeatureFitnessImpl> itAux;
    AuxFeatureFitnessImpl auxFitE;
    Collection<Rules> cr;
    Iterator<Rules> itr;
    Rules r;
    int normalClass;
    int injuryClass;
    int fit;
    int falsePositive;
    int falseNegative;
    int indefined;
    boolean evaluation;

    if ((individual != null) && (d != null)) {

      auxFitE = new AuxFeatureFitnessImpl();

      auxFit = auxFitE.mapFeaturesWithLabel(d);

      cr = parseRules(individual, "real");

      itAux = auxFit.iterator();

      fit = 0;
      falsePositive = 0;
      falseNegative = 0;
      indefined = 0;

      /**
       * To each feature, all the rules are applied. Only the rules that returns TRUE are
       * considered, and in function of the class (normal or injury) associated to the rule, one of
       * two variables are incremented ("normalClass" = normal class, and "injuryClass" = normal
       * class). At the end, the result is classified in FIT (a right prognosis), FALSE_NEGATIVE,
       * FALSE_POSITIVE or INDEFINED.
       */
      while (itAux.hasNext()) {

        auxFitE = itAux.next();

        itr = cr.iterator();

        normalClass = 0;
        injuryClass = 0;

        while (itr.hasNext()) {

          r = itr.next();

          evaluation = r.evaluatesRule(auxFitE.getFeatures(), r.getAntecedent());

          if (evaluation) {

            if (((r.getConsecuent()).toUpperCase()).equals("NORMAL")) {

              normalClass++;

            } else {

              injuryClass++;
            }
          }
        }

        /** Classification of the results. */
        if (auxFitE.getLabel().equals(new Double(0))) {

          if (normalClass > injuryClass) {

            fit++;

          } else {

            if (normalClass < injuryClass) {

              falsePositive++;

            } else {

              indefined++;
            }
          }

        } else {

          if (injuryClass > normalClass) {

            fit++;

          } else {

            if (injuryClass < normalClass) {

              falseNegative++;

            } else {

              indefined++;
            }
          }
        }
      }

      res = new LinkedList<Integer>();

      /** Add the amount of classified items (series). */
      res.add(new Integer(d.rows()));

      /** Add the amount of right prognosis (FIT). */
      res.add(new Integer(fit));

      /** Add the amount of false negatives. */
      res.add(new Integer(falseNegative));

      /** Add the amount of false positives. */
      res.add(new Integer(falsePositive));

      /** Add the amount of indefined. */
      res.add(new Integer(indefined));
    }

    return res;
  }