Exemplo n.º 1
0
 public double distanciaEuclidiana(int x1, int x2, int y1, int y2) {
   double a, b, c;
   a = Math.pow(x2 - x1, 2);
   b = Math.pow(y2 - y1, 2);
   c = Math.sqrt(a + b);
   return (c);
 }
Exemplo n.º 2
0
  public int getWrongTwo() {
    int wrongTwo = (int) (Math.random() * (Math.pow(2, bits)));
    while (wrongTwo == answer || wrongTwo == wrongOne)
      wrongOne = (int) (Math.random() * (Math.pow(2, bits)));

    return wrongTwo;
  }
Exemplo n.º 3
0
 public void generateCaverns(int count) {
   for (int i = 0; i < count; i++) {
     m_logger.debug("Generating underground erosion bubbles: " + i + "/" + count);
     int x = m_random.nextInt(m_width);
     int y = m_random.nextInt(m_height);
     int z = m_random.nextInt(m_depth / 4);
     int radius = m_random.nextInt(60) + 40;
     radius = 6;
     int type = m_random.nextInt(100);
     if (type > 90) type = BlockConstants.LAVA;
     else if (type > 45) type = BlockConstants.AIR;
     else type = BlockConstants.WATER;
     for (int m = 0; m < 2; m++) {
       BUBBLE_GEN:
       for (int j = x - radius; j < x + radius * 2; j++) {
         if (j < 0) j = 0;
         if (j >= m_width) break BUBBLE_GEN;
         for (int k = y - radius; k < y + radius * 2; k++) {
           if (k < 0) k = 0;
           if (k >= m_height) break BUBBLE_GEN;
           for (int l = z - radius; l < z + radius; l++) {
             if (l < 0) l = 0;
             if (l >= m_depth) break BUBBLE_GEN;
             double distance =
                 Math.sqrt(Math.pow(j - x, 2) + Math.pow(k - y, 2) + Math.pow(l - z, 2));
             if (Math.abs(distance / radius) <= Math.abs(m_random.nextGaussian())) {
               m_blocks[j][k][l] = (byte) type;
             }
           }
         }
       }
       x++;
     }
   }
 }
Exemplo n.º 4
0
 public void plantTrees() {
   ArrayList<Position> treeList = new ArrayList<Position>();
   for (int x = 0; x < m_width; x++) {
     for (int y = 0; y < m_height; y++) {
       boolean tooClose = false;
       for (Position p : treeList) {
         double distance = Math.sqrt(Math.pow(p.getX() - x, 2) + Math.pow(p.getY() - y, 2));
         if (distance < 30) tooClose = true;
       }
       if (!tooClose) {
         if (m_random.nextInt(100) <= 5) {
           for (int z = m_depth - 1; z > 0; z--) {
             if ((m_blocks[x][y][z] == BlockConstants.DIRT
                     || m_blocks[x][y][z] == BlockConstants.GRASS)
                 && m_blocks[x][y][z + 1] == BlockConstants.AIR) {
               plantTree(x, y, z);
               treeList.add(new Position(x, y, z));
               break;
             } else if (z < m_depth - 1 && m_blocks[x][y][z + 1] != BlockConstants.AIR) {
               break;
             }
           }
         }
       }
     }
   }
 }
 /**
  * Perform the mutation operation
  *
  * @param probability Mutation probability
  * @param solution The solution to mutate
  * @throws JMException
  */
 public void doMutation(double probability, Solution solution) throws JMException {
   double rnd, delta1, delta2, mut_pow, deltaq;
   double y, yl, yu, val, xy;
   XReal x = new XReal(solution);
   for (int var = 0; var < solution.numberOfVariables(); var++) {
     if (PseudoRandom.randDouble() <= probability) {
       y = x.getValue(var);
       yl = x.getLowerBound(var);
       yu = x.getUpperBound(var);
       delta1 = (y - yl) / (yu - yl);
       delta2 = (yu - y) / (yu - yl);
       rnd = PseudoRandom.randDouble();
       mut_pow = 1.0 / (eta_m_ + 1.0);
       if (rnd <= 0.5) {
         xy = 1.0 - delta1;
         val = 2.0 * rnd + (1.0 - 2.0 * rnd) * (Math.pow(xy, (eta_m_ + 1.0)));
         deltaq = java.lang.Math.pow(val, mut_pow) - 1.0;
       } else {
         xy = 1.0 - delta2;
         val = 2.0 * (1.0 - rnd) + 2.0 * (rnd - 0.5) * (java.lang.Math.pow(xy, (eta_m_ + 1.0)));
         deltaq = 1.0 - (java.lang.Math.pow(val, mut_pow));
       }
       y = y + deltaq * (yu - yl);
       if (y < yl) y = yl;
       if (y > yu) y = yu;
       x.setValue(var, y);
     }
   } // for
 } // doMutation
  @Override
  public double pNorm(double p) {
    double norm = 0;

    for (int i = 0; i < used; i++) norm += Math.pow(Math.abs(values[i]), p);

    return Math.pow(norm, 1.0 / p);
  }
