summaryrefslogtreecommitdiffstats
path: root/watchmaker/examples/src/java/main/org/uncommons/watchmaker/examples/travellingsalesman/RouteEvaluator.java
blob: 144d593a50f97aa26497d05c8e44f6357537476e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//=============================================================================
// Copyright 2006-2010 Daniel W. Dyer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//=============================================================================
package org.uncommons.watchmaker.examples.travellingsalesman;

import java.util.List;
import org.uncommons.watchmaker.framework.FitnessEvaluator;

/**
 * Fitness evalator that measures the total distance of a route in the travelling salesman
 * problem.  The fitness score of a route is the total distance (in km).  A route
 * is represented as a list of cities in the order that they will be visited.
 * The last leg of the journey is from the last city in the list back to the
 * first.
 * @author Daniel Dyer
 */
public class RouteEvaluator implements FitnessEvaluator<List<String>>
{
    private final DistanceLookup distances;


    /**
     * @param distances Provides distances between a set of cities.
     */
    public RouteEvaluator(DistanceLookup distances)
    {
        this.distances = distances;
    }


    /**
     * Calculates the length of an evolved route. 
     * @param candidate The route to evaluate.
     * @param population {@inheritDoc}
     * @return The total distance (in kilometres) of a journey that visits
     * each city in order and returns to the starting point.
     */
    public double getFitness(List<String> candidate,
                             List<? extends List<String>> population)
    {
        int totalDistance = 0;
        int cityCount = candidate.size();
        for (int i = 0; i < cityCount; i++)
        {
            int nextIndex = i < cityCount - 1 ? i + 1 : 0;
            totalDistance += distances.getDistance(candidate.get(i),
                                                   candidate.get(nextIndex));
        }
        return totalDistance;
    }


    /**
     * {@inheritDoc}
     * Returns false since shorter distances represent fitter candidates.
     * @return false
     */
    public boolean isNatural()
    {
        return false;
    }
}