Skip to content

Commit 2511c99

Browse files
committed
Bundling Time Window Rules
1 parent f61f171 commit 2511c99

7 files changed

+453
-1
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>io.github.route4me</groupId>
66
<artifactId>route4me-java-sdk</artifactId>
7-
<version>1.13.2</version>
7+
<version>1.14.0</version>
88
<packaging>jar</packaging>
99
<properties>
1010
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.route4me.sdk.examples.bundling;
2+
3+
import com.route4me.sdk.exception.APIException;
4+
import com.route4me.sdk.services.routing.*;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class BundlingwithTWRulesOnlyOverlappingFalse {
10+
11+
public static void main(String[] args) {
12+
13+
/*
14+
Bundling
15+
time_window_rules: it’s the list of options about handling time windows (TW).
16+
time_window_rules.resolve_strategy (type: integer; available values: 0,1; default: 0) it’s the option to pick resolving TW strategy.
17+
The strategy 0 means the min/max strategy. Example: an address X has TW = [7AM, 10AM], an address Y has TW = [9AM, 11AM], in the end we will get TW = [min(7AM, 9AM), max(10AM, 11AM)] = [7AM, 11AM]
18+
The strategy 1 means the max/min strategy. Example: an address X has TW = [7AM, 10AM], an address Y has TW = [9AM, 11AM], in the end we will get TW = [max(7AM, 9AM), min(10AM, 11AM)] = [9AM, 10AM]
19+
time_window_rules.bundle_only_overlapping (type: boolean; default: false) it’s the option to control bunlding of overlapping TW.
20+
Example: an address X has TW = [8AM, 10AM], an address Y has TW = [4PM, 6PM], so if the option is enabled these addresses won’t be bundled.
21+
Note that this option will be automatically enabled if resolve_strategy = 1 (to avoid incorrect TW)
22+
time_window_rules.bundle_service_time_greater_than_tw (type: boolean; default: true) it’s the option to control bundling of stops with service-time (ST) > 0 and TW.
23+
Example: an address X has TW = [7AM, 10AM] and ST = 1 hour, an address Y has TW = [9AM, 11AM] and ST = 1 hour, with resolve_strategy = 1 we will get TW = [9AM, 10AM] and
24+
the sum of service-times will be 1 + 1 = 2 hours, and 2 hours of ST is greater than 1 hour of TW, and in this case these addresses won’t be bundled.
25+
In the same case these addresses can be bundled with resolve_strategy = 0, because TW will be [7AM, 11AM] which means 4 hours, and 2 hours of ST is less than 4 hours of TW.
26+
*/
27+
28+
String apiKey = System.getenv("R4M_API_KEY");
29+
RoutingManager manager = new RoutingManager(apiKey);
30+
OptimizationParameters optParameters = new OptimizationParameters();
31+
32+
Parameters parameters = new Parameters();
33+
parameters.setAlgorithmType(Constants.AlgorithmType.CVRP_TW_SD.getValue());
34+
parameters.setStoreRoute(Boolean.FALSE);
35+
parameters.setShareRoute(Boolean.FALSE);
36+
parameters.setRouteTime(10 * 3600);
37+
38+
parameters.setRt(Boolean.TRUE);
39+
parameters.setRouteMaxDuration(10 * 3600);
40+
parameters.setRouteName("Bundling Stops using Time Window Rules Only Overlapping False");
41+
parameters.setOptimize(Constants.Optimize.TIME.toString());
42+
parameters.setDistanceUnit(Constants.DistanceUnit.MI.toString());
43+
parameters.setDeviceType(Constants.DeviceType.WEB.toString());
44+
parameters.setTravelMode(Constants.TravelMode.DRIVING.toString());
45+
parameters.setParts(20);
46+
optParameters.setParameters(parameters);
47+
48+
List<Address> addresses = new ArrayList<>();
49+
addresses.add(new Address("455 S 4th St, Louisville, KY 40202", true, 38.251698, -85.75730, 0));
50+
addresses.add(new Address("1604 PARKRIDGE PKWY, Louisville, KY, 40214", 38.141598, -85.793846, 300, 10 * 3600, 15 * 3600));
51+
52+
addresses.add(new Address("4629 HILLSIDE DRIVE, Louisville, KY, 40216", 38.176067, -85.824638, 300, 9 * 3600, 10 * 3600));
53+
addresses.add(new Address("4629 HILLSIDE DRIVE, Louisville, KY, 40216", 38.176067, -85.824638, 300, 11 * 3600, 12 * 3600));
54+
addresses.add(new Address("4629 HILLSIDE DRIVE, Louisville, KY, 40216", 38.176067, -85.824638, 300, 12 * 3600, 13 * 3600));
55+
56+
addresses.add(new Address("1661 W HILL ST, Louisville, KY, 40210", 38.229584, -85.783966, 300, 10 * 3600, 18 * 3600));
57+
addresses.add(new Address("1409 PHYLLIS AVE, Louisville, KY, 40215", 38.206154, -85.781387, 300, 10 * 3600, 18 * 3600));
58+
addresses.add(new Address("4504 SUNFLOWER AVE, Louisville, KY, 40216", 38.187511, -85.839149, 300, 11 * 3600, 18 * 3600));
59+
addresses.add(new Address("2512 GREENWOOD AVE, Louisville, KY, 40210", 38.241405, -85.795059, 300, 10 * 3600, 18 * 3600));
60+
addresses.add(new Address("5500 WILKE FARM AVE, Louisville, KY, 40216", 38.166065, -85.863319, 300, 11 * 3600, 18 * 3600));
61+
addresses.add(new Address("3640 LENTZ AVE, Louisville, KY, 40215", 38.193283, -85.786201, 300, 10 * 3600, 20 * 3600));
62+
addresses.add(new Address("1020 BLUEGRASS AVE, Louisville, KY, 40215", 38.17952, -85.780037, 300, 12 * 3600, 19 * 3600));
63+
addresses.add(new Address("123 NORTH 40TH ST, Louisville, KY, 40212", 38.26498, -85.814156, 300, 10 * 3600, 20 * 3600));
64+
65+
66+
Bundling bundling = new Bundling();
67+
68+
bundling.setMergeMode(BundlingEnum.BundledItemsMode.KEEP_AS_SEPARATE_DESTINATIONS.getValue());
69+
70+
bundling.setMode(BundlingEnum.BundlingMode.BUNDLING_BY_ADDRESS.getValue());
71+
ServiceTimeRules serviceTimeRules = new ServiceTimeRules();
72+
serviceTimeRules.setFirstItemMode(BundlingEnum.BundlingFirstItemMode.USE_CUSTOM_SERVICE_TIME.getValue());
73+
ArrayList<Integer> firstItemModeParams = new ArrayList<>();
74+
firstItemModeParams.add(600);
75+
serviceTimeRules.setFirstItemModeParams(firstItemModeParams);
76+
bundling.setServiceTimeRules(serviceTimeRules);
77+
TimeWindowRules timeWindowRules = new TimeWindowRules();
78+
timeWindowRules.setBundleOnlyOverlapping(Boolean.FALSE);
79+
timeWindowRules.setBundleServiceTimeGreaterThanTW(Boolean.TRUE);
80+
timeWindowRules.setResolveStrategy(0);
81+
bundling.setTimeWindowRules(timeWindowRules);
82+
parameters.setBundling(bundling);
83+
84+
85+
86+
optParameters.setAddresses(addresses);
87+
88+
try {
89+
DataObject responseObject = manager.runOptimization(optParameters);
90+
System.out.println("Optimization Problem ID:" + responseObject.getOptimizationProblemId());
91+
System.out.println("State:" + Constants.OptimizationState.get(responseObject.getState().intValue()));
92+
if (responseObject.getAddresses() != null) {
93+
for (Address address : responseObject.getAddresses()) {
94+
System.out.println(address);
95+
}
96+
}
97+
} catch (APIException e) {
98+
//handle exception
99+
e.printStackTrace();
100+
}
101+
}
102+
103+
104+
105+
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.route4me.sdk.examples.bundling;
2+
3+
import com.route4me.sdk.exception.APIException;
4+
import com.route4me.sdk.services.routing.*;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class BundlingwithTWRulesOnlyOverlappingTrue {
10+
11+
public static void main(String[] args) {
12+
13+
/*
14+
Bundling
15+
time_window_rules: it’s the list of options about handling time windows (TW).
16+
time_window_rules.resolve_strategy (type: integer; available values: 0,1; default: 0) it’s the option to pick resolving TW strategy.
17+
The strategy 0 means the min/max strategy. Example: an address X has TW = [7AM, 10AM], an address Y has TW = [9AM, 11AM], in the end we will get TW = [min(7AM, 9AM), max(10AM, 11AM)] = [7AM, 11AM]
18+
The strategy 1 means the max/min strategy. Example: an address X has TW = [7AM, 10AM], an address Y has TW = [9AM, 11AM], in the end we will get TW = [max(7AM, 9AM), min(10AM, 11AM)] = [9AM, 10AM]
19+
time_window_rules.bundle_only_overlapping (type: boolean; default: false) it’s the option to control bunlding of overlapping TW.
20+
Example: an address X has TW = [8AM, 10AM], an address Y has TW = [4PM, 6PM], so if the option is enabled these addresses won’t be bundled.
21+
Note that this option will be automatically enabled if resolve_strategy = 1 (to avoid incorrect TW)
22+
time_window_rules.bundle_service_time_greater_than_tw (type: boolean; default: true) it’s the option to control bundling of stops with service-time (ST) > 0 and TW.
23+
Example: an address X has TW = [7AM, 10AM] and ST = 1 hour, an address Y has TW = [9AM, 11AM] and ST = 1 hour, with resolve_strategy = 1 we will get TW = [9AM, 10AM] and
24+
the sum of service-times will be 1 + 1 = 2 hours, and 2 hours of ST is greater than 1 hour of TW, and in this case these addresses won’t be bundled.
25+
In the same case these addresses can be bundled with resolve_strategy = 0, because TW will be [7AM, 11AM] which means 4 hours, and 2 hours of ST is less than 4 hours of TW.
26+
*/
27+
28+
String apiKey = System.getenv("R4M_API_KEY");
29+
RoutingManager manager = new RoutingManager(apiKey);
30+
OptimizationParameters optParameters = new OptimizationParameters();
31+
32+
Parameters parameters = new Parameters();
33+
parameters.setAlgorithmType(Constants.AlgorithmType.CVRP_TW_SD.getValue());
34+
parameters.setStoreRoute(Boolean.FALSE);
35+
parameters.setShareRoute(Boolean.FALSE);
36+
parameters.setRouteTime(10 * 3600);
37+
38+
parameters.setRt(Boolean.TRUE);
39+
parameters.setRouteMaxDuration(10 * 3600);
40+
parameters.setRouteName("Bundling Stops using Time Window Rules Only Overlapping True");
41+
parameters.setOptimize(Constants.Optimize.TIME.toString());
42+
parameters.setDistanceUnit(Constants.DistanceUnit.MI.toString());
43+
parameters.setDeviceType(Constants.DeviceType.WEB.toString());
44+
parameters.setTravelMode(Constants.TravelMode.DRIVING.toString());
45+
parameters.setParts(20);
46+
optParameters.setParameters(parameters);
47+
48+
List<Address> addresses = new ArrayList<>();
49+
addresses.add(new Address("455 S 4th St, Louisville, KY 40202", true, 38.251698, -85.75730, 0));
50+
addresses.add(new Address("1604 PARKRIDGE PKWY, Louisville, KY, 40214", 38.141598, -85.793846, 300, 10 * 3600, 15 * 3600));
51+
52+
addresses.add(new Address("4629 HILLSIDE DRIVE, Louisville, KY, 40216", 38.176067, -85.824638, 300, 9 * 3600, 10 * 3600));
53+
addresses.add(new Address("4629 HILLSIDE DRIVE, Louisville, KY, 40216", 38.176067, -85.824638, 300, 11 * 3600, 12 * 3600));
54+
addresses.add(new Address("4629 HILLSIDE DRIVE, Louisville, KY, 40216", 38.176067, -85.824638, 300, 12 * 3600, 13 * 3600));
55+
56+
addresses.add(new Address("1661 W HILL ST, Louisville, KY, 40210", 38.229584, -85.783966, 300, 10 * 3600, 18 * 3600));
57+
addresses.add(new Address("1409 PHYLLIS AVE, Louisville, KY, 40215", 38.206154, -85.781387, 300, 10 * 3600, 18 * 3600));
58+
addresses.add(new Address("4504 SUNFLOWER AVE, Louisville, KY, 40216", 38.187511, -85.839149, 300, 11 * 3600, 18 * 3600));
59+
addresses.add(new Address("2512 GREENWOOD AVE, Louisville, KY, 40210", 38.241405, -85.795059, 300, 10 * 3600, 18 * 3600));
60+
addresses.add(new Address("5500 WILKE FARM AVE, Louisville, KY, 40216", 38.166065, -85.863319, 300, 11 * 3600, 18 * 3600));
61+
addresses.add(new Address("3640 LENTZ AVE, Louisville, KY, 40215", 38.193283, -85.786201, 300, 10 * 3600, 20 * 3600));
62+
addresses.add(new Address("1020 BLUEGRASS AVE, Louisville, KY, 40215", 38.17952, -85.780037, 300, 12 * 3600, 19 * 3600));
63+
addresses.add(new Address("123 NORTH 40TH ST, Louisville, KY, 40212", 38.26498, -85.814156, 300, 10 * 3600, 20 * 3600));
64+
65+
66+
Bundling bundling = new Bundling();
67+
68+
bundling.setMergeMode(BundlingEnum.BundledItemsMode.KEEP_AS_SEPARATE_DESTINATIONS.getValue());
69+
70+
bundling.setMode(BundlingEnum.BundlingMode.BUNDLING_BY_ADDRESS.getValue());
71+
ServiceTimeRules serviceTimeRules = new ServiceTimeRules();
72+
serviceTimeRules.setFirstItemMode(BundlingEnum.BundlingFirstItemMode.USE_CUSTOM_SERVICE_TIME.getValue());
73+
ArrayList<Integer> firstItemModeParams = new ArrayList<>();
74+
firstItemModeParams.add(600);
75+
serviceTimeRules.setFirstItemModeParams(firstItemModeParams);
76+
bundling.setServiceTimeRules(serviceTimeRules);
77+
TimeWindowRules timeWindowRules = new TimeWindowRules();
78+
timeWindowRules.setBundleOnlyOverlapping(Boolean.TRUE);
79+
timeWindowRules.setBundleServiceTimeGreaterThanTW(Boolean.TRUE);
80+
timeWindowRules.setResolveStrategy(0);
81+
bundling.setTimeWindowRules(timeWindowRules);
82+
parameters.setBundling(bundling);
83+
84+
85+
86+
optParameters.setAddresses(addresses);
87+
88+
try {
89+
DataObject responseObject = manager.runOptimization(optParameters);
90+
System.out.println("Optimization Problem ID:" + responseObject.getOptimizationProblemId());
91+
System.out.println("State:" + Constants.OptimizationState.get(responseObject.getState().intValue()));
92+
if (responseObject.getAddresses() != null) {
93+
for (Address address : responseObject.getAddresses()) {
94+
System.out.println(address);
95+
}
96+
}
97+
} catch (APIException e) {
98+
//handle exception
99+
e.printStackTrace();
100+
}
101+
}
102+
103+
104+
105+
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.route4me.sdk.examples.bundling;
2+
3+
import com.route4me.sdk.exception.APIException;
4+
import com.route4me.sdk.services.routing.*;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class BundlingwithTWRulesResolveStrategy0 {
10+
11+
public static void main(String[] args) {
12+
13+
/*
14+
Bundling
15+
time_window_rules: it’s the list of options about handling time windows (TW).
16+
time_window_rules.resolve_strategy (type: integer; available values: 0,1; default: 0) it’s the option to pick resolving TW strategy.
17+
The strategy 0 means the min/max strategy. Example: an address X has TW = [7AM, 10AM], an address Y has TW = [9AM, 11AM], in the end we will get TW = [min(7AM, 9AM), max(10AM, 11AM)] = [7AM, 11AM]
18+
The strategy 1 means the max/min strategy. Example: an address X has TW = [7AM, 10AM], an address Y has TW = [9AM, 11AM], in the end we will get TW = [max(7AM, 9AM), min(10AM, 11AM)] = [9AM, 10AM]
19+
time_window_rules.bundle_only_overlapping (type: boolean; default: false) it’s the option to control bunlding of overlapping TW.
20+
Example: an address X has TW = [8AM, 10AM], an address Y has TW = [4PM, 6PM], so if the option is enabled these addresses won’t be bundled.
21+
Note that this option will be automatically enabled if resolve_strategy = 1 (to avoid incorrect TW)
22+
time_window_rules.bundle_service_time_greater_than_tw (type: boolean; default: true) it’s the option to control bundling of stops with service-time (ST) > 0 and TW.
23+
Example: an address X has TW = [7AM, 10AM] and ST = 1 hour, an address Y has TW = [9AM, 11AM] and ST = 1 hour, with resolve_strategy = 1 we will get TW = [9AM, 10AM] and
24+
the sum of service-times will be 1 + 1 = 2 hours, and 2 hours of ST is greater than 1 hour of TW, and in this case these addresses won’t be bundled.
25+
In the same case these addresses can be bundled with resolve_strategy = 0, because TW will be [7AM, 11AM] which means 4 hours, and 2 hours of ST is less than 4 hours of TW.
26+
*/
27+
28+
String apiKey = System.getenv("R4M_API_KEY");
29+
RoutingManager manager = new RoutingManager(apiKey);
30+
OptimizationParameters optParameters = new OptimizationParameters();
31+
32+
Parameters parameters = new Parameters();
33+
parameters.setAlgorithmType(Constants.AlgorithmType.CVRP_TW_SD.getValue());
34+
parameters.setStoreRoute(Boolean.FALSE);
35+
parameters.setShareRoute(Boolean.FALSE);
36+
parameters.setRouteTime(10 * 3600);
37+
38+
parameters.setRt(Boolean.TRUE);
39+
parameters.setRouteMaxDuration(10 * 3600);
40+
parameters.setRouteName("Bundling Stops using Time Window Rules Resolve Strategy 0");
41+
parameters.setOptimize(Constants.Optimize.TIME.toString());
42+
parameters.setDistanceUnit(Constants.DistanceUnit.MI.toString());
43+
parameters.setDeviceType(Constants.DeviceType.WEB.toString());
44+
parameters.setTravelMode(Constants.TravelMode.DRIVING.toString());
45+
parameters.setParts(20);
46+
optParameters.setParameters(parameters);
47+
48+
List<Address> addresses = new ArrayList<>();
49+
addresses.add(new Address("455 S 4th St, Louisville, KY 40202", true, 38.251698, -85.75730, 0));
50+
addresses.add(new Address("1604 PARKRIDGE PKWY, Louisville, KY, 40214", 38.141598, -85.793846, 300, 10 * 3600, 15 * 3600));
51+
52+
addresses.add(new Address("4629 HILLSIDE DRIVE, Louisville, KY, 40216", 38.176067, -85.824638, 300, 9 * 3600, 10 * 3600));
53+
addresses.add(new Address("4629 HILLSIDE DRIVE, Louisville, KY, 40216", 38.176067, -85.824638, 300, 11 * 3600, 12 * 3600));
54+
addresses.add(new Address("4629 HILLSIDE DRIVE, Louisville, KY, 40216", 38.176067, -85.824638, 300, 12 * 3600, 13 * 3600));
55+
56+
addresses.add(new Address("1661 W HILL ST, Louisville, KY, 40210", 38.229584, -85.783966, 300, 10 * 3600, 18 * 3600));
57+
addresses.add(new Address("1409 PHYLLIS AVE, Louisville, KY, 40215", 38.206154, -85.781387, 300, 10 * 3600, 18 * 3600));
58+
addresses.add(new Address("4504 SUNFLOWER AVE, Louisville, KY, 40216", 38.187511, -85.839149, 300, 11 * 3600, 18 * 3600));
59+
addresses.add(new Address("2512 GREENWOOD AVE, Louisville, KY, 40210", 38.241405, -85.795059, 300, 10 * 3600, 18 * 3600));
60+
addresses.add(new Address("5500 WILKE FARM AVE, Louisville, KY, 40216", 38.166065, -85.863319, 300, 11 * 3600, 18 * 3600));
61+
addresses.add(new Address("3640 LENTZ AVE, Louisville, KY, 40215", 38.193283, -85.786201, 300, 10 * 3600, 20 * 3600));
62+
addresses.add(new Address("1020 BLUEGRASS AVE, Louisville, KY, 40215", 38.17952, -85.780037, 300, 12 * 3600, 19 * 3600));
63+
addresses.add(new Address("123 NORTH 40TH ST, Louisville, KY, 40212", 38.26498, -85.814156, 300, 10 * 3600, 20 * 3600));
64+
65+
66+
Bundling bundling = new Bundling();
67+
68+
bundling.setMergeMode(BundlingEnum.BundledItemsMode.KEEP_AS_SEPARATE_DESTINATIONS.getValue());
69+
70+
bundling.setMode(BundlingEnum.BundlingMode.BUNDLING_BY_ADDRESS.getValue());
71+
ServiceTimeRules serviceTimeRules = new ServiceTimeRules();
72+
serviceTimeRules.setFirstItemMode(BundlingEnum.BundlingFirstItemMode.USE_CUSTOM_SERVICE_TIME.getValue());
73+
ArrayList<Integer> firstItemModeParams = new ArrayList<>();
74+
firstItemModeParams.add(600);
75+
serviceTimeRules.setFirstItemModeParams(firstItemModeParams);
76+
bundling.setServiceTimeRules(serviceTimeRules);
77+
TimeWindowRules timeWindowRules = new TimeWindowRules();
78+
timeWindowRules.setBundleOnlyOverlapping(Boolean.FALSE);
79+
timeWindowRules.setBundleServiceTimeGreaterThanTW(Boolean.TRUE);
80+
timeWindowRules.setResolveStrategy(0);
81+
bundling.setTimeWindowRules(timeWindowRules);
82+
parameters.setBundling(bundling);
83+
84+
85+
86+
optParameters.setAddresses(addresses);
87+
88+
try {
89+
DataObject responseObject = manager.runOptimization(optParameters);
90+
System.out.println("Optimization Problem ID:" + responseObject.getOptimizationProblemId());
91+
System.out.println("State:" + Constants.OptimizationState.get(responseObject.getState().intValue()));
92+
if (responseObject.getAddresses() != null) {
93+
for (Address address : responseObject.getAddresses()) {
94+
System.out.println(address);
95+
}
96+
}
97+
} catch (APIException e) {
98+
//handle exception
99+
e.printStackTrace();
100+
}
101+
}
102+
103+
104+
105+
106+
}

0 commit comments

Comments
 (0)