Exemplo n.º 7
0
  /**
   * Calculates word order similarity between the sentences, with weighted words
   *
   * @param s1 sentence 1
   * @param s2 sentence 2
   * @param weights1 of sentence 1
   * @param weights2 of sentence 2
   * @return Word order similarity value
   */
  public double orderSimilarity(
      List<double[]> s1,
      List<double[]> s2,
      List<double[]> weights1,
      List<double[]> weights2,
      String sent2,
      String unique) {
    double[] s1Dist = s1.get(0);
    double[] s1Friend = s1.get(1);
    double[] s2Dist = s2.get(0);
    double[] s2Friend = s2.get(1);
    double[] r1 = new double[s1Dist.length];
    double[] r2 = new double[s2Dist.length];
    String[] sent = sent2.split(" ");
    String[] un = unique.split(" ");
    String word;

    // Specifies word order vectors for either sentence.
    // Threshold specifies that words can be seen as the same if similar enough
    // If same word not found in unique sentence, the order value is 0
    for (int i = 0; i < r1.length; i++) {
      if (s1Dist[i] == 1.0) {
        r1[i] = i + 1;
      } else if (s1Dist[i] >= threshold) {
        r1[i] = s1Friend[i] + 1;

      } else {
        r1[i] = 0;
      }
    }
    for (int i = 0; i < r2.length; i++) {
      if (s2Dist[i] == 1.0) {
        word = un[i];
        r2[i] = Arrays.asList(sent).indexOf(word) + 1;
      } else if (s2Dist[i] >= threshold) {
        r2[i] = s2Friend[i] + 1;

      } else {
        r2[i] = 0.0;
      }
    }
    double numerator = 0.0;
    double denominator = 0.0;
    // Calculate order similarity while avoiding division by 0
    for (int i = 0; i < r1.length; i++) {
      numerator = numerator + Math.pow((r1[i] - r2[i]) * weights1.get(0)[i], 2);
      denominator = denominator + Math.pow((r1[i] + r2[i]) * weights1.get(0)[i], 2);
    }
    numerator = Math.sqrt(numerator);
    denominator = Math.sqrt(denominator);
    if (denominator == 0.0) {
      numerator = 1;
      denominator = 1;
    }

    return numerator / denominator;
  }
  @Override
  public double integrate(final double dblBegin, final double dblEnd) throws java.lang.Exception {
    if (!org.drip.quant.common.NumberUtil.IsValid(dblBegin)
        || !org.drip.quant.common.NumberUtil.IsValid(dblEnd))
      throw new java.lang.Exception("NaturalLogSeriesElement::integrate => Invalid Inputs");

    return (java.lang.Math.pow(dblEnd, _iExponent) - java.lang.Math.pow(dblBegin, _iExponent))
        / org.drip.quant.common.NumberUtil.Factorial(_iExponent + 1);
  }
