-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Optimization Time Prediction, and Retail Location Example
- Loading branch information
Showing
6 changed files
with
340 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
...main/java/com/route4me/sdk/examples/advancedconstraints/AdvancedConstraintsExample12.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/* | ||
* 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.opencsv.CSVReader; | ||
import com.opencsv.CSVReaderBuilder; | ||
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.DistanceUnit; | ||
import com.route4me.sdk.services.routing.Constants.OptimizationState; | ||
import com.route4me.sdk.services.routing.Constants.Optimize; | ||
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 java.io.FileNotFoundException; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
public class AdvancedConstraintsExample12 { | ||
|
||
public static List<Address> readAddressesFromCSV(String filename, List<String> zone1, List<String> zone2, List<String> zone3, Integer serviceTime) { | ||
List<Address> addresses = new ArrayList<>(); | ||
CSVReader csvReader; | ||
try { | ||
csvReader = new CSVReaderBuilder(new FileReader(filename)).withSkipLines(1).build(); | ||
String[] nextRecord; | ||
while ((nextRecord = csvReader.readNext()) != null) { | ||
Address address = new Address(); | ||
address.setAlias(nextRecord[0]); | ||
address.setAddress(nextRecord[0]); | ||
address.setLatitude(Double.parseDouble(nextRecord[1])); | ||
address.setLongitude(Double.parseDouble(nextRecord[2])); | ||
int group = Integer.parseInt(nextRecord[3]); | ||
|
||
switch (group) { | ||
case 0: | ||
address.setTags(zone1); | ||
break; | ||
case 1: | ||
address.setTags(zone2); | ||
break; | ||
case 2: | ||
address.setTags(zone3); | ||
break; | ||
} | ||
addresses.add(address); | ||
} | ||
|
||
} catch (FileNotFoundException ex) { | ||
Logger.getLogger(AdvancedConstraintsExample12.class.getName()).log(Level.SEVERE, null, ex); | ||
} catch (IOException ex) { | ||
Logger.getLogger(AdvancedConstraintsExample12.class.getName()).log(Level.SEVERE, null, ex); | ||
} | ||
|
||
return addresses; | ||
|
||
} | ||
|
||
//********************************************************************** | ||
// TEST CASE: Drivers Schedules with Territories | ||
// 30 Schedules | ||
// 3 Territories | ||
//********************************************************************** | ||
public static void main(String[] args) { | ||
String apiKey = "11111111111111111111111111111111"; | ||
RoutingManager manager = new RoutingManager(apiKey, Boolean.TRUE); | ||
OptimizationParameters optParameters = new OptimizationParameters(); | ||
|
||
// General Route Parameters | ||
Parameters parameters = new Parameters(); | ||
parameters.setAlgorithmType(AlgorithmType.ADVANCED_CVRP_TW.getValue()); | ||
parameters.setStoreRoute(Boolean.FALSE); | ||
parameters.setShareRoute(Boolean.FALSE); | ||
parameters.setRt(Boolean.TRUE); | ||
parameters.setParts(30); | ||
parameters.setRouteName("Drivers Schedules - 3 Territories"); | ||
parameters.setOptimize(Optimize.DISTANCE.toString()); | ||
parameters.setDistanceUnit(DistanceUnit.MI.toString()); | ||
parameters.setDeviceType(DeviceType.WEB.toString()); | ||
parameters.setTravelMode(TravelMode.DRIVING.toString()); | ||
|
||
// ********************** | ||
// Territories | ||
// ********************** | ||
List<String> zone1 = Arrays.asList("ZONE 01"); | ||
List<String> zone2 = Arrays.asList("ZONE 02"); | ||
List<String> zone3 = Arrays.asList("ZONE 03"); | ||
|
||
// ********************** | ||
// Schedules | ||
// ********************** | ||
AdvancedConstraints schedule; | ||
List<String> zone = new ArrayList<>(); | ||
List<AdvancedConstraints> advancedConstraints = new ArrayList<>(); | ||
|
||
Address retailLocationAddress = new Address("RETAIL LOCATION", "RETAIL LOCATION", 25.8741751,-80.1288583, 300); | ||
|
||
List <Object> locationsSequence = Arrays.asList("", retailLocationAddress); | ||
|
||
|
||
|
||
for (int i = 0; i < 30; i++) { | ||
schedule = new AdvancedConstraints(); | ||
switch (i % 3) { | ||
case 0: | ||
zone = zone1; | ||
break; | ||
case 1: | ||
zone = zone2; | ||
break; | ||
case 2: | ||
zone = zone3; | ||
break; | ||
} | ||
|
||
schedule.setTags(zone); | ||
schedule.setMembersCount(1); | ||
List<List<Integer>> timeWindowsSchedule = new ArrayList<>(); | ||
List<Integer> timeWindowSchedule = Arrays.asList((8 + 5) * 3600, (11 + 5) * 3600); | ||
timeWindowsSchedule.add(timeWindowSchedule); | ||
schedule.setAvailableTimeWindows(timeWindowsSchedule); | ||
schedule.setLocationSequencePattern(locationsSequence); | ||
|
||
advancedConstraints.add(schedule); | ||
} | ||
|
||
// Schedules registration | ||
parameters.setAdvancedConstraints(advancedConstraints); | ||
optParameters.setParameters(parameters); | ||
|
||
// Addresses | ||
List<Address> addresses = new ArrayList<>(); | ||
// Depot | ||
|
||
Integer serviceTime = 300; | ||
|
||
Path currentPath = Paths.get(System.getProperty("user.dir")); | ||
Path filePath = Paths.get(currentPath.toString(),"src", "main", "java", "com", "route4me", "sdk", "examples", "advancedconstraints", "locations.csv"); | ||
|
||
|
||
addresses = readAddressesFromCSV(filePath.toString(), zone1, zone2, zone3, serviceTime); | ||
|
||
Address address = new Address("DEPOT", true, 25.723025, -80.452883, 0); | ||
addresses.add(address); | ||
|
||
// Stops | ||
|
||
|
||
|
||
|
||
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 (Iterator<Address> it = responseObject.getAddresses().iterator(); it.hasNext();) { | ||
address = it.next(); | ||
System.out.println(address); | ||
} | ||
} | ||
} catch (APIException e) { | ||
//handle exception | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
...com/route4me/sdk/examples/optimizations/time/predict/PredictOptimizationTimeExample1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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.optimizations.time.predict; | ||
|
||
import com.route4me.sdk.exception.APIException; | ||
import com.route4me.sdk.services.routing.Address; | ||
import com.route4me.sdk.services.routing.Constants; | ||
import com.route4me.sdk.services.routing.OptimizationParameters; | ||
import com.route4me.sdk.services.routing.Parameters; | ||
import com.route4me.sdk.services.routing.predict.optimization.time.OptimizationTimePrediction; | ||
import com.route4me.sdk.services.routing.predict.optimization.time.PredictOptimizationTimeManager; | ||
import com.route4me.sdk.services.routing.predict.optimization.time.TimePredictionModel; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* | ||
* @author Route4Me | ||
*/ | ||
public class PredictOptimizationTimeExample1 { | ||
|
||
public static void main(String[] args) { | ||
String apiKey = "11111111111111111111111111111111"; | ||
PredictOptimizationTimeManager manager = new PredictOptimizationTimeManager(apiKey); | ||
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.setRouteTime(0); | ||
parameters.setParts(20); | ||
parameters.setRouteMaxDuration(3600 * 0.5); | ||
parameters.setVehicleCapacity("100"); | ||
parameters.setRouteName("Single Depot, Multiple Driver"); | ||
parameters.setOptimize(Constants.Optimize.DISTANCE.toString()); | ||
parameters.setDistanceUnit(Constants.DistanceUnit.MI.toString()); | ||
parameters.setDeviceType(Constants.DeviceType.WEB.toString()); | ||
parameters.setTravelMode(Constants.TravelMode.DRIVING.toString()); | ||
optParameters.setParameters(parameters); | ||
|
||
List<Address> addresses = new ArrayList<>(); | ||
addresses.add(new Address("1604 PARKRIDGE PKWY, Louisville, KY, 40214", true, 38.141598, -85.793846, 300)); | ||
addresses.add(new Address("1407 MCCOY, Louisville, KY, 40215", 38.202496, -85.786514, 300)); | ||
addresses.add(new Address("4805 BELLEVUE AVE, Louisville, KY, 40215", 38.178844, -85.774864, 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)); | ||
addresses.add(new Address("4738 BELLEVUE AVE, Louisville, KY, 40215", 38.179806, -85.775558, 300)); | ||
addresses.add(new Address("318 SO. 39TH STREET, Louisville, KY, 40212", 38.259335, -85.815094, 300)); | ||
addresses.add(new Address("1324 BLUEGRASS AVE, Louisville, KY, 40215", 38.179253, -85.785118, 300)); | ||
addresses.add(new Address("7305 ROYAL WOODS DR, Louisville, KY, 40214", 38.162472, -85.792854, 300)); | ||
optParameters.setAddresses(addresses); | ||
|
||
try { | ||
OptimizationTimePrediction responseObject = manager.runOptimizationPrediction(optParameters); | ||
|
||
for (TimePredictionModel model: responseObject.getTimePrediction()){ | ||
System.out.println("Model Name: " + model.getModel().toUpperCase() + ", predicted time, " + model.getValue() + " " + model.getUnit() ); | ||
} | ||
|
||
} catch (APIException e) { | ||
//handle exception | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
|
||
|
||
} |
22 changes: 22 additions & 0 deletions
22
...m/route4me/sdk/services/routing/predict/optimization/time/OptimizationTimePrediction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* 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.services.routing.predict.optimization.time; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import java.util.List; | ||
import lombok.Data; | ||
|
||
/** | ||
* | ||
* @author Route4Me | ||
*/ | ||
@Data | ||
public class OptimizationTimePrediction { | ||
|
||
@SerializedName("time-prediction") | ||
private List<TimePredictionModel> timePrediction; | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...ute4me/sdk/services/routing/predict/optimization/time/PredictOptimizationTimeManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* 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.services.routing.predict.optimization.time; | ||
|
||
import com.route4me.sdk.Manager; | ||
import com.route4me.sdk.RequestMethod; | ||
import com.route4me.sdk.exception.APIException; | ||
import com.route4me.sdk.services.routing.OptimizationParameters; | ||
import org.apache.http.client.utils.URIBuilder; | ||
|
||
/** | ||
* | ||
* @author Route4Me | ||
*/ | ||
public class PredictOptimizationTimeManager extends Manager { | ||
|
||
|
||
public static final String OPTIMIZATION_PREDICTION_URL = "/api.v4/predict-optimization-time.php"; | ||
|
||
|
||
public PredictOptimizationTimeManager(String apiKey) { | ||
super(apiKey); | ||
} | ||
|
||
public OptimizationTimePrediction runOptimizationPrediction(OptimizationParameters parameters) throws APIException { | ||
URIBuilder builder = Manager.defaultBuilder(OPTIMIZATION_PREDICTION_URL); | ||
return this.makeJSONRequest(RequestMethod.POST, builder, parameters, OptimizationTimePrediction.class); | ||
} | ||
|
||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...java/com/route4me/sdk/services/routing/predict/optimization/time/TimePredictionModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* 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.services.routing.predict.optimization.time; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import lombok.Data; | ||
|
||
/** | ||
* | ||
* @author Route4Me | ||
*/ | ||
|
||
@Data | ||
public class TimePredictionModel { | ||
|
||
@SerializedName("model") | ||
private String model; | ||
@SerializedName("value") | ||
private Long value; | ||
@SerializedName("unit") | ||
private String unit; | ||
} |