Examples : LoadConstraintExample.cs
using System;
using System.Collections.Generic;
using System.Globalization;
using VRSolver;
namespace Examples
{
/// <summary>
/// A simple example which shows how to use the Job and Resource Load/Capacity
/// constraints.
/// </summary>
internal class LoadConstraintExample
{
internal void Start()
{
//
// Create the problem and set some weights and parameters
//
Problem problem = new Problem
{
TotalDistanceWeight = 1,
TotalWorkTimeWeight = 0,
};
//
// Add a Job to the Problem. The Job contains two JobParts. When
// a Resource completes the first JobPart (A1) the Resource's load
// increases by 10. When the Resource completes the second JobPart
// (A2) the Resource's load decreases by 10.
//
//
problem.AddJob
(
new Job
{
// An ID for the Job
ID = "A",
// The penalty/cost for not assigning this Job to any Resource
UnassignedWeight = 100000,
// The JobParts that make up this Job
Parts = new JobPart[]
{
new JobPart
{
//
// Create a Load parameter with the ID "LoadSize"
// The ID will be referenced in the Resources' Load
// constraints defined later.
//
LoadChange = new Dictionary<string, double>
{
["LoadSize"] = 10,
},
// An ID for the JobPart
ID = "A1",
// Set the location for the Job
// (This is the index for this Job in the TravelTime array)
Loc = new Location(0),
// Set the earliest time this JobPart can be started
WindowOpen = DateTime.Parse("2018-02-28T10:30:00", CultureInfo.InvariantCulture),
// Set the latest time this JobPart must be started by
WindowClose = DateTime.Parse("2018-02-28T11:00:00", CultureInfo.InvariantCulture),
// Set how long it takes to complete this JobPart
JobTime = TimeSpan.FromMinutes(30),
},
new JobPart
{
LoadChange = new Dictionary<string, double>
{
["LoadSize"] = -10,
},
ID = "A2",
Loc = new Location(1),
WindowOpen = DateTime.Parse("2018-02-28T14:30:00", CultureInfo.InvariantCulture),
WindowClose = DateTime.Parse("2018-02-28T15:00:00", CultureInfo.InvariantCulture),
JobTime = TimeSpan.FromMinutes(30),
}
},
}
);
//
// Add another Job with a "LoadSize" of 20
//
problem.AddJob
(
new Job
{
ID = "B",
UnassignedWeight = 100000,
Parts = new JobPart[]
{
new JobPart
{
LoadChange = new Dictionary<string, double>
{
["LoadSize"] = 20,
},
ID = "B1",
Loc = new Location(1),
WindowOpen = DateTime.Parse("2018-02-28T12:30:00", CultureInfo.InvariantCulture),
WindowClose = DateTime.Parse("2018-02-28T13:00:00", CultureInfo.InvariantCulture),
JobTime = TimeSpan.FromMinutes(30),
}
},
}
);
//
// Add a Resource/vehicle to the problem
//
problem.AddResource
(
new Resource
{
//
// Set the maximum accumulated value for each of the
// LoadChange ID's defined in the Jobs. This Resource can
// not exceed an accumulated total of 25 of "LoadSize".
// For example, it cannot complete a JobPart of "LoadSize"
// 10 and then complete another JobPart "LoadSize" 20
// because the accumulated load after the second JobPart
// would be 10 + 20 = 30.
//
MaxLoad = new Dictionary<string, double>
{
["LoadSize"] = 25,
},
// Set an ID for this Resource
ID = "R1",
// Set a penalty/cost for using this Resource
UseCost = 0,
// See the Index for this Resource's travel times in the TravelTime matrix
TimeMatrixIndex = 0,
// Set the starting location for this Resource
// (This is the index for this Resource in the TravelTime array)
StartLocation = new Location(2),
// Set the finish location for this Resource
EndLocation = new Location(2),
// Set the earliest time this Resource can depart its starting location
EarliestStart = DateTime.Parse("2018-02-28T06:00:00", CultureInfo.InvariantCulture),
// Set the latest time this Resource must depart its starting location by
LatestStart = DateTime.Parse("2018-02-28T18:00:00", CultureInfo.InvariantCulture),
// Set the latest this Resource must arrive at its final location by
LatestReturn = DateTime.Parse("2018-02-28T18:00:00", CultureInfo.InvariantCulture),
}
);
// Add another Resource
problem.AddResource
(
new Resource
{
MaxLoad = new Dictionary<string, double>
{
["LoadSize"] = 25,
},
ID = "R2",
UseCost = 0,
TimeMatrixIndex = 0,
StartLocation = new Location(3),
EndLocation = new Location(3),
EarliestStart = DateTime.Parse("2018-02-28T06:00:00", CultureInfo.InvariantCulture),
LatestStart = DateTime.Parse("2018-02-28T18:00:00", CultureInfo.InvariantCulture),
LatestReturn = DateTime.Parse("2018-02-28T18:00:00", CultureInfo.InvariantCulture),
}
);
//
// Create the travel time and distance multi-dimensional array.
// The travel time and distance array is a matrix containing the
// travel times (and distances) between every pair of locations in
// the problem. In this example, we will create two vehicles and
// two jobs, each with different locations, so we need a 4x4
// matrix. The first dimension in the array is to allow for
// different travel times for different vehicles. Therefore we
// could have a different matrix for every vehicle but because in
// this example we will assume every vehicle travels at the same
// speed we will simply set the length of the first dimension as
// one and every vehicle can share the same travel time matrix.
// This saves a lot of memory for larger problems and can make the
// solver faster.
//
TravelTime[,,] traveltimes = new TravelTime[1, 4, 4];
// Set the travel time from location 0 to locations 0..3
// The distances can be in any unit e.g. km, miles, meters etc
traveltimes[0, 0, 0] = new TravelTime(TimeSpan.FromSeconds(0), 0.0);
traveltimes[0, 0, 1] = new TravelTime(TimeSpan.FromSeconds(1800), 20.5);
traveltimes[0, 0, 2] = new TravelTime(TimeSpan.FromSeconds(1200), 9.4);
traveltimes[0, 0, 3] = new TravelTime(TimeSpan.FromSeconds(3600), 38.0);
// Set the travel time from location 1 to locations 0..3
traveltimes[0, 1, 0] = new TravelTime(TimeSpan.FromSeconds(1800), 20.5);
traveltimes[0, 1, 1] = new TravelTime(TimeSpan.FromSeconds(0), 0.0);
traveltimes[0, 1, 2] = new TravelTime(TimeSpan.FromSeconds(600), 5.4);
traveltimes[0, 1, 3] = new TravelTime(TimeSpan.FromSeconds(900), 7.2);
// Set the travel time from location 2 to locations 0..3
traveltimes[0, 2, 0] = new TravelTime(TimeSpan.FromSeconds(1200), 9.4);
traveltimes[0, 2, 1] = new TravelTime(TimeSpan.FromSeconds(600), 5.4);
traveltimes[0, 2, 2] = new TravelTime(TimeSpan.FromSeconds(0), 0.0);
traveltimes[0, 2, 3] = new TravelTime(TimeSpan.FromSeconds(2400), 28.2);
// Set the travel time from location 3 to locations 0..3
traveltimes[0, 3, 0] = new TravelTime(TimeSpan.FromSeconds(3600), 38.0);
traveltimes[0, 3, 1] = new TravelTime(TimeSpan.FromSeconds(900), 7.2);
traveltimes[0, 3, 2] = new TravelTime(TimeSpan.FromSeconds(2400), 28.2);
traveltimes[0, 3, 3] = new TravelTime(TimeSpan.FromSeconds(0), 0.0);
//
// Set the TravelTimes matrix in the Problem
//
problem.SetTravelTimes(traveltimes);
//
// Create the Solver and solve the Problem
//
Solver solver = new Solver
{
// Set the maximum time to give the solver
MaximumSolveTime = new TimeSpan(0, 0, 1)
};
//
// The Solve method returns a Solution object which contains the Job
// assignments and schedules for each Resource
//
Console.WriteLine("Starting solver...");
Solution solution = solver.Solve(problem);
Console.WriteLine("Solver finished. Solution = " + solution.ObjectiveFunctionValue);
//
// Write the solution to text file
//
WriteSolutionExample.WriteSolution(solution, "Solution.txt");
}
}
}