[1201] | 1 | ;;; -*- Mode: Lisp -*-
|
---|
[76] | 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 |
|
---|
[449] | 22 | (defpackage "PAIR-QUEUE"
|
---|
[3949] | 23 | (:use :cl :priority-queue :monom :polynomial :symbolic-polynomial :utils)
|
---|
[3920] | 24 | (:export "CRITICAL-PAIR"
|
---|
| 25 | "CRITICAL-PAIR-FIRST"
|
---|
| 26 | "CRITICAL-PAIR-SECOND"
|
---|
[3948] | 27 | "CRITICAL-PAIR-QUEUE"
|
---|
[3964] | 28 | "SELECTION-STRATEGY"
|
---|
| 29 | "MIN-TOTAL-DEGREE-STRATEGY"
|
---|
[3965] | 30 | "MIN-COMBINED-LENGTH-STRATEGY"
|
---|
[3963] | 31 | "ENQUEUE"
|
---|
| 32 | "DEQUEUE"
|
---|
| 33 | "QUEUE-SIZE"
|
---|
| 34 | "QUEUE-EMPTY-P"
|
---|
[3918] | 35 | )
|
---|
| 36 | (:documentation "Critical pair queue implementation. The pair queue is a list of critical
|
---|
| 37 | pairs, ordered by some partial order. Pair queue is a kind of priority queue.")
|
---|
| 38 | )
|
---|
[449] | 39 |
|
---|
[450] | 40 | (in-package :pair-queue)
|
---|
| 41 |
|
---|
[3919] | 42 | (defclass critical-pair ()
|
---|
[3921] | 43 | ((first :initform nil :initarg :first :accessor critical-pair-first :type poly)
|
---|
| 44 | (second :initform nil :initarg :second :accessor critical-pair-second :type poly))
|
---|
[3930] | 45 | (:documentation "Represents a critical pair, i.e. a pair of two polynomials. The derived
|
---|
| 46 | classes may add extra data used in computing the order of critical pairs."))
|
---|
[3919] | 47 |
|
---|
[3921] | 48 | (defmethod print-object ((self critical-pair) stream)
|
---|
[3920] | 49 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 50 | (with-accessors ((first critical-pair-first)
|
---|
| 51 | (second critical-pair-second))
|
---|
| 52 | self
|
---|
| 53 | (format stream "FIRST=~A SECOND=~A"
|
---|
| 54 | first second))))
|
---|
| 55 |
|
---|
[3927] | 56 | (defclass selection-strategy ()
|
---|
[3959] | 57 | ((pair-key-fn :initform #'(lambda (p q) (universal-lcm (leading-monomial p) (leading-monomial q)))
|
---|
| 58 | :initarg :pair-key-fn
|
---|
| 59 | :accessor pair-key-fn)
|
---|
| 60 | (pair-order-fn :initform #'lex>
|
---|
| 61 | :initarg :pair-order-fn
|
---|
| 62 | :accessor pair-order-fn))
|
---|
[3940] | 63 | (:documentation "Represents the normal critical pair selection
|
---|
| 64 | strategy. The two ingredients of a strategy is a function
|
---|
| 65 | PAIR-KEY-FUNCTION which computes a key from the critical pair, which
|
---|
| 66 | can be any type of data, and a function PAIR-ORDER-FN used to compaire
|
---|
| 67 | the calculated keys to determine which pair is more promising and
|
---|
[3941] | 68 | should be considered first. The normal selection strategy for a given
|
---|
| 69 | monomial order PAIR-ORDER-FN consists in selecting the pair with the
|
---|
| 70 | minimal LCM of leading monomials first."))
|
---|
[60] | 71 |
|
---|
[3932] | 72 | (defmethod print-object ((self selection-strategy) stream)
|
---|
| 73 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 74 | (with-accessors ((pair-key-fn pair-key-fn)
|
---|
| 75 | (pair-order-fn pair-order-fn))
|
---|
| 76 | self
|
---|
| 77 | (format stream "PAIR-KEY-FN=~A PAIR-ORDER-FN=~A"
|
---|
| 78 | pair-key-fn pair-order-fn))))
|
---|
| 79 |
|
---|
[3933] | 80 | (defclass min-total-degree-strategy (selection-strategy)
|
---|
| 81 | ((pair-key-fn :initform #'(lambda (p q) (total-degree (universal-lcm (leading-monomial p) (leading-monomial q)))))
|
---|
[3943] | 82 | (pair-order-fn :initform #'<))
|
---|
[3933] | 83 | (:documentation "Make a selection strategy where a pair with a
|
---|
| 84 | minimum total degree of LCM of leading monomials is selected."))
|
---|
[3932] | 85 |
|
---|
[3964] | 86 | (defclass min-combined-length-strategy (selection-strategy)
|
---|
[3935] | 87 | ((pair-key-fn :initform #'(lambda (p q) (+ (poly-length p) (poly-length q))))
|
---|
[3942] | 88 | (pair-order-fn :initform #'<))
|
---|
[3933] | 89 | (:documentation "Make a selection strategy where a pair with the minimum combined length of both
|
---|
| 90 | polynomials is selected."))
|
---|
| 91 |
|
---|
[3938] | 92 | (defclass critical-pair-queue (selection-strategy)
|
---|
[3972] | 93 | ((pq :initform nil :initarg :pq :accessor pq :type priority-queue))
|
---|
| 94 | (:documentation "Represents the priority queue of critical pairs. It is a subclass
|
---|
| 95 | of SELECTION-STRATEGY."))
|
---|
[3927] | 96 |
|
---|
[3951] | 97 | (defmethod initialize-instance :after ((self critical-pair-queue) &key (poly-list nil) (start 1))
|
---|
[3958] | 98 | "Initializes the priority queue SELF of critical pairs, where POLY-LIST is the initial list of polynomials.
|
---|
| 99 | and START is the first position beyond the elements which form a
|
---|
| 100 | partial Grobner basis, i.e. satisfy the Buchberger criterion."
|
---|
[3940] | 101 | (with-accessors ((pair-key-fn pair-key-fn)
|
---|
| 102 | (pair-order-fn pair-order-fn)
|
---|
| 103 | (pq pq))
|
---|
[3941] | 104 | self
|
---|
| 105 | (setf pq (make-priority-queue
|
---|
[3939] | 106 | :element-type 'critical-pair
|
---|
| 107 | :element-key #'(lambda (pair) (funcall pair-key-fn (critical-pair-first pair) (critical-pair-second pair)))
|
---|
[3945] | 108 | :test pair-order-fn))
|
---|
[3955] | 109 | ;; Add critical pairs for polynomials in POLY-LIST
|
---|
[3952] | 110 | (let* ((s (1- (length poly-list)))
|
---|
| 111 | (b (nconc (makelist (make-instance 'critical-pair :first (elt poly-list i) :second (elt poly-list j))
|
---|
[3945] | 112 | (i 0 (1- start)) (j start s))
|
---|
[3952] | 113 | (makelist (make-instance 'critical-pair :first (elt poly-list i) :second (elt poly-list j))
|
---|
[3945] | 114 | (i start (1- s)) (j (1+ i) s)))))
|
---|
[3954] | 115 | (dolist (pair b)
|
---|
[3956] | 116 | (priority-queue-insert pq pair)))))
|
---|
[3938] | 117 |
|
---|
[3944] | 118 | (defmethod print-object ((self critical-pair-queue) stream)
|
---|
| 119 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 120 | (with-accessors ((pair-key-fn pair-key-fn)
|
---|
| 121 | (pair-order-fn pair-order-fn)
|
---|
| 122 | (pq pq))
|
---|
| 123 | self
|
---|
| 124 | (format stream "PAIR-KEY-FN=~A PAIR-ORDER-FN=~A PQ=~A"
|
---|
| 125 | pair-key-fn pair-order-fn pq))))
|
---|
| 126 |
|
---|
[3960] | 127 | (defgeneric enqueue (self object)
|
---|
[3961] | 128 | (:documentation "Insert an object OBJECT into a queue SELF.")
|
---|
[3960] | 129 | (:method ((self critical-pair-queue) (pair critical-pair))
|
---|
| 130 | (with-slots (pq)
|
---|
| 131 | self
|
---|
| 132 | (priority-queue-insert pq pair))))
|
---|
[60] | 133 |
|
---|
[3960] | 134 | (defgeneric dequeue (self)
|
---|
[3961] | 135 | (:documentation "Remove an object from a queue SELF. Returns the removed object.")
|
---|
[3960] | 136 | (:method ((self critical-pair-queue))
|
---|
| 137 | (with-slots (pq)
|
---|
| 138 | self
|
---|
| 139 | (priority-queue-remove pq))))
|
---|
[60] | 140 |
|
---|
[3960] | 141 | (defgeneric queue-size (self)
|
---|
[3961] | 142 | (:documentation "Returns the number of elements in the queue SELF.")
|
---|
[3960] | 143 | (:method ((self critical-pair-queue))
|
---|
| 144 | (with-slots (pq)
|
---|
| 145 | self
|
---|
| 146 | (priority-queue-size pq))))
|
---|
[60] | 147 |
|
---|
[3960] | 148 | (defgeneric queue-empty-p (self)
|
---|
[3962] | 149 | (:documentation "Returns T if the queue SELF is empty, NIL otherwise.")
|
---|
[3967] | 150 | (:method ((self critical-pair-queue))
|
---|
[3960] | 151 | (with-slots (pq)
|
---|
| 152 | self
|
---|
| 153 | (priority-queue-empty-p pq))))
|
---|
[162] | 154 |
|
---|