Clarke Wright循环问题

时间:2014-11-17 07:19:47

标签: java algorithm

我正在尝试实施Clarke Wright算法。我似乎遇到的问题是它融合了许多路线,无论我已经实施的测试如何。我已经盯着这个很长一段时间了,所以我觉得在它上面涂上一双新眼睛可能会有所帮助。

不是创建有效的路线(即,货车有足够的空间来承载所有东西),而是无论它是否有能力,它都会在各处创造路线。

public void CWVRSolution(){
    List<SavingsNode> savings = new ArrayList<SavingsNode>();
    //  ArrayList<List<Customer>> routes = new ArrayList<List<Customer>>();
    this.soln = new ArrayList<List<Customer>>();
    //ArrayList<List<Customer>> routelist = new ArrayList<List<Customer>>();


    for (int i=0; i < prob.customers.size(); i++){
        for (int j=i+1; j<prob.customers.size();j++){
            if (i != j){
                double savingVal = (prob.depot.distance(prob.customers.get(i)) + prob.depot.distance(prob.customers.get(j))) - prob.customers.get(i).distance(prob.customers.get(j));                       
                savings.add(new SavingsNode(prob.customers.get(i),prob.customers.get(j),savingVal));
            }       
        }
    }   
    Collections.sort(savings);
    Collections.reverse(savings);


    int sncounter = 0;
    for (SavingsNode sn: savings ){

        // for every route in prob.soln
        // if from or to isnt in any of those routes
        // do that stuff

        if (!soln.contains(sn.i)){
            if(!soln.contains(sn.j)){
                if ((sn.i.c + sn.j.c) <= 50){
                    ArrayList<Customer> route = new ArrayList<Customer>();
                    route.add(sn.i);
                    route.add(sn.j);
                    soln.add(route);

                }
            }

        }else if(!soln.contains(sn.j)){
            //find a route that ends in from
            int d = 0;
            int e = 0;
            for (List<Customer> c: soln){
                for (Customer x : c){
                    d += x.c;
                }
                if (c.get(c.size()-1) == sn.i){
                    if (d + sn.j.c <= 50){
                        //  Customer route = new Customer(sn.i.c, sn.j.c, cust.requirement);
                        ArrayList<Customer> newRoute = new ArrayList<Customer>();
                        newRoute.add(sn.i);

                        //  prob.customers.remove(sn.j);
                        //  soln.remove(c);
                        c.addAll(newRoute);
                        soln.add(c);
                        d=0;
                        //newRoute.clear();
                        break;
                    }
                }   
            }
        }


        if (!soln.contains(sn.i)){
            int d = 0;
            int e = 0;
            for (List<Customer> c:soln){
                if (c.get(0) == sn.j){
                    for (Customer x : c){
                        d += x.c;
                    }

                    if (d+ sn.i.c <= 50){
                        ArrayList<Customer> newRoute = new ArrayList<Customer>();
                        newRoute.add(sn.j);
                        soln.remove(c);
                        c.addAll(0, newRoute);
                        soln.add(c);
                        d=0;
                        //  soln.remove(arg0)
                        break;
                    }
                }   
            }       
        }

        List<Customer> merged = null;
        int mergescounter = 0;
        int xreq= 0;
        int yreq = 0;


        for (List<Customer> routeX: soln){
            if (merged != null){
                break;
            }
            if (routeX.get(routeX.size()-1)  == sn.i){
                for(Customer c:routeX){
                    xreq += c.c;
                }
                for (List<Customer> routeY: soln){
                    if (routeY.get(0) == sn.j){
                        for(Customer c:routeY){
                            yreq += c.c;
                        }
                        if ((routeX!=null)&&(routeY!=null)){
                            if (routeX != routeY){
                                int capnec = 0;
                                capnec = yreq+xreq;
                                if (sn.i.c +sn.j.c + capnec <= 50){
                                    List<Customer> merged1 = new ArrayList<Customer>();

                                    merged1.addAll(routeY);
                                    merged1.addAll(routeX);
                                    //  route.add(merged1);
                                    soln.add(merged1);
                                    mergescounter++;
                                    merged = routeX;
                                    capnec=0;
                                    break;

                                }
                            }
                        }
                    }
                }   
            }
        }
        if (merged!=null){
            soln.remove(merged);
        }
 }

客户类:

public class Customer extends Point2D.Double{

// Requirements of the customer (number to be delivered)
public static int c;
public int requirement;
public Customer(int x, int y, int requirement){
    this.x = x;
    this.y = y;
    //parcel requirement
    this.c = requirement;
}

SavingsNode class

SavingsNode(Customer i, Customer j, double savingVal){
    this.i = i;
    this.j= j;
    this.savingVal = savingVal;
}

任何帮助都将不胜感激。

0 个答案:

没有答案