Example #1
0
 /*
  * This implementation of map2 passes the initial RNG to the first argument
  * and the resulting RNG to the second argument. It's not necessarily wrong to
  * do this the other way around, since the results are random anyway. We could
  * even pass the initial RNG to both `f` and `g`, but that might have
  * unexpected results. E.g. if both arguments are `RNG.int` then we would
  * always get two of the same `Int` in the result. When implementing functions
  * like this, it's important to consider how we would test them for
  * correctness.
  */
 public static <A, B, C> Rand<C> map2_(Rand<A> ra, Rand<B> rb, Function<A, Function<B, C>> f) {
   return rng -> {
     Tuple<A, RNG> t1 = ra.apply(rng);
     Tuple<B, RNG> t2 = rb.apply(t1._2);
     return new Tuple<>(f.apply(t1._1).apply(t2._1), t2._2);
   };
 }
Example #2
0
 public double mutate(int nDeleMutation, int nBeneMutation) {
   double fitnessEffect = 1;
   for (int i = 0; i < nDeleMutation; i++) {
     double u = Rand.getFloat();
     fitnessEffect *=
         1 - ((-ModelParameters.getFloat("DEFAULT_DELETERIOUS_EFFECT")) * Math.log(1 - u));
   }
   for (int j = 0; j < nBeneMutation; j++) {
     double u = Rand.getFloat();
     fitnessEffect *=
         1 + ((-ModelParameters.getFloat("DEFAULT_BENEFICIAL_EFFECT")) * Math.log(1 - u));
   }
   return (fitnessEffect);
 }
Example #3
0
 private void lethalMutate() {
   double mutationRate =
       ModelParameters.getDouble("BASE_LETHAL_MUTATION_RATE") * getMutatorStrength();
   if (Rand.getDouble() < mutationRate) {
     die();
   }
 }
  @Override
  public void updateLong() {
    if (!transmitting) {
      if (nextBossOrcSpawn == 0) {
        // spawn melvin every 10 - 30 min
        nextBossOrcSpawn = registry.currentTime + Rand.getRange(10 * 60 * 1000, 30 * 60 * 1000);
      }
      if (nextSnailRiderSpawn == 0) {
        // spawn melvin every 10 - 30 min
        nextSnailRiderSpawn = registry.currentTime + Rand.getRange(10 * 60 * 1000, 30 * 60 * 1000);
      }

      Monster monster = null;
      ArrayList deadMonsters = new ArrayList();

      // make sure we have enough bad guys on the map
      if (gameController.multiplayerMode != gameController.multiplayerMode.CLIENT) {
        spawnNearPlayers();
        spawnNearPlaceable();
      }

      try {
        for (String key : monsters.keySet()) {
          monster = (Monster) monsters.get(key);
          if (!monster.isFeared()) {
            gameController.checkIfFeared(monster);
          }
          gameController.checkPlaceableDamageAgainstMob(monster);

          monster.updateLong();
          if (monster.isDirty()) {
            deadMonsters.add(key);
          }
        }

        if (deadMonsters.size() > 0) {
          for (int i = 0; i < deadMonsters.size(); i++) {
            // EIError.debugMsg((String) deadMonsters.get(i));
            monsters.remove((String) deadMonsters.get(i));
          }
        }
      } catch (ConcurrentModificationException concEx) {
        // another thread was trying to modify monsters while iterating
        // we'll continue and the new item can be grabbed on the next update
      }
    }
  }
Example #5
0
  public int getSample() {
    int pos = Rand.getInteger(0, num - 1);
    int value = sample[pos];
    sample[pos] = sample[num - 1];
    num--;

    if (num == 0) initSampling();

    return value;
  }
Example #6
0
 @Test
 public void testNextDouble() {
   RandomTester tester = new SimpleRandomTester();
   MersenneTwister r = new MersenneTwister(Rand.nextLong());
   for (int i = 0; i < 100000; ++i) {
     double d = r.nextDouble();
     assertTrue("Random value out of range", 0 <= d && d < 1);
     tester.addSample(d);
   }
   assertTrue("Samples are not random", tester.hasRandomSamples());
 }
Example #7
0
 private void beneficialMutate(int currentGeneration, ArrayList mutationProperties) {
   double mutationRate =
       ModelParameters.getDouble("BASE_BENEFICIAL_MUTATION_RATE") * getMutatorStrength();
   //        Poisson poisson = new Poisson(mutationRate, Rand.getEngine());
   //        int poissonObs = poisson.nextInt();
   int poissonObs = Util.getPoisson(mutationRate);
   for (int nMutation = 0; nMutation < poissonObs; nMutation++) {
     double u = Rand.getFloat();
     double fitnessEffect =
         1 + ((-ModelParameters.getFloat("DEFAULT_BENEFICIAL_EFFECT")) * Math.log(1 - u));
     updateMutationInformation(currentGeneration, mutationProperties, fitnessEffect);
   }
 }
