@Override
    public void mutate(){
        TspPopulation offspring = new TspPopulation();
        for (int i = 0; i < population.getSize(); ++i){
            double r;
            synchronized (rng){
                r = rng.nextDouble();
            }
            if (r < PROBABILITY){
                TspIndividual mutant = new TspIndividual(population.getIndividual(i));
                int possition1;
                int length = 0;
                synchronized (rng){
                    length = rng.nextInt(3);
                }
                length += 3;
                synchronized (rng){
                    possition1 = Math.abs(rng.nextInt(mutant.size() - length + 1));
                }
                int possition2 = possition1 + length - 1;
                int[] path = mutant.getPath();
                List<Integer> shuffled = new ArrayList<Integer>();
                for (int k = possition1; k <= possition2; ++k){
                    shuffled.add(path[k]);
                }
                Collections.shuffle(shuffled);
                for (int k = possition1; k <= possition2; ++k){
                    path[k] = shuffled.get(k - possition1);
                }
                mutant.setPath(path);
                offspring.addIndividual(mutant);
                if (!mutant.check()){
                    System.out.println("ALARM!!!");
                }
            } else {
                offspring.addIndividual(new TspIndividual(population.getIndividual(i)));
            }
        }

        offspring.setGraph(population.getGraph());

        double bestOld = population.getFittest();
        double bestNew = offspring.getFittest();
        if (bestNew < bestOld){
            reward = 1;
//            reward = (bestOld - bestNew) / bestOld;
            for (int i = 0; i < population.getSize(); ++i){
                population.setIndividual(i, offspring.getIndividual(i));
            }
        } else if (bestNew == bestOld){
//            reward = 0.5;
            reward = 0;
        } else {
            reward = 0;
        }
    }