Exemple #1
0
 public static void main(String argv[]) {
   System.out.println("Alas");
   int max = 100;
   double averate = 0;
   double avedelta = 0;
   for (int i = 0; i < max; i++) {
     Color c1 = GetRandomColor();
     Color c2 = GetRandomColor();
     double Altitude1 = GetAltitude(c1);
     double Altitude2 = GetAltitude(c2);
     double dealtaltitude = Math.abs(Altitude1 - Altitude2);
     double dist = ColorDistCalculator.CalDist(c1, c2);
     double rate = Math.abs((dealtaltitude - dist) / dist);
     double delta = Math.abs((dealtaltitude - dist));
     averate += rate;
     avedelta += delta;
     System.out.println(
         c1.toHexString()
             + " "
             + c2.toHexString()
             + " "
             + Altitude1
             + " "
             + Altitude2
             + " "
             + dist);
   }
   averate /= max;
   avedelta /= max;
   System.out.println("Final " + averate + " " + avedelta);
 }
Exemple #2
0
  public GenericGame test31() {
    GameQuick wo = new GameQuick(10, 10);
    wo.setBPrint(true);

    int heteroTask = 10;
    int heteroMachine = 10;

    int[] iaLength = new int[wo.getIClass()];
    for (int j = 0; j < wo.getIClass(); j++) {
      iaLength[j] = 1000 + (int) (Math.random() * 1000);
    }
    wo.setIaTask(iaLength);

    int[] iaCPU = new int[wo.getISite()];
    for (int j = 0; j < wo.getISite(); j++) {
      iaCPU[j] = 16 + (int) (Math.random() * 32);
    }
    wo.setIaCPU(iaCPU);

    double tmpPrediciton;

    double[][] dmPrediction = new double[wo.getIClass()][wo.getISite()];
    for (int i = 0; i < wo.getIClass(); i++) {
      tmpPrediciton = 10 + Math.random() * heteroTask * 10;
      for (int j = 0; j < wo.getISite(); j++) {
        dmPrediction[i][j] = tmpPrediciton * (0.5 + Math.random());
      }
    }
    wo.setDmPrediction(dmPrediction);
    wo.schedule();
    wo.printExeTimesForEachClass();

    return wo;
  }
  public void valueIteration(int i) {
    double delta;
    double v;
    List<State> states = mdp.getStates();
    State win = states.get(3);
    State death = states.get(7);
    State blocked = states.get(5);
    states.remove(win);
    states.remove(death);
    states.remove(blocked);
    while (true) {
      delta = 0;
      for (State s : states) {
        v = values.get(s);
        double vdelta = maxa(s);
        values.put(s, vdelta);
        //				System.out.println("vdelta: "+vdelta);
        delta = Math.max(delta, Math.abs(v - vdelta));
        //				System.out.println("delta:" + delta);

      }
      if (delta < EPS || i == 0) {
        //				values.put(mdp.getStates().get(3), 0.00);
        //				values.put(mdp.getStates().get(7), 0.00);
        return;
      }
      i--;
    }
  }
  @Test
  public void testClasserParAire() throws Exception {

    ListeFormes listeFormes = new ListeFormes();
    ClasseurFormes classeur;

    listeFormes.ajouterForme(new Rectangle(10, 20, 30, 50, 13)); // carre
    listeFormes.ajouterForme(new Ovale(10, 20, 30, 14)); // cercle a=3600
    listeFormes.ajouterForme(new Ligne(10, 20, 30, 40, 45)); // ligne
    listeFormes.ajouterForme(new Rectangle(30, 20, 50, 90, 12)); // rectangle
    listeFormes.ajouterForme(new Ovale(40, 30, 10, 20, 34)); // ovale a=800

    classeur = new ClasseurFormes(listeFormes);
    listeFormes = classeur.getListeFormes();

    classeur.classerParAire(true);
    listeFormes.premiere();
    assert listeFormes.getNoeudCourant().getForme().getAire() == (int) Math.sqrt(20 * 20 + 20 * 20)
        && listeFormes.getNoeudCourant().getForme().getNumeroSequence() == 45;
    listeFormes.suivant();
    assert listeFormes.getNoeudCourant().getForme().getAire() == 600
        && listeFormes.getNoeudCourant().getForme().getNumeroSequence() == 13;
    listeFormes.suivant();
    assert listeFormes.getNoeudCourant().getForme().getAire() == (int) (Math.PI * 20 * 10)
        && listeFormes.getNoeudCourant().getForme().getNumeroSequence() == 34;
    listeFormes.suivant();
    assert listeFormes.getNoeudCourant().getForme().getAire() == 1400
        && listeFormes.getNoeudCourant().getForme().getNumeroSequence() == 12;
    listeFormes.suivant();
    assert listeFormes.getNoeudCourant().getForme().getAire() == (int) (Math.PI * 30 * 30)
        && listeFormes.getNoeudCourant().getForme().getNumeroSequence() == 14;

    classeur.classerParAire(false);
    listeFormes.premiere();
    assert listeFormes.getNoeudCourant().getForme().getAire() == (int) (Math.PI * 30 * 30)
        && listeFormes.getNoeudCourant().getForme().getNumeroSequence() == 14;
    listeFormes.suivant();
    assert listeFormes.getNoeudCourant().getForme().getAire() == 1400
        && listeFormes.getNoeudCourant().getForme().getNumeroSequence() == 12;
    listeFormes.suivant();
    assert listeFormes.getNoeudCourant().getForme().getAire() == (int) (Math.PI * 20 * 10)
        && listeFormes.getNoeudCourant().getForme().getNumeroSequence() == 34;
    listeFormes.suivant();
    assert listeFormes.getNoeudCourant().getForme().getAire() == 600
        && listeFormes.getNoeudCourant().getForme().getNumeroSequence() == 13;
    listeFormes.suivant();
    assert listeFormes.getNoeudCourant().getForme().getAire() == (int) Math.sqrt(20 * 20 + 20 * 20)
        && listeFormes.getNoeudCourant().getForme().getNumeroSequence() == 45;
  }
