Uploaded by marchuk.liliya

Лабораторная №4

advertisement
МИНОБРНАУКИ РОССИИ
САНКТ-ПЕТЕРБУРГСКИЙ ГОСУДАРСТВЕННЫЙ
ЭЛЕКТРОТЕХНИЧЕСКИЙ УНИВЕРСИТЕТ
«ЛЭТИ» ИМ. В.И. УЛЬЯНОВА (ЛЕНИНА)
Кафедра ВТ
ОТЧЕТ
по лабораторной работе №4
по дисциплине «Искусственный интеллект»
Тема: Методы неинформированного (слепого) поиска
(реализация на CLIPS)
Стратегия поиска в глубину и двунаправленный поиск
Вариант 4
Козлов Д.В.
Марисева Л.А.
Студенты гр. 5307
Марчук Л.Б.
Преподаватель
Родионов С.В.
Санкт-Петербург
2018
Цель работы.
Самостоятельное практическое закрепление понимания общих идей
поиска в пространстве состояний и стратегий слепого поиска, реализация
заданных стратегий поиска на языке CLIPS.
Постановка задачи.
Рассматривается задача «Головоломка 8-ка».
Задана доска с 8 пронумерованными фишками и с одним пустым
участком.
Фишка, смежная с пустым участком, может быть передвинута на этот
участок. Требуется достичь указанного целевого состояния.
Начальное состояние:
6
5
4
2
3
8
1
7
Конечное состояние:
1
8
7
2
6
3
4
5
Состояния. Описание состояния определяет местонахождение каждой из
этих 8 фишек и пустого участка на одном из 9 квадратов.
Начальное состояние задано.
Функция
последователей.
Эта
функция
формирует
допустимые
состояния, которые являются результатом попыток осуществления указанных
четырех действий теоретически возможных ходов (Left, Right, Up или Down).
В программе должна быть реализована функция определения повторного
состояния.
Функция достижения целевого состояния. Она позволяет определить,
соответствует ли данное состояние целевой конфигурации, которая задана.
2
Программа должна поддерживать пошаговый режим (очередной шаг
выполняться по нажатию клавиши) и выводить на каждом шаге:
- вновь добавленные после раскрытия вершины,
- выявленные повторные вершины,
- текущее состояние «каймы» (вершин, ожидающих раскрытия );
- текущую вершину, выбираемую для раскрытия на данном шаге;
- факт достижения целевого состояния.
Описание выбранных структур данных (классов), представление
функции определения последователей.
Стратегия поиска в глубину.
Для представления вершины в системе CLIPS был создан шаблон, в
котором игровое поле представлено в виде набора полей, хранящих номер
размещённой в ячейке фишки, глубины, на которой построена вершина,
уникального идентификатора вершины, статуса вершины (не раскрыта,
раскрыта, решение) и идентификатора предка вершины (Листинг 1).
Листинг 1:
(deftemplate vertex "Структура вершины"
(slot 00-field (type NUMBER))
(slot 01-field (type NUMBER))
(slot 02-field (type NUMBER))
(slot 10-field (type NUMBER))
(slot 11-field (type NUMBER))
(slot 12-field (type NUMBER))
(slot 20-field (type NUMBER))
(slot 21-field (type NUMBER))
(slot 22-field (type NUMBER))
(slot depth (type NUMBER))
(slot vertexID (type NUMBER) (default 0))
(slot status (type NUMBER) (default 0));; 0 - не раскрыта, 1 - раскрыта, 2 - решение
(slot from (type NUMBER)))
3
Для реализации функции определения последователей были созданы
правила перехода между состояниями. В правилах перехода если для данной
существующей
вершины
не
существует
потомка
для
определённого
перемещения фишки на доске, то данный потомок добавляется в систему,
причём глубина построения потомка равна глубине построения вершины плюс
один (Листинг 2).
Листинг 2:
;Пустое место в левом верхнем углу
;Перемещение пустого места вправо
(defrule getNewVertexFrom-00-field-rightChild
(declare (salience 100))
?f<-(vertex (status 0) (depth ?L) (vertexID ?ID)
(00-field 0) (01-field ?MT) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
;;Отбрасываем возможное повторное состояние
(not (exists (vertex (00-field ?MT) (01-field 0) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field ?RB))))
=>
(assert (vertex (00-field ?MT) (01-field 0) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(depth(+ ?L 1)) (from ?ID) (status 0) (vertexID (getID))))
(if (eq ?*RUNBYUSER* "y") then
(printout t crlf "Opened vertex:" crlf)
(printout t 0 ?MT ?RT crlf)
(printout t ?LM ?MM ?RM crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?MT 0 ?RT crlf)
(printout t ?LM ?MM ?RM crlf)
4
(printout t ?LB ?MB ?RB crlf)
(printout t crlf "Press any key to continue." crlf)
(read)
)
(incSTEPS)
)
Стратегия двунаправленного поиска.
Шаблон вершины для данной стратегии поиска представляет собой
шаблон вершины для стратегии поиска в глубину, дополненный полями tree
(старт-дерево, цель-дерево) и isSolution (листинг 3).
Листинг 3:
(deftemplate vertex "Структура вершины"
(slot 00-field (type NUMBER))
(slot 01-field (type NUMBER))
(slot 02-field (type NUMBER))
(slot 10-field (type NUMBER))
(slot 11-field (type NUMBER))
(slot 12-field (type NUMBER))
(slot 20-field (type NUMBER))
(slot 21-field (type NUMBER))
(slot 22-field (type NUMBER))
(slot depth (type NUMBER))
(slot vertexID (type NUMBER) (default 0))
(slot status (type NUMBER) (default 0));; 0 - не раскрыта, 1 - раскрыта, 2 - решение
(slot from (type NUMBER))
(slot tree (type NUMBER));; 0 - старт-дерево, 1 - цель-дерево
(slot isSolution (type NUMBER)); 0 - данная вершина - не решение, 2 - данная вершина решение
)
Правила перехода между состояниями отличаются от правил перехода
между состояниями, реализованных для стратегии поиска в глубину, наличием
5
элементарного условия (not (exists (solution))) и использованием полей tree и
isSolution (листинг 4).
Листинг 4:
;Перемещение пустого места вправо
(defrule getNewVertexFrom-00-field-rightChild
(declare (salience 100))
?f<-(vertex (status 0) (depth ?L) (vertexID ?ID) (tree ?tr) (isSolution 0)
(00-field 0) (01-field ?MT) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
;;Отбрасываем возможное повторное состояние
(not (exists (vertex (00-field ?MT) (01-field 0) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field ?RB) (tree ?tr)
(isSolution 0))))
(not (exists (solution)))
=>
(assert (vertex (00-field ?MT) (01-field 0) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(depth(+ ?L 1)) (from ?ID) (status 0) (vertexID (getID)) (tree
?tr) (isSolution 0)))
(if (eq ?*RUNBYUSER* "y") then
(printout t crlf "Opened vertex:" crlf)
(printout t 0 ?MT ?RT crlf)
(printout t ?LM ?MM ?RM crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?MT 0 ?RT crlf)
(printout t ?LM ?MM ?RM crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t crlf "Press any key to continue." crlf)
(read)
6
)
(incSTEPS)
)
Описание алгоритма.
Стратегия поиска в глубину.
Порождение новых состояний реализовано с помощью правил перехода
между состояниями. Если для данной вершины существуют все потомки, то она
помечается как раскрытая (листинг 5).
Листинг 5:
(defrule vertexFrom-00-field-Opened
(declare (salience 200))
?f<-(vertex (status 0) (depth ?L) (vertexID ?ID)
(00-field 0) (01-field ?MT) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(exists (vertex (00-field ?MT) (01-field 0) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field ?RB)))
(exists (vertex (00-field ?LM) (01-field ?MT) (02-field ?RT)
(10-field 0) (11-field ?MM) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field ?RB)))
=>
(modify ?f(status 1)); Порождены все потомки, вершина раскрыта
(incSTEPS)
)
Таким образом, исключается возможность порождения повторных
вершин.
Так как повторные вершины не могут быть порождены, для проверки
нахождения целевого состояния созданы 4 правила для проверки, найдены ли
7
состояния доски, которые можно получить одним перемещением фишки из
целевого состояния (листинг 6).
Листинг 6:
(defrule testGoal1
(declare (salience 500))
?f<-(vertex (00-field 1) (01-field 0) (02-field 3)
(10-field 8) (11-field 2) (12-field 4)
(20-field 7) (21-field 6) (22-field 5)
(from ?ID) (depth ?L) (status ~2))
=>
(modify ?f(status 2))
(assert (solution1Depth))
(printout t "Solution has been found after " ?L " steps." crlf)
(incSTEPS)
)
(defrule testGoal2
(declare (salience 500))
?f<-(vertex (00-field 1) (01-field 2) (02-field 3)
(10-field 0) (11-field 8) (12-field 4)
(20-field 7) (21-field 6) (22-field 5)
(from ?ID) (depth ?L) (status ~2))
(exists (solution1Depth))
=>
(modify ?f(status 2))
(assert (solution2Depth))
(printout t "Solution has been found after " ?L " steps." crlf)
(incSTEPS)
)
(defrule testGoal3
(declare (salience 500))
?f<-(vertex (00-field 1) (01-field 2) (02-field 3)
(10-field 8) (11-field 4) (12-field 0)
(20-field 7) (21-field 6) (22-field 5)
(from ?ID) (depth ?L) (status ~2))
8
(exists (solution1Depth))
(exists (solution2Depth))
=>
(modify ?f(status 2))
(assert (solution3Depth))
(printout t "Solution has been found after " ?L " steps." crlf)
(incSTEPS)
)
(defrule testGoal4
(declare (salience 500))
?f<-(vertex (00-field 1) (01-field 2) (02-field 3)
(10-field 8) (11-field 6) (12-field 4)
(20-field 7) (21-field 0) (22-field 5)
(from ?ID) (depth ?L) (status ~2))
(exists (solution1Depth))
(exists (solution2Depth))
(exists (solution3Depth))
=>
(modify ?f(status 2))
(printout t "Solution has been found after " ?L " steps." crlf)
(incSTEPS)
)
Если все вершины были раскрыты, а решения не было найдено,
выводится соответствующее сообщение (листинг 7).
Листинг 7:
(defrule noSolutions
(declare (salience 200))
(not (vertex(status 0|2)))
=>
(halt)
(printout t "No solutions." crlf)
(incSTEPS)
)
9
Стратегия двунаправленного поиска.
Порождение новых состояний так же реализовано с помощью правил
перехода между состояниями. Если для данной вершины существуют все
потомки, то она помечается как раскрытая (листинг 5).
В правиле проверки нахождения решения создаются две переменные,
хранящие адреса фактов проверяемых вершин, принадлежащих разным
деревьям, и производится сравнение размещения фишек на доске в
проверяемых вершинах. Если они совпадают, добавляется факт нахождения
решения и поиск заканчивается (листинг 8).
Листинг 8:
(defrule testGoal
(declare (salience 500))
?f<-(vertex (tree 0) (isSolution ?fs) (depth ?fL)
(00-field ?fLT) (01-field ?fMT) (02-field ?fRT)
(10-field ?fLM) (11-field ?fMM) (12-field ?fRM)
(20-field ?fLB) (21-field ?fMB) (22-field ?fRB))
?g<-(vertex (tree 1) (isSolution ?gs) (depth ?gL)
(00-field ?gLT) (01-field ?gMT) (02-field ?gRT)
(10-field ?gLM) (11-field ?gMM) (12-field ?gRM)
(20-field ?gLB) (21-field ?gMB) (22-field ?gRB))
(not (exists (solution)))
=>
(if (and (and (and (and (and (and (and (and (eq ?fLT ?gLT) (eq ?fMT ?gMT)) (eq ?fRT
?gRT)) (eq ?fLM ?gLM)) (eq ?fMM ?gMM)) (eq ?fRM ?gRM)) (eq ?fLB ?gLB)) (eq ?fMB ?gMB)) (eq
?fRB ?gRB)) then
(bind ?fs 1)
(bind ?gs 1)
(printout t "Solution has been found after " ?fL " steps of start tree and " ?gL "
vertexes of goal-tree." crlf)
(assert (solution))
)
)
10
Исходный код.
Стратегия поиска в глубину.
;;Определение структуры вершины
(deffunction incSTEPS()
;Настройки проекта!!!
(bind ?*STEPS* (+ ?*STEPS* 1))
(deftemplate vertex "Структура вершины"
)
(slot 00-field (type NUMBER))
(slot 01-field (type NUMBER))
;;Объявление факта начальной вершины
(slot 02-field (type NUMBER))
(deffacts start-goal_vertexes
(slot 10-field (type NUMBER))
(vertex (00-field 6) (01-field 0) (02-field 8)
(slot 11-field (type NUMBER))
(10-field 5) (11-field 2) (12-field 1)
(slot 12-field (type NUMBER))
(20-field 4) (21-field 3) (22-field 7)
(slot 20-field (type NUMBER))
(depth 0) (from 0) (status 0) (vertexID
(slot 21-field (type NUMBER))
(getID))))
(slot 22-field (type NUMBER))
(slot depth (type NUMBER))
(defrule askUser
(slot vertexID (type NUMBER) (default 0))
(declare (salience 1000))
(slot status (type NUMBER) (default 0));; 0 -
(exists (initial-fact))
не раскрыта, 1 - раскрыта, 2 - решение
(not (exists (userAsked)))
(slot from (type NUMBER)));;
=>
(printout t "Do you want to enable step-by-
переменная
;;Глобальная
для
подсчёта
step mode (y/n)?" crlf)
общего количества вершин
(bind ?answer(read))
(defglobal
;Считываем только "n" и "y"
?*ID* = 0)
(while (and (neq ?answer n) (neq ?answer y))
;Глобальная
переменная
для
подсчёта
do
общего количества шагов
(printout t crlf "Please, enter correct answer."
(defglobal
crlf)
?*STEPS* = 0)
(printout t "Do you want to enable step-bystep mode (y/n)?" crlf)
;Включён ли пользовательский режим
(bind ?answer(read))
(defglobal
)
?*RUNBYUSER* = "n")
(if (eq ?answer y) then
;;Функция
получения
ID
следующей
(bind ?*RUNBYUSER* "y")
вершины
)
(deffunction getID()
(bind ?*ID* (+ ?*ID* 1)) ;;Инкремент ID
(assert (userAsked))
?*ID*)
)
;Функция инкремента количества шагов
;Проверки достижения целевого состояния
11
(defrule testGoal1
(exists (solution2Depth))
(declare (salience 500))
=>
?f<-(vertex (00-field 1) (01-field 0) (02-field
(modify ?f(status 2))
3)
(assert (solution3Depth))
(10-field 8) (11-field 2) (12-field 4)
(printout t "Solution has been found after " ?L
(20-field 7) (21-field 6) (22-field 5)
" steps." crlf)
(from ?ID) (depth ?L) (status ~2))
(incSTEPS)
=>
)
(modify ?f(status 2))
(assert (solution1Depth))
(defrule testGoal4
(printout t "Solution has been found after " ?L
(declare (salience 500))
" steps." crlf)
?f<-(vertex (00-field 1) (01-field 2) (02-field
(incSTEPS)
3)
)
(10-field 8) (11-field 6) (12-field 4)
(20-field 7) (21-field 0) (22-field 5)
(from ?ID) (depth ?L) (status ~2))
(defrule testGoal2
(declare (salience 500))
(exists (solution1Depth))
?f<-(vertex (00-field 1) (01-field 2) (02-field
(exists (solution2Depth))
3)
(exists (solution3Depth))
(10-field 0) (11-field 8) (12-field 4)
=>
(20-field 7) (21-field 6) (22-field 5)
(modify ?f(status 2))
(from ?ID) (depth ?L) (status ~2))
(printout t "Solution has been found after " ?L
" steps." crlf)
(exists (solution1Depth))
(incSTEPS)
=>
)
(modify ?f(status 2))
;Останов программы только тогда, когда
(assert (solution2Depth))
обошли все вершины
(printout t "Solution has been found after " ?L
" steps." crlf)
(defrule noSolutions
(incSTEPS)
(declare (salience 200))
)
(not (vertex(status 0|2)))
=>
(defrule testGoal3
(halt)
(declare (salience 500))
(printout t "No solutions." crlf)
?f<-(vertex (00-field 1) (01-field 2) (02-field
(incSTEPS)
3)
)
(10-field 8) (11-field 4) (12-field 0)
;Вывод
(20-field 7) (21-field 6) (22-field 5)
ёмкостных
характеристик
(from ?ID) (depth ?L) (status ~2))
(defrule total
(exists (solution1Depth))
(declare (salience 200))
12
и
временных
(not (vertex(status 0)))
(not (exists (vertex (00-field ?MT) (01-field 0)
=>
(02-field ?RT)
(printout t crlf "Number of vertexes used: "
(10-field ?LM) (11-field ?MM) (12-field
?*ID* "." crlf)
?RM)
(printout t "Number
of
steps used: "
(20-field ?LB) (21-field ?MB) (22-field
?*STEPS* "." crlf)
?RB))))
)
=>
(assert (vertex (00-field ?MT) (01-field 0) (02-
;Вывод "каймы"
field ?RT)
(defrule remainingVertexes
(10-field ?LM) (11-field ?MM) (12-field
(declare (salience 500))
?RM)
?f<-(vertex (status 0) (depth ?L) (vertexID
(20-field ?LB) (21-field ?MB) (22-field ?RB)
?ID)
(depth(+ ?L 1)) (from ?ID) (status 0)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(vertexID (getID))))
(10-field ?LM) (11-field ?MM) (12-field
?RM)
(if (eq ?*RUNBYUSER* "y") then
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(printout t crlf "Opened vertex:" crlf)
=>
(printout t 0 ?MT ?RT crlf)
(if (eq ?*RUNBYUSER* "y") then
(printout t ?LM ?MM ?RM crlf)
(printout t crlf "Remaining vertex:" crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?LM ?MM ?RM crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t ?MT 0 ?RT crlf)
)
(printout t ?LM ?MM ?RM crlf)
)
(printout t ?LB ?MB ?RB crlf)
;Правила перехода между состояниями
(printout t crlf "Press any key to continue."
;Пустое место в левом верхнем углу
crlf)
;Перемещение пустого места вправо
(read)
(defrule
)
getNewVertexFrom-00-field-
rightChild
(incSTEPS)
(declare (salience 100))
)
?f<-(vertex (status 0) (depth ?L) (vertexID
;Перемещение пустого места вниз
?ID)
(00-field 0) (01-field ?MT) (02-field ?RT)
(defrule
(10-field ?LM) (11-field ?MM) (12-field
getNewVertexFrom-00-field-
bottomChild
?RM)
(declare (salience 100))
(20-field ?LB) (21-field ?MB) (22-field ?RB))
?f<-(vertex (status 0) (depth ?L) (vertexID
?ID)
;;Отбрасываем
возможное
повторное
(00-field 0) (01-field ?MT) (02-field ?RT)
состояние
(10-field ?LM) (11-field ?MM) (12-field
?RM)
13
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(10-field ?LM) (11-field ?MM) (12-field
?RM)
повторное
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(not (exists (vertex (00-field ?LM) (01-field
(exists (vertex (00-field ?MT) (01-field 0) (02-
;;Отбрасываем
возможное
состояние
?MT) (02-field ?RT)
field ?RT)
(10-field 0) (11-field ?MM) (12-field ?RM)
(10-field ?LM) (11-field ?MM) (12-field
(20-field ?LB) (21-field ?MB) (22-field
?RM)
?RB))))
(20-field ?LB) (21-field ?MB) (22-field
=>
?RB)))
(assert (vertex (00-field ?LM) (01-field ?MT)
(02-field ?RT)
(exists (vertex (00-field ?LM) (01-field ?MT)
(10-field 0) (11-field ?MM) (12-field ?RM)
(02-field ?RT)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(10-field 0) (11-field ?MM) (12-field ?RM)
(depth(+ ?L 1)) (from ?ID) (status 0)
(20-field ?LB) (21-field ?MB) (22-field
(vertexID (getID))))
?RB)))
=>
(if (eq ?*RUNBYUSER* "y") then
(modify
?f(status
1));
Порождены
все
потомки, вершина раскрыта
(printout t crlf "Opened vertex:" crlf)
(printout t 0 ?MT ?RT crlf)
(incSTEPS)
(printout t ?LM ?MM ?RM crlf)
)
(printout t ?LB ?MB ?RB crlf)
;Пустое место в центре сверху
(printout t crlf "Added vertex:" crlf)
;Перемещение пустого места влево
(printout t ?LM ?MT ?RT crlf)
(defrule
(printout t 0 ?MM ?RM crlf)
getNewVertexFrom-01-field-
leftChild
(printout t ?LB ?MB ?RB crlf)
(declare (salience 100))
?f<-(vertex (status 0) (depth ?L) (vertexID
(printout t crlf "Press any key to continue."
?ID)
crlf)
(00-field ?LT) (01-field 0) (02-field ?RT)
(read)
(10-field ?LM) (11-field ?MM) (12-field
)
?RM)
(incSTEPS)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
)
;;Отбрасываем
;Вершина раскрыта, если существуют все
возможное
повторное
состояние
её производные вершины
(not (exists (vertex (00-field 0) (01-field ?LT)
(defrule vertexFrom-00-field-Opened
(02-field ?RT)
(declare (salience 200))
(10-field ?LM) (11-field ?MM) (12-field
?f<-(vertex (status 0) (depth ?L) (vertexID
?RM)
?ID)
(20-field ?LB) (21-field ?MB) (22-field
(00-field 0) (01-field ?MT) (02-field ?RT)
?RB))))
14
=>
(10-field ?LM) (11-field 0) (12-field ?RM)
(assert (vertex (00-field 0) (01-field ?LT) (02-
(20-field ?LB) (21-field ?MB) (22-field
field ?RT)
?RB))))
(10-field ?LM) (11-field ?MM) (12-field
=>
?RM)
(assert (vertex (00-field ?LT) (01-field ?MM)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(02-field ?RT)
(depth(+ ?L 1)) (from ?ID) (status 0)
(10-field ?LM) (11-field 0) (12-field ?RM)
(vertexID (getID))))
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(depth(+ ?L 1)) (from ?ID) (status 0)
(if (eq ?*RUNBYUSER* "y") then
(vertexID (getID))))
(printout t crlf "Opened vertex:" crlf)
(printout t ?LT 0 ?RT crlf)
(if (eq ?*RUNBYUSER* "y") then
(printout t ?LM ?MM ?RM crlf)
(printout t crlf "Opened vertex:" crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t ?LT 0 ?RT crlf)
(printout t ?LM ?MM ?RM crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t 0 ?LT ?RT crlf)
(printout t ?LM ?MM ?RM crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t ?LT ?MM ?RT crlf)
(printout t ?LM 0 ?RM crlf)
(printout t crlf "Press any key to continue."
(printout t ?LB ?MB ?RB crlf)
(read)
(printout t crlf "Press any key to continue."
crlf)
)
crlf)
(incSTEPS)
(read)
)
)
(incSTEPS)
;Перемещение пустого места вниз
(defrule
)
getNewVertexFrom-01-field;Перемещение пустого места вправо
bottomChild
(declare (salience 100))
(defrule
?f<-(vertex (status 0) (depth ?L) (vertexID
getNewVertexFrom-01-field-
rightChild
?ID)
(declare (salience 100))
(00-field ?LT) (01-field 0) (02-field ?RT)
?f<-(vertex (status 0) (depth ?L) (vertexID
(10-field ?LM) (11-field ?MM) (12-field
?ID)
?RM)
(00-field ?LT) (01-field 0) (02-field ?RT)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(10-field ?LM) (11-field ?MM) (12-field
?RM)
;;Отбрасываем
возможное
повторное
(20-field ?LB) (21-field ?MB) (22-field ?RB))
состояние
(not (exists (vertex (00-field ?LT) (01-field
;;Отбрасываем
состояние
?MM) (02-field ?RT)
15
возможное
повторное
(not (exists (vertex (00-field ?LT) (01-field
(20-field ?LB) (21-field ?MB) (22-field ?RB))
?RT) (02-field 0)
(10-field ?LM) (11-field ?MM) (12-field
(exists (vertex (00-field 0) (01-field ?LT) (02-
?RM)
field ?RT)
(20-field ?LB) (21-field ?MB) (22-field
(10-field ?LM) (11-field ?MM) (12-field
?RB))))
?RM)
=>
(20-field ?LB) (21-field ?MB) (22-field
(assert (vertex (00-field ?LT) (01-field ?RT)
?RB)))
(02-field 0)
(10-field ?LM) (11-field ?MM) (12-field
(exists (vertex (00-field ?LT) (01-field ?MM)
?RM)
(02-field ?RT)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(10-field ?LM) (11-field 0) (12-field ?RM)
(depth(+ ?L 1)) (from ?ID) (status 0)
(20-field ?LB) (21-field ?MB) (22-field
(vertexID (getID))))
?RB)))
(if (eq ?*RUNBYUSER* "y") then
(exists (vertex (00-field ?LT) (01-field ?RT)
(printout t crlf "Opened vertex:" crlf)
(02-field 0)
(printout t ?LT 0 ?RT crlf)
(10-field ?LM) (11-field ?MM) (12-field
(printout t ?LM ?MM ?RM crlf)
?RM)
(printout t ?LB ?MB ?RB crlf)
(20-field ?LB) (21-field ?MB) (22-field
?RB)))
(printout t crlf "Added vertex:" crlf)
=>
(printout t ?LT ?RT 0 crlf)
(modify
?f(status
1));
Порождены
все
потомки, вершина раскрыта
(printout t ?LM ?MM ?RM crlf)
(printout t ?LB ?MB ?RB crlf)
(incSTEPS)
)
(printout t crlf "Press any key to continue."
;Пустое место в правом верхнем углу
crlf)
(read)
;Перемещение пустого места вниз
)
(defrule
(incSTEPS)
getNewVertexFrom-02-field-
bottomChild
)
(declare (salience 100))
?f<-(vertex (status 0) (depth ?L) (vertexID
;Вершина раскрыта, если существуют все
?ID)
её производные вершины
(00-field ?LT) (01-field ?MT) (02-field 0)
(defrule vertexFrom-01-field-Opened
(10-field ?LM) (11-field ?MM) (12-field
(declare (salience 200))
?RM)
?f<-(vertex (status 0) (depth ?L) (vertexID
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(00-field ?LT) (01-field 0) (02-field ?RT)
;;Отбрасываем
?ID)
состояние
(10-field ?LM) (11-field ?MM) (12-field
?RM)
16
возможное
повторное
(not (exists (vertex (00-field ?LT) (01-field
;;Отбрасываем
возможное
повторное
состояние
?MT) (02-field ?RM)
(10-field ?LM) (11-field ?MM) (12-field 0)
(not (exists (vertex (00-field ?LT) (01-field 0)
(20-field ?LB) (21-field ?MB) (22-field
(02-field ?MT)
?RB))))
(10-field ?LM) (11-field ?MM) (12-field
=>
?RM)
(assert (vertex (00-field ?LT) (01-field ?MT)
(20-field ?LB) (21-field ?MB) (22-field
(02-field ?RM)
?RB))))
(10-field ?LM) (11-field ?MM) (12-field 0)
=>
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(assert (vertex (00-field ?LT) (01-field 0) (02-
(depth(+ ?L 1)) (from ?ID) (status 0)
field ?MT)
(vertexID (getID))))
(10-field ?LM) (11-field ?MM) (12-field
?RM)
(if (eq ?*RUNBYUSER* "y") then
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(printout t crlf "Opened vertex:" crlf)
(depth(+ ?L 1)) (from ?ID) (status 0)
(printout t ?LT ?MT 0 crlf)
(vertexID (getID))))
(printout t ?LM ?MM ?RM crlf)
(printout t ?LB ?MB ?RB crlf)
(if (eq ?*RUNBYUSER* "y") then
(printout t crlf "Opened vertex:" crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LT ?MT 0 crlf)
(printout t ?LT ?MT ?RM crlf)
(printout t ?LM ?MM ?RM crlf)
(printout t ?LM ?MM 0 crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t crlf "Added vertex:" crlf)
(printout t crlf "Press any key to continue."
(printout t ?LT 0 ?MT crlf)
crlf)
(printout t ?LM ?MM ?RM crlf)
(read)
(printout t ?LB ?MB ?RB crlf)
)
(incSTEPS)
(printout t crlf "Press any key to continue."
)
crlf)
(read)
;Перемещение пустого места влево
)
(defrule
(incSTEPS)
getNewVertexFrom-02-field-
leftChild
)
(declare (salience 100))
;Вершина раскрыта, если существуют все
?f<-(vertex (status 0) (depth ?L) (vertexID
её производные вершины
?ID)
(00-field ?LT) (01-field ?MT) (02-field 0)
(defrule vertexFrom-02-field-Opened
(10-field ?LM) (11-field ?MM) (12-field
(declare (salience 200))
?RM)
?f<-(vertex (status 0) (depth ?L) (vertexID
(20-field ?LB) (21-field ?MB) (22-field ?RB))
?ID)
(00-field ?LT) (01-field ?MT) (02-field 0)
17
(10-field ?LM) (11-field ?MM) (12-field
(10-field ?LB) (11-field ?MM) (12-field ?RM)
?RM)
(20-field 0) (21-field ?MB) (22-field ?RB)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(depth(+ ?L 1)) (from ?ID) (status 0)
(vertexID (getID))))
(exists (vertex (00-field ?LT) (01-field ?MT)
(02-field ?RM)
(if (eq ?*RUNBYUSER* "y") then
(10-field ?LM) (11-field ?MM) (12-field 0)
(printout t crlf "Opened vertex:" crlf)
(20-field ?LB) (21-field ?MB) (22-field
(printout t ?LT ?MT ?RT crlf)
?RB)))
(printout t 0 ?MM ?RM crlf)
(printout t ?LB ?MB ?RB crlf)
(exists (vertex (00-field ?LT) (01-field 0) (02-
field ?MT)
(printout t crlf "Added vertex:" crlf)
(10-field ?LM) (11-field ?MM) (12-field
(printout t ?LT ?MT ?RT crlf)
?RM)
(printout t ?LB ?MM ?RM crlf)
(20-field ?LB) (21-field ?MB) (22-field
(printout t 0 ?MB ?RB crlf)
=>
(printout t crlf "Press any key to continue."
?RB)))
(modify
?f(status
1));
Порождены
все
crlf)
потомки, вершина раскрыта
(read)
(incSTEPS)
)
)
(incSTEPS)
)
;Пустое место в центре слева
;Перемещение пустого места вниз
;Перемещение пустого места вверх
(defrule
(defrule getNewVertexFrom-10-field-topChild
getNewVertexFrom-10-field-
bottomChild
(declare (salience 100))
(declare (salience 100))
?f<-(vertex (status 0) (depth ?L) (vertexID
?f<-(vertex (status 0) (depth ?L) (vertexID
?ID)
?ID)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(10-field 0) (11-field ?MM) (12-field ?RM)
(10-field 0) (11-field ?MM) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(20-field ?LB) (21-field ?MB) (22-field ?RB))
;;Отбрасываем
;;Отбрасываем
возможное
повторное
возможное
повторное
состояние
состояние
(not (exists (vertex (00-field 0) (01-field ?MT)
(not (exists (vertex (00-field ?LT) (01-field
(02-field ?RT)
?MT) (02-field ?RT)
(10-field ?LT) (11-field ?MM) (12-field ?RM)
(10-field ?LB) (11-field ?MM) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field
(20-field 0) (21-field ?MB) (22-field ?RB))))
?RB))))
=>
=>
(assert (vertex (00-field ?LT) (01-field ?MT)
(assert (vertex (00-field 0) (01-field ?MT) (02-
(02-field ?RT)
field ?RT)
18
(10-field ?LT) (11-field ?MM) (12-field ?RM)
(assert (vertex (00-field ?LT) (01-field ?MT)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(02-field ?RT)
(depth(+ ?L 1)) (from ?ID) (status 0)
(10-field ?MM) (11-field 0) (12-field ?RM)
(vertexID (getID))))
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(depth(+ ?L 1)) (from ?ID) (status 0)
(if (eq ?*RUNBYUSER* "y") then
(vertexID (getID))))
(printout t crlf "Opened vertex:" crlf)
(printout t ?LT ?MT ?RT crlf)
(if (eq ?*RUNBYUSER* "y") then
(printout t 0 ?MM ?RM crlf)
(printout t crlf "Opened vertex:" crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t 0 ?MM ?RM crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t 0 ?MT ?RT crlf)
(printout t ?LT ?MM ?RM crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?MM 0 ?RM crlf)
(printout t crlf "Press any key to continue."
(printout t ?LB ?MB ?RB crlf)
(read)
(printout t crlf "Press any key to continue."
crlf)
)
crlf)
(incSTEPS)
(read)
)
)
(incSTEPS)
;Перемещение пустого места вправо
(defrule
)
getNewVertexFrom-10-field;Вершина раскрыта, если существуют все
rightChild
её производные вершины
(declare (salience 100))
?f<-(vertex (status 0) (depth ?L) (vertexID
(defrule vertexFrom-10-field-Opened
?ID)
(declare (salience 200))
(00-field ?LT) (01-field ?MT) (02-field ?RT)
?f<-(vertex (status 0) (depth ?L) (vertexID
(10-field 0) (11-field ?MM) (12-field ?RM)
?ID)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(10-field 0) (11-field ?MM) (12-field ?RM)
повторное
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(not (exists (vertex (00-field ?LT) (01-field
(exists (vertex (00-field ?LT) (01-field ?MT)
;;Отбрасываем
возможное
состояние
?MT) (02-field ?RT)
(02-field ?RT)
(10-field ?MM) (11-field 0) (12-field ?RM)
(10-field ?LB) (11-field ?MM) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field
(20-field 0) (21-field ?MB) (22-field ?RB)))
=>
(exists (vertex (00-field 0) (01-field ?MT) (02-
?RB))))
field ?RT)
19
(10-field ?LT) (11-field ?MM) (12-field ?RM)
(printout t ?LT ?MT ?RT crlf)
(20-field ?LB) (21-field ?MB) (22-field
(printout t ?LM 0 ?RM crlf)
?RB)))
(printout t ?LB ?MB ?RB crlf)
(exists (vertex (00-field ?LT) (01-field ?MT)
(printout t crlf "Added vertex:" crlf)
(02-field ?RT)
(printout t ?LT ?MT ?RT crlf)
(10-field ?MM) (11-field 0) (12-field ?RM)
(printout t ?LM ?MB ?RM crlf)
(20-field ?LB) (21-field ?MB) (22-field
(printout t ?LB 0 ?RB crlf)
=>
(printout t crlf "Press any key to continue."
?RB)))
(modify
?f(status
1));
Порождены
все
crlf)
потомки, вершина раскрыта
(read)
(incSTEPS)
)
)
(incSTEPS)
)
;Пустое место в центре
;Перемещение пустого места вниз
;Перемещение пустого места влево
(defrule
(defrule
getNewVertexFrom-11-field-
bottomChild
getNewVertexFrom-11-field-
leftChild
(declare (salience 100))
(declare (salience 100))
?f<-(vertex (status 0) (depth ?L) (vertexID
?f<-(vertex (status 0) (depth ?L) (vertexID
?ID)
?ID)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(10-field ?LM) (11-field 0) (12-field ?RM)
(10-field ?LM) (11-field 0) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(20-field ?LB) (21-field ?MB) (22-field ?RB))
;;Отбрасываем
возможное
повторное
;;Отбрасываем
состояние
возможное
повторное
состояние
(not (exists (vertex (00-field ?LT) (01-field
(not (exists (vertex (00-field ?LT) (01-field
?MT) (02-field ?RT)
?MT) (02-field ?RT)
(10-field ?LM) (11-field ?MB) (12-field ?RM)
(10-field 0) (11-field ?LM) (12-field ?RM)
(20-field ?LB) (21-field 0) (22-field ?RB))))
(20-field ?LB) (21-field ?MB) (22-field
=>
?RB))))
(assert (vertex (00-field ?LT) (01-field ?MT)
=>
(02-field ?RT)
(assert (vertex (00-field ?LT) (01-field ?MT)
(10-field ?LM) (11-field ?MB) (12-field ?RM)
(02-field ?RT)
(20-field ?LB) (21-field 0) (22-field ?RB)
(10-field 0) (11-field ?LM) (12-field ?RM)
(depth(+ ?L 1)) (from ?ID) (status 0)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(vertexID (getID))))
(depth(+ ?L 1)) (from ?ID) (status 0)
(vertexID (getID))))
(if (eq ?*RUNBYUSER* "y") then
(printout t crlf "Opened vertex:" crlf)
(if (eq ?*RUNBYUSER* "y") then
20
(printout t crlf "Opened vertex:" crlf)
(printout t crlf "Opened vertex:" crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?LM 0 ?RM crlf)
(printout t ?LM 0 ?RM crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t crlf "Added vertex:" crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?LT 0 ?RT crlf)
(printout t 0 ?LM ?RM crlf)
(printout t ?LM ?MT ?RM crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t crlf "Press any key to continue."
(printout t crlf "Press any key to continue."
crlf)
crlf)
(read)
(read)
)
)
(incSTEPS)
(incSTEPS)
)
)
;Перемещение пустого места вверх
;Перемещение пустого места вправо
(defrule getNewVertexFrom-11-field-topChild
(defrule
(declare (salience 100))
getNewVertexFrom-11-field-
rightChild
?f<-(vertex (status 0) (depth ?L) (vertexID
(declare (salience 100))
?ID)
?f<-(vertex (status 0) (depth ?L) (vertexID
(00-field ?LT) (01-field ?MT) (02-field ?RT)
?ID)
(10-field ?LM) (11-field 0) (12-field ?RM)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(10-field ?LM) (11-field 0) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
;;Отбрасываем
возможное
повторное
состояние
;;Отбрасываем
возможное
повторное
состояние
(not (exists (vertex (00-field ?LT) (01-field 0)
(02-field ?RT)
(not (exists (vertex (00-field ?LT) (01-field
(10-field ?LM) (11-field ?MT) (12-field ?RM)
?MT) (02-field ?RT)
(20-field ?LB) (21-field ?MB) (22-field
(10-field ?LM) (11-field ?RM) (12-field 0)
?RB))))
(20-field ?LB) (21-field ?MB) (22-field
=>
?RB))))
(assert (vertex (00-field ?LT) (01-field 0) (02-
=>
field ?RT)
(assert (vertex (00-field ?LT) (01-field ?MT)
(10-field ?LM) (11-field ?MT) (12-field ?RM)
(02-field ?RT)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(10-field ?LM) (11-field ?RM) (12-field 0)
(depth(+ ?L 1)) (from ?ID) (status 0)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(vertexID (getID))))
(depth(+ ?L 1)) (from ?ID) (status 0)
(vertexID (getID))))
(if (eq ?*RUNBYUSER* "y") then
21
(if (eq ?*RUNBYUSER* "y") then
(20-field ?LB) (21-field ?MB) (22-field
(printout t crlf "Opened vertex:" crlf)
?RB)))
(printout t ?LT ?MT ?RT crlf)
(printout t ?LM 0 ?RM crlf)
(exists (vertex (00-field ?LT) (01-field ?MT)
(printout t ?LB ?MB ?RB crlf)
(02-field ?RT)
(10-field ?LM) (11-field ?RM) (12-field 0)
(printout t crlf "Added vertex:" crlf)
(20-field ?LB) (21-field ?MB) (22-field
(printout t ?LT ?MT ?RT crlf)
?RB)))
(printout t ?LM ?RM 0 crlf)
=>
(printout t ?LB ?MB ?RB crlf)
(modify
?f(status
1));
Порождены
все
потомки, вершина раскрыта
(printout t crlf "Press any key to continue."
(incSTEPS)
crlf)
)
(read)
)
;Пустое место в центре справа
(incSTEPS)
;Перемещение пустого места вниз
)
(defrule
getNewVertexFrom-12-field-
bottomChild
;Вершина раскрыта, если существуют все
(declare (salience 100))
её производные вершины
?f<-(vertex (status 0) (depth ?L) (vertexID
(defrule vertexFrom-11-field-Opened
?ID)
(declare (salience 200))
(00-field ?LT) (01-field ?MT) (02-field ?RT)
?f<-(vertex (status 0) (depth ?L) (vertexID
(10-field ?LM) (11-field ?MM) (12-field 0)
?ID)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(10-field ?LM) (11-field 0) (12-field ?RM)
;;Отбрасываем
возможное
повторное
состояние
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(not (exists (vertex (00-field ?LT) (01-field
(exists (vertex (00-field ?LT) (01-field ?MT)
?MT) (02-field ?RT)
(02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field ?RB)
(10-field ?LM) (11-field ?MB) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field 0))))
(20-field ?LB) (21-field 0) (22-field ?RB)))
=>
(assert (vertex (00-field ?LT) (01-field ?MT)
(exists (vertex (00-field ?LT) (01-field ?MT)
(02-field ?RT)
(02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field ?RB)
(10-field 0) (11-field ?LM) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field 0)
(20-field ?LB) (21-field ?MB) (22-field
(depth(+ ?L 1)) (from ?ID) (status 0)
?RB)))
(vertexID (getID))))
(exists (vertex (00-field ?LT) (01-field 0) (02-
(if (eq ?*RUNBYUSER* "y") then
field ?RT)
(printout t crlf "Opened vertex:" crlf)
(10-field ?LM) (11-field ?MT) (12-field ?RM)
(printout t ?LT ?MT ?RT crlf)
22
(printout t ?LM ?MM 0 crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t ?LM ?MM 0 crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LM ?MM ?RB crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?LB ?MB 0 crlf)
(printout t ?LM 0 ?MM crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t crlf "Press any key to continue."
crlf)
(printout t crlf "Press any key to continue."
(read)
crlf)
)
(read)
(incSTEPS)
)
)
(incSTEPS)
)
;Перемещение пустого места влево
(defrule
;Перемещение пустого места вверх
getNewVertexFrom-12-field-
leftChild
(defrule getNewVertexFrom-12-field-topChild
(declare (salience 100))
(declare (salience 100))
?f<-(vertex (status 0) (depth ?L) (vertexID
?f<-(vertex (status 0) (depth ?L) (vertexID
?ID)
?ID)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field 0)
(10-field ?LM) (11-field ?MM) (12-field 0)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(20-field ?LB) (21-field ?MB) (22-field ?RB))
;;Отбрасываем
возможное
повторное
;;Отбрасываем
состояние
возможное
повторное
состояние
(not (exists (vertex (00-field ?LT) (01-field
(not (exists (vertex (00-field ?LT) (01-field
?MT) (02-field ?RT)
?MT) (02-field 0)
(10-field ?LM) (11-field 0) (12-field ?MM)
(10-field ?LM) (11-field ?MM) (12-field ?RT)
(20-field ?LB) (21-field ?MB) (22-field
(20-field ?LB) (21-field ?MB) (22-field
?RB))))
?RB))))
=>
=>
(assert (vertex (00-field ?LT) (01-field ?MT)
(assert (vertex (00-field ?LT) (01-field ?MT)
(02-field ?RT)
(02-field 0)
(10-field ?LM) (11-field 0) (12-field ?MM)
(10-field ?LM) (11-field ?MM) (12-field ?RT)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(depth(+ ?L 1)) (from ?ID) (status 0)
(depth(+ ?L 1)) (from ?ID) (status 0)
(vertexID (getID))))
(vertexID (getID))))
(if (eq ?*RUNBYUSER* "y") then
(if (eq ?*RUNBYUSER* "y") then
(printout t crlf "Opened vertex:" crlf)
(printout t crlf "Opened vertex:" crlf)
23
(printout t ?LT ?MT ?RT crlf)
=>
(printout t ?LM ?MM 0 crlf)
(modify
?f(status
1));
Порождены
все
потомки, вершина раскрыта
(printout t ?LB ?MB ?RB crlf)
(incSTEPS)
(printout t crlf "Added vertex:" crlf)
)
(printout t ?LT ?MT 0 crlf)
(printout t ?LM ?MM ?RT crlf)
;Пустое место в левом нижнем углу
(printout t ?LB ?MB ?RB crlf)
;Перемещаем пустое место вверх
(defrule getNewVertexFrom-20-field-topChild
(printout t crlf "Press any key to continue."
(declare (salience 100))
crlf)
?f<-(vertex (status 0) (depth ?L) (vertexID
(read)
?ID)
)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(incSTEPS)
(10-field ?LM) (11-field ?MM) (12-field
)
?RM)
(20-field 0) (21-field ?MB) (22-field ?RB))
;Вершина раскрыта, если существуют все
её производные вершины
;;Отбрасываем
возможное
повторное
состояние
(defrule vertexFrom-12-field-Opened
(declare (salience 200))
(not (exists (vertex (00-field ?LT) (01-field
?f<-(vertex (status 0) (depth ?L) (vertexID
?MT) (02-field ?RT)
?ID)
(10-field 0) (11-field ?MM) (12-field ?RM)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(20-field ?LM) (21-field ?MB) (22-field
(10-field ?LM) (11-field ?MM) (12-field 0)
?RB))))
(20-field ?LB) (21-field ?MB) (22-field ?RB))
=>
(assert (vertex (00-field ?LT) (01-field ?MT)
(exists (vertex (00-field ?LT) (01-field ?MT)
(02-field ?RT)
(02-field ?RT)
(10-field 0) (11-field ?MM) (12-field ?RM)
(10-field ?LM) (11-field ?MM) (12-field ?RB)
(20-field ?LM) (21-field ?MB) (22-field ?RB)
(20-field ?LB) (21-field ?MB) (22-field 0)))
(depth(+ ?L 1)) (from ?ID) (status 0)
(vertexID (getID))))
(exists (vertex (00-field ?LT) (01-field ?MT)
(02-field ?RT)
(if (eq ?*RUNBYUSER* "y") then
(10-field ?LM) (11-field 0) (12-field ?MM)
(printout t crlf "Opened vertex:" crlf)
(20-field ?LB) (21-field ?MB) (22-field
(printout t ?LT ?MT ?RT crlf)
?RB)))
(printout t ?LM ?MM ?RM crlf)
(printout t 0 ?MB ?RB crlf)
(exists (vertex (00-field ?LT) (01-field ?MT)
(02-field 0)
(printout t crlf "Added vertex:" crlf)
(10-field ?LM) (11-field ?MM) (12-field ?RT)
(printout t ?LT ?MT ?RT crlf)
(20-field ?LB) (21-field ?MB) (22-field
(printout t 0 ?MM ?RM crlf)
?RB)))
(printout t ?LM ?MB ?RB crlf)
24
(printout t ?LT ?MT ?RT crlf)
(printout t crlf "Press any key to continue."
(printout t ?LM ?MM ?RM crlf)
crlf)
(printout t ?MB 0 ?RB crlf)
(read)
)
(printout t crlf "Press any key to continue."
(incSTEPS)
crlf)
)
(read)
)
;Перемещаем пустое место вправо
(incSTEPS)
(defrule
)
getNewVertexFrom-20-field-
rightChild
;Вершина раскрыта, если существуют все
(declare (salience 100))
её производные вершины
?f<-(vertex (status 0) (depth ?L) (vertexID
?ID)
(defrule vertexFrom-20-field-Opened
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(declare (salience 200))
(10-field ?LM) (11-field ?MM) (12-field
?f<-(vertex (status 0) (depth ?L) (vertexID
?RM)
?ID)
(20-field 0) (21-field ?MB) (22-field ?RB))
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field
;;Отбрасываем
возможное
повторное
?RM)
состояние
(20-field 0) (21-field ?MB) (22-field ?RB))
(not (exists (vertex (00-field ?LT) (01-field
?MT) (02-field ?RT)
(exists (vertex (00-field ?LT) (01-field ?MT)
(10-field ?LM) (11-field ?MM) (12-field
(02-field ?RT)
?RM)
(10-field 0) (11-field ?MM) (12-field ?RM)
(20-field ?MB) (21-field 0) (22-field ?RB))))
(20-field ?LM) (21-field ?MB) (22-field
=>
?RB)))
(assert (vertex (00-field ?LT) (01-field ?MT)
(02-field ?RT)
(exists (vertex (00-field ?LT) (01-field ?MT)
(10-field ?LM) (11-field ?MM) (12-field
(02-field ?RT)
?RM)
(10-field ?LM) (11-field ?MM) (12-field
(20-field ?MB) (21-field 0) (22-field ?RB)
?RM)
(depth(+ ?L 1)) (from ?ID) (status 0)
(20-field ?MB) (21-field 0) (22-field ?RB)))
(vertexID (getID))))
=>
(modify
?f(status
1));
Порождены
потомки, вершина раскрыта
(if (eq ?*RUNBYUSER* "y") then
(printout t crlf "Opened vertex:" crlf)
(incSTEPS)
(printout t ?LT ?MT ?RT crlf)
)
(printout t ?LM ?MM ?RM crlf)
;Пустое место в центре снизу
(printout t 0 ?MB ?RB crlf)
;Перемещаем пустое место влево
(printout t crlf "Added vertex:" crlf)
25
все
(defrule
getNewVertexFrom-21-field-
)
leftChild
(declare (salience 100))
;Перемещаем пустое место вверх
?f<-(vertex (status 0) (depth ?L) (vertexID
(defrule getNewVertexFrom-21-field-topChild
?ID)
(declare (salience 100))
(00-field ?LT) (01-field ?MT) (02-field ?RT)
?f<-(vertex (status 0) (depth ?L) (vertexID
(10-field ?LM) (11-field ?MM) (12-field
?ID)
?RM)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(20-field ?LB) (21-field 0) (22-field ?RB))
(10-field ?LM) (11-field ?MM) (12-field
?RM)
;;Отбрасываем
возможное
повторное
(20-field ?LB) (21-field 0) (22-field ?RB))
состояние
(not (exists (vertex (00-field ?LT) (01-field
;;Отбрасываем
возможное
повторное
состояние
?MT) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field
(not (exists (vertex (00-field ?LT) (01-field
?RM)
?MT) (02-field ?RT)
(20-field 0) (21-field ?LB) (22-field ?RB))))
(10-field ?LM) (11-field 0) (12-field ?RM)
=>
(20-field ?LB) (21-field ?MM) (22-field
(assert (vertex (00-field ?LT) (01-field ?MT)
?RB))))
(02-field ?RT)
=>
(10-field ?LM) (11-field ?MM) (12-field
(assert (vertex (00-field ?LT) (01-field ?MT)
?RM)
(02-field ?RT)
(20-field 0) (21-field ?LB) (22-field ?RB)
(10-field ?LM) (11-field 0) (12-field ?RM)
(depth(+ ?L 1)) (from ?ID) (status 0)
(20-field ?LB) (21-field ?MM) (22-field ?RB)
(vertexID (getID))))
(depth(+ ?L 1)) (from ?ID) (status 0)
(vertexID (getID))))
(if (eq ?*RUNBYUSER* "y") then
(printout t crlf "Opened vertex:" crlf)
(if (eq ?*RUNBYUSER* "y") then
(printout t ?LT ?MT ?RT crlf)
(printout t crlf "Opened vertex:" crlf)
(printout t ?LM ?MM ?RM crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?LB 0 ?RB crlf)
(printout t ?LM ?MM ?RM crlf)
(printout t ?LB 0 ?RB crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LM ?MM ?RM crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t 0 ?LB ?RB crlf)
(printout t ?LM 0 ?RM crlf)
(printout t ?LB ?MM ?RB crlf)
(printout t crlf "Press any key to continue."
crlf)
(printout t crlf "Press any key to continue."
(read)
crlf)
)
(read)
(incSTEPS)
)
26
(incSTEPS)
(printout t crlf "Press any key to continue."
)
crlf)
(read)
;Перемещаем пустое место вправо
)
(defrule
(incSTEPS)
getNewVertexFrom-21-field-
rightChild
)
(declare (salience 100))
;Вершина раскрыта, если существуют все
?f<-(vertex (status 0) (depth ?L) (vertexID
её производные вершины
?ID)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(defrule vertexFrom-21-field-Opened
(10-field ?LM) (11-field ?MM) (12-field
(declare (salience 200))
?RM)
?f<-(vertex (status 0) (depth ?L) (vertexID
(20-field ?LB) (21-field 0) (22-field ?RB))
?ID)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
;;Отбрасываем
возможное
повторное
(10-field ?LM) (11-field ?MM) (12-field
состояние
?RM)
(not (exists (vertex (00-field ?LT) (01-field
(20-field ?LB) (21-field 0) (22-field ?RB))
?MT) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field
(exists (vertex (00-field ?LT) (01-field ?MT)
?RM)
(02-field ?RT)
(20-field ?LB) (21-field ?RB) (22-field 0))))
(10-field ?LM) (11-field ?MM) (12-field
=>
?RM)
(assert (vertex (00-field ?LT) (01-field ?MT)
(20-field 0) (21-field ?LB) (22-field ?RB)))
(02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field
(exists (vertex (00-field ?LT) (01-field ?MT)
?RM)
(02-field ?RT)
(20-field ?LB) (21-field ?RB) (22-field 0)
(10-field ?LM) (11-field 0) (12-field ?RM)
(depth(+ ?L 1)) (from ?ID) (status 0)
(20-field ?LB) (21-field ?MM) (22-field
(vertexID (getID))))
?RB)))
(if (eq ?*RUNBYUSER* "y") then
(exists (vertex (00-field ?LT) (01-field ?MT)
(printout t crlf "Opened vertex:" crlf)
(02-field ?RT)
(printout t ?LT ?MT ?RT crlf)
(10-field ?LM) (11-field ?MM) (12-field
(printout t ?LM ?MM ?RM crlf)
?RM)
(printout t ?LB 0 ?RB crlf)
(20-field ?LB) (21-field ?RB) (22-field 0)))
=>
(modify
(printout t crlf "Added vertex:" crlf)
?f(status
1));
Порождены
потомки, вершина раскрыта
(printout t ?LT ?MT ?RT crlf)
(printout t ?LM ?MM ?RM crlf)
(incSTEPS)
(printout t ?LB ?RB 0 crlf)
)
;Пустое место в правом нижнем углу
27
все
;Перемещаем пустое место влево
(incSTEPS)
(defrule
)
getNewVertexFrom-22-field-
leftChild
(declare (salience 100))
;Перемещаем пустое место вверх
?f<-(vertex (status 0) (depth ?L) (vertexID
(defrule getNewVertexFrom-22-field-topChild
?ID)
(declare (salience 100))
(00-field ?LT) (01-field ?MT) (02-field ?RT)
?f<-(vertex (status 0) (depth ?L) (vertexID
(10-field ?LM) (11-field ?MM) (12-field
?ID)
?RM)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(20-field ?LB) (21-field ?MB) (22-field 0))
(10-field ?LM) (11-field ?MM) (12-field
?RM)
;;Отбрасываем
возможное
повторное
(20-field ?LB) (21-field ?MB) (22-field 0))
состояние
(not (exists (vertex (00-field ?LT) (01-field
;;Отбрасываем
возможное
повторное
состояние
?MT) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field
(not (exists (vertex (00-field ?LT) (01-field
?RM)
?MT) (02-field ?RT)
(20-field ?LB) (21-field 0) (22-field ?MB))))
(10-field ?LM) (11-field ?MM) (12-field 0)
=>
(20-field ?LB) (21-field ?MB) (22-field
(assert (vertex (00-field ?LT) (01-field ?MT)
?RM))))
(02-field ?RT)
=>
(10-field ?LM) (11-field ?MM) (12-field
(assert (vertex (00-field ?LT) (01-field ?MT)
?RM)
(02-field ?RT)
(20-field ?LB) (21-field 0) (22-field ?MB)
(10-field ?LM) (11-field ?MM) (12-field 0)
(depth(+ ?L 1)) (from ?ID) (status 0)
(20-field ?LB) (21-field ?MB) (22-field ?RM)
(vertexID (getID))))
(depth(+ ?L 1)) (from ?ID) (status 0)
(vertexID (getID))))
(if (eq ?*RUNBYUSER* "y") then
(printout t crlf "Opened vertex:" crlf)
(if (eq ?*RUNBYUSER* "y") then
(printout t ?LT ?MT ?RT crlf)
(printout t crlf "Opened vertex:" crlf)
(printout t ?LM ?MM ?RM crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?LB ?MB 0 crlf)
(printout t ?LM ?MM ?RM crlf)
(printout t ?LB ?MB 0 crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LM ?MM ?RM crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?LB 0 ?MB crlf)
(printout t ?LM ?MM 0 crlf)
(printout t ?LB ?MB ?RM crlf)
(printout t crlf "Press any key to continue."
crlf)
(printout t crlf "Press any key to continue."
(read)
crlf)
)
(read)
28
)
(exists (vertex (00-field ?LT) (01-field ?MT)
(incSTEPS)
(02-field ?RT)
)
(10-field ?LM) (11-field ?MM) (12-field
?RM)
;Вершина раскрыта, если существуют все
(20-field ?LB) (21-field 0) (22-field ?MB)))
её производные вершины
(defrule vertexFrom-22-field-Opened
(exists (vertex (00-field ?LT) (01-field ?MT)
(declare (salience 200))
(02-field ?RT)
?f<-(vertex (status 0) (depth ?L) (vertexID
(10-field ?LM) (11-field ?MM) (12-field 0)
?ID)
(20-field ?LB) (21-field ?MB) (22-field
(00-field ?LT) (01-field ?MT) (02-field ?RT)
?RM)))
(10-field ?LM) (11-field ?MM) (12-field
=>
(modify
?RM)
?f(status
1));
Порождены
все
потомки, вершина раскрыта
(20-field ?LB) (21-field ?MB) (22-field 0))
(incSTEPS)
)
Стратегия двунаправленного поиска.
;;Определение структуры вершины
;Настройки проекта!!!
;;Глобальная
(deftemplate vertex "Структура вершины"
переменная
для
подсчёта
для
подсчёта
общего количества вершин
(slot 00-field (type NUMBER))
(defglobal
(slot 01-field (type NUMBER))
?*ID* = 0)
(slot 02-field (type NUMBER))
;Глобальная
(slot 10-field (type NUMBER))
переменная
(slot 11-field (type NUMBER))
общего количества шагов
(slot 12-field (type NUMBER))
(defglobal
(slot 20-field (type NUMBER))
?*STEPS* = 0)
(slot 21-field (type NUMBER))
(slot 22-field (type NUMBER))
;Включён ли пользовательский режим
(slot depth (type NUMBER))
(defglobal
(slot vertexID (type NUMBER) (default 0))
?*RUNBYUSER* = "n")
(slot status (type NUMBER) (default 0));; 0 не раскрыта, 1 - раскрыта, 2 - решение
;;Функция
получения
ID
следующей
вершины
(slot from (type NUMBER))
(slot tree (type NUMBER));; 0 - старт-
(deffunction getID()
дерево, 1 - цель-дерево
(bind ?*ID* (+ ?*ID* 1)) ;;Инкремент ID
(slot isSolution (type NUMBER)); 0 - данная
?*ID*)
вершина - не решение, 2 - данная вершина решение
;Функция инкремента количества шагов
)
(deffunction incSTEPS()
29
(bind ?*STEPS* (+ ?*STEPS* 1))
(assert (userAsked))
)
)
;;Объявление фактов начальной и конечной
;Проверка достижения целевого состояния
вершин
(defrule testGoal
(deffacts start-goal_vertexes
(declare (salience 500))
(vertex (00-field 6) (01-field 0) (02-field 8)
?f<-(vertex (tree 0) (isSolution ?fs) (depth
(10-field 5) (11-field 2) (12-field 1)
?fL)
(20-field 4) (21-field 3) (22-field 7)
(00-field ?fLT) (01-field ?fMT) (02-field
(depth 0) (from 0) (status 0) (vertexID
?fRT)
(getID)) (tree 0) (isSolution 0))
(10-field ?fLM) (11-field ?fMM) (12-field
?fRM)
(vertex (00-field 1) (01-field 2) (02-field 3)
(20-field ?fLB) (21-field ?fMB) (22-field
(10-field 8) (11-field 0) (12-field 4)
?fRB))
(20-field 7) (21-field 6) (22-field 5)
(depth 0) (from 0) (status 0) (vertexID
?g<-(vertex (tree 1) (isSolution ?gs) (depth
(getID)) (tree 1) (isSolution 0))
?gL)
)
(00-field ?gLT) (01-field ?gMT) (02-field
?gRT)
(defrule askUser
(10-field ?gLM) (11-field ?gMM) (12-field
(declare (salience 1000))
?gRM)
(exists (initial-fact))
(20-field ?gLB) (21-field ?gMB) (22-field
(not (exists (userAsked)))
?gRB))
=>
(printout t "Do you want to enable step-by-
(not (exists (solution)))
step mode (y/n)?" crlf)
=>
(bind ?answer(read))
(if (and (and (and (and (and (and (and (and (eq
?fLT ?gLT) (eq ?fMT ?gMT)) (eq ?fRT ?gRT))
;Считываем только "n" и "y"
(eq ?fLM ?gLM)) (eq ?fMM ?gMM)) (eq
(while (and (neq ?answer n) (neq ?answer y))
?fRM ?gRM))
do
(eq ?fLB ?gLB)) (eq ?fMB ?gMB)) (eq ?fRB
(printout t crlf "Please, enter correct answer."
?gRB)) then
crlf)
(printout t "Do you want to enable step-by-
(bind ?fs 1)
step mode (y/n)?" crlf)
(bind ?gs 1)
(bind ?answer(read))
(printout t "Solution has been found after " ?fL
)
" steps of start tree and " ?gL " vertexes of goal-tree."
crlf)
(if (eq ?answer y) then
(bind ?*RUNBYUSER* "y")
(assert (solution))
)
)
)
30
;Останов программы только тогда, когда
;Правила перехода между состояниями
обошли все вершины
;Пустое место в левом верхнем углу
(defrule noSolutions
;Перемещение пустого места вправо
(declare (salience 200))
(defrule
(not (vertex(status 0)))
getNewVertexFrom-00-field-
rightChild
(not (exists (solution)))
(declare (salience 100))
=>
?f<-(vertex (status 0) (depth ?L) (vertexID
(halt)
?ID) (tree ?tr) (isSolution 0)
(printout t "No solutions." crlf)
(00-field 0) (01-field ?MT) (02-field ?RT)
(incSTEPS)
(10-field ?LM) (11-field ?MM) (12-field
)
?RM)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
;Вывод
ёмкостных
и
временных
характеристик
;;Отбрасываем
повторное
состояние
(defrule total
(declare (salience 200))
(not (exists (vertex (00-field ?MT) (01-field 0)
(exists (solution))
(02-field ?RT)
=>
(10-field ?LM) (11-field ?MM) (12-field
(printout t crlf "Number of structures used: "
?RM)
?*ID* "." crlf)
(printout t "Number
возможное
(20-field ?LB) (21-field ?MB) (22-field ?RB)
of
steps used: "
(tree ?tr) (isSolution 0))))
?*STEPS* "." crlf)
(not (exists (solution)))
)
=>
(assert (vertex (00-field ?MT) (01-field 0) (02-
;Вывод "каймы"
field ?RT)
(defrule remainingVertexes
(10-field ?LM) (11-field ?MM) (12-field
(declare (salience 500))
?RM)
?f<-(vertex (status 0) (depth ?L) (vertexID
(20-field ?LB) (21-field ?MB) (22-field ?RB)
?ID)
(depth(+ ?L 1)) (from ?ID) (status 0)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(10-field ?LM) (11-field ?MM) (12-field
?RM)
(if (eq ?*RUNBYUSER* "y") then
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(printout t crlf "Opened vertex:" crlf)
=>
(printout t 0 ?MT ?RT crlf)
(if (eq ?*RUNBYUSER* "y") then
(printout t ?LM ?MM ?RM crlf)
(printout t crlf "Remaining vertex:" crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?LM ?MM ?RM crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t ?MT 0 ?RT crlf)
)
(printout t ?LM ?MM ?RM crlf)
)
(printout t ?LB ?MB ?RB crlf)
31
(printout t ?LM ?MT ?RT crlf)
(printout t crlf "Press any key to continue."
(printout t 0 ?MM ?RM crlf)
crlf)
(printout t ?LB ?MB ?RB crlf)
(read)
)
(printout t crlf "Press any key to continue."
(incSTEPS)
crlf)
)
(read)
)
;Перемещение пустого места вниз
(incSTEPS)
(defrule
)
getNewVertexFrom-00-field-
bottomChild
;Вершина раскрыта, если существуют все
(declare (salience 100))
её производные вершины
?f<-(vertex (status 0) (depth ?L) (vertexID
?ID) (tree ?tr) (isSolution 0)
(defrule vertexFrom-00-field-Opened
(00-field 0) (01-field ?MT) (02-field ?RT)
(declare (salience 200))
(10-field ?LM) (11-field ?MM) (12-field
?f<-(vertex (status 0) (depth ?L) (vertexID
?RM)
?ID) (tree ?tr)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(00-field 0) (01-field ?MT) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field
;;Отбрасываем
возможное
повторное
?RM)
состояние
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(not (exists (vertex (00-field ?LM) (01-field
?MT) (02-field ?RT)
(exists (vertex (00-field ?MT) (01-field 0) (02-
(10-field 0) (11-field ?MM) (12-field ?RM)
field ?RT)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(10-field ?LM) (11-field ?MM) (12-field
(tree ?tr) (isSolution 0))))
?RM)
(not (exists (solution)))
(20-field ?LB) (21-field ?MB) (22-field ?RB)
=>
(tree ?tr)))
(assert (vertex (00-field ?LM) (01-field ?MT)
(02-field ?RT)
(exists (vertex (00-field ?LM) (01-field ?MT)
(10-field 0) (11-field ?MM) (12-field ?RM)
(02-field ?RT)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(10-field 0) (11-field ?MM) (12-field ?RM)
(depth(+ ?L 1)) (from ?ID) (status 0)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(tree ?tr)))
=>
(modify
(if (eq ?*RUNBYUSER* "y") then
?f(status
1));
Порождены
потомки, вершина раскрыта
(printout t crlf "Opened vertex:" crlf)
(printout t 0 ?MT ?RT crlf)
(incSTEPS)
(printout t ?LM ?MM ?RM crlf)
)
(printout t ?LB ?MB ?RB crlf)
;Пустое место в центре сверху
;Перемещение пустого места влево
(printout t crlf "Added vertex:" crlf)
32
все
(defrule
getNewVertexFrom-01-field-
)
leftChild
(incSTEPS)
(declare (salience 100))
)
?f<-(vertex (status 0) (depth ?L) (vertexID
;Перемещение пустого места вниз
?ID) (tree ?tr) (isSolution 0)
(00-field ?LT) (01-field 0) (02-field ?RT)
(defrule
(10-field ?LM) (11-field ?MM) (12-field
getNewVertexFrom-01-field-
bottomChild
?RM)
(declare (salience 100))
(20-field ?LB) (21-field ?MB) (22-field ?RB))
?f<-(vertex (status 0) (depth ?L) (vertexID
?ID) (tree ?tr) (isSolution 0)
;;Отбрасываем
возможное
повторное
(00-field ?LT) (01-field 0) (02-field ?RT)
состояние
(10-field ?LM) (11-field ?MM) (12-field
(not (exists (vertex (00-field 0) (01-field ?LT)
?RM)
(02-field ?RT)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(10-field ?LM) (11-field ?MM) (12-field
?RM)
;;Отбрасываем
возможное
повторное
состояние
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(tree ?tr) (isSolution 0))))
(not (exists (vertex (00-field ?LT) (01-field
(not (exists (solution)))
?MM) (02-field ?RT)
=>
(10-field ?LM) (11-field 0) (12-field ?RM)
(assert (vertex (00-field 0) (01-field ?LT) (02-
(20-field ?LB) (21-field ?MB) (22-field ?RB)
field ?RT)
(tree ?tr) (isSolution 0))))
(10-field ?LM) (11-field ?MM) (12-field
(not (exists (solution)))
?RM)
=>
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(assert (vertex (00-field ?LT) (01-field ?MM)
(depth(+ ?L 1)) (from ?ID) (status 0)
(02-field ?RT)
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(10-field ?LM) (11-field 0) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(if (eq ?*RUNBYUSER* "y") then
(depth(+ ?L 1)) (from ?ID) (status 0)
(printout t crlf "Opened vertex:" crlf)
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(printout t ?LT 0 ?RT crlf)
(printout t ?LM ?MM ?RM crlf)
(if (eq ?*RUNBYUSER* "y") then
(printout t ?LB ?MB ?RB crlf)
(printout t crlf "Opened vertex:" crlf)
(printout t ?LT 0 ?RT crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LM ?MM ?RM crlf)
(printout t 0 ?LT ?RT crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t ?LM ?MM ?RM crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LT ?MM ?RT crlf)
(printout t crlf "Press any key to continue."
(printout t ?LM 0 ?RM crlf)
crlf)
(printout t ?LB ?MB ?RB crlf)
(read)
33
(printout t crlf "Press any key to continue."
(printout t crlf "Added vertex:" crlf)
crlf)
(printout t ?LT ?RT 0 crlf)
(read)
(printout t ?LM ?MM ?RM crlf)
)
(printout t ?LB ?MB ?RB crlf)
(incSTEPS)
)
(printout t crlf "Press any key to continue."
crlf)
;Перемещение пустого места вправо
(read)
(defrule
)
getNewVertexFrom-01-field-
rightChild
(incSTEPS)
(declare (salience 100))
)
?f<-(vertex (status 0) (depth ?L) (vertexID
;Вершина раскрыта, если существуют все
?ID) (tree ?tr) (isSolution 0)
её производные вершины
(00-field ?LT) (01-field 0) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field
(defrule vertexFrom-01-field-Opened
?RM)
(declare (salience 200))
(20-field ?LB) (21-field ?MB) (22-field ?RB))
?f<-(vertex (status 0) (depth ?L) (vertexID
?ID) (tree ?tr)
;;Отбрасываем
возможное
повторное
(00-field ?LT) (01-field 0) (02-field ?RT)
состояние
(10-field ?LM) (11-field ?MM) (12-field
(not (exists (vertex (00-field ?LT) (01-field
?RM)
?RT) (02-field 0)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(10-field ?LM) (11-field ?MM) (12-field
?RM)
(exists (vertex (00-field 0) (01-field ?LT) (02(20-field ?LB) (21-field ?MB) (22-field ?RB)
field ?RT)
(tree ?tr) (isSolution 0))))
(10-field ?LM) (11-field ?MM) (12-field
(not (exists (solution)))
?RM)
=>
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(assert (vertex (00-field ?LT) (01-field ?RT)
(tree ?tr)))
(02-field 0)
(10-field ?LM) (11-field ?MM) (12-field
(exists (vertex (00-field ?LT) (01-field ?MM)
?RM)
(02-field ?RT)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(10-field ?LM) (11-field 0) (12-field ?RM)
(depth(+ ?L 1)) (from ?ID) (status 0)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(tree ?tr)))
(if (eq ?*RUNBYUSER* "y") then
(exists (vertex (00-field ?LT) (01-field ?RT)
(printout t crlf "Opened vertex:" crlf)
(02-field 0)
(printout t ?LT 0 ?RT crlf)
(10-field ?LM) (11-field ?MM) (12-field
(printout t ?LM ?MM ?RM crlf)
?RM)
(printout t ?LB ?MB ?RB crlf)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(tree ?tr)))
34
=>
(modify
(printout t ?LM ?MM 0 crlf)
?f(status
1));
Порождены
все
(printout t ?LB ?MB ?RB crlf)
потомки, вершина раскрыта
(incSTEPS)
(printout t crlf "Press any key to continue."
)
crlf)
(read)
;Пустое место в правом верхнем углу
)
;Перемещение пустого места вниз
(incSTEPS)
(defrule
)
getNewVertexFrom-02-field-
bottomChild
(declare (salience 100))
;Перемещение пустого места влево
?f<-(vertex (status 0) (depth ?L) (vertexID
(defrule
?ID) (tree ?tr) (isSolution 0)
getNewVertexFrom-02-field-
leftChild
(00-field ?LT) (01-field ?MT) (02-field 0)
(declare (salience 100))
(10-field ?LM) (11-field ?MM) (12-field
?f<-(vertex (status 0) (depth ?L) (vertexID
?RM)
?ID) (tree ?tr) (isSolution 0)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(00-field ?LT) (01-field ?MT) (02-field 0)
(10-field ?LM) (11-field ?MM) (12-field
;;Отбрасываем
возможное
повторное
?RM)
состояние
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(not (exists (vertex (00-field ?LT) (01-field
?MT) (02-field ?RM)
;;Отбрасываем
возможное
повторное
состояние
(10-field ?LM) (11-field ?MM) (12-field 0)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(not (exists (vertex (00-field ?LT) (01-field 0)
(tree ?tr) (isSolution 0))))
(02-field ?MT)
(not (exists (solution)))
(10-field ?LM) (11-field ?MM) (12-field
=>
?RM)
(assert (vertex (00-field ?LT) (01-field ?MT)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(02-field ?RM)
(tree ?tr) (isSolution 0))))
(10-field ?LM) (11-field ?MM) (12-field 0)
(not (exists (solution)))
(20-field ?LB) (21-field ?MB) (22-field ?RB)
=>
(depth(+ ?L 1)) (from ?ID) (status 0)
(assert (vertex (00-field ?LT) (01-field 0) (02-
(vertexID (getID)) (tree ?tr) (isSolution 0)))
field ?MT)
(10-field ?LM) (11-field ?MM) (12-field
(if (eq ?*RUNBYUSER* "y") then
?RM)
(printout t crlf "Opened vertex:" crlf)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(printout t ?LT ?MT 0 crlf)
(depth(+ ?L 1)) (from ?ID) (status 0)
(printout t ?LM ?MM ?RM crlf)
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(printout t ?LB ?MB ?RB crlf)
(if (eq ?*RUNBYUSER* "y") then
(printout t crlf "Added vertex:" crlf)
(printout t crlf "Opened vertex:" crlf)
(printout t ?LT ?MT ?RM crlf)
(printout t ?LT ?MT 0 crlf)
35
(printout t ?LM ?MM ?RM crlf)
)
(printout t ?LB ?MB ?RB crlf)
;Пустое место в центре слева
(printout t crlf "Added vertex:" crlf)
;Перемещение пустого места вниз
(printout t ?LT 0 ?MT crlf)
(defrule
(printout t ?LM ?MM ?RM crlf)
getNewVertexFrom-10-field-
bottomChild
(printout t ?LB ?MB ?RB crlf)
(declare (salience 100))
?f<-(vertex (status 0) (depth ?L) (vertexID
(printout t crlf "Press any key to continue."
?ID) (tree ?tr) (isSolution 0)
crlf)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(read)
(10-field 0) (11-field ?MM) (12-field ?RM)
)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(incSTEPS)
)
;;Отбрасываем
возможное
повторное
состояние
;Вершина раскрыта, если существуют все
(not (exists (vertex (00-field ?LT) (01-field
её производные вершины
?MT) (02-field ?RT)
(defrule vertexFrom-02-field-Opened
(10-field ?LB) (11-field ?MM) (12-field ?RM)
(declare (salience 200))
(20-field 0) (21-field ?MB) (22-field ?RB)
?f<-(vertex (status 0) (depth ?L) (vertexID
(tree ?tr) (isSolution 0))))
?ID) (tree ?tr)
(not (exists (solution)))
(00-field ?LT) (01-field ?MT) (02-field 0)
=>
(10-field ?LM) (11-field ?MM) (12-field
(assert (vertex (00-field ?LT) (01-field ?MT)
?RM)
(02-field ?RT)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(10-field ?LB) (11-field ?MM) (12-field ?RM)
(20-field 0) (21-field ?MB) (22-field ?RB)
(exists (vertex (00-field ?LT) (01-field ?MT)
(depth(+ ?L 1)) (from ?ID) (status 0)
(02-field ?RM)
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(10-field ?LM) (11-field ?MM) (12-field 0)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(if (eq ?*RUNBYUSER* "y") then
(tree ?tr)))
(printout t crlf "Opened vertex:" crlf)
(printout t ?LT ?MT ?RT crlf)
(exists (vertex (00-field ?LT) (01-field 0) (02-
(printout t 0 ?MM ?RM crlf)
field ?MT)
(printout t ?LB ?MB ?RB crlf)
(10-field ?LM) (11-field ?MM) (12-field
?RM)
(printout t crlf "Added vertex:" crlf)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(printout t ?LT ?MT ?RT crlf)
(tree ?tr)))
(printout t ?LB ?MM ?RM crlf)
=>
(modify
(printout t 0 ?MB ?RB crlf)
?f(status
1));
Порождены
все
потомки, вершина раскрыта
(printout t crlf "Press any key to continue."
(incSTEPS)
crlf)
36
(read)
(printout t crlf "Press any key to continue."
)
crlf)
(incSTEPS)
(read)
)
)
(incSTEPS)
;Перемещение пустого места вверх
)
(defrule getNewVertexFrom-10-field-topChild
(declare (salience 100))
;Перемещение пустого места вправо
?f<-(vertex (status 0) (depth ?L) (vertexID
(defrule
?ID) (tree ?tr) (isSolution 0)
getNewVertexFrom-10-field-
rightChild
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(declare (salience 100))
(10-field 0) (11-field ?MM) (12-field ?RM)
?f<-(vertex (status 0) (depth ?L) (vertexID
(20-field ?LB) (21-field ?MB) (22-field ?RB))
?ID) (tree ?tr) (isSolution 0)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
;;Отбрасываем
возможное
повторное
(10-field 0) (11-field ?MM) (12-field ?RM)
состояние
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(not (exists (vertex (00-field 0) (01-field ?MT)
(02-field ?RT)
;;Отбрасываем
возможное
повторное
состояние
(10-field ?LT) (11-field ?MM) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(not (exists (vertex (00-field ?LT) (01-field
(tree ?tr) (isSolution 0))))
?MT) (02-field ?RT)
(not (exists (solution)))
(10-field ?MM) (11-field 0) (12-field ?RM)
=>
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(assert (vertex (00-field 0) (01-field ?MT) (02-
(tree ?tr) (isSolution 0))))
field ?RT)
(not (exists (solution)))
(10-field ?LT) (11-field ?MM) (12-field ?RM)
=>
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(assert (vertex (00-field ?LT) (01-field ?MT)
(depth(+ ?L 1)) (from ?ID) (status 0)
(02-field ?RT)
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(10-field ?MM) (11-field 0) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(if (eq ?*RUNBYUSER* "y") then
(depth(+ ?L 1)) (from ?ID) (status 0)
(printout t crlf "Opened vertex:" crlf)
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(printout t ?LT ?MT ?RT crlf)
(printout t 0 ?MM ?RM crlf)
(if (eq ?*RUNBYUSER* "y") then
(printout t ?LB ?MB ?RB crlf)
(printout t crlf "Opened vertex:" crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t crlf "Added vertex:" crlf)
(printout t 0 ?MM ?RM crlf)
(printout t 0 ?MT ?RT crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t ?LT ?MM ?RM crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?MM 0 ?RM crlf)
37
;Пустое место в центре
(printout t ?LB ?MB ?RB crlf)
;Перемещение пустого места вниз
(printout t crlf "Press any key to continue."
(defrule
crlf)
getNewVertexFrom-11-field-
bottomChild
(read)
(declare (salience 100))
)
?f<-(vertex (status 0) (depth ?L) (vertexID
(incSTEPS)
?ID) (tree ?tr) (isSolution 0)
)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(10-field ?LM) (11-field 0) (12-field ?RM)
;Вершина раскрыта, если существуют все
(20-field ?LB) (21-field ?MB) (22-field ?RB))
её производные вершины
(defrule vertexFrom-10-field-Opened
;;Отбрасываем
возможное
повторное
состояние
(declare (salience 200))
?f<-(vertex (status 0) (depth ?L) (vertexID
(not (exists (vertex (00-field ?LT) (01-field
?ID) (tree ?tr)
?MT) (02-field ?RT)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(10-field ?LM) (11-field ?MB) (12-field ?RM)
(10-field 0) (11-field ?MM) (12-field ?RM)
(20-field ?LB) (21-field 0) (22-field ?RB)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(tree ?tr) (isSolution 0))))
(not (exists (solution)))
(exists (vertex (00-field ?LT) (01-field ?MT)
=>
(02-field ?RT)
(assert (vertex (00-field ?LT) (01-field ?MT)
(10-field ?LB) (11-field ?MM) (12-field ?RM)
(02-field ?RT)
(20-field 0) (21-field ?MB) (22-field ?RB)
(10-field ?LM) (11-field ?MB) (12-field ?RM)
(tree ?tr)))
(20-field ?LB) (21-field 0) (22-field ?RB)
(depth(+ ?L 1)) (from ?ID) (status 0)
(exists (vertex (00-field 0) (01-field ?MT) (02-
(vertexID (getID)) (tree ?tr) (isSolution 0)))
field ?RT)
(10-field ?LT) (11-field ?MM) (12-field ?RM)
(if (eq ?*RUNBYUSER* "y") then
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(printout t crlf "Opened vertex:" crlf)
(tree ?tr)))
(printout t ?LT ?MT ?RT crlf)
(printout t ?LM 0 ?RM crlf)
(exists (vertex (00-field ?LT) (01-field ?MT)
(printout t ?LB ?MB ?RB crlf)
(02-field ?RT)
(10-field ?MM) (11-field 0) (12-field ?RM)
(printout t crlf "Added vertex:" crlf)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(printout t ?LT ?MT ?RT crlf)
(tree ?tr)))
(printout t ?LM ?MB ?RM crlf)
=>
(modify
(printout t ?LB 0 ?RB crlf)
?f(status
1));
Порождены
все
потомки, вершина раскрыта
(printout t crlf "Press any key to continue."
(incSTEPS)
crlf)
)
(read)
)
38
(incSTEPS)
(read)
)
)
(incSTEPS)
;Перемещение пустого места влево
(defrule
)
getNewVertexFrom-11-field;Перемещение пустого места вверх
leftChild
(declare (salience 100))
(defrule getNewVertexFrom-11-field-topChild
?f<-(vertex (status 0) (depth ?L) (vertexID
(declare (salience 100))
?ID) (tree ?tr) (isSolution 0)
?f<-(vertex (status 0) (depth ?L) (vertexID
(00-field ?LT) (01-field ?MT) (02-field ?RT)
?ID) (tree ?tr) (isSolution 0)
(10-field ?LM) (11-field 0) (12-field ?RM)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(10-field ?LM) (11-field 0) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
;;Отбрасываем
возможное
повторное
состояние
;;Отбрасываем
возможное
повторное
состояние
(not (exists (vertex (00-field ?LT) (01-field
?MT) (02-field ?RT)
(not (exists (vertex (00-field ?LT) (01-field 0)
(10-field 0) (11-field ?LM) (12-field ?RM)
(02-field ?RT)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(10-field ?LM) (11-field ?MT) (12-field ?RM)
(tree ?tr) (isSolution 0))))
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(not (exists (solution)))
(tree ?tr) (isSolution 0))))
=>
(not (exists (solution)))
(assert (vertex (00-field ?LT) (01-field ?MT)
=>
(02-field ?RT)
(assert (vertex (00-field ?LT) (01-field 0) (02-
(10-field 0) (11-field ?LM) (12-field ?RM)
field ?RT)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(10-field ?LM) (11-field ?MT) (12-field ?RM)
(depth(+ ?L 1)) (from ?ID) (status 0)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(depth(+ ?L 1)) (from ?ID) (status 0)
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(if (eq ?*RUNBYUSER* "y") then
(printout t crlf "Opened vertex:" crlf)
(if (eq ?*RUNBYUSER* "y") then
(printout t ?LT ?MT ?RT crlf)
(printout t crlf "Opened vertex:" crlf)
(printout t ?LM 0 ?RM crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t ?LM 0 ?RM crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t crlf "Added vertex:" crlf)
(printout t 0 ?LM ?RM crlf)
(printout t ?LT 0 ?RT crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t ?LM ?MT ?RM crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t crlf "Press any key to continue."
crlf)
39
(printout t crlf "Press any key to continue."
(printout t ?LB ?MB ?RB crlf)
(read)
(printout t crlf "Press any key to continue."
crlf)
)
crlf)
(incSTEPS)
(read)
)
)
(incSTEPS)
;Перемещение пустого места вправо
(defrule
)
getNewVertexFrom-11-field;Вершина раскрыта, если существуют все
rightChild
её производные вершины
(declare (salience 100))
?f<-(vertex (status 0) (depth ?L) (vertexID
(defrule vertexFrom-11-field-Opened
?ID) (tree ?tr) (isSolution 0)
(declare (salience 200))
(00-field ?LT) (01-field ?MT) (02-field ?RT)
?f<-(vertex (status 0) (depth ?L) (vertexID
(10-field ?LM) (11-field 0) (12-field ?RM)
?ID) (tree ?tr)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(10-field ?LM) (11-field 0) (12-field ?RM)
повторное
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(not (exists (vertex (00-field ?LT) (01-field
(exists (vertex (00-field ?LT) (01-field ?MT)
;;Отбрасываем
возможное
состояние
?MT) (02-field ?RT)
(02-field ?RT)
(10-field ?LM) (11-field ?RM) (12-field 0)
(10-field ?LM) (11-field ?MB) (12-field ?RM)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(20-field ?LB) (21-field 0) (22-field ?RB)
(tree ?tr) (isSolution 0))))
(tree ?tr)))
(not (exists (solution)))
=>
(exists (vertex (00-field ?LT) (01-field ?MT)
(assert (vertex (00-field ?LT) (01-field ?MT)
(02-field ?RT)
(02-field ?RT)
(10-field 0) (11-field ?LM) (12-field ?RM)
(10-field ?LM) (11-field ?RM) (12-field 0)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(tree ?tr)))
(depth(+ ?L 1)) (from ?ID) (status 0)
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(exists (vertex (00-field ?LT) (01-field 0) (02field ?RT)
(if (eq ?*RUNBYUSER* "y") then
(10-field ?LM) (11-field ?MT) (12-field ?RM)
(printout t crlf "Opened vertex:" crlf)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(printout t ?LT ?MT ?RT crlf)
(tree ?tr)))
(printout t ?LM 0 ?RM crlf)
(printout t ?LB ?MB ?RB crlf)
(exists (vertex (00-field ?LT) (01-field ?MT)
(02-field ?RT)
(printout t crlf "Added vertex:" crlf)
(10-field ?LM) (11-field ?RM) (12-field 0)
(printout t ?LT ?MT ?RT crlf)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(printout t ?LM ?RM 0 crlf)
(tree ?tr)))
40
=>
(modify
(printout t ?LB ?MB 0 crlf)
?f(status
1));
Порождены
все
потомки, вершина раскрыта
(printout t crlf "Press any key to continue."
(incSTEPS)
crlf)
)
(read)
)
;Пустое место в центре справа
(incSTEPS)
;Перемещение пустого места вниз
)
(defrule
getNewVertexFrom-12-field;Перемещение пустого места влево
bottomChild
(declare (salience 100))
(defrule
?f<-(vertex (status 0) (depth ?L) (vertexID
getNewVertexFrom-12-field-
leftChild
?ID) (tree ?tr) (isSolution 0)
(declare (salience 100))
(00-field ?LT) (01-field ?MT) (02-field ?RT)
?f<-(vertex (status 0) (depth ?L) (vertexID
(10-field ?LM) (11-field ?MM) (12-field 0)
?ID) (tree ?tr) (isSolution 0)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field 0)
;;Отбрасываем
возможное
повторное
(20-field ?LB) (21-field ?MB) (22-field ?RB))
состояние
(not (exists (vertex (00-field ?LT) (01-field
;;Отбрасываем
возможное
повторное
состояние
?MT) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field ?RB)
(not (exists (vertex (00-field ?LT) (01-field
(20-field ?LB) (21-field ?MB) (22-field 0)
?MT) (02-field ?RT)
(tree ?tr) (isSolution 0))))
(10-field ?LM) (11-field 0) (12-field ?MM)
(not (exists (solution)))
(20-field ?LB) (21-field ?MB) (22-field ?RB)
=>
(tree ?tr) (isSolution 0))))
(assert (vertex (00-field ?LT) (01-field ?MT)
(not (exists (solution)))
(02-field ?RT)
=>
(10-field ?LM) (11-field ?MM) (12-field ?RB)
(assert (vertex (00-field ?LT) (01-field ?MT)
(20-field ?LB) (21-field ?MB) (22-field 0)
(02-field ?RT)
(depth(+ ?L 1)) (from ?ID) (status 0)
(10-field ?LM) (11-field 0) (12-field ?MM)
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(depth(+ ?L 1)) (from ?ID) (status 0)
(if (eq ?*RUNBYUSER* "y") then
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(printout t crlf "Opened vertex:" crlf)
(printout t ?LT ?MT ?RT crlf)
(if (eq ?*RUNBYUSER* "y") then
(printout t ?LM ?MM 0 crlf)
(printout t crlf "Opened vertex:" crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?LM ?MM 0 crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?LM ?MM ?RB crlf)
(printout t crlf "Added vertex:" crlf)
41
(printout t ?LT ?MT ?RT crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LM 0 ?MM crlf)
(printout t ?LT ?MT 0 crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t ?LM ?MM ?RT crlf)
(printout t ?LB ?MB ?RB crlf)
(printout t crlf "Press any key to continue."
crlf)
(printout t crlf "Press any key to continue."
(read)
crlf)
)
(read)
(incSTEPS)
)
)
(incSTEPS)
)
;Перемещение пустого места вверх
;Вершина раскрыта, если существуют все
(defrule getNewVertexFrom-12-field-topChild
её производные вершины
(declare (salience 100))
?f<-(vertex (status 0) (depth ?L) (vertexID
(defrule vertexFrom-12-field-Opened
?ID) (tree ?tr) (isSolution 0)
(declare (salience 200))
(00-field ?LT) (01-field ?MT) (02-field ?RT)
?f<-(vertex (status 0) (depth ?L) (vertexID
(10-field ?LM) (11-field ?MM) (12-field 0)
?ID) (tree ?tr)
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field 0)
повторное
(20-field ?LB) (21-field ?MB) (22-field ?RB))
(not (exists (vertex (00-field ?LT) (01-field
(exists (vertex (00-field ?LT) (01-field ?MT)
;;Отбрасываем
возможное
состояние
?MT) (02-field 0)
(02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field ?RB)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(20-field ?LB) (21-field ?MB) (22-field 0)
(tree ?tr) (isSolution 0))))
(tree ?tr)))
(not (exists (solution)))
=>
(exists (vertex (00-field ?LT) (01-field ?MT)
(assert (vertex (00-field ?LT) (01-field ?MT)
(02-field ?RT)
(02-field 0)
(10-field ?LM) (11-field 0) (12-field ?MM)
(10-field ?LM) (11-field ?MM) (12-field ?RT)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(tree ?tr)))
(depth(+ ?L 1)) (from ?ID) (status 0)
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(exists (vertex (00-field ?LT) (01-field ?MT)
(02-field 0)
(if (eq ?*RUNBYUSER* "y") then
(10-field ?LM) (11-field ?MM) (12-field ?RT)
(printout t crlf "Opened vertex:" crlf)
(20-field ?LB) (21-field ?MB) (22-field ?RB)
(printout t ?LT ?MT ?RT crlf)
(tree ?tr)))
(printout t ?LM ?MM 0 crlf)
=>
(printout t ?LB ?MB ?RB crlf)
(modify
?f(status
потомки, вершина раскрыта
42
1));
Порождены
все
(incSTEPS)
(printout t crlf "Press any key to continue."
)
crlf)
(read)
;Пустое место в левом нижнем углу
)
;Перемещаем пустое место вверх
(incSTEPS)
(defrule getNewVertexFrom-20-field-topChild
)
(declare (salience 100))
;Перемещаем пустое место вправо
?f<-(vertex (status 0) (depth ?L) (vertexID
?ID) (tree ?tr) (isSolution 0)
(defrule
(00-field ?LT) (01-field ?MT) (02-field ?RT)
getNewVertexFrom-20-field-
rightChild
(10-field ?LM) (11-field ?MM) (12-field
(declare (salience 100))
?RM)
?f<-(vertex (status 0) (depth ?L) (vertexID
(20-field 0) (21-field ?MB) (22-field ?RB))
?ID) (tree ?tr) (isSolution 0)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
;;Отбрасываем
возможное
повторное
(10-field ?LM) (11-field ?MM) (12-field
состояние
?RM)
(not (exists (vertex (00-field ?LT) (01-field
(20-field 0) (21-field ?MB) (22-field ?RB))
?MT) (02-field ?RT)
(10-field 0) (11-field ?MM) (12-field ?RM)
;;Отбрасываем
возможное
повторное
состояние
(20-field ?LM) (21-field ?MB) (22-field ?RB)
(tree ?tr) (isSolution 0))))
(not (exists (vertex (00-field ?LT) (01-field
(not (exists (solution)))
?MT) (02-field ?RT)
=>
(10-field ?LM) (11-field ?MM) (12-field
(assert (vertex (00-field ?LT) (01-field ?MT)
?RM)
(02-field ?RT)
(20-field ?MB) (21-field 0) (22-field ?RB)
(10-field 0) (11-field ?MM) (12-field ?RM)
(tree ?tr) (isSolution 0))))
(20-field ?LM) (21-field ?MB) (22-field ?RB)
(not (exists (solution)))
(depth(+ ?L 1)) (from ?ID) (status 0)
=>
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(assert (vertex (00-field ?LT) (01-field ?MT)
(02-field ?RT)
(if (eq ?*RUNBYUSER* "y") then
(10-field ?LM) (11-field ?MM) (12-field
(printout t crlf "Opened vertex:" crlf)
?RM)
(printout t ?LT ?MT ?RT crlf)
(20-field ?MB) (21-field 0) (22-field ?RB)
(printout t ?LM ?MM ?RM crlf)
(depth(+ ?L 1)) (from ?ID) (status 0)
(printout t 0 ?MB ?RB crlf)
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(printout t crlf "Added vertex:" crlf)
(if (eq ?*RUNBYUSER* "y") then
(printout t ?LT ?MT ?RT crlf)
(printout t crlf "Opened vertex:" crlf)
(printout t 0 ?MM ?RM crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?LM ?MB ?RB crlf)
(printout t ?LM ?MM ?RM crlf)
(printout t 0 ?MB ?RB crlf)
43
(printout t crlf "Added vertex:" crlf)
;Перемещаем пустое место влево
(printout t ?LT ?MT ?RT crlf)
(defrule
(printout t ?LM ?MM ?RM crlf)
getNewVertexFrom-21-field-
leftChild
(printout t ?MB 0 ?RB crlf)
(declare (salience 100))
?f<-(vertex (status 0) (depth ?L) (vertexID
(printout t crlf "Press any key to continue."
?ID) (tree ?tr) (isSolution 0)
crlf)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(read)
(10-field ?LM) (11-field ?MM) (12-field
)
?RM)
(incSTEPS)
(20-field ?LB) (21-field 0) (22-field ?RB))
)
;;Отбрасываем
;Вершина раскрыта, если существуют все
возможное
повторное
состояние
её производные вершины
(not (exists (vertex (00-field ?LT) (01-field
(defrule vertexFrom-20-field-Opened
?MT) (02-field ?RT)
(declare (salience 200))
(10-field ?LM) (11-field ?MM) (12-field
?f<-(vertex (status 0) (depth ?L) (vertexID
?RM)
?ID) (tree ?tr)
(20-field 0) (21-field ?LB) (22-field ?RB)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(tree ?tr) (isSolution 0))))
(10-field ?LM) (11-field ?MM) (12-field
(not (exists (solution)))
?RM)
=>
(20-field 0) (21-field ?MB) (22-field ?RB))
(assert (vertex (00-field ?LT) (01-field ?MT)
(02-field ?RT)
(exists (vertex (00-field ?LT) (01-field ?MT)
(10-field ?LM) (11-field ?MM) (12-field
(02-field ?RT)
?RM)
(10-field 0) (11-field ?MM) (12-field ?RM)
(20-field 0) (21-field ?LB) (22-field ?RB)
(20-field ?LM) (21-field ?MB) (22-field ?RB)
(depth(+ ?L 1)) (from ?ID) (status 0)
(tree ?tr)))
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(exists (vertex (00-field ?LT) (01-field ?MT)
(if (eq ?*RUNBYUSER* "y") then
(02-field ?RT)
(printout t crlf "Opened vertex:" crlf)
(10-field ?LM) (11-field ?MM) (12-field
(printout t ?LT ?MT ?RT crlf)
?RM)
(printout t ?LM ?MM ?RM crlf)
(20-field ?MB) (21-field 0) (22-field ?RB)
(printout t ?LB 0 ?RB crlf)
(tree ?tr)))
=>
(modify
(printout t crlf "Added vertex:" crlf)
?f(status
1));
Порождены
все
(printout t ?LT ?MT ?RT crlf)
потомки, вершина раскрыта
(printout t ?LM ?MM ?RM crlf)
(incSTEPS)
(printout t 0 ?LB ?RB crlf)
)
(printout t crlf "Press any key to continue."
;Пустое место в центре снизу
crlf)
44
(read)
(printout t crlf "Press any key to continue."
)
crlf)
(incSTEPS)
(read)
)
)
(incSTEPS)
;Перемещаем пустое место вверх
)
(defrule getNewVertexFrom-21-field-topChild
(declare (salience 100))
;Перемещаем пустое место вправо
?f<-(vertex (status 0) (depth ?L) (vertexID
(defrule
?ID) (tree ?tr) (isSolution 0)
getNewVertexFrom-21-field-
rightChild
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(declare (salience 100))
(10-field ?LM) (11-field ?MM) (12-field
?f<-(vertex (status 0) (depth ?L) (vertexID
?RM)
?ID) (tree ?tr) (isSolution 0)
(20-field ?LB) (21-field 0) (22-field ?RB))
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field
;;Отбрасываем
возможное
повторное
?RM)
состояние
(20-field ?LB) (21-field 0) (22-field ?RB))
(not (exists (vertex (00-field ?LT) (01-field
?MT) (02-field ?RT)
;;Отбрасываем
возможное
повторное
состояние
(10-field ?LM) (11-field 0) (12-field ?RM)
(20-field ?LB) (21-field ?MM) (22-field ?RB)
(not (exists (vertex (00-field ?LT) (01-field
(tree ?tr) (isSolution 0))))
?MT) (02-field ?RT)
(not (exists (solution)))
(10-field ?LM) (11-field ?MM) (12-field
=>
?RM)
(assert (vertex (00-field ?LT) (01-field ?MT)
(20-field ?LB) (21-field ?RB) (22-field 0)
(02-field ?RT)
(tree ?tr) (isSolution 0))))
(10-field ?LM) (11-field 0) (12-field ?RM)
(not (exists (solution)))
(20-field ?LB) (21-field ?MM) (22-field ?RB)
=>
(depth(+ ?L 1)) (from ?ID) (status 0)
(assert (vertex (00-field ?LT) (01-field ?MT)
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field
(if (eq ?*RUNBYUSER* "y") then
?RM)
(printout t crlf "Opened vertex:" crlf)
(20-field ?LB) (21-field ?RB) (22-field 0)
(printout t ?LT ?MT ?RT crlf)
(depth(+ ?L 1)) (from ?ID) (status 0)
(printout t ?LM ?MM ?RM crlf)
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(printout t ?LB 0 ?RB crlf)
(if (eq ?*RUNBYUSER* "y") then
(printout t crlf "Added vertex:" crlf)
(printout t crlf "Opened vertex:" crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?LM 0 ?RM crlf)
(printout t ?LM ?MM ?RM crlf)
(printout t ?LB ?MM ?RB crlf)
(printout t ?LB 0 ?RB crlf)
45
(printout t crlf "Added vertex:" crlf)
=>
(printout t ?LT ?MT ?RT crlf)
(modify
?f(status
1));
Порождены
все
потомки, вершина раскрыта
(printout t ?LM ?MM ?RM crlf)
(printout t ?LB ?RB 0 crlf)
(incSTEPS)
)
(printout t crlf "Press any key to continue."
;Пустое место в правом нижнем углу
crlf)
(read)
;Перемещаем пустое место влево
)
(defrule
(incSTEPS)
getNewVertexFrom-22-field-
leftChild
)
(declare (salience 100))
?f<-(vertex (status 0) (depth ?L) (vertexID
;Вершина раскрыта, если существуют все
?ID) (tree ?tr) (isSolution 0)
её производные вершины
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(defrule vertexFrom-21-field-Opened
(10-field ?LM) (11-field ?MM) (12-field
(declare (salience 200))
?RM)
?f<-(vertex (status 0) (depth ?L) (vertexID
(20-field ?LB) (21-field ?MB) (22-field 0))
?ID) (tree ?tr)
(00-field ?LT) (01-field ?MT) (02-field ?RT)
;;Отбрасываем
возможное
повторное
состояние
(10-field ?LM) (11-field ?MM) (12-field
?RM)
(not (exists (vertex (00-field ?LT) (01-field
(20-field ?LB) (21-field 0) (22-field ?RB))
?MT) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field
(exists (vertex (00-field ?LT) (01-field ?MT)
?RM)
(02-field ?RT)
(20-field ?LB) (21-field 0) (22-field ?MB)
(10-field ?LM) (11-field ?MM) (12-field
(tree ?tr) (isSolution 0))))
?RM)
(not (exists (solution)))
(20-field 0) (21-field ?LB) (22-field ?RB)
=>
(tree ?tr)))
(assert (vertex (00-field ?LT) (01-field ?MT)
(02-field ?RT)
(exists (vertex (00-field ?LT) (01-field ?MT)
(10-field ?LM) (11-field ?MM) (12-field
(02-field ?RT)
?RM)
(10-field ?LM) (11-field 0) (12-field ?RM)
(20-field ?LB) (21-field 0) (22-field ?MB)
(20-field ?LB) (21-field ?MM) (22-field ?RB)
(depth(+ ?L 1)) (from ?ID) (status 0)
(tree ?tr)))
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(exists (vertex (00-field ?LT) (01-field ?MT)
(if (eq ?*RUNBYUSER* "y") then
(02-field ?RT)
(printout t crlf "Opened vertex:" crlf)
(10-field ?LM) (11-field ?MM) (12-field
(printout t ?LT ?MT ?RT crlf)
?RM)
(printout t ?LM ?MM ?RM crlf)
(20-field ?LB) (21-field ?RB) (22-field 0)
(printout t ?LB ?MB 0 crlf)
(tree ?tr)))
46
(printout t crlf "Added vertex:" crlf)
(printout t ?LB ?MB 0 crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?LM ?MM ?RM crlf)
(printout t crlf "Added vertex:" crlf)
(printout t ?LB 0 ?MB crlf)
(printout t ?LT ?MT ?RT crlf)
(printout t ?LM ?MM 0 crlf)
(printout t crlf "Press any key to continue."
(printout t ?LB ?MB ?RM crlf)
(read)
(printout t crlf "Press any key to continue."
crlf)
)
crlf)
(incSTEPS)
(read)
)
)
(incSTEPS)
;Перемещаем пустое место вверх
)
(defrule getNewVertexFrom-22-field-topChild
;Вершина раскрыта, если существуют все
(declare (salience 100))
её производные вершины
?f<-(vertex (status 0) (depth ?L) (vertexID
?ID) (tree ?tr) (isSolution 0)
(defrule vertexFrom-22-field-Opened
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(declare (salience 200))
(10-field ?LM) (11-field ?MM) (12-field
?f<-(vertex (status 0) (depth ?L) (vertexID
?RM)
?ID) (tree ?tr)
(20-field ?LB) (21-field ?MB) (22-field 0))
(00-field ?LT) (01-field ?MT) (02-field ?RT)
(10-field ?LM) (11-field ?MM) (12-field
;;Отбрасываем
возможное
повторное
?RM)
состояние
(20-field ?LB) (21-field ?MB) (22-field 0))
(not (exists (vertex (00-field ?LT) (01-field
?MT) (02-field ?RT)
(exists (vertex (00-field ?LT) (01-field ?MT)
(10-field ?LM) (11-field ?MM) (12-field 0)
(02-field ?RT)
(20-field ?LB) (21-field ?MB) (22-field ?RM)
(10-field ?LM) (11-field ?MM) (12-field
(tree ?tr) (isSolution 0))))
?RM)
(not (exists (solution)))
(20-field ?LB) (21-field 0) (22-field ?MB)
=>
(tree ?tr)))
(assert (vertex (00-field ?LT) (01-field ?MT)
(02-field ?RT)
(exists (vertex (00-field ?LT) (01-field ?MT)
(10-field ?LM) (11-field ?MM) (12-field 0)
(02-field ?RT)
(20-field ?LB) (21-field ?MB) (22-field ?RM)
(10-field ?LM) (11-field ?MM) (12-field 0)
(depth(+ ?L 1)) (from ?ID) (status 0)
(20-field ?LB) (21-field ?MB) (22-field ?RM)
(vertexID (getID)) (tree ?tr) (isSolution 0)))
(tree ?tr)))
=>
(modify
(if (eq ?*RUNBYUSER* "y") then
?f(status
потомки, вершина раскрыта
(printout t crlf "Opened vertex:" crlf)
(printout t ?LT ?MT ?RT crlf)
(incSTEPS)
(printout t ?LM ?MM ?RM crlf)
)
47
1));
Порождены
все
Результат работы.
Стратегия поиска в глубину.
Стратегия двунаправленного поиска.
Выводы.
В ходе выполнения данной лабораторной работы были преодолены
следующие трудности:
– исправлена опечатка в программном коде при реализации стратегии
поиска в глубину;
– было значительно уменьшено время выполнения программы с помощью
отключения статусных окон;
– при реализации стратегии поиска в глубину в связи с тем, что правила
построены так, чтобы не допустить порождения повторных
вершин,
необходимо было создать четыре правила нахождения решения, причём
размещения фишек на доске в них на одно перемещение отличаются от
размещения фишек целевого состояния;
– при реализации стратегии двунаправленного поиска для определения
принадлежности вершины тому или иному дереву в шаблоне вершины было
введено поле tree (0 – старт-дерево, 1 – цель-дерево).
Download