使用java解决车辆路径问题时如何解决运行时问题而不得到结果

问题描述

我想在尊重时间窗口的约束的同时解决 vehicle routing problem我有以下课程

  public class VrpProblem {

private static int maxLocomotives=3;
private int numRames;
private int[] serviceTimes;
private int[] windowStartTimes;
private int[] windowEndTimes;
public double[][] rameTimes;
public double[] timesFrommultimodal;

private double maxTime;

public VrpProblem(int numRames,double[][] rameTimes,double[] timesFrommultimodal,int[] serviceTimes,int[] windowStartTimes,int[] windowEndTimes) {

    this.numRames = numRames;
    this.serviceTimes = serviceTimes;
    this.windowStartTimes = windowStartTimes;
    this.windowEndTimes = windowEndTimes;
    this.timesFrommultimodal = timesFrommultimodal;
    double[][] mat=new double[numRames][];
    for (int i=0 ; i<mat.length; i++)
        mat[i]=new double[numRames];
    for (int i = 0; i < numRames; i++) {
          
          for (int j = 0; j < numRames; j++) {
            mat [i][j] = rameTimes[i][j];
            if (rameTimes[i][j] > maxTime) {
              maxTime= rameTimes[i][j];
            }
          }
        }
    
    this.rameTimes = mat;


      }

public int[] getServiceTimes() {
    return serviceTimes;
}

public int[] getwindowStartTimes() {
    return windowStartTimes;
}

public double getMaxTime() {
    return maxTime;
}

//si Id est negatif,Il refere au multimodal
  public double getTime(int custId1,int custId2) {
    if (custId1 >= 0 && custId2 >= 0) {
      return rameTimes[custId1][custId2];
    } else if (custId1 >= 0) {
      return timesFrommultimodal[custId1];
    } else if (custId2 >= 0){
      return timesFrommultimodal[custId2];
    } else {
      return 0; //les 2 multimodal
    }
  }

public int[] getwindowEndTimes() {
    return windowEndTimes;
}

public double[][] getRameTimes() {
    return rameTimes;
}
public double[] getTimesFrommultimodal() {
    return timesFrommultimodal;
}

public int getNumRames() {
        return this.numRames;
      }}

还有 VrpReader

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

  public class VrpReader {

private static File f;

public static  VrpProblem readSolomon(String FilePath,int numRames) throws IOException{

        
    f = new File(FilePath);
    BufferedReader br = new BufferedReader(new FileReader(f));
    String line = br.readLine();
     String[] tokens = line.trim().split("\\s+");
        int depotX = (int)(Double.parseDouble(tokens[1]));
        int depotY = (int)(Double.parseDouble(tokens[2]));
        List<String> lines = new ArrayList<String>();
        for (int i = 0; i < numRames && (line = br.readLine()) != null; i++) {
          lines.add(line);
        }
        numRames = lines.size();
        int[] xCoors = new int[numRames];
        int[] yCoors = new int[numRames];
        int[] demands = new int[numRames];
        int[] windowStarts = new int[numRames];
        int[] windowEnds = new int[numRames];
        int[] serviceTimes = new int[numRames];
        double[][] rameTimes = new double[numRames][numRames];
        double[] timesFrommultimodal = new double[numRames];
        
        for (int i = 0; i < numRames; i++) {
            tokens = lines.get(i).trim().split("\\s+");
            //CUST NO.   XCOORD.   YCOORD.    DEMAND   READY TIME   DUE DATE   SERVICE TIME
            int x = (int)(Double.parseDouble(tokens[1]));
            xCoors[i] = (x);
            int y = (int)(Double.parseDouble(tokens[2]));
            yCoors[i] = (y);
            int windowStart = (int)(Double.parseDouble(tokens[4]));
            windowStarts[i] = (windowStart);
            int windowEnd = (int)(Double.parseDouble(tokens[5]));
            windowEnds[i] = (windowEnd);
            int serviceTime = (int)(Double.parseDouble(tokens[6]));
            serviceTimes[i] = (serviceTime);
          }
        
        for (int i = 0; i < numRames; i++) {
            for (int j = 0; j < numRames; j++) {
            int xDiff = xCoors[i] - xCoors[j];
            int yDiff = yCoors[i] - yCoors[j];
            rameTimes[i][j] = Math.sqrt(xDiff * xDiff + yDiff * yDiff);
            
          }
        }
    
        for (int i = 0; i < numRames; i++) {
              int xDiffFromDepot = xCoors[i] - depotX;
              int yDiffFromDepot = yCoors[i] - depotY;
              timesFrommultimodal[i] = Math.sqrt(xDiffFromDepot * xDiffFromDepot + yDiffFromDepot * yDiffFromDepot);
        }
        
        VrpProblem problem = new VrpProblem(numRames,rameTimes,timesFrommultimodal,serviceTimes,windowStarts,windowEnds);
        return problem;
        } }

