Exemple #1
0
  /* play the game */
  private void play() {
    t0 = System.currentTimeMillis(); /*Times the beginning of the game */
    currentTime = System.currentTimeMillis() - t0;
    generateRandomOrderTimes();
    System.out.println("next t1 is in " + orderTimes.get(0));
    System.out.println("next t2 is in " + orderTimes.get(1));
    System.out.println("next t3 is in " + orderTimes.get(2));

    while (System.currentTimeMillis() - t0 < ENDOFGAMETIME) {
      pause(100);
      /*Update the days label*/
      daysElapsed = (int) ((System.currentTimeMillis() - t0) / (LENGTHOFADAY * 1000));
      daysElapsedLabel.setLabel("DAYS " + daysElapsed);
      scanEquipments();
    }
  }
Exemple #2
0
 public void onTick() {
   long currentTime = System.currentTimeMillis();
   long timeDiff = currentTime - lastTickTime;
   millisSinceLastDragonFrameChange += timeDiff;
   if (millisSinceLastDragonFrameChange > 500) {
     cycleDragonFrame();
     millisSinceLastDragonFrameChange = 0;
   }
   millisSinceLastFlagFrameChange += timeDiff;
   if (millisSinceLastFlagFrameChange > 500) {
     cycleFlagFrame();
     millisSinceLastFlagFrameChange = 0;
   }
   lastTickTime = currentTime;
   try {
     getWorld().onTick();
   } catch (SQLException exception) {
     client
         .getLogger()
         .log(
             WARNING,
             "Failed to update unit in database (but this is the client, so it should be fine)",
             exception);
   }
   repaint();
 }
Exemple #3
0
 /*setup the game */
 private void setup() {
   t0 = System.currentTimeMillis(); /*Times the beginning of the game */
   // int size;
   Scanner in = new Scanner(System.in);
   // System.out.println("Enter the size of available");
   // size = in.nextInt();
   initiateScores();
   placeWalls();
   placeStates();
   placeLabels();
   randomizeOrderRent(); /*randomizes the order of the equipment for the On Rent state */
   System.out.println(
       "How many High Runners at price $" + EQUIPMENTCOSTS[0] + " do you want to buy?");
   int type1Equip = in.nextInt();
   System.out.println(
       "How many Medium Runners at price $" + EQUIPMENTCOSTS[1] + " do you want to buy?");
   int type2Equip = in.nextInt();
   System.out.println(
       "How many Low Runners at price $" + EQUIPMENTCOSTS[2] + " do you want to buy?");
   int type3Equip = in.nextInt();
   capitalInvested =
       EQUIPMENTCOSTS[0] * type1Equip
           + EQUIPMENTCOSTS[1] * type2Equip
           + EQUIPMENTCOSTS[2] * type3Equip;
   capitalLabel.setLabel("Capital Invested: $" + capitalInvested);
   fillAvailable(type1Equip, type2Equip, type3Equip); // fills with the proper number of equipment
   // fillStates(size,INITRENT,INITSHOP);
   placeEquipments();
 }
 void run() throws Exception {
   in = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
   out = new PrintWriter(System.out);
   long s = System.currentTimeMillis();
   solve();
   out.flush();
   //		pr(System.currentTimeMillis() - s + "ms");
 }
