{"_id":"57358bc41f16241700c8a03e","__v":14,"category":{"_id":"5706099c21cfed0e00e8c60f","project":"5706099c21cfed0e00e8c60b","version":"5706099c21cfed0e00e8c60e","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-04-07T07:17:48.833Z","from_sync":false,"order":0,"slug":"documentation","title":"Introduction"},"version":{"_id":"5706099c21cfed0e00e8c60e","hasDoc":true,"project":"5706099c21cfed0e00e8c60b","__v":6,"hasReference":true,"createdAt":"2016-04-07T07:17:48.808Z","releaseDate":"2016-04-07T07:17:48.808Z","categories":["5706099c21cfed0e00e8c60f","573548f4afab4417007239cf","57354970fc5f1e0e001a463c","573549791f16241700c89fc9","57441052583f470e000a7947","57a068f90c933e0e00249bae"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"","version_clean":"1.0.17","version":"1.0.17"},"user":"570614bde2df830e00d52774","githubsync":"","parentDoc":null,"project":"5706099c21cfed0e00e8c60b","metadata":{"title":"","description":"","image":[]},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-05-13T08:09:40.904Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":2,"body":"# Introduction\nOn top of drivers and jobs, a routing can also have a number of constraints that need to be satisfied. For example, there might be a restriction that a certain type of vehicle cannot carry more than a certain number of boxes. These constraints can take two forms, where some of them must be satisfied, while for others, simply trying to optimize their values suffice. The example of the former would be a capacity constraint, and the example of the latter would be a distance constraint. These conditions are modelled as \"hard constraint\" and \"soft constraint\" respectively. You can designate which set of constraints should be hard, and which should be soft.\n\nAlso, not all of the constraints should carry the same weight, as their values can vary greatly. For example, because the capacity constraint is either satisfied or not, its value can be either 1 or 0. On the other hand, the value of the distance constraint is defined as the sum of the travel distances among all drivers, and as a result, the range and the magnitude of its value would be markedly different from that of the capacity constraint. Thus, you can also specify the weight for each of the constraints. Note that the \"cost\" of the solution is a linear combination of the constraints' values.\n\nIn the following section, we list all available constraints, along with the required parameters and how their values are determined. If the constraint doesn't have any other parameter, you only have to provide the name of the constraint in \"type\" field and the weight of the constraint in \"constraint\" field. For example, Capacity Constraint, which does not take any parameter, can be specified as:\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"{\\n \\\"type\\\" : \\\"CapacityConstraint\\\",\\n \\\"weight\\\" : 1.0\\n}\",\n \"language\": \"json\"\n }\n ]\n}\n[/block]\nOn the other hand, Balance Constraint can be specified as:\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"{\\n \\\"type\\\": \\\"BalanceConstraint\\\",\\n \\\"weight\\\": 20.0,\\n \\\"equation\\\": [\\n [\\n 2.0,\\n [ \\\"Task\\\", \\\"Task\\\" ]\\n ],\\n [\\n 3.0,\\n [ \\\"Distance\\\" ]\\n ]\\n ]\\n}\",\n \"language\": \"json\"\n }\n ]\n}\n[/block]\n# List of all available constraints\n\n## Balance Constraint\nBalance constraint is, in essence, a requirement that all routes should have the same value for some equation. We provide you four variables (Task, Difficulty, Distance, SkillLevel), and you can construct an equation to be a combination of these variables. Task is the number of tasks in the route, Difficulty is the sum of all tasks' difficulty values, Distance is the travel distance of the route, and SkillLevel is the driver's skill level.The value of the constraint is the difference between the maximum and the minimum values of all routes' equations. \n[block:parameters]\n{\n \"data\": {\n \"h-0\": \"Parameter\",\n \"h-1\": \"Type\",\n \"h-2\": \"Description\",\n \"0-0\": \"equation\",\n \"0-1\": \"Array[Array[Double, Array[Variable]]]\",\n \"0-2\": \"An equation to be calculated for each routes. The value is determined as the sum of all terms specified by the inner value. For example, if the equation is\\n[\\n [\\n 2.0,\\n [\\\"Task\\\", \\\"Task\\\"]\\n ],\\n [\\n 1.0,\\n [\\\"Difficulty\\\"]\\n ]\\n]\\nit is translated as 2 * Task * Task + Difficulty.\"\n },\n \"cols\": 3,\n \"rows\": 1\n}\n[/block]\n## Capacity Constraint\nCapacity Constraint limits the volume/weight of the boxes each vehicle can carry. Its value is 1 if the capacity exceeds the limit, or 0 if the constraint is satisfied.\n\n## Convex Hull No Intersection Constraint\nWhen the route is created, the points that comprises of the route create convex hull on the map. The constraint makes sure that none of the convex hulls created by the routes overlap with the given lines. The value is the number of lines that intersects with any of the convex hulls.\n[block:parameters]\n{\n \"data\": {\n \"h-0\": \"Parameter\",\n \"h-1\": \"Type\",\n \"h-2\": \"Description\",\n \"0-0\": \"lines\",\n \"0-1\": \"Array[Line]\",\n \"0-2\": \"An array of lines that drivers should not cross.\"\n },\n \"cols\": 3,\n \"rows\": 1\n}\n[/block]\n## Distance Constraint\nDistance constraint ensures that the lower the total distance travelled by all drivers, the better the solution is. It takes the sum of travel distances of all drivers as its value.\n\n## Driver Preference Constraint\nIn some cases, certain locations, or the \"features\" of the locations (for example, zip code), are preferred by some drivers. Given the list of the drivers and the labels of their preferred locations, the constraint takes its value the number of tasks in the route that does not satisfy the driver's preference.\n\n[block:parameters]\n{\n \"data\": {\n \"h-0\": \"Parameter\",\n \"h-1\": \"Type\",\n \"h-2\": \"Description\",\n \"0-0\": \"Preferences\",\n \"0-1\": \"Map[String, Array[String]]\",\n \"0-2\": \"A map from driver IDs to the list of preferred locations.\"\n },\n \"cols\": 3,\n \"rows\": 1\n}\n[/block]\n## Location Label Single Visit Constraint\nIt is desirable to make sure that the driver visits the locations with certain features only once. For example, it might be required that certain zip code is visited only once by the same vehicle. The constraint counts the number of times any location labels are visited if they are visited more than once.\n\n## Max Active Driver Constraint\nThe constraint aims to make sure that all of the available drivers are used in the solution. Its value is the number of idle drivers, i.e the number of total drivers - the number of drivers occupied with at least one job.\n\n## Max Capacity Constraint\nSometimes it is not possible to fully utilize the capacity of the vehicle. The max capacity constraint takes the percentage of the total capacity that can be used (for example, 0.8) for each capacity type and counts the number of vehicles that failed to meet the capacity restriction.\n[block:parameters]\n{\n \"data\": {\n \"h-0\": \"Parameter\",\n \"h-1\": \"Type\",\n \"h-2\": \"Description\",\n \"0-0\": \"maxCapacityRatio\",\n \"0-1\": \"Map[String, Double]\",\n \"0-2\": \"The map from capacity type (\\\"Volume\\\" or \\\"Weight\\\") to the maximum capacity ratio.\"\n },\n \"cols\": 3,\n \"rows\": 1\n}\n[/block]\n## Max Job Size Constraint\nBecause the drivers have different skill levels, the number of jobs they can handle are different. Given the maximum number of jobs that can be handled by people with certain skill level, the constraint counts the difference between the number of jobs assigned to the driver and the number of jobs the driver is allowed to have, if the driver is assigned more jobs than he can handle.\n\n[block:parameters]\n{\n \"data\": {\n \"h-0\": \"Parameter\",\n \"h-1\": \"Type\",\n \"h-2\": \"Description\",\n \"0-0\": \"maxSize\",\n \"0-1\": \"Map[Int, Int]\",\n \"0-2\": \"A map from a skill level to the number of jobs allowed by people with the skill level.\"\n },\n \"cols\": 3,\n \"rows\": 1\n}\n[/block]\n## Max Visit Constraint\nMax Visit Constraint ensures that the driver visits maximum of specified number of locations. The value is the number of locations that exceeds the specified maximum number of locations, i.e the number of locations that the driver visits - the allowed maximum number of locations.\n[block:parameters]\n{\n \"data\": {\n \"h-0\": \"Parameter\",\n \"h-1\": \"Type\",\n \"h-2\": \"Description\",\n \"0-0\": \"maxVisit\",\n \"0-1\": \"Int\",\n \"0-2\": \"The maximum number of locations that drivers are allowed to visit.\"\n },\n \"cols\": 3,\n \"rows\": 1\n}\n[/block]\n## Min Load Capacity Constraint\nJust like Max Capacity Constraint, for economical reasons, it might be desirable to ensure that all vehicles have at least certain percentage of capacity filled when carrying out the delivery or pick up. The value of the constraint is 1 if the driver satisfies the minimum load constraint, and 0 otherwise.\n[block:parameters]\n{\n \"data\": {\n \"h-0\": \"Parameter\",\n \"h-1\": \"Type\",\n \"h-2\": \"Description\",\n \"0-0\": \"minCapacityRatio\",\n \"0-1\": \"Map[String, Double]\",\n \"0-2\": \"A map from capacity value (\\\"Volume\\\" or \\\"Weight\\\") to the ratio of capacity (between 0 and 1) that the vehicle must load.\"\n },\n \"cols\": 3,\n \"rows\": 1\n}\n[/block]\n## Min Unique Location Label Constraint\nMin Unique Location Label Constraint ensures that minimum number of the \"representation\" of locations are visited. For example, it might be required, or advantageous, for each driver to visit only the region covered by one zip code. The value of the constraint is the number of the unique location labels that the driver visits if no labels are supplied. If the set of labels are provided, only the location labels in the set are considered.\n[block:parameters]\n{\n \"data\": {\n \"h-0\": \"Parameter\",\n \"h-1\": \"Type\",\n \"h-2\": \"Description\",\n \"0-0\": \"labels\",\n \"0-1\": \"Array[String]\",\n \"0-2\": \"A set of location labels to be considered. One example of the parameter would be the entire list of zip codes. If empty, all labels of the visited locations are considered.\"\n },\n \"cols\": 3,\n \"rows\": 1\n}\n[/block]\n## No Convex Hull Overlap Constraint\nSimilar to Convex Hull No Intersection Constraint, which prevents the convex hulls created by routes from intersecting the given lines, No Convex Hull Overlap Constraint makes sure that none of the convex hulls created by routes cover the same region. The constraint's value is the number of convex hulls that overlap with each other.\n\n## No Cross Route Constraint\nNo Cross Route Constraint is similar to No Convex Hull Overlap Constraint in a sense that they both consider the regions / locations covered by drivers. One difference is that the value of No Cross Route Constraint is determined by the number of times the routes intersect each other, and as a result it is possible for two routes that overlap with each other to contribute to the value of the constraint by more than 1. Also, since creating the convex hull of a route is effectively same as adding an \"edge\" between the start and the end of the route, there exist cases which violate No Convex Hull Overlap Constraint but not No Cross Route Constraint.\n\n## No Intersection Constraint\nThe relationship between No Convex Hull Overlap Constraint and No Cross Route Constraint is very similar to the relationship between Convex Hull No Intersection Constraint and No Intersection Constraint. While Convex Hull No Intersection Constraint counts the number of convex hulls that intersect with the given lines, No Intersection Constraint counts the actual number of intersections between the routes and the given lines. \n[block:parameters]\n{\n \"data\": {\n \"h-0\": \"Parameter\",\n \"h-1\": \"Type\",\n \"h-2\": \"Description\",\n \"0-0\": \"lines\",\n \"0-1\": \"Array[Line]\",\n \"0-2\": \"A set of lines that drivers should not cross.\"\n },\n \"cols\": 3,\n \"rows\": 1\n}\n[/block]\n## Preference Center Constraint\nIn some cases, some drivers prefer to perform pick up and delivery in certain location, as they might be familiar with that region. Using the constraint increases the probability of satisfying those preferences. The value of the constraint is the sum of the distances between the preferred location and the task locations in the route.\n[block:parameters]\n{\n \"data\": {\n \"h-0\": \"Parameter\",\n \"h-1\": \"Type\",\n \"h-2\": \"Description\",\n \"0-0\": \"driverPreference\",\n \"0-1\": \"Map[String, Location]\",\n \"0-2\": \"A map from a driver ID to the location preferred by the driver.\"\n },\n \"cols\": 3,\n \"rows\": 1\n}\n[/block]\n## Preference No Intersection Constraint\nAlthough Preference Center Constraint helps creating routes that satisfy the preferences of drivers by preventing jobs assigned to drivers from being too far from their preferred locations, the measure might not truly reflect the preference in some cases. For example, if a driver is familiar with certain \nregion but is unaccustomed to the mountain right next to it, the delivery to the mountain might be assigned to the driver if we simply enforce jobs to be near the preferred location. Using the constraint mitigates the situation by penalizing the route every time the line created by connecting the preferred location and the task locations intersect with the given line. In this case, the given line can be the line across the mountain. The value of the constraint is the count of the intersection between the given line and the line from the preferred location to the task location.\n[block:parameters]\n{\n \"data\": {\n \"h-0\": \"Parameter\",\n \"h-1\": \"Type\",\n \"h-2\": \"Description\",\n \"0-0\": \"driverPreference\",\n \"0-1\": \"Map[String, Location]\",\n \"0-2\": \"A map from a driver ID to the driver's preferred location.\",\n \"1-0\": \"noIntersectionLines\",\n \"1-1\": \"Array[Line]\",\n \"1-2\": \"A set of lines that the drivers should not cross.\"\n },\n \"cols\": 3,\n \"rows\": 2\n}\n[/block]\n## Route Dispersion Constraint\nIn general, a tightly packed route spanning small region is preferred to the route covering a large region, because the former implies that the driver travels less and covers less area. The constraint helps creating such routes by enforcing the \"diagonal distance\" (the longest distance between the tasks in a route) to be small. The value of the constraint is the square of the diagonal distance of a given route.\n\n## Route Duration Constraint\nSimilar in concept to Route Dispersion Constraint, a route that takes small amount of time is desirable. The constraint has the total travel time (the sum of the travel time among all routes, not to be confused with the difference between the earliest start time and the latest end time among all routes) as its value, thus penalizing routes that take long time.\n\n## Single Driver Visit Constraint\nIf there are multiple deliveries or pick ups to be made in one location (for example, the driver might have to deliver packages to different people in one apartment complex), it is often desirable for a single driver to visit location, as capacity permits, to avoid wasting resources. The constraint's value is the number of locations that are visited by more than one driver, effectively penalizing solutions with locations visited by multiple drivers.\n\n## Single Visit Constraint\nWhile it is important for a location to be visited by a single driver, it is also important for the driver to not return to the location after he already made a visit. The constraint enforces this restriction by having a value of 1 if the constraint is violated, and 0 otherwise.\n\n## Task Assignment Constraint\nGiven a list of tasks and the drivers that can perform each of those tasks, the constraint helps generating a solution that satisfies the task assignment as much as possible. The value of the constraint is the number of the tasks that are not carried out by one of the designated drivers.\n[block:parameters]\n{\n \"data\": {\n \"h-0\": \"Parameter\",\n \"h-1\": \"Type\",\n \"h-2\": \"Description\",\n \"0-0\": \"assignments\",\n \"0-1\": \"Map[String, Array[String]]\",\n \"0-2\": \"A map from task IDs to the IDs of the drivers that can perform the task.\"\n },\n \"cols\": 3,\n \"rows\": 1\n}\n[/block]\n## Task Vehicle Constraint\nSimilar to Task Assignment Constraint, some tasks can be performed only by certain types of vehicles. In order to penalize the solution that does not fulfill those requirements, the constraint's value is the number of the tasks not carried out by the required vehicle.\n[block:parameters]\n{\n \"data\": {\n \"h-0\": \"Parameter\",\n \"h-1\": \"Type\",\n \"h-2\": \"Description\",\n \"0-0\": \"requirements\",\n \"0-1\": \"Map[String, Array[String]]\",\n \"0-2\": \"A map from task IDs to the array of features that vehicles should contain in order to perform the task. Note that the vehicle is not required to have all the features in the list; only having one of those is sufficient.\"\n },\n \"cols\": 3,\n \"rows\": 1\n}\n[/block]\n## Time Window Constraint\nSome tasks have to be carried out within certain time period if, for example, requested by the customer. The value of the constraint is the number of tasks that is performed outside of the required time window. Note that the time window is a part of the task object, and is not supplied as an argument to the constraint.\n\n## Zone Assignment Constraint\nDue to a company policy, there might be a requirement that certain areas should only be served by designated drivers. The constraint penalizes solutions that fails to meet the requirement: its value is the number of tasks performed in the restricted zones by unqualified driver.\n[block:parameters]\n{\n \"data\": {\n \"h-0\": \"Parameter\",\n \"h-1\": \"Type\",\n \"h-2\": \"Description\",\n \"0-0\": \"assignment\",\n \"0-1\": \"Map[Polygon, Array[String]]\",\n \"0-2\": \"A map from zone, specified by polygon, to the list of the IDs of the driver that can perform tasks inside the area.\"\n },\n \"cols\": 3,\n \"rows\": 1\n}\n[/block]\n## Zone Vehicle Constraint\nThe constraint is exactly same as Zone Assignment Constraint, except instead of IDs of drivers, the vehicle features are used.\n[block:parameters]\n{\n \"data\": {\n \"h-0\": \"Parameter\",\n \"h-1\": \"Type\",\n \"h-2\": \"Description\",\n \"0-0\": \"requirements\",\n \"0-1\": \"Map[Polygon, Array[String]]\",\n \"0-2\": \"A map from zone, specified by polygon, to the list of features that the vehicles should have in order to carry out tasks in the given zone. Note that the vehicle can have only one of those features to be qualified.\"\n },\n \"cols\": 3,\n \"rows\": 1\n}\n[/block]","excerpt":"This page contains information about constraints.","slug":"constraints","type":"basic","title":"Constraints"}

# Constraints

This page contains information about constraints.