예제 #1
0
  public void onTick(long ticks) {
    // Every second
    if (ticks % 20 == 0) {
      for (Map.Entry<Timer, Long> entry : timers.entrySet()) {
        if (entry.getKey().shouldKill()) {
          timers.remove(entry.getKey());
          continue;
        }
        long entryTime = entry.getValue();
        if (entryTime > 1) {
          // Decrement current time
          entry.setValue(--entryTime);
        } else {
          Timer timer = entry.getKey();

          // Run the timer method
          try {
            if (timer.getMethodObject() == null) {
              Method m = TimeEvents.class.getMethod(timer.getEventMethod());
              m.invoke(timeEvents);
            } else {
              Method m = timer.getMethodObject().getClass().getMethod(timer.getEventMethod());
              m.invoke(timer.getMethodObject());
            }
          } catch (NoSuchMethodException e) {
            TeamJug.errorMessage("The method " + timer.getEventMethod() + " does not exist!");
            e.printStackTrace();
          } catch (InvocationTargetException e) {
            e.printStackTrace();
          } catch (IllegalAccessException e) {
            e.printStackTrace();
          }

          if (timer.isRecurring()) {
            // Reset time
            entry.setValue(timer.getTime());
          } else {
            timers.remove(timer);
          }
        }
      }
    }
  }
예제 #2
0
 public void tick() {
   timer.tick();
   if (timer.getTime() == 0) {
     game.state = Game.STATE.LOSE;
   }
 }
예제 #3
0
 public void addTimer(Timer timer) {
   long time = timer.getTime();
   timers.put(timer, time);
 }
예제 #4
0
  /**
   * Perform one evolution step with the given evolution {@code start} object New phenotypes are
   * created with the fitness function and fitness scaler defined by this <em>engine</em>
   *
   * <p><em>This method is thread-safe.</em>
   *
   * @since 3.1
   * @see #evolve(org.jenetics.Population, long)
   * @param start the evolution start object
   * @return the evolution result
   * @throws java.lang.NullPointerException if the given evolution {@code start} is {@code null}
   */
  public EvolutionResult<G, C> evolve(final EvolutionStart<G, C> start) {
    final Timer timer = Timer.of().start();

    final Population<G, C> startPopulation = start.getPopulation();

    // Initial evaluation of the population.
    final Timer evaluateTimer = Timer.of(_clock).start();
    evaluate(startPopulation);
    evaluateTimer.stop();

    // Select the offspring population.
    final CompletableFuture<TimedResult<Population<G, C>>> offspring =
        _executor.async(() -> selectOffspring(startPopulation), _clock);

    // Select the survivor population.
    final CompletableFuture<TimedResult<Population<G, C>>> survivors =
        _executor.async(() -> selectSurvivors(startPopulation), _clock);

    // Altering the offspring population.
    final CompletableFuture<TimedResult<AlterResult<G, C>>> alteredOffspring =
        _executor.thenApply(offspring, p -> alter(p.result, start.getGeneration()), _clock);

    // Filter and replace invalid and to old survivor individuals.
    final CompletableFuture<TimedResult<FilterResult<G, C>>> filteredSurvivors =
        _executor.thenApply(survivors, pop -> filter(pop.result, start.getGeneration()), _clock);

    // Filter and replace invalid and to old offspring individuals.
    final CompletableFuture<TimedResult<FilterResult<G, C>>> filteredOffspring =
        _executor.thenApply(
            alteredOffspring, pop -> filter(pop.result.population, start.getGeneration()), _clock);

    // Combining survivors and offspring to the new population.
    final CompletableFuture<Population<G, C>> population =
        filteredSurvivors.thenCombineAsync(
            filteredOffspring,
            (s, o) -> {
              final Population<G, C> pop = s.result.population;
              pop.addAll(o.result.population);
              return pop;
            },
            _executor.get());

    // Evaluate the fitness-function and wait for result.
    final Population<G, C> pop = population.join();
    final TimedResult<Population<G, C>> result = TimedResult.of(() -> evaluate(pop), _clock).get();

    final EvolutionDurations durations =
        EvolutionDurations.of(
            offspring.join().duration,
            survivors.join().duration,
            alteredOffspring.join().duration,
            filteredOffspring.join().duration,
            filteredSurvivors.join().duration,
            result.duration.plus(evaluateTimer.getTime()),
            timer.stop().getTime());

    final int killCount =
        filteredOffspring.join().result.killCount + filteredSurvivors.join().result.killCount;

    final int invalidCount =
        filteredOffspring.join().result.invalidCount + filteredSurvivors.join().result.invalidCount;

    return EvolutionResult.of(
        _optimize,
        result.result,
        start.getGeneration(),
        durations,
        killCount,
        invalidCount,
        alteredOffspring.join().result.alterCount);
  }