Exemplo n.º 9
0
 private double getDistanceTo(double x, double y, double z, Player player) {
   double x2 = player.getX();
   double y2 = player.getY();
   double z2 = player.getZ();
   double xResult = java.lang.Math.pow(java.lang.Math.max(x, x2) - java.lang.Math.min(x, x2), 2);
   double yResult = java.lang.Math.pow(java.lang.Math.max(y, y2) - java.lang.Math.min(y, y2), 2);
   double zResult = java.lang.Math.pow(java.lang.Math.max(z, z2) - java.lang.Math.min(z, z2), 2);
   return java.lang.Math.sqrt(xResult + yResult + zResult);
 }
  /** Calculate how repusled the scanning robot is by a nearby object. */
  public double calcObjRepulseSpeed(double robotX, double robotY, double obsX, double obsY) {
    double speed = 0.0;
    double distance = Math.sqrt(Math.pow(robotX - obsX, 2) + Math.pow(robotY - obsY, 2));

    if (distance <= OBJ_DISTANCE) {
      speed = ((OBJ_DISTANCE - distance) / OBJ_DISTANCE) * MAX_SPEED;
    }

    return speed;
  }
  @Override
  public double[] populationMetricNorm() {
    int iPNorm = outputMetricVectorSpace().pNorm();

    if (java.lang.Integer.MAX_VALUE == iPNorm) return populationSupremumNorm();

    org.drip.spaces.metric.R1Combinatorial r1CombinatorialInput =
        (org.drip.spaces.metric.R1Combinatorial) inputMetricVectorSpace();

    org.drip.measure.continuous.R1 distR1 = r1CombinatorialInput.borelSigmaMeasure();

    java.util.List<java.lang.Double> lsElem = r1CombinatorialInput.elementSpace();

    org.drip.function.definition.R1ToRd funcR1ToRd = function();

    if (null == distR1 || null == funcR1ToRd) return null;

    double dblProbabilityDensity = java.lang.Double.NaN;
    double[] adblPopulationMetricNorm = null;
    int iOutputDimension = -1;
    double dblNormalizer = 0.;

    for (double dblElement : lsElem) {
      try {
        dblProbabilityDensity = distR1.density(dblElement);
      } catch (java.lang.Exception e) {
        e.printStackTrace();

        return null;
      }

      double[] adblValue = funcR1ToRd.evaluate(dblElement);

      if (null == adblValue || 0 == (iOutputDimension = adblValue.length)) return null;

      dblNormalizer += dblProbabilityDensity;

      if (null == adblPopulationMetricNorm) {
        adblPopulationMetricNorm = new double[iOutputDimension];

        for (int i = 0; i < iOutputDimension; ++i) adblPopulationMetricNorm[i] = 0;
      }

      for (int i = 0; i < iOutputDimension; ++i)
        adblPopulationMetricNorm[i] +=
            dblProbabilityDensity * java.lang.Math.pow(java.lang.Math.abs(adblValue[i]), iPNorm);
    }

    for (int i = 0; i < iOutputDimension; ++i)
      adblPopulationMetricNorm[i] +=
          java.lang.Math.pow(adblPopulationMetricNorm[i] / dblNormalizer, 1. / iPNorm);

    return adblPopulationMetricNorm;
  }
Exemplo n.º 12
0
 public String outputBinary(int x, int bits) {
   int val = x;
   String temp = "";
   for (int i = bits; i >= 0; i--) {
     if ((val - Math.pow(2, i)) >= 0) {
       temp = temp + "1";
       val = val - (int) Math.pow(2, i);
     } else temp = temp + "0";
   }
   return temp.substring(1);
 }
  /** Linearly decay the speed of the robot when it nears the goal. */
  public double calcRobotSpeedLinear(double robotX, double robotY, double goalX, double goalY) {
    double speed = 0;

    double distance = Math.sqrt(Math.pow(robotX - goalX, 2) + Math.pow(robotY - goalY, 2));
    if (distance >= GOAL_DISTANCE) {
      speed = MAX_SPEED;
    } else {
      speed = (distance / GOAL_DISTANCE) * MAX_SPEED + 0.5;
    }

    return speed;
  }
Exemplo n.º 14
0
  public void sculptHill(int centerX, int centerY, int height, int radius, boolean additive) {
    // int maxHeight = 1;
    if (additive) m_contour[centerX][centerY] += height;
    else m_contour[centerX][centerY] = height;

    for (int x = centerX - radius; x < centerX + radius; x++) {
      for (int y = centerY - radius; y < centerY + radius; y++) {
        double distance = Math.sqrt(Math.pow(x - centerX, 2) + Math.pow(y - centerY, 2));
        if (Math.abs(radius - distance) <= 1) interpolateLine(x, y, centerX, centerY);
      }
    }
  }
  @Override
  public double variance() {
    if (varianceCache != null) return varianceCache;

    double mu = mean();
    double tmp = 0;

    double N = length();

    for (double x : values) tmp += Math.pow(x - mu, 2) / N;
    // Now add all the zeros into it
    tmp += (length() - used) * Math.pow(0 - mu, 2) / N;

    return (varianceCache = tmp);
  }
