Skip to content

Commit

Permalink
Added Bundling and Advanced Constraints Examples. Change Depot in Adv…
Browse files Browse the repository at this point in the history
…ance Constrints from Source to Address
  • Loading branch information
r4m-juan committed Jul 8, 2022
1 parent 8799acb commit 31ef6e5
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.route4me</groupId>
<artifactId>route4me-java-sdk</artifactId>
<version>1.11.3</version>
<version>1.12.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.route4me.sdk.examples.advancedconstraints;

import com.route4me.sdk.exception.APIException;
import com.route4me.sdk.services.routing.Address;
import com.route4me.sdk.services.routing.Constants.AlgorithmType;
import com.route4me.sdk.services.routing.Constants.DeviceType;
import com.route4me.sdk.services.routing.Constants.OptimizationState;
import com.route4me.sdk.services.routing.Constants.TravelMode;
import com.route4me.sdk.services.routing.DataObject;
import com.route4me.sdk.services.routing.OptimizationParameters;
import com.route4me.sdk.services.routing.Parameters;
import com.route4me.sdk.services.routing.RoutingManager;
import com.route4me.sdk.services.routing.advancedconstraints.AdvancedConstraints;
import com.route4me.sdk.services.territories.TerritoriesManager;
import com.route4me.sdk.services.territories.Territory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author juan
*/
public class OptimizationUsingTerritoriesOrdersDifferentDepot {



public static void main(String[] args) {
try {
String apiKey = System.getenv("R4M_API_KEY");
RoutingManager manager = new RoutingManager(apiKey, true);
OptimizationParameters optParameters = new OptimizationParameters();

//**********************************************************************
// TEST CASE: Optimization using Territories Orders
// 3 Territories
//**********************************************************************

TerritoriesManager territoriesManager = new TerritoriesManager(apiKey);


Parameters parameters = new Parameters();
parameters.setAlgorithmType(AlgorithmType.ADVANCED_CVRP_TW.getValue());
parameters.setStoreRoute(Boolean.FALSE);
parameters.setShareRoute(Boolean.FALSE);
parameters.setRouteTime((8 + 5) * 3600);
parameters.setRouteName("Multiple Depot, Multiple Driver - 3 Territories Order IDs");
parameters.setDeviceType(DeviceType.WEB.toString());
parameters.setTravelMode(TravelMode.DRIVING.toString());
optParameters.setParameters(parameters);


// Territories
// **********************
List<String> zone1 = Arrays.asList("477FF1EB618FDE5C1CA85292B2A6DE92");
List<String> zone2 = Arrays.asList("2A6E027E74A3623762BEA3D6E94B643E");
List<String> zone3 = Arrays.asList("C8B7DEFFC4C42AB7674EA27CBC607B3F");



List<Address> addresses = new ArrayList<>();

Address address;

Territory territory = territoriesManager.getOrdersInTerritory(zone1.get(0));
for (Integer orderId: territory.getOrders()){
address = new Address();
address.setOrderId(orderId);
address.setTags(zone1);
addresses.add(address);
}

territory = territoriesManager.getOrdersInTerritory(zone2.get(0));
for (Integer orderId: territory.getOrders()){
address = new Address();
address.setOrderId(orderId);
address.setTags(zone2);
addresses.add(address);
}

territory = territoriesManager.getOrdersInTerritory(zone3.get(0));
for (Integer orderId: territory.getOrders()){
address = new Address();
address.setTags(zone3);
address.setOrderId(orderId);
addresses.add(address);
}

AdvancedConstraints schedule1 = new AdvancedConstraints();
schedule1.setDepotAddress(new Address("15110 Oakmont St, Overland Park, KS 66221, USA", "DEPOT KS", 38.854268, -94.7333895, 0));
schedule1.setTags(zone1);
schedule1.setMembersCount(1);
List<List<Integer>> timeWindowsSchedule1 = new ArrayList<>();
List<Integer> timeWindowSchedule1 = Arrays.asList((8 + 5) * 3600 , (11 + 5) * 3600);
timeWindowsSchedule1.add(timeWindowSchedule1);
schedule1.setAvailableTimeWindows(timeWindowsSchedule1);


// Schedule 2
// Time Window Start: 8:00 am EST
// Time Window End: 12:00 pm EST
AdvancedConstraints schedule2 = new AdvancedConstraints();
schedule2.setDepotAddress(new Address("754 5th Ave, New York, NY 10019, USA", "DEPOT NY", 40.7636197, -73.9744388, 0));
schedule2.setTags(zone2);
schedule2.setMembersCount(1);
List<List<Integer>> timeWindowsSchedule2 = new ArrayList<>();
List<Integer> timeWindowSchedule2 = Arrays.asList((8 + 5) * 3600 , (12 + 5) * 3600);
timeWindowsSchedule2.add(timeWindowSchedule2);
schedule2.setAvailableTimeWindows(timeWindowsSchedule2);


// Schedule 3
// Time Window Start: 8:00 am EST
// Time Window End: 01:00 pm EST
AdvancedConstraints schedule3 = new AdvancedConstraints();
schedule3.setDepotAddress(new Address("106 W Columbus Dr, Tampa, FL 33602, USA", "DEPOT FL", 27.9664433, -82.4564934, 0));

schedule3.setTags(zone3);
schedule3.setMembersCount(1);
List<List<Integer>> timeWindowsSchedule3 = new ArrayList<>();
List<Integer> timeWindowSchedule3 = Arrays.asList((8 + 5) * 3600 , (13 + 5) * 3600);
timeWindowsSchedule3.add(timeWindowSchedule3);
schedule3.setAvailableTimeWindows(timeWindowsSchedule3);


// Schedules registration
List<AdvancedConstraints> advancedConstraints = Arrays.asList(schedule1, schedule2, schedule3);
parameters.setAdvancedConstraints(advancedConstraints);

optParameters.setAddresses(addresses);

try {
DataObject responseObject = manager.runOptimization(optParameters);
System.out.println("Optimization Problem ID:" + responseObject.getOptimizationProblemId());
System.out.println("State:" + OptimizationState.get(responseObject.getState().intValue()));
if (responseObject.getAddresses() != null) {
for (Address addressResponse : responseObject.getAddresses()) {
System.out.println(addressResponse);
}
}
} catch (APIException e) {
//handle exception
e.printStackTrace();
}
} catch (APIException ex) {
Logger.getLogger(OptimizationUsingTerritoriesOrdersDifferentDepot.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* The MIT License
*
* Copyright 2022 Route4Me.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.route4me.sdk.examples.bundling;

import com.route4me.sdk.exception.APIException;
import com.route4me.sdk.services.routing.Address;
import com.route4me.sdk.services.routing.Bundling;
import com.route4me.sdk.services.routing.BundlingEnum;
import com.route4me.sdk.services.routing.Constants;
import com.route4me.sdk.services.routing.DataObject;
import com.route4me.sdk.services.routing.OptimizationParameters;
import com.route4me.sdk.services.routing.Parameters;
import com.route4me.sdk.services.routing.RoutingManager;
import com.route4me.sdk.services.routing.ServiceTimeRules;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
*
* @author route4me
*/
public class BundlingByAddressAndProrityConstraint {

public static void main(String[] args) {
String apiKey = System.getenv("R4M_API_KEY");
RoutingManager manager = new RoutingManager(apiKey, true);
OptimizationParameters optParameters = new OptimizationParameters();

Parameters parameters = new Parameters();
parameters.setAlgorithmType(Constants.AlgorithmType.CVRP_TW_SD.getValue());
parameters.setStoreRoute(Boolean.FALSE);
parameters.setShareRoute(Boolean.FALSE);
parameters.setRouteName("Single Depot, Multiple Driver, Bundling by Address and having Priority Constraint");
parameters.setTravelMode(Constants.TravelMode.DRIVING.toString());

List<Address> addresses = new ArrayList<>();
// DEPOT
addresses.add(new Address("1604 PARKRIDGE PKWY, Louisville, KY, 40214", true, 38.141598, -85.793846, 300));

// Addresses
addresses.add(new Address("1407 MCCOY, Louisville, KY, 40215", 38.202496, -85.786514, 300));
addresses.add(new Address("730 CECIL AVENUE, Louisville, KY, 40211", 38.248684, -85.821121, 300));
addresses.add(new Address("650 SOUTH 29TH ST UNIT 315, Louisville, KY, 40211", 38.251923, -85.800034, 300));
addresses.add(new Address("4629 HILLSIDE DRIVE, Louisville, KY, 40216", 38.176067, -85.824638, 300));

// Bundling Addresses
Address address;

address = new Address("318 SO. 39TH STREET, Louisville, KY, 40212", 38.259335, -85.815094, 300);
address.setPriority(1);
addresses.add(address);
address = new Address("318 SO. 39TH STREET, Louisville, KY, 40212", 38.259335, -85.815094, 300);
address.setPriority(2);
addresses.add(address);

address = new Address("4629 HILLSIDE DRIVE, Louisville, KY, 40216", 38.176067, -85.824638, 300);
address.setPriority(2);
addresses.add(address);
address = new Address("4629 HILLSIDE DRIVE, Louisville, KY, 40216", 38.176067, -85.824638, 300);
address.setPriority(3);
addresses.add(address);



address = new Address("4738 BELLEVUE AVE, Louisville, KY, 40215", 38.179806, -85.775558, 300);
address.setPriority(3);
addresses.add(address);
address = new Address("4738 BELLEVUE AVE, Louisville, KY, 40215", 38.179806, -85.775558, 300);
address.setPriority(4);
addresses.add(address);
address = new Address("4738 BELLEVUE AVE, Louisville, KY, 40215", 38.179806, -85.775558, 300);
address.setPriority(5);
addresses.add(address);
address = new Address("4738 BELLEVUE AVE, Louisville, KY, 40215", 38.179806, -85.775558, 300);
address.setPriority(6);


address = new Address("1324 BLUEGRASS AVE, Louisville, KY, 40215", 38.179253, -85.785118, 300);
address.setPriority(4);
addresses.add(address);
address = new Address("1324 BLUEGRASS AVE, Louisville, KY, 40215", 38.179253, -85.785118, 300);
address.setPriority(5);
addresses.add(address);
address = new Address("1324 BLUEGRASS AVE, Louisville, KY, 40215", 38.179253, -85.785118, 300);
address.setPriority(6);
addresses.add(address);
address = new Address("1324 BLUEGRASS AVE, Louisville, KY, 40215", 38.179253, -85.785118, 300);
address.setPriority(7);
addresses.add(address);
address = new Address("1324 BLUEGRASS AVE, Louisville, KY, 40215", 38.179253, -85.785118, 300);
address.setPriority(8);
addresses.add(address);
address = new Address("1324 BLUEGRASS AVE, Louisville, KY, 40215", 38.179253, -85.785118, 300);
address.setPriority(9);
addresses.add(address);

address = new Address("4805 BELLEVUE AVE, Louisville, KY, 40215", 38.162472, -85.792854, 300);
address.setPriority(5);
addresses.add(address);
address = new Address("4805 BELLEVUE AVE, Louisville, KY, 40215", 38.162472, -85.792854, 300);
address.setPriority(6);
addresses.add(address);
address = new Address("4805 BELLEVUE AVE, Louisville, KY, 40215", 38.162472, -85.792854, 300);
address.setPriority(7);
addresses.add(address);
address = new Address("4805 BELLEVUE AVE, Louisville, KY, 40215", 38.162472, -85.792854, 300);
address.setPriority(8);
addresses.add(address);

optParameters.setAddresses(addresses);

Bundling bundling = new Bundling();

bundling.setMergeMode(BundlingEnum.BundledItemsMode.KEEP_AS_SEPARATE_DESTINATIONS.getValue());

bundling.setMode(BundlingEnum.BundlingMode.BUNDLING_BY_ADDRESS.getValue());
List<String> modeParams = Arrays.asList("custom_fields.BUNDLING_KEY", "custom_fields.LOCATION_ID");
bundling.setModeParams(modeParams);
ServiceTimeRules serviceTimeRules = new ServiceTimeRules();
serviceTimeRules.setFirstItemMode(BundlingEnum.BundlingFirstItemMode.USE_CUSTOM_SERVICE_TIME.getValue());
serviceTimeRules.setAdditionalItemsMode(BundlingEnum.BundlingAdditionalItemMode.USE_CUSTOM_SERVICE_TIME_FOR_ADDITIONAL_ITEM.getValue());
ArrayList<Integer> firstItemModeParams = new ArrayList();
firstItemModeParams.add(900);
ArrayList<Integer> additionalItemModeParams = new ArrayList();
additionalItemModeParams.add(200);
serviceTimeRules.setFirstItemModeParams(firstItemModeParams);
serviceTimeRules.setAdditionalItemsModeParams(additionalItemModeParams);
bundling.setServiceTimeRules(serviceTimeRules);
parameters.setBundling(bundling);

optParameters.setParameters(parameters);

try {
DataObject responseObject = manager.runOptimization(optParameters);
System.out.println("Optimization Problem ID:" + responseObject.getOptimizationProblemId());
System.out.println("State:" + Constants.OptimizationState.get(responseObject.getState().intValue()));
} catch (APIException e) {
//handle exception
e.printStackTrace();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static void main(String[] args) {

bundling.setMode(BundlingEnum.BundlingMode.BUNDLING_BY_ANY_STATIC_ADDRESS_FIELD.getValue());

bundling.setMergeMode(BundlingEnum.BundledItemsMode.MERGE_INTO_SINGLE_DESTINATION.getValue());
bundling.setMergeMode(BundlingEnum.BundledItemsMode.KEEP_AS_SEPARATE_DESTINATIONS.getValue());


List<String> modeParams = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static void main(String[] args) {

Bundling bundling = new Bundling();

bundling.setMergeMode(BundlingEnum.BundledItemsMode.MERGE_INTO_SINGLE_DESTINATION.getValue());
bundling.setMergeMode(BundlingEnum.BundledItemsMode.KEEP_AS_SEPARATE_DESTINATIONS.getValue());

bundling.setMode(BundlingEnum.BundlingMode.BUNDLING_BY_ANY_CUSTOM_DATA.getValue());
List<String> modeParams = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static void main(String[] args) {

Bundling bundling = new Bundling();

bundling.setMergeMode(BundlingEnum.BundledItemsMode.MERGE_INTO_SINGLE_DESTINATION.getValue());
bundling.setMergeMode(BundlingEnum.BundledItemsMode.KEEP_AS_SEPARATE_DESTINATIONS.getValue());

bundling.setMode(BundlingEnum.BundlingMode.BUNDLING_BY_ADDRESS.getValue());
ServiceTimeRules serviceTimeRules = new ServiceTimeRules();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static void main(String[] args) {

Bundling bundling = new Bundling();

bundling.setMergeMode(BundlingEnum.BundledItemsMode.MERGE_INTO_SINGLE_DESTINATION.getValue());
bundling.setMergeMode(BundlingEnum.BundledItemsMode.KEEP_AS_SEPARATE_DESTINATIONS.getValue());

bundling.setMode(BundlingEnum.BundlingMode.BUNDLING_BY_ADDRESS.getValue());
ServiceTimeRules serviceTimeRules = new ServiceTimeRules();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static void main(String[] args) {

Bundling bundling = new Bundling();

bundling.setMergeMode(BundlingEnum.BundledItemsMode.MERGE_INTO_SINGLE_DESTINATION.getValue());
bundling.setMergeMode(BundlingEnum.BundledItemsMode.KEEP_AS_SEPARATE_DESTINATIONS.getValue());

bundling.setMode(BundlingEnum.BundlingMode.BUNDLING_BY_ADDRESS.getValue());
ServiceTimeRules serviceTimeRules = new ServiceTimeRules();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import com.google.gson.annotations.SerializedName;
import com.route4me.sdk.queryconverter.QueryParameter;
import com.route4me.sdk.services.routing.Source;
import com.route4me.sdk.services.routing.Address;
import java.util.List;
import lombok.Data;

Expand Down Expand Up @@ -40,7 +40,7 @@ public class AdvancedConstraints {

@SerializedName("depot_address")
@QueryParameter("depot_address")
private Source depotAddress;
private Address depotAddress;

@SerializedName("location_sequence_pattern")
@QueryParameter("location_sequence_pattern")
Expand Down

0 comments on commit 31ef6e5

Please sign in to comment.