Exemple #5
0
 private int findGCDInTwoNumber(int a, int b) {
   if (a == 0 || b == 0) {
     return 0;
   }
   if (a == 1 || b == 1) {
     return 1;
   }
   int max = Math.max(a, b);
   int min = Math.min(a, b);
   if (max % min != 0) {
     return findGCDInTwoNumber(min, max % min);
   } else {
     return min;
   }
 }
  public static void process(TreeMap<Double, Double> points, double threshold) {
    double[] sol;
    while (true) {
      sol = solve(points);

      double maxError = 0;
      double maxErrorPoint = 0;
      for (double s : points.keySet()) {
        double dist = Math.abs(s * sol[0] + sol[1] - points.get(s));
        if (dist > maxError) {
          maxError = dist;
          maxErrorPoint = s;
        }
      }

      points.remove(maxErrorPoint);
      if (maxError < threshold) break;
    }

    System.out.println(
        "solution: "
            + sol[0]
            + ", "
            + sol[1]
            + "; fix: "
            + sol[1] / (1 - sol[0])
            + "; "
            + points.size()
            + " points");
  }
Exemple #7
0
 public double robinRound(int[] arrival, int[] execute, int q) {
   if (arrival == null || arrival.length == 0 || execute == null || execute.length == 0) {
     return 0.0;
   }
   int currentTime = 0;
   int waitingTime = 0;
   int nextProIndex = 0;
   Queue<Process> queue = new LinkedList<Process>();
   while (!queue.isEmpty() || nextProIndex < arrival.length) {
     if (!queue.isEmpty()) {
       Process cur = queue.poll();
       waitingTime += currentTime - cur.arri;
       currentTime += Math.min(cur.exe, q);
       for (int i = nextProIndex; i < arrival.length; i++) {
         if (arrival[i] <= currentTime) {
           queue.offer(new Process(arrival[i], execute[i]));
           nextProIndex++;
         } else {
           break;
         }
       }
       if (cur.exe > q) {
         queue.offer(new Process(currentTime, cur.exe - q));
       }
     } else {
       queue.offer(new Process(arrival[nextProIndex], execute[nextProIndex]));
       currentTime += arrival[nextProIndex++];
     }
   }
   return (double) waitingTime / arrival.length;
 }
