1 | ;;; -*- Mode: Lisp -*-
|
---|
2 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
3 | ;;;
|
---|
4 | ;;; Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>
|
---|
5 | ;;;
|
---|
6 | ;;; This program is free software; you can redistribute it and/or modify
|
---|
7 | ;;; it under the terms of the GNU General Public License as published by
|
---|
8 | ;;; the Free Software Foundation; either version 2 of the License, or
|
---|
9 | ;;; (at your option) any later version.
|
---|
10 | ;;;
|
---|
11 | ;;; This program is distributed in the hope that it will be useful,
|
---|
12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | ;;; GNU General Public License for more details.
|
---|
15 | ;;;
|
---|
16 | ;;; You should have received a copy of the GNU General Public License
|
---|
17 | ;;; along with this program; if not, write to the Free Software
|
---|
18 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
19 | ;;;
|
---|
20 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
21 |
|
---|
22 | (defpackage "PAIR-QUEUE"
|
---|
23 | (:use :cl :priority-queue :monom :polynomial :symbolic-polynomial :utils)
|
---|
24 | (:export "CRITICAL-PAIR"
|
---|
25 | "CRITICAL-PAIR-FIRST"
|
---|
26 | "CRITICAL-PAIR-SECOND"
|
---|
27 | "CRITICAL-PAIR-QUEUE"
|
---|
28 | "SELECTION-STRATEGY"
|
---|
29 | "MIN-TOTAL-DEGREE-STRATEGY"
|
---|
30 | "ENQUEUE"
|
---|
31 | "DEQUEUE"
|
---|
32 | "QUEUE-SIZE"
|
---|
33 | "QUEUE-EMPTY-P"
|
---|
34 | )
|
---|
35 | (:documentation "Critical pair queue implementation. The pair queue is a list of critical
|
---|
36 | pairs, ordered by some partial order. Pair queue is a kind of priority queue.")
|
---|
37 | )
|
---|
38 |
|
---|
39 | (in-package :pair-queue)
|
---|
40 |
|
---|
41 | (defclass critical-pair ()
|
---|
42 | ((first :initform nil :initarg :first :accessor critical-pair-first :type poly)
|
---|
43 | (second :initform nil :initarg :second :accessor critical-pair-second :type poly))
|
---|
44 | (:documentation "Represents a critical pair, i.e. a pair of two polynomials. The derived
|
---|
45 | classes may add extra data used in computing the order of critical pairs."))
|
---|
46 |
|
---|
47 | (defmethod print-object ((self critical-pair) stream)
|
---|
48 | (print-unreadable-object (self stream :type t :identity t)
|
---|
49 | (with-accessors ((first critical-pair-first)
|
---|
50 | (second critical-pair-second))
|
---|
51 | self
|
---|
52 | (format stream "FIRST=~A SECOND=~A"
|
---|
53 | first second))))
|
---|
54 |
|
---|
55 | (defclass selection-strategy ()
|
---|
56 | ((pair-key-fn :initform #'(lambda (p q) (universal-lcm (leading-monomial p) (leading-monomial q)))
|
---|
57 | :initarg :pair-key-fn
|
---|
58 | :accessor pair-key-fn)
|
---|
59 | (pair-order-fn :initform #'lex>
|
---|
60 | :initarg :pair-order-fn
|
---|
61 | :accessor pair-order-fn))
|
---|
62 | (:documentation "Represents the normal critical pair selection
|
---|
63 | strategy. The two ingredients of a strategy is a function
|
---|
64 | PAIR-KEY-FUNCTION which computes a key from the critical pair, which
|
---|
65 | can be any type of data, and a function PAIR-ORDER-FN used to compaire
|
---|
66 | the calculated keys to determine which pair is more promising and
|
---|
67 | should be considered first. The normal selection strategy for a given
|
---|
68 | monomial order PAIR-ORDER-FN consists in selecting the pair with the
|
---|
69 | minimal LCM of leading monomials first."))
|
---|
70 |
|
---|
71 | (defmethod print-object ((self selection-strategy) stream)
|
---|
72 | (print-unreadable-object (self stream :type t :identity t)
|
---|
73 | (with-accessors ((pair-key-fn pair-key-fn)
|
---|
74 | (pair-order-fn pair-order-fn))
|
---|
75 | self
|
---|
76 | (format stream "PAIR-KEY-FN=~A PAIR-ORDER-FN=~A"
|
---|
77 | pair-key-fn pair-order-fn))))
|
---|
78 |
|
---|
79 | (defclass min-total-degree-strategy (selection-strategy)
|
---|
80 | ((pair-key-fn :initform #'(lambda (p q) (total-degree (universal-lcm (leading-monomial p) (leading-monomial q)))))
|
---|
81 | (pair-order-fn :initform #'<))
|
---|
82 | (:documentation "Make a selection strategy where a pair with a
|
---|
83 | minimum total degree of LCM of leading monomials is selected."))
|
---|
84 |
|
---|
85 | (defclass min-combined-length-strategy (selection-strategy)
|
---|
86 | ((pair-key-fn :initform #'(lambda (p q) (+ (poly-length p) (poly-length q))))
|
---|
87 | (pair-order-fn :initform #'<))
|
---|
88 | (:documentation "Make a selection strategy where a pair with the minimum combined length of both
|
---|
89 | polynomials is selected."))
|
---|
90 |
|
---|
91 | (defclass critical-pair-queue (selection-strategy)
|
---|
92 | ((pq :initform nil :initarg :pq :accessor pq :type priority-queue)))
|
---|
93 |
|
---|
94 | (defmethod initialize-instance :after ((self critical-pair-queue) &key (poly-list nil) (start 1))
|
---|
95 | "Initializes the priority queue SELF of critical pairs, where POLY-LIST is the initial list of polynomials.
|
---|
96 | and START is the first position beyond the elements which form a
|
---|
97 | partial Grobner basis, i.e. satisfy the Buchberger criterion."
|
---|
98 | (with-accessors ((pair-key-fn pair-key-fn)
|
---|
99 | (pair-order-fn pair-order-fn)
|
---|
100 | (pq pq))
|
---|
101 | self
|
---|
102 | (setf pq (make-priority-queue
|
---|
103 | :element-type 'critical-pair
|
---|
104 | :element-key #'(lambda (pair) (funcall pair-key-fn (critical-pair-first pair) (critical-pair-second pair)))
|
---|
105 | :test pair-order-fn))
|
---|
106 | ;; Add critical pairs for polynomials in POLY-LIST
|
---|
107 | (let* ((s (1- (length poly-list)))
|
---|
108 | (b (nconc (makelist (make-instance 'critical-pair :first (elt poly-list i) :second (elt poly-list j))
|
---|
109 | (i 0 (1- start)) (j start s))
|
---|
110 | (makelist (make-instance 'critical-pair :first (elt poly-list i) :second (elt poly-list j))
|
---|
111 | (i start (1- s)) (j (1+ i) s)))))
|
---|
112 | (dolist (pair b)
|
---|
113 | (priority-queue-insert pq pair)))))
|
---|
114 |
|
---|
115 | (defmethod print-object ((self critical-pair-queue) stream)
|
---|
116 | (print-unreadable-object (self stream :type t :identity t)
|
---|
117 | (with-accessors ((pair-key-fn pair-key-fn)
|
---|
118 | (pair-order-fn pair-order-fn)
|
---|
119 | (pq pq))
|
---|
120 | self
|
---|
121 | (format stream "PAIR-KEY-FN=~A PAIR-ORDER-FN=~A PQ=~A"
|
---|
122 | pair-key-fn pair-order-fn pq))))
|
---|
123 |
|
---|
124 |
|
---|
125 |
|
---|
126 | (defgeneric enqueue (self object)
|
---|
127 | (:documentation "Insert an object OBJECT into a queue SELF.")
|
---|
128 | (:method ((self critical-pair-queue) (pair critical-pair))
|
---|
129 | (with-slots (pq)
|
---|
130 | self
|
---|
131 | (priority-queue-insert pq pair))))
|
---|
132 |
|
---|
133 | (defgeneric dequeue (self)
|
---|
134 | (:documentation "Remove an object from a queue SELF. Returns the removed object.")
|
---|
135 | (:method ((self critical-pair-queue))
|
---|
136 | (with-slots (pq)
|
---|
137 | self
|
---|
138 | (priority-queue-remove pq))))
|
---|
139 |
|
---|
140 | (defgeneric queue-size (self)
|
---|
141 | (:documentation "Returns the number of elements in the queue SELF.")
|
---|
142 | (:method ((self critical-pair-queue))
|
---|
143 | (with-slots (pq)
|
---|
144 | self
|
---|
145 | (priority-queue-size pq))))
|
---|
146 |
|
---|
147 | (defgeneric queue-empty-p (self)
|
---|
148 | (:documentation "Returns T if the queue SELF is empty, NIL otherwise.")
|
---|
149 | (:method (self)
|
---|
150 | (with-slots (pq)
|
---|
151 | self
|
---|
152 | (priority-queue-empty-p pq))))
|
---|
153 |
|
---|