Examples : ManualAssignExample.cs

using System;
using VRSolver;
 
namespace Examples
{
    /// <summary>
    /// An example showing how to manually assign/unassign Jobs to/from 
    /// Resources and get information on resulting violations in the Solution.
    /// </summary>
    internal class ManualAssignExample
    {
        internal void Start()
        {
            //
            // Load a Problem from a JSON file
            //
            Problem problem = Problem.Load("ExampleData\\Demo-C.json");
 
            //
            // Create an empty solution
            //
            Solution solution = new Solution(problem);
 
            //
            // Get the JobPart to assign and a Resource to assign it to
            //
            JobPart jobpart = problem.GetJobPart("AA-C");
            Resource resource = problem.GetResource("D");
 
            //
            // Insert the JobPart at the start of the route
            //
            solution.InsertJobPart(resource, jobpart, 0);
 
            //
            // Get the new RouteMetrics and Violations in this route
            //
            RouteMetrics metrics = solution.GetRouteMetrics(resource);
 
            //
            // Print out any violations
            //
            Console.WriteLine("After assigning JobPart:");
 
            ListViolations(metrics);
            
            //
            // Remove the JobPart again
            //
            solution.UnassignJobPart(resource, jobpart);
 
            Console.WriteLine("After removing JobPart:");
 
            metrics = solution.GetRouteMetrics(resource);
 
            ListViolations(metrics);
 
            //
            // Use the FindBestInsertion method to assign another Job
            //
 
            Job job = problem.GetJob("1");
 
            JobInsertion result = solution.FindBestInsertion(resource, job);
 
            //
            // If the Job can be assigned to this Resource then do this assignment
            //
            if (result.IsFeasible)
            {
                Console.WriteLine("Assigning Job " + job.ID + " to resource " + resource.ID 
                                  + " reduces the Solution's ObjectiveFunction by " 
                                  + result.ObjectiveFunctionChange);
 
                //
                // Complete the insertion
                //
                result.DoInsert();
            }
            else
            {
                Console.WriteLine("Job " + job.ID + " cannot be assigned to resource " + resource.ID);
            }
 
            //
            // Use the Solver to assign the rest of the Jobs and improve the Solution
            //
            Solver solver = new Solver
            {
                // Set the maximum time to give the solver
                MaximumSolveTime = new TimeSpan(0, 0, 5)
            };
 
            //
            // The Solve method returns a Solution object which contains the Job 
            // assignments and schedules for each Resource
            //
            Console.WriteLine("Starting solver...");
 
            solution = solver.Solve(solution);
 
            Console.WriteLine("Solver finished. Solution = " + solution.ObjectiveFunctionValue);
        }
 
        internal static void ListViolations(RouteMetrics metrics)
        {
            if (metrics.Violations.Length==0)
                Console.WriteLine("No violations.");
 
            for (int i = 0; i < metrics.Violations.Length; i++)
            {
                Violation violation = metrics.Violations[i];
 
                Console.Write("Violation of type " + violation.ViolationType + " at positions : ");
 
                foreach (int index in violation.RoutePositions)
                {
                    Console.Write(index + " ");
                }
 
                Console.WriteLine();
            }
        }
    }
}