Exemplo n.º 16
0
  public ArrayList<Node> RegionQuery(Node P, ArrayList<Node> points, int eps) {
    int x2 = P.getX();
    int y2 = P.getY();
    ArrayList<Node> neighbors = new ArrayList<>();
    for (int i = 0; i < points.size(); i++) {
      Node temp = points.get(i);
      int x1 = temp.getX();
      int y1 = temp.getY();

      if ((Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2)) < Math.pow(eps, 2)) {
        neighbors.add(temp);
      }
    }
    return neighbors;
  }
  public void dataReceived(int ch1, int ch2) {
    // This application processes the computed features on the server side to summarize Physical
    // Activity and Estimate
    // Energy Expenditure

    double EE_minute = 0;

    // Process the KCAL features
    // KCAL computes a vertical activity count (ch 2) and two horizontal activity counts (ch 1 and
    // 3)

    accum_minute_V +=
        (((double) ch1) / 30.0)
            / 1024.0; // rescale the sensor data by 1024.  // note, divide by 30 because of 30
                      // samples within a sec.
    accum_minute_H += (((double) ch2) / 30.0) / 1024.0;

    // accum_minute_V = (((double)ch1)/30.0)/1024.0;
    // accum_minute_H = (((double)ch2)/30.0)/1024.0;

    counter++;

    // EEact(k) = a*H^p1 + b*V^p2
    //
    // assume:  mass(kg) = 80 kg
    // 			gender = 1 (male)
    //
    // a = (12.81 * mass(kg) + 843.22) / 1000 = 1.87
    // b = (38.90 * mass(kg) - 682.44 * gender(1=male,2=female) + 692.50)/1000 = 3.12
    // p1 = (2.66 * mass(kg) + 146.72)/1000 = 0.36
    // p2 = (-3.85 * mass(kg) + 968.28)/1000 = 0.66

    if (counter >= 60) {
      EE_minute =
          1.87 * java.lang.Math.pow(accum_minute_H, 0.36)
              + 3.12 * java.lang.Math.pow(accum_minute_V, 0.66);
      // System.out.println("Activity count per minute: " + accum_minute_V);
      accum_minute_V = 0;
      accum_minute_H = 0;
      counter = 0;

      // System.out.println("*************************************");
      // System.out.println("EE in units kcal/minute: " + EE_minute/4.184);		// the 4.184 is to
      // convert from KJ to kilocalories
      results.addElement(new Double(EE_minute / 4.184));
      // System.out.println("*************************************");
    }
  }
Exemplo n.º 18
0
  /**
   * -=========================================/ generateOutput() -- Fill R.id.numberBox with bases
   * and squares and cubes. Note that this method simply assumes that start >= end. This should
   * always be the case, as setLimits() does this error checking.
   * /==========================================-
   */
  private void generateOutput(int start, int end) {
    ViewGroup.LayoutParams widthHeightSettings =
        new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    // I will later pass this object to TableRows (as they are created) to define their width and
    // height.
    TableLayout numberContainer = (TableLayout) findViewById(R.id.numberBox);
    numberContainer.removeAllViews();
    int row, column;
    Context appContext = getApplicationContext();

    for (row = 0; row <= (end - start); row++) {
      // Create a TableRow row.
      TableRow aRow = new TableRow(appContext);
      aRow.setId(row);
      aRow.setLayoutParams(widthHeightSettings);
      numberContainer.addView(aRow);

      for (column = 0; column < 3; column++) {
        // Create a TextView column, and place it in the just-created TableRow row.
        TextView aNumber = new TextView(appContext);
        // aNumber.setLayoutParams(widthHeightSettings);
        // Fails. I don't know why.
        aNumber.setText(Double.toString(Math.pow(start + row, 1 + column)));
        ((TableRow) numberContainer.findViewById(row)).addView(aNumber);
      }
    }
  }
