Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2352626

Browse files
Explain backtracking_search parameters
1 parent 7832f97 commit 2352626

File tree

1 file changed

+121
-1
lines changed

1 file changed

+121
-1
lines changed

csp.ipynb

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,127 @@
384384
"cell_type": "markdown",
385385
"metadata": {},
386386
"source": [
387-
"### Visualization\n",
387+
"Now let us explore the optional keyword arguments that the **backtracking_search** function takes. These optional arguments help speed up the assignment further. Along with these, we will also point out to methods in the CSP class that help make this work. \n",
388+
"\n",
389+
"The first of these is **select_unassigned_variable**. It takes in a function that helps in deciding the order in which variables will be selected for assignment. We use a heuristic called Most Restricted Variable which is implemented by the function **mrv**. The idea behind **mrv** is to choose the variable with the fewest legal values left in its domain. The intuition behind selecting the **mrv** or the most constrained variable is that it allows us to encounter failure quickly before going too deep into a tree if we have selected a wrong step before. The **mrv** implementation makes use of another function **num_legal_values** to sort out the variables by a number of legal values left in its domain. This function, in turn, calls the **nconflicts** method of the **CSP** to return such values.\n"
390+
]
391+
},
392+
{
393+
"cell_type": "code",
394+
"execution_count": null,
395+
"metadata": {
396+
"collapsed": false
397+
},
398+
"outputs": [],
399+
"source": [
400+
"%psource mrv"
401+
]
402+
},
403+
{
404+
"cell_type": "code",
405+
"execution_count": null,
406+
"metadata": {
407+
"collapsed": false
408+
},
409+
"outputs": [],
410+
"source": [
411+
"%psource num_legal_values"
412+
]
413+
},
414+
{
415+
"cell_type": "code",
416+
"execution_count": null,
417+
"metadata": {
418+
"collapsed": false
419+
},
420+
"outputs": [],
421+
"source": [
422+
"%psource CSP.nconflicts"
423+
]
424+
},
425+
{
426+
"cell_type": "markdown",
427+
"metadata": {},
428+
"source": [
429+
"Another ordering related parameter **order_domain_values** governs the value ordering. Here we select the Least Constraining Value which is implemented by the function **lcv**. The idea is to select the value which rules out the fewest values in the remaining variables. The intuition behind selecting the **lcv** is that it leaves a lot of freedom to assign values later. The idea behind selecting the mrc and lcv makes sense because we need to do all variables but for values, we might better try the ones that are likely. So for vars, we face the hard ones first.\n"
430+
]
431+
},
432+
{
433+
"cell_type": "code",
434+
"execution_count": null,
435+
"metadata": {
436+
"collapsed": false
437+
},
438+
"outputs": [],
439+
"source": [
440+
"%psource lcv"
441+
]
442+
},
443+
{
444+
"cell_type": "markdown",
445+
"metadata": {},
446+
"source": [
447+
"Finally, the third parameter **inference** can make use of one of the two techniques called Arc Consistency or Forward Checking. The details of these methods can be found in the **Section 6.3.2** of the book. In short the idea of inference is to detect the possible failure before it occurs and to look ahead to not make mistakes. **mac** and **forward_checking** implement these two techniques. The **CSP** methods **support_pruning**, **suppose**, **prune**, **choices**, **infer_assignment** and **restore** help in using these techniques. You can know more about these by looking up the source code."
448+
]
449+
},
450+
{
451+
"cell_type": "markdown",
452+
"metadata": {},
453+
"source": [
454+
"Now let us compare the performance with these parameters enabled vs the default parameters. We will use the Graph Coloring problem instance usa for comparison. We will call the instances **solve_simple** and **solve_parameters** and solve them using backtracking and compare the number of assignments."
455+
]
456+
},
457+
{
458+
"cell_type": "code",
459+
"execution_count": null,
460+
"metadata": {
461+
"collapsed": true
462+
},
463+
"outputs": [],
464+
"source": [
465+
"solve_simple = copy.deepcopy(usa)\n",
466+
"solve_parameters = copy.deepcopy(usa)"
467+
]
468+
},
469+
{
470+
"cell_type": "code",
471+
"execution_count": null,
472+
"metadata": {
473+
"collapsed": false
474+
},
475+
"outputs": [],
476+
"source": [
477+
"backtracking_search(solve_simple)\n",
478+
"backtracking_search(solve_parameters, order_domain_values=lcv, select_unassigned_variable=mrv, inference=mac )"
479+
]
480+
},
481+
{
482+
"cell_type": "code",
483+
"execution_count": null,
484+
"metadata": {
485+
"collapsed": false
486+
},
487+
"outputs": [],
488+
"source": [
489+
"solve_simple.nassigns"
490+
]
491+
},
492+
{
493+
"cell_type": "code",
494+
"execution_count": null,
495+
"metadata": {
496+
"collapsed": false
497+
},
498+
"outputs": [],
499+
"source": [
500+
"solve_parameters.nassigns"
501+
]
502+
},
503+
{
504+
"cell_type": "markdown",
505+
"metadata": {},
506+
"source": [
507+
"## Graph Coloring Visualization\n",
388508
"\n",
389509
"Next, we define some functions to create the visualisation from the assingment_history of **coloring_problem1**. The reader need not concern himself with the code that immediately follows as it is the usage of Matplotib with IPython Widgets. If you are interested in reading more about these visit [ipywidgets.readthedocs.io](http://ipywidgets.readthedocs.io). We will be using the **networkx** library to generate graphs. These graphs can be treated as the graph that needs to be colored or as a constraint graph for this problem. If interested you can read a dead simple tutorial [here](https://www.udacity.com/wiki/creating-network-graphs-with-python). We start by importing the necessary libraries and initializing matplotlib inline.\n"
390510
]

0 commit comments

Comments
 (0)