Exemple #8
0
 public static void main(String[] args) {
   double result;
   Scanner in = new Scanner(System.in);
   System.out.println("¬ведите число");
   int number = in.nextInt();
   result = Math.log(Factorial(number));
   System.out.print(result);
 }
 private void searchFor(int n, Searcher searcher) throws Exception {
   System.out.println("Searching for " + n);
   Hits hits = searcher.search(QueryParser.parse(intToEnglish(n), "contents", ANALYZER));
   System.out.println("Search for " + n + ": total=" + hits.length());
   for (int j = 0; j < Math.min(3, hits.length()); j++) {
     System.out.println("Hit for " + n + ": " + hits.doc(j).get("id"));
   }
 }
  private static void paint(final Graphics2D g2d, final float size) {

    final Path2D.Float path = new Path2D.Float();

    for (float angle = 1f / 3f; angle <= 90f; angle += 1f) {
      double angRad = Math.toRadians(angle);

      double cos = Math.cos(angRad);
      double sin = Math.sin(angRad);

      path.reset();

      path.moveTo(5f * cos, 5f * sin);
      path.lineTo(size * cos, size * sin);

      g2d.draw(path);
    }
  }
Exemple #11
0
  public GenericGame test3consistent() {
    GameQuick wo = new GameQuick(10, 10);
    wo.setBPrint(true);

    //		int heteroTask = 1000;//HiHi
    //		int heteroMachine = 100;

    int heteroTask = 1000; // HiLo
    int heteroMachine = 10;

    //		int heteroTask = 10;//LoHi
    //		int heteroMachine = 100;

    //		int heteroTask = 10;//LoLo
    //		int heteroMachine = 10;

    int[] iaLength = new int[wo.getIClass()];
    for (int j = 0; j < wo.getIClass(); j++) {
      iaLength[j] = 1000 + (int) (Math.random() * 1000);
    }
    wo.setIaTask(iaLength);

    int[] iaCPU = new int[wo.getISite()];
    double[] iaSpeedCPU = new double[wo.getISite()];
    for (int j = 0; j < wo.getISite(); j++) {
      iaCPU[j] = 16 + (int) (Math.random() * 16);
      iaSpeedCPU[j] = Math.random() * heteroMachine + 1;
    }
    wo.setIaCPU(iaCPU);

    double tmpPrediciton;

    double[][] dmPrediction = new double[wo.getIClass()][wo.getISite()];
    for (int i = 0; i < wo.getIClass(); i++) {
      tmpPrediciton = 10 + Math.random() * heteroTask * 10;
      for (int j = 0; j < wo.getISite(); j++) {
        dmPrediction[i][j] = tmpPrediciton * iaSpeedCPU[j] * (0.5 + Math.random());
      }
    }
    wo.setDmPrediction(dmPrediction);

    return wo;
  }
Exemple #12
0
 public int findMinPath(TreeNode root) {
   if (root == null) {
     return 0;
   } else if (root.left == null) {
     return findMinPath(root.right) + root.val;
   } else if (root.right == null) {
     return findMinPath(root.left) + root.val;
   } else {
     return Math.min(findMinPath(root.left), findMinPath(root.right)) + root.val;
   }
 }
  private void reportLatencies() {
    LOG.debug("Reporting latency for each message:");
    int validEntries = 0;
    long totalTime = 0, min = Long.MAX_VALUE, max = 0;
    for (int i = 0; i < TEST_ITERATIONS; i++) {
      long latencyNanos = endTimeNanos[i] - startTimeNanos[i];
      long time = TimeUnit.NANOSECONDS.toMicros(latencyNanos);
      if (time > 0) {
        totalTime += time;
        validEntries++;
        min = Math.min(min, time);
        max = Math.max(max, time);
      }
      LOG.debug("message #" + i + ": latency= " + time + " micros.");
    }

    String report =
        "%s iterations - latency in micros to send %s bytes via TCP/IP: avg=%s, min=%s, max=%s";
    LOG.info(
        String.format(report, TEST_ITERATIONS, payload.length, totalTime / validEntries, min, max));
  }
  public void xtestAging() {
    ProphetRouter r4 = (ProphetRouter) h4.getRouter();
    ProphetRouter r5 = (ProphetRouter) h5.getRouter();

    h4.connect(h5);
    assertEquals(ProphetRouter.P_INIT, r4.getPredFor(h5));
    assertEquals(ProphetRouter.P_INIT, r5.getPredFor(h4));

    disconnect(h5);

    clock.advance(SECONDS_IN_TIME_UNIT * 2);
    double newPred = ProphetRouter.P_INIT * Math.pow(ProphetRouter.GAMMA, 2);

    assertEquals(newPred, r4.getPredFor(h5));
    assertEquals(newPred, r5.getPredFor(h4));

    clock.advance(SECONDS_IN_TIME_UNIT / 10);
    newPred = newPred * Math.pow(ProphetRouter.GAMMA, 1.0 / 10);

    assertEquals(newPred, r4.getPredFor(h5));
    assertEquals(newPred, r5.getPredFor(h4));
  }