Exemplo n.º 19
0
    public double lockKorn() {

      double diff = _machCurrent - calculateMachCr.korn();

      if (diff > 0) _cdw = 20 * Math.pow((diff), 4);
      return _cdw;
    }
Exemplo n.º 20
0
  /**
   * Returns the power set from the given set by using a binary counter Example: S = {a,b,c} P(S) =
   * {[], [c], [b], [b, c], [a], [a, c], [a, b], [a, b, c]}
   *
   * @param set String[]
   * @return LinkedHashSet
   */
  private <T> ArrayList<ArrayList<T>> powerSet(T[] set) {

    // create the empty power set
    ArrayList<ArrayList<T>> power = new ArrayList<ArrayList<T>>();

    // get the number of elements in the set
    int elements = set.length;

    // the number of members of a power set is 2^n
    int powerElements = (int) Math.pow(2, elements);

    // run a binary counter for the number of power elements
    for (int i = 0; i < powerElements; i++) {

      // convert the binary number to a string containing n digits
      String binary = intToBinary(i, elements);

      // create a new set
      ArrayList<T> innerSet = new ArrayList<T>();

      // convert each digit in the current binary number to the corresponding element
      // in the given set
      for (int j = 0; j < binary.length(); j++) {
        if (binary.charAt(j) == '1') innerSet.add(set[j]);
      }

      // add the new set to the power set
      power.add(innerSet);
    }

    return power;
  }
Exemplo n.º 21
0
  @Override
  public void evaluate(Solution solution) {
    double[] theta = new double[numberOfObjectives - 1];

    double[] f = new double[numberOfObjectives];
    double[] x = EncodingUtils.getReal(solution);

    int k = numberOfVariables - numberOfObjectives + 1;

    double g = 0.0;
    for (int i = numberOfVariables - k; i < numberOfVariables; i++) {
      g += java.lang.Math.pow(x[i], 0.1);
    }

    double t = java.lang.Math.PI / (4.0 * (1.0 + g));
    theta[0] = x[0] * java.lang.Math.PI / 2;
    for (int i = 1; i < (numberOfObjectives - 1); i++) {
      theta[i] = t * (1.0 + 2.0 * g * x[i]);
    }

    for (int i = 0; i < numberOfObjectives; i++) {
      f[i] = 1.0 + g;

      for (int j = 0; j < numberOfObjectives - (i + 1); j++) {
        f[i] *= java.lang.Math.cos(theta[j]);
      }
      if (i != 0) {
        int aux = numberOfObjectives - (i + 1);
        f[i] *= java.lang.Math.sin(theta[aux]);
      }

      solution.setObjective(i, f[i]);
    }
  }
Exemplo n.º 22
0
 public static List<Double> root(List<Double> data, double root) {
   List<Double> t_list = new ArrayList<Double>(data.size());
   for (double i : data) {
     t_list.add(Math.pow(i, 1.0 / root));
   }
   return t_list;
 }
Exemplo n.º 23
0
  /**
   * Evaluates a solution
   *
   * @param solution The solution to evaluate
   * @throws JMException
   */
  public void evaluate(Solution solution) throws JMException {
    DecisionVariables gen = solution.getDecisionVariables();

    double[] x = new double[numberOfVariables_];
    double[] f = new double[numberOfObjectives_];
    double[] theta = new double[numberOfObjectives_ - 1];
    int k = numberOfVariables_ - numberOfObjectives_ + 1;

    for (int i = 0; i < numberOfVariables_; i++) x[i] = gen.variables_[i].getValue();

    double g = 0.0;
    for (int i = numberOfVariables_ - k; i < numberOfVariables_; i++)
      g += java.lang.Math.pow(x[i], 0.1);

    double t = java.lang.Math.PI / (4.0 * (1.0 + g));
    theta[0] = x[0] * java.lang.Math.PI / 2;
    for (int i = 1; i < (numberOfObjectives_ - 1); i++) theta[i] = t * (1.0 + 2.0 * g * x[i]);

    for (int i = 0; i < numberOfObjectives_; i++) f[i] = 1.0 + g;

    for (int i = 0; i < numberOfObjectives_; i++) {
      for (int j = 0; j < numberOfObjectives_ - (i + 1); j++) f[i] *= java.lang.Math.cos(theta[j]);
      if (i != 0) {
        int aux = numberOfObjectives_ - (i + 1);
        f[i] *= java.lang.Math.sin(theta[aux]);
      } // if
    } // for

    for (int i = 0; i < numberOfObjectives_; i++) solution.setObjective(i, f[i]);
  } // evaluate
