/*
 * SATFitness.java
 *
 * Created on March 11, 2006, 12:46 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package cz.pcland.ga.fitness;

import cz.pcland.ga.BaseIndividual;
import cz.pcland.ga.BinaryIndividual;
import java.util.*;
//import java.io.*;
import cz.pcland.sat.*;
/**
 *
 * @author petr
 */
public class SATFitness implements Evaluator{
    SATInstance instance;
    
    public void setDataObject(Object instance){
        if(instance instanceof SATInstance) {
            this.instance = (SATInstance)instance;
        }
    }
    
    public void init(BaseIndividual[] population) {
        
    }
    public void onStart(BaseIndividual[] population){
        
    }
    
    public void postEvaluate(BaseIndividual[] population, double min, double max) {
        
    }
    
    public boolean isEnd(BaseIndividual bi){
        return (bi.getEvaluation() == 1000);
    }
    
    public double getEvaluation(BaseIndividual str){
        int count = 0;
        for(int i=0;i<instance.clausesCount;i++){
            if(instance.canSatisfyClause(i,(BinaryIndividual)str))count++;
        }
        return (((double)count/(double)instance.clausesCount)*1000);
    }
    
    public void setSATInstance(SATInstance instance){
        this.instance = instance;
    }
    
    public String toString(){
        return "SAT Fitness";
    }
    
    /** Creates a new instance of SATFitness */
    public SATFitness() {
        super();
    }
    
}
