Skip to content

Commit

Permalink
Added Examples of Orders with Demands, Optimization using callback UR…
Browse files Browse the repository at this point in the history
…L and Advanced Constraints using Territories IDs
  • Loading branch information
r4m-juan committed Dec 22, 2021
1 parent be96ecf commit c6f28f6
Show file tree
Hide file tree
Showing 14 changed files with 392 additions and 34 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.route4me</groupId>
<artifactId>route4me-java-sdk</artifactId>
<version>1.6.5</version>
<version>1.6.6</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<log4j.version>2.16.0</log4j.version>
<log4j.version>2.17.0</log4j.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Expand Down Expand Up @@ -53,13 +53,13 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.16.0</version>
<version>2.17.0</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.16.0</version>
<version>2.17.0</version>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
Expand Down
49 changes: 25 additions & 24 deletions src/main/java/com/route4me/sdk/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,29 @@
import java.lang.reflect.Type;
import java.net.URISyntaxException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.HttpClientConnection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.entity.ContentLengthStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.ConnSupport;
import org.apache.http.impl.DefaultBHttpClientConnection;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.io.ChunkedOutputStream;
import org.apache.http.impl.io.ContentLengthOutputStream;
import org.apache.http.impl.io.DefaultHttpRequestWriterFactory;
import org.apache.http.impl.io.HttpTransportMetricsImpl;
import org.apache.http.impl.io.IdentityOutputStream;
import org.apache.http.impl.io.SessionOutputBufferImpl;
import org.apache.http.io.HttpMessageWriter;
import org.apache.http.io.SessionOutputBuffer;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestExecutor;
import org.apache.logging.log4j.LogManager;