Exemplo n.º 24
0
 public Complex powi(float p) {
   float magnew = (float) Math.pow((double) abs(), (double) p);
   float phasenew = arg() * p;
   re = magnew * (float) Math.cos(phasenew);
   im = magnew * (float) Math.sin(phasenew);
   return this;
 }
Exemplo n.º 25
0
  @Override
  public double empiricalLoss(
      final org.drip.function.definition.RdToR1 funcLearnerRdToR1,
      final org.drip.spaces.instance.GeneralizedValidatedVector gvviX,
      final org.drip.spaces.instance.GeneralizedValidatedVector gvviY)
      throws java.lang.Exception {
    if (null == funcLearnerRdToR1
        || null == gvviX
        || !(gvviX instanceof org.drip.spaces.instance.ValidatedRd)
        || null == gvviY
        || !(gvviY instanceof org.drip.spaces.instance.ValidatedR1))
      throw new java.lang.Exception("LpLossLearner::empiricalLoss => Invalid Inputs");

    double[][] aadblX = ((org.drip.spaces.instance.ValidatedRd) gvviX).instance();

    double[] adblY = ((org.drip.spaces.instance.ValidatedR1) gvviY).instance();

    double dblEmpiricalLoss = 0.;
    int iNumSample = aadblX.length;

    if (iNumSample != adblY.length)
      throw new java.lang.Exception("LpLossLearner::empiricalLoss => Invalid Inputs");

    for (int i = 0; i < iNumSample; ++i)
      dblEmpiricalLoss +=
          java.lang.Math.pow(
              java.lang.Math.abs(funcLearnerRdToR1.evaluate(aadblX[i]) - adblY[i]),
              _dblLossExponent);

    return dblEmpiricalLoss / _dblLossExponent;
  }
Exemplo n.º 26
0
  /**
   * Calculates a standardized normal distributed value (using the polar method).
   *
   * @return The value
   */
  public double standNormalDistrDouble() {

    double q = Double.MAX_VALUE;
    double u1 = 0;
    double u2;

    while (q >= 1d || q == 0) {
      u1 = randomDouble();
      u2 = randomDouble();

      q = java.lang.Math.pow(u1, 2) + java.lang.Math.pow(u2, 2);
    }

    double p = java.lang.Math.sqrt((-2d * (java.lang.Math.log(q))) / q);
    return u1 * p; // or u2 * p
  }
Exemplo n.º 27
0
  public void grav() {

    double maga = 10 / Math.pow(distPts2D(x, y, xcp, ycp), 2);
    xa = (xcp - x) * maga;
    ya = (ycp - y) * maga;

    find();
  }
Exemplo n.º 28
0
 /**
  * The page table constructor. Must call
  *
  * <p>super(ownerTask)
  *
  * <p>as its first statement. @OSPProject Memory
  */
 public PageTable(TaskCB ownerTask) {
   super(ownerTask);
   size = (int) Math.pow(2, MMU.getPageAddressBits());
   pages = new PageTableEntry[size];
   for (int i = 0; i < size; i++) {
     pages[i] = new PageTableEntry(this, i);
   }
 }
Exemplo n.º 29
0
    public double perkinsAndHage() {

      // Difference between current mach number and critical mach number
      double diff = _machCurrent - calculateMachCr.perkinsAndHage();

      if (diff > 0) _cdw = 9.5 * Math.pow((diff), 2.8) + 0.00193;
      return _cdw;
    }
 @Override
 public double calcDerivative(final double dblVariate, final int iOrder)
     throws java.lang.Exception {
   return iOrder > _iExponent
       ? 0.
       : java.lang.Math.pow(dblVariate, _iExponent - iOrder)
           / org.drip.quant.common.NumberUtil.Factorial(_iExponent - iOrder);
 }