📄 page442.html
字号:
<HTML><HEAD><TITLE>Abstract Backtracking Solvers</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html6265" HREF="page443.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6263" HREF="page439.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6257" HREF="page441.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6267" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0014230000000000000000">Abstract Backtracking Solvers</A></H2><P>The usual way to implement a backtracking algorithmis to write a method which traverses the solution space.This section presents an alternate, object-oriented approachthat is based on the notion of an<em>abstract solver</em><A NAME=32491> </A><A NAME=32492> </A>.<P>Think of a solver as an abstract machine,the sole purpose of which is to search a given solution spacefor the best possible solution.A machine is an object.Therefore, it makes sense thatwe represent it as an instance of some class.<P>Program <A HREF="page442.html#progsolvera"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>Solver</tt> class.The abstract <tt>Solver</tt> classextends the abstract <tt>Object</tt> classintroduced in Program <A HREF="page116.html#progobjecta"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.The <tt>Solver</tt> class contains two instance attributes,<tt>_bestSolution</tt> and <tt>_bestObjective</tt>,two concrete methods,<tt>updateBest</tt> and <tt>solve</tt>and the abstract method <tt>search</tt>.Since <tt>search</tt> is an abstract method,its implementation must be given in a derived class.<P><P><A NAME="33419"> </A><A NAME="progsolvera"> </A> <IMG WIDTH=575 HEIGHT=485 ALIGN=BOTTOM ALT="program32505" SRC="img1731.gif" ><BR><STRONG>Program:</STRONG> Abstract <tt>Solver</tt> class.<BR><P><P>The purpose of the <tt>solve</tt> methodis to solve the problem by conducting a search of the solution space.In addition to <tt>self</tt>,this method takes as its argumentan instance of a class derived from <tt>Solution</tt> that is the nodein the solution space from which to begin the search.The <tt>solve</tt> method returns theto the best solution found.<P>The <tt>solve</tt> method does not search the solution space itself--it merely sets things up for the <tt>search</tt> method.It is the <tt>search</tt> method,which is provided by a derived class,that does the actual searching.When <tt>search</tt> returns it is expected that the<tt>_bestSolution</tt> instance attribute will refer to the best solutionand that <tt>_bestObjective</tt> will be the value of the objective functionfor the best solution.<P>The <tt>updateBest</tt> methodis meant to be called by the <tt>search</tt> methodas it explores the solution space.As each complete solution is encountered,the <tt>updateBest</tt> method is called to keep trackof the solution which <em>minimizes</em> the objective function.<P><BR> <HR><UL> <LI> <A NAME="tex2html6268" HREF="page443.html#SECTION0014231000000000000000">Depth-First Solver</A><LI> <A NAME="tex2html6269" HREF="page444.html#SECTION0014232000000000000000">Breadth-First Solver</A></UL><HR><A NAME="tex2html6265" HREF="page443.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6263" HREF="page439.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6257" HREF="page441.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6267" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright © 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -