Example #1
0
  public FuzzyCMeansResults calculate() throws FileNotFoundException, UnsupportedEncodingException {
    membershipMatrix = new MembershipMatrix(numberOfPoints, numberOfClusters);

    List<Centroid> newCentroids = calculateNewCentroids();
    List<Centroid> oldCentroids;
    double exponent = 2.0 / (fuzziness - 1.0);

    clock.clockStart();

    for (int loop = 0; ; loop++) {
      prevMembershipMatrix = membershipMatrix;
      oldCentroids = newCentroids;
      setInfo("[Fuzzy_C_Means] Loop " + loop);
      for (int p = 0; p < numberOfPoints; p++) {
        setInfo("[Fuzzy_C_Means] Loop " + loop + " | point " + p);
        for (int c = 0; c < numberOfClusters; c++) {
          setInfo("[Fuzzy_C_Means] Loop " + loop + " | point " + p + " | cluster " + c);

          double TEMP_denominator = 0;
          double point_cluster_dist = euclideanDistance(points.get(p), newCentroids.get(c));
          double TEMP_nominator = Math.pow((1.0 / point_cluster_dist), exponent);

          for (int r = 0; r < numberOfClusters; r++) {
            double summary_point_cluster_dist =
                euclideanDistance(points.get(p), newCentroids.get(r));
            double ratio = Math.pow(point_cluster_dist / summary_point_cluster_dist, exponent);
            TEMP_denominator += Math.pow((1.0 / summary_point_cluster_dist), exponent);
          }
          membershipMatrix.set(p, c, TEMP_nominator / TEMP_denominator);
        }
      }
      newCentroids = calculateNewCentroids();
      if (matrixDifferencesSmallerThanEpsilon()) {
        break;
      }
    }
    double time = clock.clockEnd();
    System.out.println(newCentroids);
    setInfo(
        "Skin segmented. Fuzz: "
            + fuzziness
            + " Eps: "
            + epsilon
            + " Pix: "
            + numberOfPoints
            + " Time: "
            + time);
    return new FuzzyCMeansResults(newCentroids, membershipMatrix);
  }
 private double distanceTo(Actor opponent) {
   double actorToMoveX = opponent.getAvatar().getTranslateX();
   double actorToMoveY = opponent.getAvatar().getTranslateY();
   double currentX = getAvatar().getTranslateX();
   double currentY = getAvatar().getTranslateY();
   double deltaX = actorToMoveX - currentX;
   double deltaY = actorToMoveY - currentY;
   double calculatedDistance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
   return calculatedDistance;
 }
Example #3
0
  private Centroid calculateCentroidOfIndex(int index) {
    double[] values = new double[pointsDimensions];
    double nominator, denominator;

    for (int dim = 0; dim < pointsDimensions; dim++) {
      nominator = 0;
      denominator = 0;

      for (int i = 0; i < numberOfPoints; i++) {
        nominator +=
            Math.pow(membershipMatrix.get(i, index), fuzziness) * points.get(i).getDim(dim);
        denominator += Math.pow(membershipMatrix.get(i, index), fuzziness);
      }

      values[dim] = nominator / denominator;
    }

    return new Centroid(values);
  }
