コード例 #1
0
  /**
   * Returns a behavior model based on the behavior mix.
   *
   * @return the behavior model and null if no behavior chosen (list of behaviors empty or all
   *     entries have a relative frequency of 0.0).
   * @throws BehaviorException when an error occurs.
   */
  public BehaviorMixEntry getBehavior() throws BehaviorException {
    List<Double> cumProbList = new ArrayList();
    double curCumProb = 0.0;

    for (int i = 0; i < this.behaviorList.size(); i++) {
      /* add all to have the same number of entries in
       * both lists */
      curCumProb += this.behaviorList.get(i).getRFreq();
      cumProbList.add(curCumProb);
    }
    if (cumProbList.isEmpty() || (cumProbList.get(cumProbList.size() - 1) == 0)) {
      logger.fatalError("No behavior matches");
      throw new BehaviorException("no behavior matches");
    }
    // System.out.print("Choosing behavior ... -> cumProbList " + cumProbList.toString() + " ->");
    double rndVal = rand.nextDouble() * (cumProbList.get(cumProbList.size() - 1));
    // System.out.print("rnd: " + rndVal + " ");
    BehaviorMixEntry behavior = this.behaviorList.get(0);
    for (int i = 0; i < cumProbList.size() - 1; i++) {
      if (rndVal < cumProbList.get(i)) break;
      behavior = this.behaviorList.get(i + 1);
    }
    // System.out.println("-> Behavior: " + behavior.getBName());

    return behavior;
  }