Exemple #5
0
  public static void main(String[] args) {

    double[] sigma = {3E-4, 1E-6, 2.4E-2, 6.67E-5, 1E-6, 6E-3, 4.4E-4, 1E-2, 1E-6};
    double[] mu = {0.42E9, 2.27};
    double alpha = 0.0;
    Sampler sampler = new Sampler(mu, sigma, alpha);
    double[][] M = sampler.getCovMat();
    System.out.println("The Covirance Matrix is:");
    sampler.printMatrix(M);

    double[][] A = sampler.getCholDecompA();
    System.out.println("The decomposed Matrix A is");
    sampler.printMatrix(A);

    try {

      File file = new File("/Users/Weizheng/Documents/JavaWorkPlace/CLS_MCIntegrator/output.txt");
      // if file doesnt exists, then create it
      if (!file.exists()) {
        file.createNewFile();
      } else {
        file.delete();
        file.createNewFile();
      }

      PrintWriter fw = new PrintWriter(file);

      long startTime = System.currentTimeMillis();

      for (int i = 0; i < 1E3; i++) {
        double[] MultiNormalVector = sampler.nextMultiNormalVector();
        //				for(double a:MultiNormalVector ){
        //					fw.printf("%4.2e ", a);
        //				}
        //				fw.println("");
      }
      fw.close();
      long endTime = System.currentTimeMillis();
      System.out.println("That took " + (endTime - startTime) + " milliseconds");

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  public static void main(String[] args) {
    String sourceFile = args[0];
    int m = Integer.parseInt(args[1]);

    String format = sourceFile.substring(1 + sourceFile.lastIndexOf('.'));
    String targetBase = sourceFile.substring(0, sourceFile.lastIndexOf('.'));
    String targetFile0 = targetBase + "_" + m + "_0." + format;
    String targetFile1 = targetBase + "_" + m + "_1." + format;
    String targetFile2 = targetBase + "_" + m + "_2." + format;

    KernelJAI gaussKernel = createGaussianKernel(m);
    dumpKernelData("Gaussian", gaussKernel);
    KernelJAI distKernel = createDistancesKernel(m);
    dumpKernelData("Distances", distKernel);

    RenderedOp source = FileLoadDescriptor.create(sourceFile, null, true, null);
    source = BandSelectDescriptor.create(source, new int[1], null);
    System.out.println("Writing " + targetFile0);
    FileStoreDescriptor.create(source, targetFile0, format, null, false, null);

    long t1 = System.currentTimeMillis();
    System.out.println("Computing " + targetFile1);
    BorderExtender borderExtender = BorderExtender.createInstance(BorderExtender.BORDER_COPY);
    RenderedOp target1 =
        ConvolveDescriptor.create(
            source, gaussKernel, new RenderingHints(JAI.KEY_BORDER_EXTENDER, borderExtender));
    RenderedOp mask = ClampDescriptor.create(source, new double[] {0}, new double[] {1}, null);
    mask = MultiplyConstDescriptor.create(mask, new double[] {255}, null);
    mask = NotDescriptor.create(mask, null);
    mask = ClampDescriptor.create(mask, new double[] {0}, new double[] {1}, null);
    target1 = MultiplyDescriptor.create(target1, mask, null);
    target1 = AddDescriptor.create(target1, source, null);
    System.out.println("Writing " + targetFile1);
    FileStoreDescriptor.create(target1, targetFile1, format, null, false, null);
    System.out.println("Done in " + (System.currentTimeMillis() - t1) + " ms");

    long t2 = System.currentTimeMillis();
    System.out.println("Computing " + targetFile2);
    BufferedImage target2 = convolveICOL(source, gaussKernel, distKernel);
    System.out.println("Writing " + targetFile2);
    FileStoreDescriptor.create(target2, targetFile2, format, null, false, null);
    System.out.println("Done in " + (System.currentTimeMillis() - t2) + " ms");
  }
Exemple #7
0
  /*compute the initial Time outs for all equipments */
  private void computeInitialTimes() {
    System.out.println("t0 is" + t0);
    double timeDiff = 0;

    /*Times for available equipment will depend on the type and position in queue as it's FIFO */
    generateRandomOrderTimes();

    /*for equipment on rent, it's simply at the end of the rental period */
    for (Equipment e : rentEquipment) {
      e.timeIn = System.currentTimeMillis() - t0;
      timeDiff = nextRandomTime(RENTALFREQUENCY[e.type - 1]);
      e.timeOut = System.currentTimeMillis() + timeDiff - t0;
      System.out.println("I am in state " + e.state + " and order " + rentEquipment.indexOf(e));
      System.out.println(
          "My time in is "
              + e.timeIn
              + " And my time out is "
              + e.timeOut
              + " and the difference between the 2 is "
              + (e.timeOut - e.timeIn));
      /*System.out.println("and nextRandomTime returns" + nextRandomTime(e.timeIn,RENTALFREQUENCY[e.type-1]) +" for type "+e.type+" for RentalFrequency "+RENTALFREQUENCY[e.type-1]);
      System.out.println("For equipment in state "+e.state+" and order "+rentEquipment.indexOf(e)+" : inter-arrival is "+(e.timeOut-e.timeIn)); */
    }

    /*for equipment in the shop, it's simply at the end of the rental period */
    for (Equipment e : shopEquipment) {
      e.timeIn = System.currentTimeMillis() - t0;
      timeDiff = nextRandomTime(SHOPFREQUENCY[e.type - 1]);
      e.timeOut = System.currentTimeMillis() + timeDiff - t0;
      System.out.println("I am in state " + e.state + " and order " + shopEquipment.indexOf(e));
      System.out.println(
          "My time in is "
              + e.timeIn
              + " And my time out is "
              + e.timeOut
              + " and the difference between the 2 is "
              + (e.timeOut - e.timeIn));
      /*System.out.println("For equipment in state "+e.state+" and order "+shopEquipment.indexOf(e)+" : inter-arrival is "+(e.timeOut-e.timeIn)); */
    }
  }
Exemple #8
0
  /*Scans the particular equipment and decides whether or not it shoud be moved */
  private int scan(Equipment equip) {
    double currentTime = System.currentTimeMillis() - t0;
    int orderMoved = -1;
    int or = -1;
    switch (equip.state) {
      case 1:
        or = availEquipment.indexOf(equip);
        break;
      case 2:
        or = rentEquipment.indexOf(equip);
        break;
      case 3:
        or = shopEquipment.indexOf(equip);
        break;
    }
    // System.out.println("Now looking at state "+equip.state+" and type "+equip.type+" and order"
    // +or);
    // System.out.println("Current time is "+currentTime+" while equip.timeOut time is
    // "+equip.timeOut+" and the difference is "+(equip.timeOut-currentTime));

    switch (equip.state) {
      case 1:
        break;
      case 2:
        if (equip.timeOut < System.currentTimeMillis() - t0) {
          orderMoved = rentEquipment.indexOf(equip);
          return orderMoved;
        }
        break;
      case 3:
        if (equip.timeOut < System.currentTimeMillis() - t0) {
          orderMoved = shopEquipment.indexOf(equip);
          // System.out.println("I have just moved equipment from state 3 "+equip.state+" of type
          // "+equip.type+" and of order "+shopEquipment.indexOf(equip));
          return orderMoved;
        }
        break;
    }
    return orderMoved;
  }
 public CA3(float SCSymmetricDivBalance, int maxDivisions, float densityV) {
   int time = (int) System.currentTimeMillis();
   random = new MersenneTwisterFast(5); // choose a seed a number or 'time'
   asymmetricRatio = SCSymmetricDivBalance;
   maxProDivisions = maxDivisions;
   maxMatureCellAge = 1;
   // densityVasculature=densityV;
   carriedGenome = new StringBuilder[size][size];
   // tree = new Hashtable<Integer,Integer>();
   // timeTree = new Hashtable<Integer, Integer>();
   reset();
   // resetVasculature();
 }
Exemple #10
0
  /* Generates random order times for the orders of each type in the Available state */
  private void generateRandomOrderTimes() {
    orderTime1 = System.currentTimeMillis() + nextRandomTime(ARRIVAL1) - t0;
    orderTime2 = System.currentTimeMillis() + nextRandomTime(ARRIVAL2) - t0;
    orderTime3 = System.currentTimeMillis() + nextRandomTime(ARRIVAL2) - t0;

    double[] arrivalFrequencies = {ARRIVAL1, ARRIVAL2, ARRIVAL3};
    orderTimes.add(System.currentTimeMillis() + nextRandomTime(ARRIVAL1) - t0);
    orderTimes.add(System.currentTimeMillis() + nextRandomTime(ARRIVAL2) - t0);
    orderTimes.add(System.currentTimeMillis() + nextRandomTime(ARRIVAL3) - t0);

    /*for (double counter : arrivalFrequencies) {
    	System.out.println("t0 is = "+t0+" and current time is "+currentTime);
    	currentTime=System.currentTimeMillis()-t0;
    	orderTimes.add(System.currentTimeMillis()+nextRandomTime(counter)-t0);
    }*/
  }
 void run() throws Exception {
   long s = System.currentTimeMillis();
   solve();
   out.flush();
   pr(System.currentTimeMillis() - s + "ms");
 }
Exemple #12
0
 private static long nextOccurence(long period) {
   return (System.currentTimeMillis() - startTime) % period;
 }
Exemple #13
0
 private static String time() {
   float now = (float) (System.currentTimeMillis() - startTime) / 1000;
   return TIME_FORMAT.format(now);
 }
Exemple #14
0
/**
 * A probe for measuring performance. This class defines several basic probes: {@link SimpleCounter
 * SimpleCounter}, {@link ValueRecorder ValueRecorder}, {@link TimeRecorder TimeRecorder} and {@link
 * VariableSampler VariableSampler}.
 *
 * <p>A probe is identified by a name. The information it collects is automatically output by
 * calling the {@link #toString()} method when the JVM terminates. In addition, this information may
 * be output periodically during the execution of the JVM, as specified by the {@link
 * #setMonitoring} method. The output is by default the standard output of the JVM. It can be
 * redirected to a file through the {@link #setOutput(String)} method.
 *
 * @author J-M. Busca INRIA/Regal
 */
public abstract class PerformanceProbe {

  //
  // CONSTANTS
  //
  private static final long NOT_PERIODIC = -1;
  private static final long ALL_RECORDERS = -2;
  private static final DecimalFormat TIME_FORMAT = new DecimalFormat("000.000");

  //
  // CLASS FIELDS
  //
  @SuppressWarnings("unused")
  private static Thread probeShutdown = new ProbeShutdown();

  private static Timer probeTimer = new Timer("Probe timer", true);
  private static HashMap<Long, Collection<PerformanceProbe>> _perfProbes =
      new HashMap<Long, Collection<PerformanceProbe>>();

  private static long startTime = System.currentTimeMillis();
  private static PrintStream outputStream = System.out;

  //
  // OBJECT FIELDS
  //
  // probe parameters
  private String probeName;
  private long outputPeriod;
  private boolean doReset;

  //
  // CONSTRUCTORS
  //
  /**
   * Creates a simple probe with the specified name.
   *
   * @param name the name of the probe.
   */
  public PerformanceProbe(String name) {
    this.probeName = name;
    this.outputPeriod = NOT_PERIODIC;
    this.doReset = false;
    add();
  }

  /** @inheritDoc */
  public String toString() {
    return "PerfsProbe";
  }

  //
  // OUTPUT CONTROL
  //
  /**
   * Changes the output of all probes to the specified file. This method may be called multiple
   * times during the execution of the JVM.
   *
   * @param pathname the pathname of the output file.
   */
  public static void setOutput(String pathname) {

    // open specified file
    PrintStream previous = outputStream;
    try {
      outputStream = new PrintStream(new FileOutputStream(pathname));
    } catch (FileNotFoundException e) {
      // if file cannot be opened, output remains unchanged
      return;
    }

    // close previous output if not standard output
    if (previous != null && !previous.equals(System.out)) {
      previous.close();
    }

    // print date and time
    Format format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    outputStream.println("Starting on " + format.format(new Date()));
  }

  /**
   * Starts or stops monitoring this probe according to specified parameters. When monitoring is on,
   * this probe is periodically output at the specified period. If requested, the probe is reset
   * after each periodical output. When monitoring if off, this probe is only output when the JVM
   * terminates.
   *
   * <p>This method may be called several times during the execution of the JVM in order to start,
   * stop, restart, etc. monitoring or to change monitoring period.
   *
   * @param period the monitoring period; specifying {@link #NOT_PERIODIC} stops monitoring.
   * @param doReset {@code true} if the {@link #reset()} must be called after each output and {@code
   *     false} otherwise.
   */
  public void setMonitoring(long period, boolean doReset) {
    remove();
    this.outputPeriod = period;
    this.doReset = doReset;
    add();
  }

  //
  // PROBE CONTROL
  //
  /** Discards all information that this probe has collected. */
  public abstract void reset();

  /**
   * Releases this probe. It will not be output any more, whether periodically or upon termination.
   */
  public void release() {
    remove();
  }

  //
  // PROBE TYPES
  //
  /**
   * This probe is a simple counter incremented and decremented through the {@link #incr() incr()}
   * and {@link #decr() decr()}. It outputs the current value of the counter.
   *
   * @author J-M. Busca INRIA/Regal
   */
  public static class SimpleCounter extends PerformanceProbe {

    //
    // Object fields
    //
    private long _counterValue;

    //
    // Constructors
    //
    public SimpleCounter(String name) {
      super(name);
    }

    public String toString() {
      return _counterValue + "";
    }

    //
    // Methods
    //
    /** Increments the value of this counter. */
    public void incr() {
      _counterValue += 1;
    }

    /** Decrements the value of this counter. */
    public void decr() {
      _counterValue -= 1;
    }

    public void reset() {
      _counterValue = 0;
    }
  }

  /**
   * This probe records a set of {@code double} values provided through the {@link #add(long) add()}
   * method. It outputs the min, max, total, average and standard deviation of the current set.
   *
   * @author J-M. Busca INRIA/Regal
   */
  public static class FloatValueRecorder extends PerformanceProbe {

    //
    // Constants
    //
    public static final String DEFAULT_FORMAT = "%m/%a/%M/%t [#%c]";

    //
    // Object fields
    //
    // parameters
    private float outputFactor = 1;
    private MessageFormat outputFormat = new MessageFormat(format(DEFAULT_FORMAT));

    // stats values
    private double valueCount;
    private double nullCount;
    private double minValue = Long.MAX_VALUE;
    private double maxValue = Long.MIN_VALUE;
    private double totalCount;
    private double totalCount2;

    //
    // Constructors
    //
    public FloatValueRecorder(String name) {
      super(name);
    }

    public String toString() {
      return stats();
    }

    //
    // Accessors
    //
    /**
     * Sets the output format of this value recorder to the specified string. The string may include
     * the following parameters: %a (average), %c (count), %d (deviation), %m (min), %M (max) %n
     * (null count) and %t (total).
     *
     * @param format the output format to apply.
     */
    public void setFormat(String format) {
      outputFormat = new MessageFormat(format(format));
    }

    /**
     * Sets the conversion factor of this value recorder to the specified value. The conversion
     * factor is the ratio between the registered values and the output statistics.
     *
     * @param factor the conversion factor to apply.
     */
    public void setFactor(float factor) {
      outputFactor = factor;
    }

    //
    // Interface
    //
    /**
     * Adds the specified value this value recorder.
     *
     * @param value the value to add.
     * @return {@code true} if this call updated the min or the max value and {@code false}
     *     otherwise.
     */
    public boolean add(double value) {

      // update count
      valueCount += 1;
      if (value == 0) {
        nullCount += 1;
      }

      // update min, max
      boolean updated = false;
      if (value < minValue) {
        updated = (valueCount > 1);
        minValue = value;
      }
      if (maxValue < value) {
        updated = (valueCount > 1);
        maxValue = value;
      }

      // update totals
      totalCount += value;
      totalCount2 += (value / outputFactor) * (value / outputFactor);

      return updated;
    }

    /**
     * Adds the specified value recorder to this value recorder.
     *
     * @param other the recorder to add to this one.
     * @return {@code true} if this call updated the min or the max value and {@code false}
     *     otherwise.
     */
    public boolean add(ValueRecorder other) {

      if (other.outputFactor != outputFactor) {
        throw new RuntimeException("cannot add sample with different factor");
      }

      valueCount += other.valueCount;
      nullCount += other.nullCount;

      boolean updated = false;
      if (other.minValue < minValue) {
        updated = true;
        minValue = other.minValue;
      }
      if (other.maxValue > maxValue) {
        updated = true;
        maxValue = other.maxValue;
      }

      totalCount += other.totalCount;
      totalCount2 += other.totalCount2;

      return updated;
    }

    public void reset() {
      valueCount = 0;
      nullCount = 0;
      minValue = Long.MAX_VALUE;
      maxValue = Long.MIN_VALUE;
      totalCount = 0;
      totalCount2 = 0;
    }

    //
    // Internal methods
    //
    private String format(String format) {
      String result = format;
      result = result.replace("%a", "{0}"); // average
      result = result.replace("%c", "{1}"); // count
      result = result.replace("%d", "{2}"); // deviation
      result = result.replace("%m", "{3}"); // min
      result = result.replace("%M", "{4}"); // max
      result = result.replace("%n", "{5}"); // nulls
      result = result.replace("%t", "{6}"); // total
      return result;
    }

    private String stats() {

      String minString, averageString, maxString, totalString, deviationString;

      // compute string representing stats values
      if (valueCount == 0) {
        minString = "0";
        averageString = "0";
        maxString = "0";
        totalString = "0";
        deviationString = "0";
      } else {
        minString = ((double) (minValue / outputFactor)) + "";
        maxString = ((double) (maxValue / outputFactor)) + "";
        totalString = Double.toString(totalCount / outputFactor);
        if (minValue == maxValue) {
          averageString = minString;
          deviationString = "0";
        } else {
          double average = totalCount / valueCount / outputFactor;
          double deviation = sqrt((totalCount2 / valueCount) - average * average);
          averageString = Double.toString(average);
          deviationString = Double.toString(deviation);
        }
      }

      // return stats values according to format
      String[] args =
          new String[] {
            averageString,
            valueCount + "",
            deviationString,
            minString,
            maxString,
            nullCount + "",
            totalString
          };
      return outputFormat.format(args);
    }
  }

  /**
   * This probe records a set of {@code long} values provided through the {@link #add(long) add()}
   * method. It outputs the min, max, total, average and standard deviation of the current set.
   *
   * @author J-M. Busca INRIA/Regal
   */
  public static class ValueRecorder extends PerformanceProbe {

    //
    // Constants
    //
    public static final String DEFAULT_FORMAT = "%m/%a/%M/%t [#%c]";

    //
    // Object fields
    //
    // parameters
    private float outputFactor = 1;
    private MessageFormat outputFormat = new MessageFormat(format(DEFAULT_FORMAT));

    // stats values
    private long valueCount;
    private long nullCount;
    private long minValue = Long.MAX_VALUE;
    private long maxValue = Long.MIN_VALUE;
    private long totalCount;
    private long totalCount2;

    //
    // Constructors
    //
    public ValueRecorder(String name) {
      super(name);
    }

    public String toString() {
      return stats();
    }

    //
    // Accessors
    //
    /**
     * Sets the output format of this value recorder to the specified string. The string may include
     * the following parameters: %a (average), %c (count), %d (deviation), %m (min), %M (max) %n
     * (null count) and %t (total).
     *
     * @param format the output format to apply.
     */
    public void setFormat(String format) {
      outputFormat = new MessageFormat(format(format));
    }

    /**
     * Sets the conversion factor of this value recorder to the specified value. The conversion
     * factor is the ratio between the registered values and the output statistics.
     *
     * @param factor the conversion factor to apply.
     */
    public void setFactor(float factor) {
      outputFactor = factor;
    }

    //
    // Interface
    //
    /**
     * Adds the specified value this value recorder.
     *
     * @param value the value to add.
     * @return {@code true} if this call updated the min or the max value and {@code false}
     *     otherwise.
     */
    public synchronized boolean add(long value) {

      // update count
      valueCount += 1;
      if (value == 0) {
        nullCount += 1;
      }

      // update min, max
      boolean updated = false;
      if (value < minValue) {
        updated = (valueCount > 1);
        minValue = value;
      }
      if (maxValue < value) {
        updated = (valueCount > 1);
        maxValue = value;
      }

      // update totals
      totalCount += value;
      totalCount2 += (value / outputFactor) * (value / outputFactor);

      return updated;
    }

    /**
     * Adds the specified value recorder to this value recorder.
     *
     * @param other the recorder to add to this one.
     * @return {@code true} if this call updated the min or the max value and {@code false}
     *     otherwise.
     */
    public boolean add(ValueRecorder other) {

      if (other.outputFactor != outputFactor) {
        throw new RuntimeException("cannot add sample with different factor");
      }

      valueCount += other.valueCount;
      nullCount += other.nullCount;

      boolean updated = false;
      if (other.minValue < minValue) {
        updated = true;
        minValue = other.minValue;
      }
      if (other.maxValue > maxValue) {
        updated = true;
        maxValue = other.maxValue;
      }

      totalCount += other.totalCount;
      totalCount2 += other.totalCount2;

      return updated;
    }

    public void reset() {
      valueCount = 0;
      nullCount = 0;
      minValue = Long.MAX_VALUE;
      maxValue = Long.MIN_VALUE;
      totalCount = 0;
      totalCount2 = 0;
    }

    //
    // Internal methods
    //
    private String format(String format) {
      String result = format;
      result = result.replace("%a", "{0}"); // average
      result = result.replace("%c", "{1}"); // count
      result = result.replace("%d", "{2}"); // deviation
      result = result.replace("%m", "{3}"); // min
      result = result.replace("%M", "{4}"); // max
      result = result.replace("%n", "{5}"); // nulls
      result = result.replace("%t", "{6}"); // total
      return result;
    }

    private String stats() {

      String minString, averageString, maxString, totalString, deviationString;

      // compute string representing stats values
      if (valueCount == 0) {
        minString = "0";
        averageString = "0";
        maxString = "0";
        totalString = "0";
        deviationString = "0";
      } else {
        minString = ((long) (minValue / outputFactor)) + "";
        maxString = ((long) (maxValue / outputFactor)) + "";
        totalString = ((long) (totalCount / outputFactor)) + "";
        if (minValue == maxValue) {
          averageString = minString;
          deviationString = "0";
        } else {
          double average = totalCount / valueCount / outputFactor;
          double deviation = sqrt((totalCount2 / valueCount) - average * average);
          averageString = Double.toString(average);
          deviationString = Double.toString(deviation);
        }
      }

      // return stats values according to format
      String[] args =
          new String[] {
            averageString,
            valueCount + "",
            deviationString,
            minString,
            maxString,
            nullCount + "",
            totalString
          };
      return outputFormat.format(args);
    }
  }

  /**
   * This probe records a set of execution times delimited by the {@link #start() start()} and
   * {@link #stop() stop()} methods. It outputs the min, max, total, average and standard deviation
   * of the current set. Time is measured in nanosecond with {@link System#nanoTime()} and output in
   * microsecond.
   *
   * @author J-M. Busca INRIA/Regal
   */
  public static class TimeRecorder extends ValueRecorder {

    //
    // Object fields
    //
    private long startClock;

    //
    // Constructors
    //
    public TimeRecorder(String name) {
      super(name);
      setFactor(1000000);
      setFormat("%m/%a/%Mus [#%c - %tus]");
    }

    //
    // Methods
    //
    /** Starts execution time measurement. */
    public void start() {
      startClock = System.nanoTime();
    }

    /** Stops execution time measurement. */
    public void stop() {
      add(System.nanoTime() - startClock);
    }
  }

  /**
   * This probe automatically samples a variable and records the sampled values in a {@link
   * ValueRecorder ValueRecorder}. The variable to read is specified when allocating the probe.
   */
  public static class VariableSampler extends ValueRecorder {

    //
    // Class fields
    //
    private static HashMap<Long, Collection<SamplingTask>> samplingTasks =
        new HashMap<Long, Collection<SamplingTask>>();

    //
    // Object fields
    //
    private VariableReader valueReader;
    private long samplingPeriod;

    //
    // Constructors
    //
    public VariableSampler(String name, VariableReader reader, long period) {
      super(name);
      this.valueReader = reader;
      this.samplingPeriod = period;
      add();
    }

    //
    // Methods
    //
    private void read() {
      add(valueReader.read());
    }

    public void release() {
      super.release();
      remove();
    }

    //
    // Sampling management
    //
    private void add() {
      Collection<SamplingTask> samplers = samplingTasks.get(samplingPeriod);
      if (samplers == null) {
        samplers = new Vector<SamplingTask>();
        samplingTasks.put(samplingPeriod, samplers);
        if (samplingPeriod != NOT_PERIODIC) {
          TimerTask task = new OutputTask(samplingPeriod);
          long next = nextOccurence(samplingPeriod);
          probeTimer.scheduleAtFixedRate(task, next, samplingPeriod);
        }
      }
      SamplingTask sampler = new SamplingTask(this);
      samplers.add(sampler);
    }

    private void remove() {
      Collection<SamplingTask> samplers = samplingTasks.get(samplingPeriod);
      if (samplers == null) {
        return;
      }
      samplers.remove(this);
    }

    private static class SamplingTask extends TimerTask {

      // Object fields
      private VariableSampler variableSampler;

      // Constructor
      SamplingTask(VariableSampler sampler) {
        variableSampler = sampler;
      }

      // Runnable interface
      public void run() {
        variableSampler.read();
      }
    }
  }

  /**
   * An object that reads a variable of type long.
   *
   * @author J-M. Busca INRIA/Regal
   */
  public interface VariableReader {

    public long read();
  }

  /**
   * This probe reports various indicators on the JVM memory consumption. It has no specific method.
   *
   * @author J-M. Busca INRIA/Regal
   */
  public static class JVMMemory extends PerformanceProbe {

    //
    // Constants
    //
    private static final long MEGA = 1024 * 1024;

    //
    // Object fields
    //
    private Runtime runtime;

    //
    // Constructors
    //
    public JVMMemory(String name) {
      super(name);
      runtime = Runtime.getRuntime();
    }

    public String toString() {
      String result = "f=" + (runtime.freeMemory() / MEGA) + "/";
      result += "t=" + (runtime.totalMemory() / MEGA) + "/";
      result += "m=" + (runtime.maxMemory() / MEGA) + "Mb";
      return result;
    }

    //
    // Methods
    //
    public void reset() {}
  }

  //
  // PROBE OUTPUT
  //
  private void output(String type) {
    StringBuffer result = new StringBuffer(128);
    result.append(time()).append(" ");
    result.append(type).append(" ");
    result.append(probeName).append(": ");
    result.append(toString());
    outputStream.println(result.toString());
  }

  private static String time() {
    float now = (float) (System.currentTimeMillis() - startTime) / 1000;
    return TIME_FORMAT.format(now);
  }

  //
  // PROBE MANAGEMENT
  //
  private void add() {
    Collection<PerformanceProbe> probes = _perfProbes.get(outputPeriod);
    if (probes == null) {
      probes = new Vector<PerformanceProbe>();
      _perfProbes.put(outputPeriod, probes);
      if (outputPeriod != NOT_PERIODIC) {
        TimerTask task = new OutputTask(outputPeriod);
        long next = nextOccurence(outputPeriod);
        probeTimer.scheduleAtFixedRate(task, next, outputPeriod);
      }
    }
    probes.add(this);
  }

  private void remove() {
    Collection<PerformanceProbe> probes = _perfProbes.get(outputPeriod);
    if (probes == null) {
      return;
    }
    probes.remove(this);
  }

  private static Collection<PerformanceProbe> list(long period) {
    if (period == ALL_RECORDERS) {
      Collection<PerformanceProbe> result = new Vector<PerformanceProbe>();
      for (Collection<PerformanceProbe> probes : _perfProbes.values()) {
        result.addAll(probes);
      }
      return result;
    }

    Collection<PerformanceProbe> result = _perfProbes.get(period);
    if (result == null) {
      return Collections.emptySet();
    }
    return result;
  }

  private static long nextOccurence(long period) {
    return (System.currentTimeMillis() - startTime) % period;
  }

  //
  // HELPER CLASSES
  //
  /** Outputs probes when the JVM exits. */
  private static class ProbeShutdown extends Thread {

    //
    // Constructor
    //
    public ProbeShutdown() {
      super("Probe shutdown");
      setDaemon(true);
      Runtime.getRuntime().addShutdownHook(this);
    }

    //
    // Runnable interface
    //
    public void run() {

      // output all active probes
      for (PerformanceProbe probe : list(ALL_RECORDERS)) {
        probe.output("F");
      }

      // close output if not standard output
      if (!outputStream.equals(System.out)) {
        outputStream.close();
      }
    }
  }

  /** Outputs probes periodically when the JVM executes. */
  private static class OutputTask extends TimerTask {

    //
    // Object fields
    //
    private long _taskPeriod;

    //
    // Constructor
    //
    public OutputTask(long period) {
      _taskPeriod = period;
    }

    //
    // Runnable interface
    //
    public void run() {

      // output appropriate probes
      for (PerformanceProbe probe : list(_taskPeriod)) {
        probe.output("P");
        if (probe.doReset) {
          probe.reset();
        }
      }
    }
  }
}
Exemple #15
0
  /*Scans all equipments and identifies the ones that need to be moved */
  private void scanEquipments() {
    double timeDiff = 0;
    int orderToMove = -1;
    double[] arrivalFrequencies = {ARRIVAL1, ARRIVAL2, ARRIVAL3};

    /*In the available state, the scan should not happen on the equipment as it's external customer demand and we should record a "lost sale" when there is no equipment*/
    for (int counter = 0; counter < TYPES; counter++) {
      // System.out.println("I am computing for arrival frequency "+arrivalFrequencies[counter]+ "
      // and the corresponding time is "+orderTimes.get(counter));
      if (orderTimes.get(counter) < System.currentTimeMillis() - t0) {
        System.out.println("Time is " + System.currentTimeMillis());
        orderTimes.set(
            counter, System.currentTimeMillis() + nextRandomTime(arrivalFrequencies[counter]) - t0);
        System.out.println(
            "I have just computed a new exit time for state 1 and type "
                + counter
                + " = "
                + orderTimes.get(counter));
        orderToMove =
            checkOrder(
                1,
                counter + 1); // checks the order of the type being moved in the available arraylist
        if (orderToMove >= 0) {
          Equipment e = availEquipment.get(orderToMove);
          timeDiff =
              nextRandomTime(
                  RENTALFREQUENCY[
                      e.type
                          - 1]); // Equipment is moving to rent - so its next timeOut should be that
          // of rent
          e.timeOut = System.currentTimeMillis() + timeDiff - t0;
          moveEquip(1, orderToMove);
          sales = sales + RATES[counter];
          salesLabel.setLabel("SALES : $" + sales);
        } else {
          System.out.println("You just lost a sale");
          int ls = lostSales.get(counter);
          lostSales.set(counter, ls + 1);
          switch (counter) {
            case 0:
              lostSalesLabel1.setLabel("Lost Sales HR = " + ls + 1);
              break;
            case 1:
              lostSalesLabel2.setLabel("Lost Sales MR = " + ls + 1);
              break;
            case 2:
              lostSalesLabel3.setLabel("Lost Sales LR = " + ls + 1);
              break;
          }
        }
      }
    }

    for (Equipment e : rentEquipment) {
      orderToMove = scan(e);
      if (orderToMove >= 0) {
        /*Entering this loop means there is an equipment to move */
        timeDiff =
            nextRandomTime(
                SHOPFREQUENCY[
                    e.type - 1]); // Equipment is moving to shop so its next timeOut is that of shop
        // System.out.println("Rental frequency is "+RENTALFREQUENCY[e.type-1]+" and timeDiff is
        // "+timeDiff);
        e.timeOut = System.currentTimeMillis() + timeDiff - t0;
        System.out.println("the new timeOut is " + e.timeOut);
        break;
      }
    }
    if (orderToMove >= 0) {
      moveEquip(2, orderToMove);
      // System.out.println("I have just moved equipment from state 2 and of order "+orderToMove);
      // placeEquipments();
    }

    for (Equipment e : shopEquipment) {
      orderToMove = scan(e);
      if (orderToMove >= 0) {
        /*Entering this loop means there is an equipment to move */
        e.timeOut = 0; // Doesn't matter what the time-out is as it's not governed by the equipment
        break;
      }
    }
    if (orderToMove >= 0) {
      moveEquip(3, orderToMove);
      // placeEquipments();
    }
  }