Starting of plan optimization fails

Plan optimal and cost-effective routes for any vehicle and provide users with reliable arrival times.
Post Reply
unheal1337
Posts: 5
Joined: Fri Feb 23, 2024 1:51 pm

Starting of plan optimization fails

Post by unheal1337 »

Dear community,

I was about to use the Routing API of PTV Developer in Visual Studio C#. Steps are:
  1. Create a Plan
  2. Optimize the Plan
  3. Get Status
  4. Get completed Plan
I was able to create a Plan and got the following JSON, which seems to be valid:

Code: Select all

{"id":"00000000-0000-0000-0000-000000000000","description":"Test Route Trip","locations":[{"id":"Start","type":"DEPOT","latitude":50.73117,"longitude":7.10052,"includeLastMeters":true,"matchSideOfStreet":false,"applyVehicleDependentServiceTimeFactor":true,"openingIntervals":[{"start":"2024-03-06T08:00:00+01:00","end":"2024-03-06T20:00:00+01:00"}]},{"id":"Location 1","type":"CUSTOMER","latitude":51.5159395432446,"longitude":9.27642819335112,"includeLastMeters":true,"matchSideOfStreet":false,"applyVehicleDependentServiceTimeFactor":true,"openingIntervals":[{"start":"2024-03-06T08:00:00+01:00","end":"2024-03-06T20:00:00+01:00"}]},{"id":"Location 2","type":"CUSTOMER","latitude":51.670419413097285,"longitude":10.230704532272371,"includeLastMeters":true,"matchSideOfStreet":false,"applyVehicleDependentServiceTimeFactor":true,"openingIntervals":[{"start":"2024-03-06T08:00:00+01:00","end":"2024-03-06T20:00:00+01:00"}]},{"id":"Location 3","type":"CUSTOMER","latitude":52.729127711123866,"longitude":8.8410603595343034,"includeLastMeters":true,"matchSideOfStreet":false,"applyVehicleDependentServiceTimeFactor":true,"openingIntervals":[{"start":"2024-03-06T08:00:00+01:00","end":"2024-03-06T20:00:00+01:00"}]},{"id":"Location 4","type":"CUSTOMER","latitude":52.786041492721942,"longitude":8.2988423970259717,"includeLastMeters":true,"matchSideOfStreet":false,"applyVehicleDependentServiceTimeFactor":true,"openingIntervals":[{"start":"2024-03-06T08:00:00+01:00","end":"2024-03-06T20:00:00+01:00"}]},{"id":"Location 5","type":"CUSTOMER","latitude":52.897727122550926,"longitude":10.521510108222701,"includeLastMeters":true,"matchSideOfStreet":false,"applyVehicleDependentServiceTimeFactor":true,"openingIntervals":[{"start":"2024-03-06T08:00:00+01:00","end":"2024-03-06T20:00:00+01:00"}]},{"id":"Destination","type":"CUSTOMER","latitude":53.54897,"longitude":9.99337,"includeLastMeters":true,"matchSideOfStreet":false,"applyVehicleDependentServiceTimeFactor":true,"openingIntervals":[{"start":"2024-03-06T08:00:00+01:00","end":"2024-03-06T20:00:00+01:00"}]}],"vehicles":[{"id":"Vehicle 1","capacitiesChangePosition":"BETWEEN_TRIPS","profile":"EUR_TRAILER_TRUCK","startLocationId":"Start","endLocationId":"Start","serviceTimePerTransportStop":0,"serviceTimeFactor":1.0,"ignoreMixedLoadingProhibitions":false}],"transports":[{"id":"Transport to Location 1","pickupLocationId":"Start","pickupServiceTime":0,"deliveryLocationId":"Location 1","deliveryServiceTime":0},{"id":"Transport to Location 2","pickupLocationId":"Start","pickupServiceTime":0,"deliveryLocationId":"Location 2","deliveryServiceTime":0},{"id":"Transport to Location 3","pickupLocationId":"Start","pickupServiceTime":0,"deliveryLocationId":"Location 3","deliveryServiceTime":0},{"id":"Transport to Location 4","pickupLocationId":"Start","pickupServiceTime":0,"deliveryLocationId":"Location 4","deliveryServiceTime":0},{"id":"Transport to Location 5","pickupLocationId":"Start","pickupServiceTime":0,"deliveryLocationId":"Location 5","deliveryServiceTime":0},{"id":"Transport to Destination","pickupLocationId":"Start","pickupServiceTime":0,"deliveryLocationId":"Destination","deliveryServiceTime":0}],"planningHorizon":{"start":"2024-03-06T08:00:00+01:00","end":"2024-03-06T20:00:00+01:00"}}
After creation of the plan, the plan got an id like this "c243f8dd-fa43-417b-94ef-aba566d7d199".

Now I would like to start the optimization process with the following function:

Code: Select all

