public int calculateFitness(Hypothesis h)
    {
        if (type == RANDOM)
        {
            //Just return a random fitness
            return (int)(Math.random() * 100);
        }
        else if (type == TARGET)
        {
            double sumPerc = 0;
            int hIndex = 0;
            for (int i = 0; i < targetValues.length; i++)
            {
                //Convert each block of seven bits to an integer value
                String binStr = "";
                for (int j = 0; j < 7; j++)
                {
                    binStr += h.bitstring[hIndex] + "";
                    hIndex++;
                }
                int cVal = Integer.parseInt(binStr, 2); //2 for binary
                
                if (debug) System.out.println(binStr + " -> " + cVal);
                
                //Calculate how much the hypothesis fitness value
                //differs from the target value.
                int diff = Math.abs(cVal - targetValues[i]);
                double perc = (double)100 - (double)diff / (double)128 * (double)100;
                
                //Add a small random diff to fitness for testing purpose
                if (rndFitness)
                {
                    double rndDiff = Math.random() * 10 - 5;
                    perc += rndDiff;
                    if (perc < 0) perc = 0;
                    if (perc >= 100) perc = 100;
                }
                
                sumPerc += perc;
                
                if (debug) System.out.println(targetValues[i] + "/" + cVal + ": diff=" + diff + " perc=" + perc);
            }
            
            //Return average diff (in percent)
            return (int)(sumPerc / (double)targetValues.length);
        }
        else
        {
            return 10;
        }
    }