package cz.pcland.gavisualization;
/*
 * GAVisualizationEvaluation.java
 *
 * Created on 28. prosinec 2005, 23:28
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

import cz.pcland.ga.GAConfiguration;
import cz.pcland.ga.GenerationInfo;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.geom.*;
import java.awt.font.*;

/**
 *
 * @author petr
 */




public class GAVisualizationEvaluation extends BaseVisualization {
    
    public GAVisualizationEvaluation(int xPoints, int yPoints, String label) {
        super(xPoints, yPoints, label);
    }
    
    public void setParams(){
        super.setParams();
        
        /* Finds minimum and maximum rank of all generations*/
        maxValue = ((GenerationInfo)GAInfo.get(0)).getBestRank();
        minValue = ((GenerationInfo)GAInfo.get(0)).getLowestRank();
        
        
        for(int i=1; i<GAInfo.size();i++) {
            double temp = ((GenerationInfo)GAInfo.get(i)).getBestRank();
            double worth = ((GenerationInfo)GAInfo.get(i)).getLowestRank();
            if(minValue > worth) {
                minValue = worth;
            }
            if(maxValue < temp) {
                maxValue = temp;
            }
        }
         
    }
    
    
    
    
    public void drawLegend(Graphics2D g2) {
        FontRenderContext frc = g2.getFontRenderContext();
        g2.setColor(Color.orange);
        Font f = new Font("Courier", 2, 10);
        g2.drawString( "best", 20, 18);
        
        g2.setColor(Color.green);
        g2.drawString( "avg", 60, 18);
        
        g2.setColor(Color.red);
        g2.drawString( "worth", 100, 18);
        
        g2.setColor(Color.black);
        if(label != null)g2.drawString(label , 145, 18); 
        else
        g2.drawString( "Evaluation of solution (fitness may differ)", 145, 18);
        
        
        g2.drawString( "Axes: Y [Evaluation], X [generation]", this.getWidth() - 220, 18);
        
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        Graphics2D g2;
        g2 = (Graphics2D) g;
        
        
        
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        
        
        this.setBackground(Color.white);
        
         
        
        FontRenderContext frc = g2.getFontRenderContext();
        Font f = new Font("Courier", 1, 10);
        
        
        if(this.GAInfo == null || this.GAInfo.isEmpty()) {
            g2.setColor(Color.blue);
            g2.drawString( "No data to display ...", 20, 18);
            return;
        }
        
        this.setParams();
        
        g2.setColor(Color.black);
        
        if(GAConfiguration.distributionEnabled){
            for(int i=GAInfo.size()-1;i>=0;i--) {
                drawDistribution(g2,((GenerationInfo)(GAInfo.get(i))).getDistributionRanks() , i, ((GenerationInfo)(GAInfo.get(i))).getBestRank(), ((GenerationInfo)(GAInfo.get(i))).getLowestRank());
            }
        }
        
        for(int i=GAInfo.size()-1;i>=0;i--) {
            drawValue(g2, (int)((GenerationInfo)(GAInfo.get(i))).getGenNum() , ((GenerationInfo)(GAInfo.get(i))).getBestRank(), ((GenerationInfo)(GAInfo.get(i))).getAvgRank(), ((GenerationInfo)(GAInfo.get(i))).getLowestRank() );
        }
        
        drawLegend(g2);
        drawAxes(g2);
        
    }
}


