Question

I am trying to construct a comparator for the priorityQueue that takes in returned from Weighter<? super Vlabel> class and Distancer<? super Vlabel>.

Here is the code:

public static <VLabel, ELabel> List<Graph<VLabel, ELabel>.Edge> shortestPath(
            Graph<VLabel, ELabel> G,
            Graph<VLabel, ELabel>.Vertex V0,
            Graph<VLabel, ELabel>.Vertex V1,
            Distancer<? super VLabel> h, //heuristic value
            Weighter<? super VLabel> vweighter,
            Weighting<? super ELabel> eweighter) {

        Stack<Graph<VLabel, ELabel>.Vertex> closedSet
            = new Stack<Graph<VLabel, ELabel>.Vertex>();
        int initial = G.vertexSize();
        PriorityQueue<Graph<VLabel, ELabel>.Vertex> fringe
            = new PriorityQueue(initial, new Comparator<Graph<VLabel, ELabel>.Vertex>() {
                public int compare(Graph<VLabel, ELabel>.Vertex v0, Graph<VLabel, ELabel>.Vertex v1) {
                    double temp1 = vweighter.weight(v0.getLabel()) + h.dist(v0, V1);
                    double temp2 = vweighter.weight(v1.getLabel()) + h.dist(v1, V1);
                    return - (int) temp1.compareTo(temp2);
                } 
            });
    }

However it is not working because the code complains that it is unable to identify vweighter and h inside the compare method.
BTW, I am not allowed to set any parameters for shortestpath as final.

Was it helpful?

Solution

Make the vweighter and h parameters final:

shortestPath(Graph<VLabel, ELabel> G,
             Graph<VLabel, ELabel>.Vertex V0,
             Graph<VLabel, ELabel>.Vertex V1,
             final Distancer<? super VLabel> h, //heuristic value
             final Weighter<? super VLabel> vweighter,
             Weighting<? super ELabel> eweighter) {

EDIT:

If you for some reason aren't allowed to use the final operator on the parameter variables you can redeclare them:

final Distancer<? super VLabel> myH = h;
final Weighter<? super VLabel> myVweighter = vWeighter;

And then use the redeclared variables in the comparator.

UPDATE 2:

If you want to make a class that implements the comparable interface and you want to change the functions as you go, this is how you would make that class:

public class WeightComparator implements
        Comparator<Graph<VLabel, ELabel>.Vertex> {

    private Graph<VLabel, ELabel> G;
    private Graph<VLabel, ELabel>.Vertex V0;
    private Graph<VLabel, ELabel>.Vertex V1;
    private Distancer<? super VLabel> h; //heuristic value
    private Weighter<? super VLabel> vweighter;
    private Weighting<? super ELabel> eweighter;

    public WeightComparator() {

    }

    @Override
    public int compare(Graph<VLabel, ELabel>.Vertex o1, Graph<VLabel, ELabel>.Vertex o2) {
        // Your compare statement
    }

    // Getters and setters for all variables
}

When instanciating it you should keep the reference and update the variables as needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top