还有 Solution

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

  public class InitialSolution {

private List<List<Integer>> routes;
private List<Integer> unrouted;
private int numLocomotives;
private VrpProblem problem;
private double toursCost = -1;

public InitialSolution(VrpProblem problem) {
    this.problem = problem;
    this.routes = new ArrayList<List<Integer>>();
    this.unrouted = new ArrayList<Integer>();
}

public InitialSolution(VrpProblem problem,List<List<Integer>> routes) {
    this.problem = problem;
    this.routes = routes;
    this.unrouted = new ArrayList<Integer>();

}

public InitialSolution(VrpProblem problem,List<List<Integer>> routes,List<Integer> unrouted) {
    this.problem = problem;
    this.routes = routes;
    this.unrouted = unrouted;

}

public void setNumLocomotives(int numLocomotives) {
    this.numLocomotives = numLocomotives;
}

public void setRoutes(List<List<Integer>> routes) {

    this.routes = routes;
}

public void setUnrouted(List<Integer> unrouted) {
    this.unrouted = unrouted;
}

private double calcToursCost(List<List<Integer>> routes,VrpProblem problem) {
    double[][] times = problem.getRameTimes();
    double[] timesFrommultimodal = problem.getTimesFrommultimodal();

    double toursCost = 0;
    for (List<Integer> route : routes) {
        Iterator<Integer> iter = route.iterator();
        if (!iter.hasNext()) {
            continue;
        }
        int prev = iter.next();
        toursCost += timesFrommultimodal[prev];
        while (iter.hasNext()) {
            int cur = iter.next();
            toursCost += times[prev][cur];
            prev = cur;
        }
        toursCost += timesFrommultimodal[prev];
    }
    return toursCost;
}

public VrpProblem getProblem() {
    return problem;
}

public List<List<Integer>> getRoutes() {
    return routes;
}

public List<Integer> getUninsertednodes() {
    return unrouted;
}

public int getNumLocomotives() {
    return numLocomotives;
}

public double getToursCost() {
    if (toursCost >= 0) {
        return toursCost;
    } else {
        return toursCost = calcToursCost(this.getRoutes(),this.getProblem());
    }
}

public void SolutionInitiale()

{   
    int[] windowStartTimes = problem.getwindowStartTimes();
    int[] windowEndTimes = problem.getwindowEndTimes();
    double[] timesFromDepot = problem.getTimesFrommultimodal();
    double[][] times = problem.getRameTimes();
    int[] serviceTimes = problem.getServiceTimes();
    HashSet<Integer> remainingNodes = new HashSet<Integer>();
    List<List<Integer>> routes =  new ArrayList<List<Integer>>();
    routes = this.getRoutes(); 
    for (int i = 0; i < problem.getNumRames(); i++) {
        remainingNodes.add(i);
        }
        for (List<Integer> route : routes) {
          for (Integer node : route) {
            remainingNodes.remove(node);
          }
        }
    
        List<Integer> curRoute = new ArrayList<Integer>();
        routes.add(curRoute);
        int curNodeId = -1;
        int bestNode = -1;
        double curVisitTime = 0;
        while (remainingNodes.size() > 0) {
            
            int currentServiceTime =  (curNodeId == -1) ? 0 : serviceTimes[curNodeId];
             Iterator<Integer> iter = remainingNodes.iterator();
                while (iter.hasNext()) {
                    int nextNodeId = iter.next();
                    double distance = (curNodeId == -1) ? timesFromDepot[nextNodeId] : times[curNodeId][nextNodeId];
                     double minVisitTime = Math.max(windowStartTimes[nextNodeId],curVisitTime + currentServiceTime + distance);
                      if (minVisitTime > windowEndTimes[nextNodeId]) {
                        continue;
                      }
                      bestNode = nextNodeId;
                      if (bestNode != -1) {
                            remainingNodes.remove(bestNode);
                            curRoute.add(bestNode);
                            curNodeId = bestNode;
                            curVisitTime = minVisitTime;
                          } else {
                            curRoute = new ArrayList<Integer>();
                            routes.add(curRoute);
                            curVisitTime = 0;
                            curNodeId = -1;
                          }
                      
                }
                
        }
        this.setRoutes(routes);
        this.setNumLocomotives(routes.size());
}      
 }

和主类

import java.io.IOException;
import java.util.ArrayList;
 import java.lang.management.ThreadMXBean;

       public class Test {

public static void main(String[] args) {
           VrpReader vr = new VrpReader();
          VrpProblem problem;
       try {
        problem =vr.readSolomon("D:\\c102.txt",100);
          Solution initial = new Solution(problem);
    
        initial.solutionInitiale();
        toString(initial);
        System.out.println("Pour test :"+initial.getToursCost());
        System.out.println("Pour test :"+initial.getNumLocomotives());
        System.out.println("-----------------------");




      } catch (IOException e) {
        // Todo Auto-generated catch block
        e.printstacktrace();
    }
    
  public static void toString(Solution sol) {
           System.out.println("La solution : " + sol.getRoutes()); 
  }
     }
         }

数据示例:
0 40 50 0 0 1236 0
1 45 68 10 0 1127 90
2 45 70 30 0 1125 90
3 42 66 10 0 1129 90
4 42 68 10 727 782 90

当我运行我的代码时,我在 int nextNodeId = iter.next(); 类中的 InitialSolution 行收到此错误

Exception in thread "main"
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1445)
at java.util.HashMap$KeyIterator.next(HashMap.java:1469)
at InitialSolution.solutionInitiale(InitialSolution.java:123)
at Test.main(Test.java:29)

我不知道该怎么办。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)