//check both scale and capacity public static bool checkScale(Route route, Customer customer, List<VehicleType> vehicleTypes) { int index = vehicleTypes.IndexOf(route.get_VehicleType()); if (!customer.isSuitableForType(index)) { return false; } if (route.get_totalWeight() + customer.get_totalWeight() > route.get_VehicleType().get_capacity()) { return false; } int arraySize = customer.scales[index].Count; double[] scale_sum = new double[arraySize]; for (int i = 0; i < scale_sum.Length; i++) { scale_sum[i] = 0; for (IEnumerator iterator = route.get_customerSequence().GetEnumerator(); ;) { if (!iterator.MoveNext()) break; Customer customer2 = (Customer)iterator.Current; scale_sum[i] += (double)customer2.scales[index][i]; } scale_sum[i] += (double)customer.scales[index][i]; if (scale_sum[i] > 1) { return false; } } return true; }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public static void resetAllRoutes(java.util.ArrayList<objects.Route> routes, objects.Customer depot) throws java.io.FileNotFoundException public static void resetAllRoutes(List<Route> routes, Customer depot) { foreach (Route route in routes) { //need to reset the coordinates of all items in the route Functions.pass_packingHeauristic(route, depot); if (route.get_customerSequence().Count > 2) { Functions.resetRoute(route, depot); } } }
public static void routeTrySendInvitationProcess(Route route, List<Customer> customers, List<VehicleType> vehicleTypes, Customer depot) { if (route.Flag == true) { List<CustomerDelta> deltaList = new List<CustomerDelta>(); // layer-D loop for (IEnumerator iterator = customers.GetEnumerator(); ;) { if (!iterator.MoveNext()) break; Customer customer = (Customer)iterator.Current; if (Functions.checkScale(route, customer, vehicleTypes)) { double delta1 = Functions.calculateDelta(route, customer, depot); CustomerDelta customerDelta = new CustomerDelta(customer, delta1); deltaList.Add(customerDelta); } } deltaList.Sort(CustomerDelta.sortByDelta); bool hasChosenCustomer = false; Customer chosenCustomer = null; double delta = 0; for (int j = 0; j < deltaList.Count; j++) { chosenCustomer = deltaList[j].Customer; delta = deltaList[j].Delta; if (Functions.pass_packingHeauristic(route, chosenCustomer)) { hasChosenCustomer = true; break; } } if (hasChosenCustomer) { Functions.sendInvitation(new Invitation(route, delta), chosenCustomer); } else { route.Flag = false; } } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public java.util.ArrayList<objects.Customer> readCustomers(java.util.ArrayList<objects.VehicleType> vehicleTypes) throws java.io.FileNotFoundException public virtual List<Customer> readCustomers(List<VehicleType> vehicleTypes) { List<Customer> customers = new List<Customer>(); // we put "inputs" folder under the root directory of the 2L-HFVRP // project. StreamReader customerScanner=null; if (GUIMode) { //customerScanner = new StreamReader(new FileStream(file)); } else { string customerFileName = "inputs/" + fileName_prefix + "_input_node.txt"; customerScanner = new StreamReader(customerFileName); } customerScanner.ReadLine(); int index = 0; while (!customerScanner.EndOfStream) { string line = customerScanner.ReadLine(); string[] lineScanner = line.Split(null); double x = Convert.ToDouble(lineScanner[0]); double y = Convert.ToDouble(lineScanner[1]); double totalWeight = Convert.ToDouble(lineScanner[2]); int numOfItems = Convert.ToInt32(lineScanner[3]); List<Item> items = new List<Item>(); if (numOfItems > 0) { for (int i = 0; i < numOfItems; i++) { items.Add(new Item(Convert.ToInt32(lineScanner[4+2*i]), Convert.ToInt32(lineScanner[5+2*i]), i, index)); } } Customer customer = new Customer(index, totalWeight, x, y, items, vehicleTypes); customers.Add(customer); index++; //lineScanner.Close(); } customerScanner.Close(); return customers; }
public static double calculateDelta(Route route, Customer customer, Customer depot) { if (route.get_customerSequence().Count == 1) { Customer customer_inRoute = route.get_customerSequence()[0]; double distance = depot.distance(customer_inRoute) + customer_inRoute.distance(customer) + customer.distance(depot); double delta = distance - route.get_totalDistance(); return delta; } else { int numNodes = route.get_customerSequence().Count + 2; write_linkernInputFile(route, customer, depot); double[] result = linkernTour(numNodes); double totalDistance = 0; for (int i = 0; i < result.Length; i++) { totalDistance += result[i]; } // route need to update total distance double delta = totalDistance - route.get_totalDistance(); return delta; } }
// only called when route contain >=3 customers // called in final step to reset routes public static void write_linkernInputFile_2(Route route, Customer depot) { StreamWriter writer; try { writer = new StreamWriter("linkern_input"); writer.WriteLine("NAME : 2L-HFVRP"); writer.WriteLine("COMMENT : 2L-HFVRP"); writer.WriteLine("COMMENT : 2L-HFVRP"); writer.WriteLine("TYPE : TSP"); // existing customers+depot+new customer int numNodes = route.get_customerSequence().Count + 1; writer.WriteLine("DIMENSION : " + numNodes); writer.WriteLine("EDGE_WEIGHT_TYPE : EUC_2D"); writer.WriteLine("NODE_COORD_SECTION"); writer.WriteLine("1 " + depot.get_x() + " " + depot.get_y()); for (int i = 2; i <= numNodes; i++) { Customer currentCus = route.get_customerSequence()[i - 2]; writer.WriteLine(i + " " + currentCus.get_x() + " " + currentCus.get_y()); } writer.Close(); } catch (FileNotFoundException e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } }
public static void sendInvitation(Invitation invitation, Customer customer) { customer.receiveInvitation(invitation); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public static void resetRoute(objects.Route route, objects.Customer depot) throws java.io.FileNotFoundException public static void resetRoute(Route route, Customer depot) { int numNodes = route.get_customerSequence().Count + 1; write_linkernInputFile_2(route, depot); linkernTour(numNodes); StreamReader scanner = new StreamReader("linkern_output"); List<Customer> newSequence = new List<Customer>(); int length = route.get_customerSequence().Count; // skip 2 lines scanner.ReadLine(); scanner.ReadLine(); for (int i = 0; i < length; i++) { string line = scanner.ReadLine(); String[] lineScanner = line.Split(null); int index = Convert.ToInt32(lineScanner[0]) - 1; Customer tempCustomer = route.get_customerSequence()[index]; newSequence.Add(tempCustomer); } route.CustomerSequence = newSequence; }
public static void printRoutes(List<Route> routes, Customer depot) { double totalCost = 0; for (int i = 0; i < routes.Count; i++) { Console.WriteLine(); Console.WriteLine("Route " + i + ":"); Console.Write("vehicleType is: "); routes[i].get_VehicleType().print(); Console.WriteLine("Total Weight: " + routes[i].get_totalWeight() + " Total Distance: " + routes[i].get_totalDistance() + " Total Cost: " + routes[i].get_totalCost()); totalCost += routes[i].get_totalCost(); Console.WriteLine(); // print the customer sequence of this route Console.WriteLine("depot " + "(" + depot.get_x() + "," + depot.get_y() + ") "); Console.WriteLine(); Console.WriteLine("-->"); List<Customer> sequence = routes[i].get_customerSequence(); for (int j = 0; j < sequence.Count; j++) { Customer currentCus = sequence[j]; currentCus.print(); Console.WriteLine("-->"); // System.out.print("Cus"+currentCus.get_index()+" Weight:"+currentCus.get_totalWeight()+" ("+currentCus.get_x()+","+currentCus.get_y()+") -> "); } Console.WriteLine(" depot " + "(" + depot.get_x() + "," + depot.get_y() + ")"); Console.WriteLine(); // print all items and the coordinates Console.WriteLine("Items and coordinates:"); for (int j = 0; j < sequence.Count; j++) { Customer currentCus = sequence[j]; List<Item> items = currentCus.get_items(); for (IEnumerator iterator = items.GetEnumerator(); ;) { if (!iterator.MoveNext()) break; Item item = (Item)iterator.Current; Console.WriteLine(item.ToString()); } } Console.WriteLine(); } Console.WriteLine("------------------------------------------------"); Console.WriteLine("totalCost: " + totalCost); }
public static bool pass_packingHeauristic(Route route, Customer customer) { List<Item> test = new List<Item>(); List<Item> placed = new List<Item>(); int length = route.get_customerSequence().Count; for (int i = 0; i < length; i++) { test.AddRange(route.get_customerSequence()[i].get_items()); } test.AddRange(customer.get_items()); if (PackingInGivenBin.placeItems(route.get_VehicleType().get_length(), route.get_VehicleType().get_width(), placed, test)) { // for (Item i : placed) System.out.println(i.toString()); if (PackingInGivenBin.checkForPackingFeasibility(route.get_VehicleType().get_length(), route.get_VehicleType().get_width(), placed)) { return true; } } return false; // checkForPackingFeasibility checks placed items for overlapping }
public static bool initiaRoutes(List<Route> routes, int[] initialVehicleSet, List<Customer> customers, List<VehicleType> vehicleTypes, Customer depot) { int newRouteGenerated = 0; for (int i = initialVehicleSet.Length - 1; i >= 0; i--) { int customer_index = customers.Count - 1; for (int j = 0; j < initialVehicleSet[i]; j++) { while (customer_index >= 0) { if (customers[customer_index].isSuitableForType(i)) { Route route = new Route(); route.VehicleType = vehicleTypes[i]; double fixedCost = vehicleTypes[i].get_fixedCost(); double variableCost = vehicleTypes[i].get_variableCost(); Customer customer = customers[customer_index]; customers.RemoveAt(customer_index); route.addCustomer(customer); double distance = customer.distance(depot); double cost = fixedCost + variableCost * distance * 2; route.set_totalDistance(distance * 2); route.set_totalCost(cost); routes.Add(route); newRouteGenerated++; break; } customer_index--; } if (customer_index < 0) { break; } customer_index--; if (customer_index < 0) { break; } } } if (newRouteGenerated == 0) { return false; } else { return true; } }