public void StartOptimizationAsync(string apiKey, Guid planId)
{
    using (var client = new HttpClient { BaseAddress = new Uri("https://api.myptv.com/routeoptimization/v1/")})
    {
        // Add apiKey to Header
        client.DefaultRequestHeaders.Add("apiKey", apiKey);

        RouteOptimizationClient routeOptimizationClient = new RouteOptimizationClient(client);
        routeOptimizationClient.StartOptimizationAsync(planId, OptimizationQuality.STANDARD, null, null, null);
    }
}
RouteOptimizationClient is specified by the Routing API Specification file (https://api.myptv.com/meta/services/rou ... enapi.json).
planId would be "c243f8dd-fa43-417b-94ef-aba566d7d199" in this case.

In the request, the task will instantly be cancelled (at this line):

Code: Select all

var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
with message "Eine Aufgabe wurde abgebrochen." (ENG: A task was cancelled)

Code: Select all

at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at FormsMapCS.RouteOpt.RouteOptimizationClient.<StartOptimizationAsync>d__20.MoveNext()
Do you have any clue why this happens?
I also tried to post the ID of the created plan at the API code sample website (https://developer.myptv.com/en/document ... imise-plan) and get the following error:

Code: Select all

{
  "traceId": "9a61b152a96c475ca38752adffbc4cf7",
  "errorId": "15f342b9-6276-49cf-8738-3ad48dfaab12",
  "description": "A requested resource does not exist.",
  "errorCode": "GENERAL_RESOURCE_NOT_FOUND",
  "causes": [
    {
      "description": "The ID does not exist.",
      "errorCode": "GENERAL_INVALID_ID",
      "parameter": "id",
      "details": {
        "value": "c243f8dd-fa43-417b-94ef-aba566d7d199"
      }
    }
  ]
}
I guessed that the created plan would be available on the PTV server. Is that not the case?
Thank you in advance,
Tobias Zylka
User avatar
Bernd Welter
Site Admin
Posts: 2695
Joined: Mon Apr 14, 2014 10:28 am
Contact:

Re: Starting of plan optimization fails

Post by Bernd Welter »

Hi Tobias,

at least your request seems to be valid - I can process it successful through some test client.
I will forward your IDs to some DEV colleagues. We will get back to you 1:1 I guess ;-)

Bernd
Attachments
Screenshot 2024-03-05 104214.png
Bernd Welter
Technical Partner Manager Developer Components
PTV Logistics - Germany

Bernd at... The Forum,LinkedIn, Youtube, StackOverflow
I like the smell of PTV Developer in the morning... :twisted:
User avatar
Bernd Welter
Site Admin
Posts: 2695
Joined: Mon Apr 14, 2014 10:28 am
Contact:

Re: Starting of plan optimization fails

Post by Bernd Welter »

Please create a helpdesk ticket for this ;-)

At least the codesample error message is easy:
:!: The user context of the codesample is not equal to the one you used to create the plan...
Bernd Welter
Technical Partner Manager Developer Components
PTV Logistics - Germany

Bernd at... The Forum,LinkedIn, Youtube, StackOverflow
I like the smell of PTV Developer in the morning... :twisted:
User avatar
Bernd Welter
Site Admin
Posts: 2695
Joined: Mon Apr 14, 2014 10:28 am
Contact:

Re: Starting of plan optimization fails

Post by Bernd Welter »

By the way:

Maybe you should check the following repositories - we provide various client classes (also Microsoft.NET) for the Developer APIs:
Just to mention a few of them: Bernd
Bernd Welter
Technical Partner Manager Developer Components
PTV Logistics - Germany

Bernd at... The Forum,LinkedIn, Youtube, StackOverflow
I like the smell of PTV Developer in the morning... :twisted:
unheal1337
Posts: 5
Joined: Fri Feb 23, 2024 1:51 pm

Re: Starting of plan optimization fails

Post by unheal1337 »

Hi,

I checked the clients which requires .NET 6, but I needed a lower version (4.x).

I could figure out what caused the task to cancel immediately and changed the following line in the generated client (function StartOptimizationAsync):

Code: Select all

var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
to

Code: Select all

var response_ = client_.SendAsync(request_, HttpCompletionOption.ResponseHeadersRead, cancellationToken).Result;
This way the task will run and start the optimization.

Nevertheless sooner or later I will also try out the OptiFlow Api.
So far, everything I was trying to implement is working fine. Thank you very much for your help.
Tobias
User avatar
Bernd Welter
Site Admin
Posts: 2695
Joined: Mon Apr 14, 2014 10:28 am
Contact:

Re: Starting of plan optimization fails

Post by Bernd Welter »

Awesome!

Please do not forget to close your ticket, too ;-)
Bernd Welter
Technical Partner Manager Developer Components
PTV Logistics - Germany

Bernd at... The Forum,LinkedIn, Youtube, StackOverflow
I like the smell of PTV Developer in the morning... :twisted:
Post Reply