public abstract class Manager {

protected static final org.apache.logging.log4j.Logger logger = LogManager.getLogger(Manager.class);

private final String apiKey;
protected Gson gson;
private RequestConfig requestProxyConfig;
private boolean disableRedirects;
private String callBackURL;


public Manager(String apiKey) {
this.apiKey = apiKey;
Expand All @@ -67,6 +46,12 @@ public Manager(String apiKey, boolean disableRedirects) {
this.disableRedirects = disableRedirects;
}

public Manager(String apiKey, boolean disableRedirects, String callBackURL) {
this(apiKey);
this.disableRedirects = disableRedirects;
this.callBackURL = callBackURL;
}

public Manager(String apiKey, GsonBuilder builder) {
this.apiKey = apiKey;
this.gson = builder.addSerializationExclusionStrategy(new ExclusionStrategy() {
Expand Down Expand Up @@ -204,6 +189,9 @@ private <T> T makeRequest(RequestMethod method, URIBuilder builder, HttpEntity b
builder.addParameter("redirect", "0");
client = HttpClients.custom().disableRedirectHandling().build();
}
if (this.callBackURL != null) {
builder.addParameter("optimized_callback_url", this.callBackURL);
}
if (this.apiKey != null) {
builder.addParameter("api_key", this.apiKey);
}
Expand All @@ -222,7 +210,6 @@ private <T> T makeRequest(RequestMethod method, URIBuilder builder, HttpEntity b
throw new RuntimeException("Method does not support body!");
}
//try with resources to close streams

try (CloseableHttpResponse resp = client.execute(hrb); InputStream is = resp.getEntity().getContent()) {
//response should always be present
if (is == null) {
Expand Down Expand Up @@ -282,5 +269,19 @@ private StringBuilder responseContentParser(BufferedReader br) throws IOExceptio
}
return respString;
}

/**
* @return the callBackURL
*/
public String getCallBackURL() {
return callBackURL;
}

/**
* @param callBackURL the callBackURL to set
*/
public void setCallBackURL(String callBackURL) {
this.callBackURL = callBackURL;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// codebeat:disable[SIMILARITY]
package com.route4me.sdk.examples;

import com.route4me.sdk.exception.APIException;
import com.route4me.sdk.services.routing.Constants.*;
import com.route4me.sdk.services.routing.*;

import java.util.ArrayList;
import java.util.List;

public class SingleDriverRoundTripAsyncCallBackURL {

public static void main(String[] args) {
String apiKey = "11111111111111111111111111111111";
boolean disableRedirects = true;
RoutingManager manager = new RoutingManager(apiKey, disableRedirects, "https://callback.url");
OptimizationParameters optParameters = new OptimizationParameters();

Parameters parameters = new Parameters();
parameters.setAlgorithmType(AlgorithmType.TSP.getValue());
parameters.setStoreRoute(Boolean.FALSE);
parameters.setShareRoute(Boolean.FALSE);
parameters.setRouteTime(0);
parameters.setRouteMaxDuration(86400);
parameters.setVehicleCapacity("1");
parameters.setVehicleMaxDistanceMi("10000");
parameters.setRouteName("Single Driver Round Trip");
parameters.setOptimize(Optimize.DISTANCE.toString());
parameters.setDistanceUnit(DistanceUnit.MI.toString());
parameters.setDeviceType(DeviceType.WEB.toString());
parameters.setTravelMode(TravelMode.DRIVING.toString());
optParameters.setParameters(parameters);

List<Address> addresses = new ArrayList<>();
addresses.add(new Address("754 5th Ave New York, NY 10019", true, "Bergdorf Goodman", 40.7636197, -73.9744388, 0));
addresses.add(new Address("717 5th Ave New York, NY 10022", "Giorgio Armani", 40.7669692, -73.9693864, 0));
addresses.add(new Address("888 Madison Ave New York, NY 10014", "Ralph Lauren Women's and Home", 40.7715154, -73.9669241, 0));
addresses.add(new Address("1011 Madison Ave New York, NY 10075", "Yigal Azrou'l", 40.7772129, -73.9669, 0));
addresses.add(new Address("440 Columbus Ave New York, NY 10024", "Frank Stella Clothier", 40.7808364, -73.9732729, 0));
addresses.add(new Address("324 Columbus Ave #1 New York, NY 10023", "Liana", 40.7803123, -73.9793079, 0));
addresses.add(new Address("110 W End Ave New York, NY 10023", "Toga Bike Shop", 40.7753077, -73.9861529, 0));
addresses.add(new Address("555 W 57th St New York, NY 10019", "BMW of Manhattan", 40.7718005, -73.9897716, 0));
addresses.add(new Address("57 W 57th St New York, NY 10019", "Verizon Wireless", 40.7558695, -73.9862019, 0));
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 address : responseObject.getAddresses()) {
System.out.println(address);
}
}
} catch (APIException e) {
//handle exception
e.printStackTrace();
}
}

}
// codebeat:enable[SIMILARITY]
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void main(String[] args) {
OptimizationParameters optParameters = new OptimizationParameters();

//**********************************************************************
// TEST CASE: Retail Location
// TEST CASE: Retail Location Single Depot Multiple Drivers
//**********************************************************************


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ public static List<Address> readAddressesFromCSV(String filename, List<String> z
}

//**********************************************************************
// TEST CASE: Drivers Schedules with Territories
// TEST CASE: Drivers Schedules with Territories and Retail Location
// 2000 Stops
// 30 Schedules
// 3 Territories
// Retail Location
//**********************************************************************
public static void main(String[] args) {
String apiKey = "11111111111111111111111111111111";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* 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 AdvancedConstraintsExample13 {



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

//**********************************************************************
// TEST CASE: Optimization using Territories addresses
// 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("Single Depot, Multiple Driver - 3 Territories IDs");
parameters.setDeviceType(DeviceType.WEB.toString());
parameters.setTravelMode(TravelMode.DRIVING.toString());
optParameters.setParameters(parameters);

// Depot
Address depot = new Address("1604 PARKRIDGE PKWY, Louisville, KY, 40214", 38.141598, -85.793846);
List<Object> depots = Arrays.asList(depot);
parameters.setDepots(depots);


// Territories
// **********************
List<String> zone1 = Arrays.asList("584BE87F75599DD8B9F038D81A33F5A8");
List<String> zone2 = Arrays.asList("A5C1B85BCC069FF6CBDA18A65DC95031");
List<String> zone3 = Arrays.asList("007AE22B494A149B52A45E3C6D3EC0CA");



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

Address address;

Territory territory = territoriesManager.getAddressesInTerritory(zone1.get(0));
for (Integer contactId: territory.getAddresses()){
address = new Address();
address.setContactId(contactId);
address.setTags(zone1);
addresses.add(address);
}

territory = territoriesManager.getAddressesInTerritory(zone2.get(0));
for (Integer contactId: territory.getAddresses()){
address = new Address();
address.setContactId(contactId);
address.setTags(zone2);
addresses.add(address);
}

territory = territoriesManager.getAddressesInTerritory(zone3.get(0));
for (Integer contactId: territory.getAddresses()){
address = new Address();
address.setTags(zone3);
address.setContactId(contactId);
addresses.add(address);
}

AdvancedConstraints schedule1 = new AdvancedConstraints();
schedule1.setTags(zone1);
schedule1.setMembersCount(3);
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.setTags(zone2);
schedule2.setMembersCount(4);
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.setTags(zone3);
schedule3.setMembersCount(3);
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(AdvancedConstraintsExample13.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public static List<Address> readAddressesFromCSV(String filename, List<String> z

//**********************************************************************
// TEST CASE: Drivers Schedules with Territories
// 2000 Stops
// 30 Schedules
// 3 Territories
//**********************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ public static List<Address> readAddressesFromCSV(String filename, List<String> z

//**********************************************************************
// TEST CASE: Drivers Schedules with Territories
// 2000 Stops
// 50 Drivers
// 30 Schedules
// 3 Territories
// 5 Territories
//**********************************************************************
public static void main(String[] args) {
String apiKey = "11111111111111111111111111111111";
Expand Down
Loading

0 comments on commit c6f28f6

Please sign in to comment.