示例#1
0
 int[] improve_routes_2opt(int[] sol) {
   int[] res;
   int[][] routes = get_routes(sol);
   for (int i = 0; i < routes.length; i++) if (routes[i].length > 2) tspp.one_pass_2opt(routes[i]);
   res = routes_to_array(routes);
   return res;
 }
示例#2
0
  // String exchange move - only when improves
  // returns cost of new solution
  // mode = ALWAYS_PERFORM ou PERFORM_ON_IMPROVE
  double string_exchange(
      int[][] routes,
      int r1,
      int r2,
      int x1,
      int x2,
      int c1,
      int c2,
      int mode,
      boolean feasible,
      boolean sw) {
    int[] newr1;
    int[] newr2;
    int i, j, k;
    double res;

    newr1 = new int[routes[r1].length - x1 + x2];
    newr2 = new int[routes[r2].length - x2 + x1];

    for (i = 0, k = 0; i < c1; i++, k++) newr1[k] = routes[r1][i];
    for (j = c2; j < c2 + x2; j++, k++) newr1[k] = routes[r2][j];
    for (; i < c1 + x1; i++) ;
    for (; i < routes[r1].length; i++, k++) newr1[k] = routes[r1][i];

    for (i = 0, k = 0; i < c2; i++, k++) newr2[k] = routes[r2][i];
    for (j = c1; j < c1 + x1; j++, k++) newr2[k] = routes[r1][j];
    for (; i < c2 + x2; i++) ;
    for (; i < routes[r2].length; i++, k++) newr2[k] = routes[r2][i];

    if (!feasible) {
      double oldpen = excess_demand_route(routes[r1]) + excess_demand_route(routes[r2]);
      double newpen = excess_demand_route(newr1) + excess_demand_route(newr2);
      if (mode == ALWAYS_PERFORM || newpen < oldpen) {
        if (sw) {
          routes[r1] = newr1;
          routes[r2] = newr2;
        }
        res = oldpen - newpen;
      } else res = 0.0;
    } else {
      double newpen = excess_demand_route(newr1) + excess_demand_route(newr2);
      if (newpen > 0.0 && mode != ALWAYS_PERFORM) res = 0.0;
      else {
        double oldcost = tspp.cost(routes[r1]) + tspp.cost(routes[r2]);
        tspp.one_pass_2opt(newr1);
        tspp.one_pass_2opt(newr2);
        double newcost = tspp.cost(newr1) + tspp.cost(newr2);

        if (mode == ALWAYS_PERFORM || oldcost > newcost) {
          if (sw) {
            routes[r1] = newr1;
            routes[r2] = newr2;
          }
          res = oldcost - newcost;
        } else res = 0.0;
      }
    }
    return res;
  }
示例#3
0
  // String cross move - only perform if there is improvement
  // returns improvement
  // if sw is true perform the switch when solution is better else only return value
  // mode = ALWAYS_PERFORM ou PERFORM_ON_IMPROVE
  double string_cross(
      int[][] routes, int r1, int r2, int c1, int c2, int mode, boolean feasible, boolean sw) {
    double res;
    int[] newr1;
    int[] newr2;
    int i, j;

    newr1 = new int[c1 + (routes[r2].length - c2)];
    newr2 = new int[c2 + (routes[r1].length - c1)];

    for (i = 0; i < c1; i++) newr1[i] = routes[r1][i];
    for (j = c2; j < routes[r2].length; j++, i++) newr1[i] = routes[r2][j];

    for (i = 0; i < c2; i++) newr2[i] = routes[r2][i];
    for (j = c1; j < routes[r1].length; j++, i++) newr2[i] = routes[r1][j];

    if (!feasible) {
      double oldpen = excess_demand_route(routes[r1]) + excess_demand_route(routes[r2]);
      double newpen = excess_demand_route(newr1) + excess_demand_route(newr2);
      if (mode == ALWAYS_PERFORM || newpen < oldpen) {
        if (sw) {
          routes[r1] = newr1;
          routes[r2] = newr2;
        }
        res = oldpen - newpen;
      } else res = 0.0;
    } else {
      double newpen = excess_demand_route(newr1) + excess_demand_route(newr2);
      if (newpen > 0.0 && mode != ALWAYS_PERFORM) res = 0.0;
      else {
        double oldcost = tspp.cost(routes[r1]) + tspp.cost(routes[r2]);
        tspp.one_pass_2opt(newr1);
        tspp.one_pass_2opt(newr2);
        double newcost = tspp.cost(newr1) + tspp.cost(newr2);

        if (mode == ALWAYS_PERFORM || oldcost > newcost) {
          if (sw) {
            routes[r1] = newr1;
            routes[r2] = newr2;
          }
          res = oldcost - newcost;
        } else res = 0.0;
      }
    }

    return res;
  }
示例#4
0
 void improve_routes_2opt(int[][] routes) {
   for (int i = 0; i < routes.length; i++) {
     tspp.one_pass_2opt(routes[i]);
   }
 }