Exemple #15
0
 private void maxWidthTreeHelper(
     TreeNode node, int depth, Map<Integer, Integer> map, ResultType rt) {
   for (TreeNode next : node.nextList) {
     if (next == null) {
       continue;
     } else {
       if (map.containsKey(depth)) {
         map.put(depth, map.get(depth) + 1);
       } else {
         map.put(depth, 1);
       }
       rt.max = Math.max(rt.max, map.get(depth));
       maxWidthTreeHelper(next, depth + 1, map, rt);
     }
   }
 }
 private void loop() {
   speed = Math.min(speed + DELAY / 1000d / RAMP, targetPower);
   if (Math.abs(angleError(targetAngle)) > Math.PI) System.out.println(angleError(targetAngle));
   if (Math.abs(angleError(targetAngle)) > Math.PI / 2) {
     steer.set(
         Math.max(
             -STEER_CAP, Math.min(STEER_CAP, STEER_GAIN * angleError(targetAngle - Math.PI))));
     drive.set(-speed);
   } else {
     steer.set(Math.max(-STEER_CAP, Math.min(STEER_CAP, STEER_GAIN * angleError(targetAngle))));
     drive.set(speed);
   }
   if (driveEncoder != null) {
     double enc = driveEncoder.get();
     double angle = getAngle() - gyro.getAngle() * Math.PI / 180 + Math.PI / 2;
     x += (enc - encLast) * Math.cos(angle);
     y += (enc - encLast) * Math.sin(angle);
     encLast = enc;
   }
   try {
     Thread.sleep(DELAY);
   } catch (Exception e) {
   }
 }
Exemple #17
0
 public static void main(String[] args) {
   Scanner scanner = new Scanner(System.in);
   ArrayList<Integer> srcList = new ArrayList<Integer>();
   ArrayList<Integer> countList = new ArrayList<Integer>();
   while (scanner.hasNextInt()) {
     int src = scanner.nextInt();
     int count = scanner.nextInt();
     srcList.add(src);
     countList.add(count);
   }
   DecimalFormat format = new DecimalFormat(".00");
   for (int i = 0; i < srcList.size(); i++) {
     double srcDbl = srcList.get(i);
     double sum = 0;
     for (int j = 0; j < countList.get(i); j++) {
       sum += srcDbl;
       srcDbl = Math.sqrt(srcDbl);
     }
     System.out.println(format.format(sum));
   }
   scanner.close();
 }
Exemple #18
0
  public void test3_1() {
    GameQuick wo = new GameQuick(2, 2);
    wo.setBPrint(true);

    int[] iaLength = {100, 100};
    wo.setIaTask(iaLength);

    int[] iaCPU = {10, 10};
    wo.setIaCPU(iaCPU);

    double[][] dmPrediction = {
      {24, 30},
      {21, 15}
    };
    wo.setDmPrediction(dmPrediction);

    double[] daPrice = new double[2];
    for (int j = 0; j < 2; j++) {
      daPrice[j] = 1 + Math.random() * 10;
    }
    wo.setDaPrice(daPrice);

    wo.schedule();
  }
Exemple #19
0
 public static Color GetRandomColor() {
   int r = (int) (Math.random() * 255);
   int g = (int) (Math.random() * 255);
   int b = (int) (Math.random() * 255);
   return new Color(r, g, b);
 }