Exemplo n.º 1
0
  void insert_min_cost(int[] route, int node, int l)
        // l - number of already inserted nodes
      {
    if (l == 1) route[1] = node;
    else {
      double inscost = 0.0;
      int pos = 0;
      inscost += tspp.get_distance(route[0], node);
      inscost += tspp.get_distance(node, route[1]);
      inscost -= tspp.get_distance(route[0], route[1]);

      for (int i = 1; i < l; i++) {
        double newinscost = 0.0;
        newinscost += tspp.get_distance(route[i], node);
        newinscost += tspp.get_distance(node, route[(i + 1) % l]);
        newinscost -= tspp.get_distance(route[i], route[(i + 1) % l]);
        if (newinscost < inscost) {
          inscost = newinscost;
          pos = i;
        }
      }
      for (int j = l; j > pos + 1; j--) route[j] = route[j - 1];
      route[pos + 1] = node;
    }
  }
Exemplo n.º 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;
  }
Exemplo n.º 3
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;
 }
Exemplo n.º 4
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;
  }
Exemplo n.º 5
0
  /** Function that calculates the cost of a given solution in matrix representation */
  double total_cost(int[][] routes) {
    // it doesn't check if it is a valid solution in terms of capacities
    double cost = 0;

    for (int v = 0; v < nv; v++) // for each vehicle
    {
      double costv = 0;
      // if(routes[v][0] != -1)
      cost += tspp.cost(routes[v]);
    }
    return cost;
  }
Exemplo n.º 6
0
 void improve_routes_2opt(int[][] routes) {
   for (int i = 0; i < routes.length; i++) {
     tspp.one_pass_2opt(routes[i]);
   }
 }