Example #8
0
 public void init(CA c, Initialization in) {
   Random r = new Random(Rand.seed());
   switch (in) {
     case single:
       for (int i = 0; i < c.getWidth(); i++) {
         c.set(i, 0, _colors[0]);
       }
       break;
     case random:
       for (int i = 0; i < c.getWidth(); i++) {
         for (int j = 0; j < c.getHeight(); j++) {
           c.set(i, j, _colors[r.nextInt(_colors.length)]);
         }
       }
       break;
     default:
   }
 }
Example #9
0
  /**
   * Main method
   *
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    ParserParameters.doParse(args[0]);
    LogManager.initLogManager();
    InstanceSet is = new InstanceSet();

    try {
      is.readSet(Parameters.trainInputFile, true);
    } catch (Exception e) {
      LogManager.printErr(e.toString());
      System.exit(1);
    }
    checkDataset(is);

    Discretizer dis;
    String name = Parameters.algorithmName;
    dis = new UniformFrequencyDiscretizer(Parameters.numIntervals);
    Rand.initRand();
    dis.buildCutPoints(is);
    dis.applyDiscretization(Parameters.trainInputFile, Parameters.trainOutputFile);
    dis.applyDiscretization(Parameters.testInputFile, Parameters.testOutputFile);
    LogManager.closeLog();
  }
  public void spawnNearPlaceable() {
    float prob, rand;
    Placeable placeable = registry.getPlaceableManager().getRandomPlacable();
    if (placeable != null
        && !placeable.getType().equals("TownHall")
        && !placeable.getType().equals("Cabin")
        && !placeable.getType().equals("Chest")) {

      Point p = placeable.getCenterPoint();
      HashMap<String, Player> players = registry.getPlayerManager().getPlayers();
      Player player = null;
      boolean playerNear = false;
      for (String key : players.keySet()) {
        player = (Player) players.get(key);
        Point p2 = new Point(player.getMapX(), player.getMapY());
        if (p.distance(p2) < mobSpawnRangeMin * 2) {
          playerNear = true;
        }
      }
      if (!playerNear) {
        for (MonsterType monsterType : MonsterType.values()) {
          if (monsterType.toString().equals("Porcupine")
              || monsterType.toString().equals("Snail")
              || monsterType.toString().equals("Snake")
              || monsterType.toString().equals("ZombieWalrus")) {
            rand = Rand.getFloat();
            prob = getShouldSpawn(monsterType, 0);
            if (rand <= prob / 3.0f) {
              // System.out.println("spawn near placeable " + monsterType.name());
              spawn(monsterType.name(), "Roaming", p.x, p.y);
            }
          }
        }
      }
    }
  }
  private void spawnPlants() {
    if (gameController.multiplayerMode != gameController.multiplayerMode.CLIENT) {
      // Blue Thorns
      int blueThornCount = this.getCountByType("BlueThorn");
      if (blueThornCount < 200) {
        for (int i = 0; i < 200 - blueThornCount; i++) {
          boolean canSpawn = true;
          int level = Rand.getRange(1, 3);
          Point vinePosition = new Point();
          vinePosition.x = Rand.getRange(1, gameController.getMapWidth());
          vinePosition.y =
              Rand.getRange(
                  registry.getBlockManager().getLevelBottom(level),
                  registry.getBlockManager().getLevelTop(level));

          vinePosition.y = this.findNextFloor(vinePosition.x, vinePosition.y, 60);

          if (this.doesRectContainBlocks(vinePosition.x, vinePosition.y + 100, 17, 16)) {
            Monster monster = null;
            try {
              for (String key : monsters.keySet()) {
                monster = (Monster) monsters.get(key);
                if (monster.getName().equals("BlueThorn")
                    || monster.getName().equals("VineThorn")) {
                  double distance = vinePosition.distance(monster.getCenterPoint());
                  if (distance < 750) {
                    canSpawn = false;
                    break;
                  }
                }
              }
            } catch (ConcurrentModificationException concEx) {
              // another thread was trying to modify monsters while iterating
              // we'll continue and the new item can be grabbed on the next update
            }
            if (canSpawn) {
              spawn("BlueThorn", "Roaming", vinePosition.x, vinePosition.y);
            }
          }
        }
      }

      // Vine Thorns
      int vineThornCount = this.getCountByType("VineThorn");
      if (vineThornCount < 200) {
        for (int i = 0; i < 200 - vineThornCount; i++) {
          boolean canSpawn = true;
          int level = Rand.getRange(1, 3);
          Point vinePosition = new Point();
          vinePosition.x = Rand.getRange(1, gameController.getMapWidth());
          vinePosition.y =
              Rand.getRange(
                  registry.getBlockManager().getLevelBottom(level),
                  registry.getBlockManager().getLevelTop(level));

          vinePosition.y = this.findNextFloor(vinePosition.x, vinePosition.y, 60);

          if (this.doesRectContainBlocks(vinePosition.x, vinePosition.y + 100, 17, 16)) {
            Monster monster = null;
            try {
              for (String key : monsters.keySet()) {
                monster = (Monster) monsters.get(key);
                if (monster.getName().equals("BlueThorn")
                    || monster.getName().equals("VineThorn")) {
                  double distance = vinePosition.distance(monster.getCenterPoint());
                  if (distance < 750) {
                    canSpawn = false;
                    break;
                  }
                }
              }
            } catch (ConcurrentModificationException concEx) {
              // another thread was trying to modify monsters while iterating
              // we'll continue and the new item can be grabbed on the next update
            }
            if (canSpawn) {
              spawn("VineThorn", "Roaming", vinePosition.x, vinePosition.y);
            }
          }
        }
      }
    }
  }
  public void spawnNearPlayers() {
    Monster monster = null;
    float prob, rand;
    int count;
    HashMap<String, Player> players = registry.getPlayerManager().getPlayers();
    Player player = null;
    Integer spawnCoolDown = null;
    for (String key : players.keySet()) {
      player = (Player) players.get(key);

      // check for spawning Melvin
      if (registry.currentTime >= nextBossOrcSpawn && nextBossOrcSpawn != 0) {
        // to spawn melvin, player must have 40 AP, be within a range on the map and be on the
        // surface
        if (player.getMapX() >= 2000
            && player.getMapX() <= 5000
            && player.getLevel() >= 10
            && player.getMapY() == this.findFloor(player.getMapX())) {
          spawnBossOrc(player);
        }
      }

      if (spawnCoolDowns.containsKey(key)) {
        spawnCoolDown = spawnCoolDowns.get(key);
      } else {
        spawnCoolDown = new Integer(0);
        spawnCoolDowns.put(key, spawnCoolDown);
      }
      spawnCoolDown--;
      if (spawnCoolDown < 0) {
        int x = player.getMapX();
        int y = player.getMapY();
        int groundLevel = registry.getBlockManager().getLevelByY(y);
        count = -groundLevel;
        try {
          for (String key2 : monsters.keySet()) {
            monster = (Monster) monsters.get(key2);
            if (monster.getCenterPoint().distance(player.getCenterPoint())
                < MonsterManager.mobSpawnRangeMax * 3 / 2) {
              if (monster.getTouchDamage() > 0
                  && !monster.getName().equals("BlueThorn")
                  && !monster.getName().equals("VineThorn")) {
                count++;
              }
            }
          }
        } catch (ConcurrentModificationException concEx) {
          // another thread was trying to modify monsters while iterating
          // we'll continue and the new item can be grabbed on the next update
        }
        if (count < 4) {
          for (MonsterType monsterType : MonsterType.values()) {
            if (count < 4) {
              rand = Rand.getFloat();
              prob = getShouldSpawn(monsterType, groundLevel);
              if (prob > 0.0f) {
                if ((prob - count * spawnRatioDiff) > 0.005f) {
                  prob += -count * spawnRatioDiff;
                } else {
                  prob = 0.005f;
                }
              }
              if (rand <= prob) {
                // System.out.println("spawn near player " + monsterType.name());
                spawn(monsterType.name(), "Roaming", x, y);
                count++;
              }
            }
          }
        } else {
          // EIError.debugMsg("too many to spawn");
          spawnCoolDown = 20;
        }
      }
    }
  }
 public Long op(Rand x) {
   return x.next();
 }
Example #14
0
 public static <A, B> Rand<B> flatMap(Rand<A> f, Function<A, Rand<B>> g) {
   return rng -> {
     Tuple<A, RNG> t = f.apply(rng);
     return g.apply(t._1).apply(t._2); // We pass the new input along
   };
 }
Example #15
0
 public static <A, B> Rand<B> map_(Rand<A> s, Function<A, B> f) {
   return rng -> {
     Tuple<A, RNG> t = s.apply(rng);
     return new Tuple<>(f.apply(t._1), t._2);
   };
 }