【问题标题】:How to fix Null Point Exception in Google Or-Tools solution?如何修复 Google Or-Tools 解决方案中的空点异常?
【发布时间】:2019-10-13 04:08:06
【问题描述】:

当我为 Google Or-Tools 创建测试并发送我的距离矩阵时,解决方案始终为空。

当我从这里使用默认距离矩阵时https://developers.google.com/optimization/routing/vrp 但是当我使用我的自定义 distanceMatrix 数组时,Assigment 解决方案始终为空。

哪里出错了?

完整的测试课

import com.google.ortools.constraintsolver.*;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.logging.Logger;

@RunWith(SpringRunner.class)
@SpringBootTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OrToolsTests {

    private static final Logger logger = Logger.getLogger(OrToolsTests.class.getName());

    static {
        System.loadLibrary("jniortools");
    }

    @Test
    public void b_googleOrTools() throws Exception {
        // Instantiate the data problem.
        final DataModel data = new DataModel();

        // Create Routing Index Manager
        RoutingIndexManager manager =
                new RoutingIndexManager(data.data.length, data.vehicleNumber, data.depot);

        // Create Routing Model.
        RoutingModel routing = new RoutingModel(manager);

        // Create and register a transit callback.
        final int transitCallbackIndex =
                routing.registerTransitCallback((long fromIndex, long toIndex) -> {
                    // Convert from routing variable Index to user NodeIndex.
                    int fromNode = manager.indexToNode(fromIndex);
                    int toNode = manager.indexToNode(toIndex);
                    return data.data[fromNode][toNode];
                });

        // Define cost of each arc.
        routing.setArcCostEvaluatorOfAllVehicles(transitCallbackIndex);

        // Add Distance constraint.
        routing.addDimension(transitCallbackIndex, 0, 3000,
                true, // start cumul to zero
                "Distance");
        RoutingDimension distanceDimension = routing.getMutableDimension("Distance");
        distanceDimension.setGlobalSpanCostCoefficient(100);

        // Setting first solution heuristic.
        RoutingSearchParameters searchParameters =
                main.defaultRoutingSearchParameters()
                        .toBuilder()
                        .setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
                        .build();

        // Solve the problem.
        Assignment solution = routing.solveWithParameters(searchParameters);

        // Print solution on console.
        printSolution(data, routing, manager, solution);
    }

    private void printSolution(
            DataModel data, RoutingModel routing, RoutingIndexManager manager, Assignment solution) {
        // Inspect solution.
        long maxRouteDistance = 0;
        for (int i = 0; i < data.vehicleNumber; ++i) {
            long index = routing.start(i);
            logger.info("Route for Vehicle " + i + ":");
            long routeDistance = 0;
            String route = "";
            while (!routing.isEnd(index)) {
                route += manager.indexToNode(index) + " -> ";
                long previousIndex = index;
                index = solution.value(routing.nextVar(index));
                routeDistance += routing.getArcCostForVehicle(previousIndex, index, i);
            }
            logger.info(route + manager.indexToNode(index));
            logger.info("Distance of the route: " + routeDistance + "m");
            maxRouteDistance = Math.max(routeDistance, maxRouteDistance);
        }
        logger.info("Maximum of the route distances: " + maxRouteDistance + "m");
    }

    class DataModel {
        public final long[][] data = {
                {0, 58305, 41338, 16599, 22834, 36364, 24979, 9797, 22023, 5880, 21282, 39248},
                {58147, 0, 82634, 49102, 39605, 93558, 72909, 49687, 67379, 55889, 76274, 22047},
                {41663, 81167, 0, 47046, 58798, 56366, 18071, 41586, 60532, 36117, 49432, 74933},
                {15666, 49990, 53000, 0, 7358, 51077, 30428, 13558, 34534, 13408, 33793, 30933},
                {22151, 39552, 59486, 7391, 0, 57563, 36914, 20043, 41019, 19893, 40278, 24535},
                {36164, 93036, 56237, 51331, 57566, 0, 37419, 44529, 44381, 41822, 17567, 73980},
                {25224, 72585, 18046, 30880, 37115, 37483, 0, 25147, 44093, 19678, 30549, 53529},
                {9218, 49544, 41424, 13485, 19719, 44630, 25065, 0, 28086, 5966, 27345, 30488},
                {22816, 72597, 60379, 34430, 40665, 44423, 44020, 27629, 0, 24921, 27138, 57079},
                {6858, 56163, 36826, 14458, 20693, 42270, 20467, 6781, 25726, 0, 24985, 37107},
                {22678, 75998, 49002, 34292, 40527, 17284, 30184, 27490, 27342, 24783, 0, 56941},
                {38986, 22165, 76018, 29941, 24347, 74398, 53749, 30526, 57854, 36728, 57113, 0}};
        public final int vehicleNumber = 4;
        public final int depot = 0;
    }
}

【问题讨论】:

  • 如果您遇到异常,您应该发布堆栈跟踪并指出代码中发生异常的行。
  • 空解是求解器未找到任何解的标志。因此,首先您应该保护您的代码,以便在它为空时它不会尝试显示解决方案。然后你需要调试你的模型为什么不可行(没有足够的车辆,路线限制太紧......)

标签: java spring matrix or-tools vehicle-routing


【解决方案1】:

据我了解,您添加了一个最大累积为 3000 的维度,而距离矩阵有很多超过 10k 的节点间距离。

【讨论】:

    【解决方案2】:

    #addDimension 中的3000 值代表车辆最大行驶距离。由于距离矩阵中的值较高,因此 or-tools 无法找到解决方案。

    要解决此问题,您必须更改 3000 值,使其高于距离矩阵中的最大值。例如

    改变

    // Add Distance constraint.
    routing.addDimension(transitCallbackIndex, 0, 3000,
            true, // start cumul to zero
            "Distance");
    

    // Add Distance constraint.
    routing.addDimension(transitCallbackIndex, 0, 100000,
            true, // start cumul to zero
            "Distance");
    

    你的代码就可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      • 2019-10-15
      • 2020-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-06
      相关资源
      最近更新 更多