Package algorithms

Source Code of algorithms.HandGA

package algorithms;

import general.Card;
import java.util.StringTokenizer;

import utils.Log;

import algorithms.genetic.Evolution;
import algorithms.genetic.HandEvolution;
import algorithms.genetic.HandIndividual;

public class HandGA extends DealingAlgorithm{
 
  private int popSize = 100;
  private int generationSize = 1200;
  private double mutProb = 0.2;
  private double crossProb = 0.8;
 
  public HandGA(int num) {
    super(num);
    this.num = num;
  }

  @Override
  public void deal(Card[] deck) {
    Log.write("Starting HandGA algorithm with parameters:" + popSize + "," + generationSize + "," + mutProb + "," + crossProb);
    Evolution evolution = new HandEvolution(deck, this.pointsLimits, this.cardLimits, popSize, generationSize, mutProb, crossProb);
    for (int i=0;i<num;i++){
      evolution.reset();
      evolution.evolve();
      HandIndividual best = (HandIndividual)evolution.getBest();
      if (best.isIdeal())
        answers[i] = this.putInMatrix(best.toHand());
      else
        i--;
    }
   
    this.printResults();
  }

  @Override
  public void extraConfig(String configLine) {
    int p,g;
    double m,c;
    try
    {
      StringTokenizer sT = new StringTokenizer(configLine,",");
      p = Integer.parseInt(sT.nextToken().trim());
      g = Integer.parseInt(sT.nextToken().trim());
      m = Double.parseDouble(sT.nextToken().trim());
      c = Double.parseDouble(sT.nextToken().trim());
    }
    catch (Exception ex)
    {
      p = this.popSize;
      g = this.generationSize;
      m = this.mutProb;
      c = this.crossProb;
    }
    this.popSize = p;
    this.generationSize = g;
    this.mutProb = m;
    this.crossProb = c;
  }
}
TOP

Related Classes of algorithms.HandGA

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.