Example #4
0
  private double euclideanDistance(Features point, Centroid centroid) {
    if (point.getNumberOfDim() != centroid.getNumberOfDim()) throw new IllegalArgumentException();

    double sumOfSquares = 0;
    for (int i = 0; i < point.getNumberOfDim(); i++) {
      sumOfSquares +=
          (point.getDim(i) - centroid.getDim(i)) * (point.getDim(i) - centroid.getDim(i));
    }

    return Math.sqrt(sumOfSquares);
  }
  /**
   * processes a single round of combat between two Actor objects: the <b>attacker</b> is this
   * object; the <b>defender</b> is received as an argument. This method is called by the
   * <b>attacker</b> <i>Actor</i> object. This <b>attacker</b> <i>Actor</i> object chooses another
   * <i>Actor</i> object as the <b>defender</b> by sending a reference-to the second <i>Actor</i>
   * object. When program execution arrives in <i>combatRound</i>, the method will have access to 2
   * sets of <i>Actor</i> attributes (a.k.a. instance fields). In particular, this method will need
   * to use <i>health</i> and <i>strength</i> to process a single round of combat. As an outcome of
   * the single round, both <i>Actor</i> objects: the <b>attacker</b> and the <b>defender</b> are
   * likely to loose some <i>health</i> value, but the <i>Actor</i> object with less <i>strength</i>
   * will likely incur more damage to their <i>health</i>. You access the <b>attacker</b> instance
   * fields (such as <i>health</i> using <i>this.health</i> and the <b>defender</b> instance fields
   * using <i>defender.health</i>. Of course, <i>defender</i> is the name of the stack-oriented
   * reference-to variable that is sent to the method.
   *
   * @param defender a reference to a different <i>Actor</i> object that will engage in combat with
   *     this <i>Actor</i> object.
   * @return <i>health</i> of the <i>Actor</i> following the combat round.
   */
  public double combatRound(Actor defender) {
    final double MAX_COMBAT_HEALTH_REDUCTION_OF_LOOSER =
        10.0; // health ranges 0.0 to 100.0, thus could loose 0.0 to 10.0
    final double MAX_COMBAT_HEALTH_REDUCTION_OF_WINNER = 3.0; // could loose 0.0 to 3.0
    double healthAdjustmentOfLooser =
        -(Math.random() * MAX_COMBAT_HEALTH_REDUCTION_OF_LOOSER)
            - 1.0; // looser looses at least 1.0
    double healthAdjustmentOfWinner =
        -(Math.random() * MAX_COMBAT_HEALTH_REDUCTION_OF_WINNER) + 1.0; // winner gains at least 1.0

    double proportionHitPoints =
        getHitPoints() / (getHitPoints() + defender.getHitPoints()); // between 0.0 and 1.0
    if (Math.random() > proportionHitPoints) {
      adjustHealth(healthAdjustmentOfLooser);
      defender.adjustHealth(healthAdjustmentOfWinner);
    } else {
      defender.adjustHealth(healthAdjustmentOfLooser);
      adjustHealth(healthAdjustmentOfWinner);
    }
    return getHealth();
  } // end combatRound()
Example #6
0
  private boolean matrixDifferencesSmallerThanEpsilon() {
    boolean areAllSmallerThanEpsilon = true;
    for (int p = 0; p < numberOfPoints; p++) {
      for (int c = 0; c < numberOfClusters; c++) {
        double prev = prevMembershipMatrix.get(p, c);
        double curr = membershipMatrix.get(p, c);

        if (Math.abs(curr - prev) > epsilon) {
          areAllSmallerThanEpsilon = false;
          break;
        }
      }
    }

    return areAllSmallerThanEpsilon;
  }
Example #7
0
  // Creates a countdown from time to 0
  public Countdown(final int time) {
    timeLeft = new ReadOnlyIntegerWrapper(time);
    timeLeftDouble = new ReadOnlyDoubleWrapper(time);

    // Does the counting to 0
    timeline =
        new Timeline(
            // start frame
            new KeyFrame(Duration.ZERO, new KeyValue(timeLeftDouble, time)),
            // end frame
            new KeyFrame(Duration.seconds(time), new KeyValue(timeLeftDouble, 0)));

    timeLeftDouble.addListener(
        o -> {
          timeLeft.set((int) Math.ceil(timeLeftDouble.get()));
        });
  }
 public double getHitPoints() {
   return getStrength() + getHealth() * .5 * Math.random();
 }
 /**
  * <i>Actor</i> regain health on each cycle of the simulation (and loose health in battles handled
  * by other code).
  */
 public void gameCycleHealthGain() {
   final double MAX_CYCLE_HEALTH_GAIN = 2.0;
   adjustHealth(Math.random() * MAX_CYCLE_HEALTH_GAIN);
 }