Resource Allocation GA Individuals

top

public GAAgent mutate() {
  return new RAGAAgent(alpha + (GA.random() * MUTATION_RANGE) - (MUTATION_RANGE / 2),
                        beta + (GA.random() * MUTATION_RANGE) - (MUTATION_RANGE / 2),
                        tau + (GA.random() * MUTATION_RANGE) - (MUTATION_RANGE / 2));
}

public GAAgent crossover(GAAgent otherParent) {
  return new RAGAAgent(
    GAUtilities.blendDoubles(alpha, ((RAGAAgent)otherParent).getAlpha(), SIGNIFICANT_DIGITS),
    GAUtilities.blendDoubles(beta, ((RAGAAgent)otherParent).getBeta(), SIGNIFICANT_DIGITS),
    GAUtilities.blendDoubles(tau, ((RAGAAgent)otherParent).getTau(), SIGNIFICANT_DIGITS));
}


public double calculateFitness() {
  int testsToRun = GA.getintParameterValue("RAGAAgent iters");
  
  if (testsToRun == 0)
    TestsToRun = DEFAULT_NUMBER_OF_TESTS;

  theModel = new RAModel(numberOfResources, numberOfAgents, windowSize,
                         alpha, beta, tau, DELAY_VALUE);
  theModel.addObserver(this);

  
  for (int i = testsToRun; i > 0; i--) {
    theModel.initializeWorld();
    theModel.setActivity(true);
  
    while (!doneTesting()) {
      try {
        Thread.sleep(ITERATION_PERIOD);
      } catch (InterruptedException e) {
      }
    }
    fitness += currentState.currentIteration;  		
  }
  
  theModel.setActivity(false);
  fitness = (testsToRun * ITERATION_LIMIT) / fitness;
  return fitness;
}


public void update(Observable model, Object arg) {  	
  currentState = (RAData) arg;

  if (currentState.currentIteration >= ITERATION_LIMIT) {
    theModel.setActivity(false);
  } 			
}
  	

public boolean doneTesting() {
  return ((currentState != null) &&
          ((currentState.isOptimal()) || 
           (currentState.currentIteration >= ITERATION_LIMIT)));
}