[关闭]
@Heath 2015-11-01T16:43:18.000000Z 字数 5231 阅读 1048

Chapter3

13331130 李有才
人工智能


3.19

可以使用A*算法

  1. function Find-Route(Page page1, Page page2) return a path
  2. page1 <-- 初始页面
  3. page2 <-- 目标页面
  4. //按照评估函数f(n)排序的优先权队列
  5. pri-queue <-- sorted by WebPage.RelevancyToEndPage
  6. //记录访问过的节点
  7. visited
  8. pri-queue.INSERT(page1)
  9. loop do
  10. if EMPTY(pri-queue)
  11. then
  12. return failure
  13. //选择与目标网页相关度最高的节点
  14. currentPage <-- pri-queue.POP
  15. add currentPage to visited
  16. if currentPage is page2
  17. then
  18. return path
  19. else
  20. for each item in AdjacentPages(currentPage) do
  21. if item is not in visited
  22. then
  23. currentPage.NEXT <-- item
  24. relevance CaltulateRelevancyToEndPage(item)
  25. pri-queue.INSERT-WITH-PRIORITY(item, relevance)

搜索引擎保存了页面之间的相互关系,可以实现一个前任函数,同样也适用双向搜索

3.23

Format:f:g+h
1. L:0+244=244
2. 扩展L:
M:70+241=311, T:111+329=440
3. 扩展M:
L:140+244=384, D:145+242=387, T:111+329=440
4. 扩展L:
D:145+242=387, T:111+329=440, M:210+241=451, T1:251+329=580
5. 扩展D:
C:265+160=425, T:111+329=440, M:210+241=451, M:220+241=461, T1:251+329=580
6. 扩展C
T:111+329=440, M:210+241=451, M1:220+241=461, P:403+100=503, T1:251+329=580, R:411+193=604, D:385+242=627
7. 扩展T:
M:210+241=451, M1:220+241=461, L:222+244=466, P:403+100=503, T1:251+329=580, A:229+366=595, R:411+193=604, D:385+242=627
8. 扩展M:
M1:220+241=461, L:222+244=466, P:403+100=503, L1:280+244=524, D:285+242=527, T1:251+329=580, A:229+366=595, R:411+193=604, D1:385+242=627
9. 扩展M1
L:222+244=466, P:403+100=503, L1:280+244=524, D:285+242=527, L2:290+244=534, D2220+75+242=537, T1:251+329=580, A:229+366=595, R:411+193=604, D1:385+242=627
10. 扩展L:
P:403+100=503, L1:280+244=524, D:285+242=527, M222+70+241=533, L2:290+244=534, D2220+75+242=537, T1:251+329=580, A:229+366=595, R:411+193=604, D1:385+242=627, T222+111+329=662
11. 扩展P:
B:504+0=504, L1:280+244=524, D:285+242=527, M222+70+241=533, L2:290+244=534, D2220+75+242=537, T1:251+329=580, A:229+366=595, R:411+193=604, D1:385+242=627, T222+111+329=662, R:500+193=693,C: 541 + 160 = 701$
12. B在队首,找到路径

3.27

a)状态空间(nn)(n(n1))....n
b)5n
c)曼哈顿距离
d)选择min(h1,...,h2)

3.30

a)旅行商问题要求从起点开始每个城市访问一次,最终回到起点。最小生成树连接所有点,但不能形成环,所以可以看作从旅行商问题减少了“形成环”这一限制(即通过松弛的TSP问题)得到的。
b)MST启发式考虑了城市之间的连接路径,直线距离在城市数量增多时准确性下降很快

c)

  1. function GenerateTSP(CityNumber N) return a instance
  2. Instance <-- the final graph to return
  3. Square <-- domain
  4. for i = 1 to N
  5. position <-- getRandomPositionFromSquare(Square)
  6. Instance.city[N].setPosition(position)

d)

  1. function TSPSolution(Instance instance) return a path
  2. roads[N][N] <-- roads between sities
  3. startCity <-- instacne.StartCity
  4. pri-queue <-- 按以未到达城市为根节点生成的MST的边长之和排序
  5. visited
  6. pri-queue.INSERT(startCity)
  7. loop do
  8. if pri-queue.EMPTY
  9. then
  10. return failure
  11. currentCity <-- pri-queue.POP
  12. visited[currentCity] <-- true
  13. for i in roads[N]
  14. if i is startCity and AllCityVisited
  15. then
  16. return path
  17. if i is not currentCity and cisited[i] is fasle
  18. then
  19. visited[i] = true
  20. weight <-- GetMSTValue(roads, visited)
  21. pri-queue.INSETR-WITH_PRIORITY(currnetCity, weight)
  22. explored[i] <-- false
  1. // 使用Kruskal算法求解最小生成树
  2. function Get-MST-Value(roads, visited) returns a value
  3. cities Get-Unexplored-Cities(visited)
  4. edge Get-Roads-Between-Unexplored-Cities(roads)
  5. value zero
  6. viewed empty set
  7. loop do
  8. if All-Viewed()
  9. then
  10. return value
  11. edgeNow POP-Min-Weight-Edge(edge)
  12. if edgeNow.start and edgeNow.end are all in viewed
  13. then
  14. continue
  15. value.ADD(edgeNow.weight)
  16. viewed.INSERT(edgeNow.start)
  17. viewed.INSERT(edgeNow.end)

3.32

  1. 曼哈顿距离

    1. function Get-Manhattan-Distance() return a value
    2. value <-- zero
    3. currentNodeState <-- the current state of the eight num
    4. targetNodeState <-- the target state of the eight num
    5. for node in currentNodeState do
    6. nowPosition <-- node.getPosition()
    7. num <-- nowPosition.getNum()
    8. targetPosition <-- targetNodeState.getNodeAt(num).getPosition()
    9. colDis <-- abs(nowPosition.col-targetPosition.col)
    10. rowDis <-- abs(nowPosition.row-targetPosition.row)
    11. value.ADD((int)sqrt(pow(colDis, 2)+pow(rowDis, 2)))
    12. return value
  2. 错位棋子数

    1. function Get-Wrong-Node-Count() returns a value
    2. value <-- zero
    3. currentNodeState <-- the current state of the eight num
    4. targetNodeState <-- the target state of the eight num
    5. for node in currentNodeState do
    6. nowPosition <-- node.getPosition()
    7. num nowPosition.getNum()
    8. targetPosition <-- targetNodeState.getNodeAt(num).getPosition()
    9. if nowPosition is not targetPosition then
    10. value.ADD(1)
    11. return value
  3. 后续节点不正确的个数

    1. function Get-Wrong-Node-Count() returns a value
    2. value <-- zero
    3. currentNodeState <-- the current state of the eight num
    4. for node in currentNodeState do
    5. nowPosition <-- node.getPosition()
    6. nowNextPosition <-- nowPosition.getNext()
    7. nextNodePosition <-- node.next().getPosition()
    8. if nowNextPosition is not nextNodePosition then
    9. value.ADD(1)
    10. return value
  4. 性能分析:
所使用启发式 求解过程中平均创建节点数(随机50次)
曼哈顿距离 227
错位棋子数 614
后续节点不正确的数码个数 1983
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注