塞拉利昂籍“ LEDOR ”轮遭 阿尔巴尼亚船东基恩毕船务有限公司 ... · 2017. 6. 21. · 塞拉利昂籍“ ledor ”轮遭 阿尔巴尼亚船东基恩毕船务有限公司
第三章 - intsci.ac.cn ·...
Transcript of 第三章 - intsci.ac.cn ·...
约束推理
史忠植
中国科学院计算技术研究所
高级人工智能 第三章
2015/3/18 史忠植 约束推理 2
第三章 约束推理
3.1 概述
3.2 回溯法
3.3 约束传播
3.4 回跳法
3.5 约束推理系统COPS
3.6 ILOG SOLVER
3.7 约束逻辑程序设计
在八皇后问题中,处理过程不是根
据某种确定的计算法则,而是利用
试探和回溯的探索技术求解。为了
求得合法布局,在计算机中要存储
布局的当前状态。从最初的布局状
态开始,一步步地进行试探, 每试
探一步形成一个新的状态, 整个试
探过程形成了一棵隐含的状态树。
八皇后问题
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7
采用DFS/BFS搜索策略
The DFS/BFS tree will enumerate up to
648 combinations (assume limit depth to 8).
64^8 = 2^48 = 2.8 x 10^14 Note redundancy: Q1 in (1,3), Q2 in (2,7) … vs. Q1 in (2,7), Q2 in (1,3) …
2015/3/18 史忠植 约束推理 5
概 述 一个约束满足问题(Constraint Satisfaction
Problem, 简称CSP) 包含一组变量与一组变量间的约
束。
▪变量表示领域参数,每个变量都有一个固定的值
域。一个变量的值域可能是有限的,例如一个布尔变
量的值域包含两个值;也可能是离散无限的,如整数
域;也可能是连续的,如实数域。
{x1,x2,…xn},
{D1,D2,…Dn}, .
{4,5,6,7}
{red, green,blue}
约束满足问题CSP
Given:
(1) set of variables,
(2) domains of the variables
(3) constraints that the variables have to satisfy
Find:
An assignment of values to the variables,
so that these values satisfy all the given constraints.
In optimisation problems, also specify optimisation
criterion 2015/3/18 6 史忠植 约束推理
2015/3/18 史忠植 约束推理 7
概 述 ▪约束可用于描述领域对象的性质、相互关系、任务 要求、目标等。约束 满足问题 的目标就是找到所有 变量的一个(或多个)赋值,使所有约束都得到满足。 一元谓词。 序关系语言,只包含偏序关系或实变量上的大小关系。 形如“x - y > c” 的方程。 单位系数的线性方程与不等式,即所有的系数为 -1,0,1。 任意系数的线性方程与不等式。 约束的布尔组合。 代数与三角方程。
2015/3/18 史忠植 约束推理 8
概 述 约束表示易于理解、编码及有效实现,它具有以下优点: 约束表示允许以说明性的方式来表达领域知识,表达 能力较强,应用程序只需指定问题的目标条件及数据间 的相互关系。因而具有逻辑表示的类似性质。 约束表示允许变量的域包含任意多个值,而不像命题 只取真假二值。所以它保存了问题的一些结构信息,如 变量域的大小、变量间的相关性等,从而为问题求解提供 启发式信息。 易于并行实现。因为约束网络上的信息传播可以认为是 同时的。 适合于递增型系统。约束可以递增式地加入到约束网络。 易于与领域相关的问题求解模型相衔接。各种数学规划技 术,方程求解技术等, 都可以自然地嵌入约束系统。
2015/3/18 史忠植 约束推理 9
约束推理
约束搜索
约束搜索主要研究有限域上的约束满足。对有限域而言,
约束满足问题一般情况下是 一个 NP 问题。
约束语言
2015/3/18 史忠植 约束推理 10
约束搜索
回溯法。
约束传播。
智能回溯与真值维护。
可变次序例示。
局部修正法。
2015/3/18 史忠植 约束推理 11
约束语言
CONSTRAINTS CHIP COPS ILOG
2015/3/18 史忠植 约束推理 12
CONSTRAINTS约束语言
CONSTRAINTS是一个面向电路描述的约束表示语言。
作为一个约束表示语言, 它使用了符号处理技术来求解数
学方程。在CONSTRAITS中,物理部件的功能及器件的结构都
用约束表示。这些约束一般是线性方程与不等式, 也包括条
件表达式。约束变量一般是表示物理量的实变量。也有一些
取离散值的变量。如开关的状态、三极管的工作状态等。系
统采用表达式推理与值推理。并实现相关制导的回溯。
2015/3/18 史忠植 约束推理 13
CONSTRAINTS约束语言
CONSTRAINTS 的一个优点是在类型层次中表示约束,用约束
来表示物理对象的功能与结构。其缺点是该语言缺乏类似于面向
对象语言中的方法那样的成分,不能定义特定于某个类的概念。
同时,约束传播方法比较单一,既缺乏实域上的区间传播机制,
也缺乏有限域上的 域传播机制。
2015/3/18 史忠植 约束推理 14
约束逻辑程序设计语言CHIP CHIP(Constraint handling in Prolog) 就是这样较有影响
一个约束逻辑程序设计语言,其目的是简便、灵活而有效地解决
一大类组合问题。它通过提供几种新的计算域而增强逻辑程序设
计的能力;有限域、布尔项及有理项,对于每个计算域,都提供
有效的约束求解技术,即有限域上的一致性技术,布尔域的布尔
合一技术及有理数域上的单纯型法。除此以外,CHIP还包含一个
一般的延迟计算机制。
CHIP 主要应用于两个领域: 运筹学与硬件设计。
CHIP 缺乏类型机制,而这种机制对于表达领域概念是极其重
要的。
2015/3/18 史忠植 约束推理 15
面向对象约束语言COPS
COPS系统利用面向对象技术,将说明性约束表达与类型层次
结合起来。在形式上吸收了常规语言,主要是面向对象的程序设
计语言的基本形式。内部求解时采用约束推理机制,使说明性约
束表达式与类型层次相结合,实现知识的结构化封装,充分发挥
两者的优点,力图实现一个具有较强表达能力和较高求解效率的
约束满足系统。
2015/3/18 史忠植 约束推理 16
面向对象约束语言COPS
COPS的设计考虑了软件工程的应用要求,尽量将一个不确定问
题确定化:它允许条件语句与循环语句,而不是单
纯以递归的形式来实现迭代计算; 通过类方法的重栽实现同一
约束的不同实现,提高了程序的执行效率。COPS系统同时是一个
渐增式的开放系统,用户能通过类型层次定义,实现新的数据类
型和新的约束关系。约束语言COPS具有许多人工智能程序设计语
言的特点,如约束传播、面向目标和数据驱动的问题求解、有限
步的回溯、对象分层中的继承等。
2015/3/18 史忠植 约束推理 17
在实际应用中,算法的表现形式千变万化,但是算法的情况也和数据结构类似,许多算法的设计思想具有相似之处,我们可以对它们分类进行学习和研究。 常用的算法大致有如下一些:
•贪心法
•分治法:如二分法检索
•回溯法
•动态规划法
•局部搜索法
•分支限界法
常用的算法
2015/3/18 史忠植 约束推理 18
评价一个程序优劣的重要依据是看这个程序的执行需要
占用多少机器资源。人们最关心的就是程序所用算法运行
时所要花费的时间代价和程序中使用的数据结构占有的空
间代价。
算法的空间代价(或称空间复杂性):当被解决问题的规
模(以某种单位计算)由1增至n时,解该问题的算法所需占
用的空间也以某种单位由f(1) 增至f(n),这时我们称该算
法的空间代价是f(n)。
算法的时间代价(或称时间复杂性):当问题规模以某种
单位由1增至n时,对应算法所耗费的时间也以某种单位由
g(1)增至g(n),这时我们称算法的时间代价是g(n)。
算法分析
2015/3/18 史忠植 约束推理 19
穷尽搜索方法
穷尽搜索方法
即产生所有可能的树,然后根据评价
标准选择一棵最优的树。
Exhaustive-Search-Top(P) {where P is a CSP of the
form(V,D,C)}
1. f:= the null assignment
2. return Exhaustive-Search(f,P)
2015/3/18 史忠植 约束推理 20
穷尽搜索方法
Exhaustive-Search(f,P)
1. if f is a total assignment of the variables in P
2. if f satisfies the constraints in P
3. answer := f
4. else
5. answer := Unsat
6. else
7. v := some variable in P that is not yet assigned a
value by f
8. answer := Unsat
9. for each value while answer = Unsat
10. f(v) :=
11. answer := Exhaustive-Search(f,P)
12. return answer
2015/3/18 史忠植 约束推理 21
贪心法
贪心法把构造可行解的工作分阶段来完成。在各个
阶段,选择那些在某些意义下是局部最优的方案,期
望各阶段的局部最优的选择带来整体最优。
例:Dijkstra的最短路径算法、Kruskal的求最小
生成树算法、信号灯问题
2015/3/18 史忠植 约束推理 22
回溯算法
有些问题需要彻底的搜索才能解决问题,
然而,彻底的搜索要以大量的运算时间为代
价,对于这种情况可以通过回溯法来去掉一
些分支,从而大大减少搜索的次数。
八皇后问题
迷宫问题
深度优先周游树或图
约束推理 ppt
四皇后问题中隐含的状态树
四皇后问题
2015/3/18 史忠植 约束推理 24
4-Queens Puzzle
2015/3/18 史忠植 约束推理 25
4-Queens Tree
回溯算法Backtrack Search
empty assignment
1st variable
2nd variable
3rd variable
Assignment = {}
回溯算法Backtrack Search
empty assignment
1st variable
2nd variable
3rd variable
Assignment = {(var1=v11)}
回溯算法Backtrack Search
empty assignment
1st variable
2nd variable
3rd variable
Assignment = {(var1=v11),(var2=v21)}
回溯算法Backtrack Search
empty assignment
1st variable
2nd variable
3rd variable
Assignment = {(var1=v11),(var2=v21),(var3=v31)}
回溯算法Backtrack Search
empty assignment
1st variable
2nd variable
3rd variable
Assignment = {(var1=v11),(var2=v21),(var3=v32)}
回溯算法Backtrack Search
empty assignment
1st variable
2nd variable
3rd variable
Assignment = {(var1=v11),(var2=v22)}
回溯算法Backtrack Search
empty assignment
1st variable
2nd variable
3rd variable
Assignment = {(var1=v11),(var2=v22),(var3=v31)}
2015/3/18 史忠植 约束推理 33
回溯算法
Backtracking-Top(P)
1 f := the null assignment
2 return Backtracking(f,P)
2015/3/18 史忠植 约束推理 34
回溯算法
Backtracking(f,P)
1 if f is a total assignment of the variables in P
2 answer := f
3 else
4 v := some variable in P that is not yet assigned a value
by f
5 answer := Unsat
6 for each value while answer = Umsat
7 f(v) := x
8 if f satisfies the constraints in P
9 answer := Backtracking(f,P)
10 return answer
2015/3/18 史忠植 约束推理 35
Backtracking Algorithm
Based on depth-first recursive search
Approach
1. Tests whether solution has been found
2. If found solution, return it
3. Else for each choice that can be made
a) Make that choice
b) Recur
c) If recursion returns a solution, return it
4. If no choices remain, return failure
Some times called “search tree”
2015/3/18 史忠植 约束推理 36
回溯算法
尽管回溯法好于生成测试法,但对于非平凡问题仍
然是低效的。其原因在于搜索空间中不同路径的搜索重
复相同的失败子路径。一些研究者认为,造成这种反复
的原因是所谓的局部不一致性。最简单的情形是所谓的
结点不一致性。对一个变量vi的一个一元约束。存在域
中一个值vi不满足该约束。这样,每当vi取到 a 时就
会出现
不一致性。
另一种重复的情形 是所谓的弧不一致性。
2015/3/18 史忠植 约束推理 37
约束传播 CONSTRAINT PROPAGATION
(red, green)
(red,blue)(red, green,
blue)
弧一致性 Arc consistency
2015/3/18 史忠植 约束推理 38
弧一致性
Arc consistency
如果对vi 的当前域中的所有值x,存在vj的当
前域中的某值y使得 vi=x和vj=y是vi与vj之间
的约束所允许的,则弧(vi, vj)是弧一致的。
弧一致性的概念是有向的。即(vi, vj)
是弧一致的并不自动地意味着(vj, vi)是一致
的。
2015/3/18 史忠植 约束推理 39
CONSTRAINT PROPAGATION
All of the Mackworth algorithms make use of a Revise procedure. Let Dv be the current domain of v, Let Dw be the current domain of w, Let P be the constraint predicate that holds between v and w, then Revise updates
Dv as follows: yxPDDxD wyvv ,such that ::
2015/3/18 史忠植 约束推理 40
CONSTRAINT PROPAGATION
Mackworth 1977 AC-1
AC-2 AC-3
2015/3/18 史忠植 约束推理 41
约束传播修改算法 REVISE(Vi,Vj)
1 DELETE false;
2 for each x Di do
3 if there is no such yj Dj
4 such that(x,yj) is consistent,
5 then
6 delete x from Di;
7 DELETE true;
8 endif
9 endfor
10 return DELETE;
11 end REVISE
2015/3/18 史忠植 约束推理 42
弧一致性算法AC-1
1 Q ;
2 repeat
3 CHANGE false;
4 for each (Vi, Vj) Q do
5 CHANGE REVISE(Vi, Vj) CHANGE;
6 endfor;
7 until not(CHANGE);
8 end AC-1
V V G i ji j, , arcs
2015/3/18 史忠植 约束推理 43
弧一致性算法AC-3
1 Q ;
2 While Q not empty
3 Select and delete any arc(Vk,Vm) from Q;
4 If (REVISE(Vk,Vm))
5 Then Q {(Vi,Vk) such that (Vi,Vk)arcs(G),
6 ik, im};
6 endfor;
7 endwhile;
8 end AC-3
V V G i ji j, , arcs
2015/3/18 史忠植 约束推理 46
Backjumping
Backjumping-Top(P)
1 f := the null assignment
2 <answer, conflict-set> := Backjumping(f,P)
3 return answer
2015/3/18 史忠植 约束推理 47
Backjumping
Backjumping(f,P)
1 if f is a total assignment of the variables in P
2 answer := <f,>
3 else
4 v := some variable in P that is not yet assigned a value
by f
5 answer := Unsat
6 conflict-set :=
7 for each value
8 f(v) := x
9 if f satisfies the constraints in P
10 <answer, new-conflicts> := Backjumping(f,P)
2015/3/18 史忠植 约束推理 48
Backjumping
11 else
12 new-conflicts := the set of variables in a
violated constraint
13 if answer Unsat
14 return <answer, >
15 else if v new-conflicts
16 return <Unsat, new-conflicts>
17 else
18 conflict-set := conflict-set (new-conflicts
{v})
19 return <Unsat, conflict-set>
2015/3/18 史忠植 约束推理 49
COPS
Constraint : predicate expression
P(t1, ..., tn)
where
P is built in function, such as
sum
times
eq(equal)
neq(not equal)
ge(great than or equal to)
gt(great than)
also can be defined by users
2015/3/18 史忠植 约束推理 50
COPS
Conditional constraint
condition1: constraint1;
.
.
conditionn: constraintn
where
condition1, ..., conditionn are boolean expressions.
constraint1,... constraintn are constraints or contraints table.
2015/3/18 史忠植 约束推理 51
COPS
RULE
Rule is used to define new function, method, predicate, or add
new constraint into object.
RULE [class::] predicate(varibles) (boolean expression)
{
constraint_1;
constraint_n;
CASE
boolean expression_1: constraint_1;
boolean expression_m: constraint_m;
}
2015/3/18 史忠植 约束推理 52
COPS
For example:
RULE multiple(INTEGER: *x, INTEGER: y, INTEGER: z)
(neq(y, 0))
{
equal(x, divide(z, y));
}
z = x * y
2015/3/18 史忠植 约束推理 53
COPS
CLASS [class_name][:superclass_name]
{
// attributes definition
date type: attribute_name;
...
// rule definition
rule_name;
...
//function definition
function_name;
...
// method definition
method_name;
...
}
2015/3/18 史忠植 约束推理 54
COPS
Implementation
Program written by COPS consists of classes and rules. COPS
constraint programming language is a declarative language, providing
classes, methods which are exist in object oriented language. It is
similar with C++ . COPS has the features:
constraint
object oriented
logic programming
production system
2015/3/18 史忠植 约束推理 55
COPS
COPS_Compiler
1 {
2 Call yacc to parse the program and
3 to generate internal structures.
4 Initializatiion
5 Create Cops Constant trueNode;
6 Allocate memories for global variables.
2015/3/18 史忠植 约束推理 56
COPS
7 Interprte the program with the internal structures.
8 Constraint networks are built up for Unsolved
9 constraints and variables.
10 while some constraints in the constraint networks are
triggered,
11 inteprete the triggered constraints.
12 }
2015/3/18 史忠植 约束推理 57
COPS
Interpreter:
1 {
2 switch (constraint type)
3 case Constant:
4 return Constant:
5 case global variable:
6 interprete global variable:
7 case local variable or argument:
8 interprete local variable or argument:
9 case object-attribute pair;
10 interprete object-attribute pair:
11 case function call:
2015/3/18 史忠植 约束推理 58
COPS
12 interprete function call:
13 case method call:
14 interprete method call:
15 case CASE expression:
16 interprete CASE expression:
17 ...
18 default:
20 report error
21 }
2015/3/18 史忠植 约束推理 59
ILOG SOLVER
Combines object oriented programming with constraint logic
programming, containing logic variables, incremental constraint
satisfaction and backtracking.
variables : C++ object
integer variable CtIntVar
floating variable CtFloatVar
boolean variable CtBoolVar
Memory Management
new:
delete:
2015/3/18 史忠植 约束推理 60
ILOG SOLVER
Constraints
CtTell(x == (y + z));
Basic constraints: =, , , <, >, +, -, *, /, subset,
superset, union, intersection, member, boolean or, boolean and,
boolean not, boolean xor,
CtTell((x==0) || (y==0));
CtIfThen (x < 100, x = x+1);
Search
2015/3/18 史忠植 约束推理 61
ILOG SOLVER
CTGOALn: how to execute
CTGOAL1(CtInstantiate, CtIntVar* x){
CtInt a = x->chooseValue();
CtOr(Constraint(x == a),
CtAnd(Constraint(x != a),
CtInstantiate(x)));
}
2015/3/18 史忠植 约束推理 62
ILOG Schedule 1.0
Schedule
CtSchedule class
Global object: time original ---tineMin
time horizon ---timeMax
2015/3/18 史忠植 约束推理 63
ILOG Schedule 1.0
Resources
CtResource
CtDiscreteResource
CtUnaryResource
CtDiscreteEnergy
CtStateResource
2015/3/18 史忠植 约束推理 64
ILOG Schedule 1.0
Activities
CtActivity class
CtIntervalActivity
An activity is defined by its start time, end time and duration
Activities require, provide, consume and produce resources
2015/3/18 史忠植 约束推理 65
Scheduling Problem
Prices paid as tasks begin $1000 per day
Availability: Day 0:$20000, Day 15: +$9000
2015/3/18 史忠植 约束推理 66
Constraints
// To create a schedule with origin 0 and given horizon.
CtSchedule* schedule =
new CtSchedule(0, horizon);
// To create an activity with the given duration.
CtIntervalActivity* act =
new CtIntervalActivity(schedule, duration);
//To post a precedence constraint between act1 and act2.
act2->startsAfterEnd(act1,0);
2015/3/18 史忠植 约束推理 67
Constraints
//To create a total budget of limited capacity (here 29000).
CtDiscreteResource* res =
new CtDiscreteResource(schedule,
CtRequiredResource,
capacity);
// To state that only cap (here 20000) is available prior to a
// given date (here 15).
res->setCapacityMax(0,date,cap);
// To state that an activity act consumes c units of res.
act->consumes(res, c);
2015/3/18 史忠植 约束推理 68
Algorithm Program
CtBoolean IsUnScheduled(CtActivity* act){
// Return true if act does not have a fixed start time.
if (act->getStartVariable()->isBound())
return CtFalse;
else
return CtTrue;
}
2015/3/18 史忠植 约束推理 69
Algorithm Program
CtBoolean IsMoreUrgent(CtActivity* act1,
CtActivity* act2){
// Returns true if act1 is more urgent than act2.
// Returns true if act2 is unbound (==0)
if (act2 == 0)
return CtTrue;
else if (act1->getStartMax() < act2->getStartMax())
return CtTrue;
else
return CtFalse;
}
2015/3/18 史忠植 约束推理 70
Algorithm Program
CtActivity* SelectActivity(CtSchedule* schedule){
// Returns the unscheduled activity with the smallest latest
// statrt time. Returns 0 if all activities are scheduled.
CtActivity* bestActivity = 0;
//Creates an iterator to iterate on all activities.
CtActivityIterator* iterator(schedule);
CtActivity* newActivity;
while(iterator.next(newactivity))
if((IsUnScheduled(newActivity))
&& (IsMoreUgent(newActivity, bestActivity)))
bestactivity = newActivity;
return bestActivity;
}
2015/3/18 史忠植 约束推理 71
Algorithm Program
void SolveProblem(CtSchedule* schedule){
// Solve the problem assuming constraints have been posted.
CtActivity* act = SelectActivity(schedule);
while (act !=0) {
act->setStartTime(act->getStartMin());
act = SelectActivity(schedule);
}
}
2015/3/18 史忠植 约束推理 72
Optimal Solution to the
Scheduling Problem
约束逻辑程序设计 CLP
约束逻辑程序设计( Constraint Logic Programming
)是基于人工智能中约束满足问题模型的一种程序设计
风范,是逻辑程序设计的一种推广,是八十年代发展起
来的一种新的逻辑程序设计方法。由于它继承了简单易
懂的说明性描述方法并结合了在求解间题时的效率, 使它
在解决很多AI问题〔如组合问题、资源分配、事务安排
等时有不凡的表现,更由于领域中绝大多数间题可以用
来表示所以这一方法,引起了人们的广泛注意,并在八
十年代后期得以迅速发展。 2015/3/18 73 史忠植 约束推理
约束逻辑程序设计 CLP
“Generate-and-test” approach is a common
methodology for logic programming.
Generate possible solutions
Test and eliminate non-solutions
Disadvantages of “generate-and-test” approach:
Passive use of constraints to test potential values
Inefficient for combinatorial search problems
CLP languages use the global search paradigm.
Actively pruning the search space
Recursively dividing a problem into subproblems until
its subproblems are simple enough to be solved
2015/3/18 74 史忠植 约束推理
约束逻辑程序设计典型步骤
Step 1. Declare the domains of the
variables
Step 2. Post the problem constraints
Step 3. Look for a feasible solution via
backtrack search, or look for an optimal
solution via branch-and-bound search
2015/3/18 75 史忠植 约束推理
史忠植 约束推理 76
Example: Map-Coloring
Variables WA, NT, Q, NSW, V, SA, T
Domains Di = {red,green,blue}
Constraints: adjacent regions must have different colors
e.g., WA ≠ NT, or (WA,NT) in {(red,green),(red,blue),(green,red),
(green,blue),(blue,red),(blue,green)} 2015/3/18
史忠植 约束推理 77
Example: Map-Coloring
2015/3/18
史忠植 约束推理 78
Example: Map-Coloring
Solutions are complete and consistent assignments
e.g., WA = red, NT = green, Q = red, NSW = green, V = red, SA = blue, T = green
2015/3/18
2015/3/18 史忠植 高级人工智能 79
谢 谢