[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"
|
---|
[449] | 28 | "PAIR-QUEUE-INSERT"
|
---|
| 29 | "PAIR-QUEUE-REMOVE"
|
---|
| 30 | "PAIR-QUEUE-SIZE"
|
---|
| 31 | "PAIR-QUEUE-EMPTY-P"
|
---|
| 32 | "SET-PAIR-HEURISTIC"
|
---|
[3918] | 33 | )
|
---|
| 34 | (:documentation "Critical pair queue implementation. The pair queue is a list of critical
|
---|
| 35 | pairs, ordered by some partial order. Pair queue is a kind of priority queue.")
|
---|
| 36 | )
|
---|
[449] | 37 |
|
---|
[450] | 38 | (in-package :pair-queue)
|
---|
| 39 |
|
---|
[3919] | 40 | (defclass critical-pair ()
|
---|
[3921] | 41 | ((first :initform nil :initarg :first :accessor critical-pair-first :type poly)
|
---|
| 42 | (second :initform nil :initarg :second :accessor critical-pair-second :type poly))
|
---|
[3930] | 43 | (:documentation "Represents a critical pair, i.e. a pair of two polynomials. The derived
|
---|
| 44 | classes may add extra data used in computing the order of critical pairs."))
|
---|
[3919] | 45 |
|
---|
[3921] | 46 | (defmethod print-object ((self critical-pair) stream)
|
---|
[3920] | 47 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 48 | (with-accessors ((first critical-pair-first)
|
---|
| 49 | (second critical-pair-second))
|
---|
| 50 | self
|
---|
| 51 | (format stream "FIRST=~A SECOND=~A"
|
---|
| 52 | first second))))
|
---|
| 53 |
|
---|
[3927] | 54 | (defclass selection-strategy ()
|
---|
[3944] | 55 | ((pair-key-fn :initform #'(lambda (p q) (universal-lcm (leading-monomial p) (leading-monomial q))) :initarg :pair-key-fn :accessor pair-key-fn)
|
---|
| 56 | (pair-order-fn :initform #'lex> :initarg :pair-order-fn :accessor pair-order-fn))
|
---|
[3940] | 57 | (:documentation "Represents the normal critical pair selection
|
---|
| 58 | strategy. The two ingredients of a strategy is a function
|
---|
| 59 | PAIR-KEY-FUNCTION which computes a key from the critical pair, which
|
---|
| 60 | can be any type of data, and a function PAIR-ORDER-FN used to compaire
|
---|
| 61 | the calculated keys to determine which pair is more promising and
|
---|
[3941] | 62 | should be considered first. The normal selection strategy for a given
|
---|
| 63 | monomial order PAIR-ORDER-FN consists in selecting the pair with the
|
---|
| 64 | minimal LCM of leading monomials first."))
|
---|
[60] | 65 |
|
---|
[3932] | 66 | (defmethod print-object ((self selection-strategy) stream)
|
---|
| 67 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 68 | (with-accessors ((pair-key-fn pair-key-fn)
|
---|
| 69 | (pair-order-fn pair-order-fn))
|
---|
| 70 | self
|
---|
| 71 | (format stream "PAIR-KEY-FN=~A PAIR-ORDER-FN=~A"
|
---|
| 72 | pair-key-fn pair-order-fn))))
|
---|
| 73 |
|
---|
[3933] | 74 | (defclass min-total-degree-strategy (selection-strategy)
|
---|
| 75 | ((pair-key-fn :initform #'(lambda (p q) (total-degree (universal-lcm (leading-monomial p) (leading-monomial q)))))
|
---|
[3943] | 76 | (pair-order-fn :initform #'<))
|
---|
[3933] | 77 | (:documentation "Make a selection strategy where a pair with a
|
---|
| 78 | minimum total degree of LCM of leading monomials is selected."))
|
---|
[3932] | 79 |
|
---|
[3933] | 80 | (defclass minimal-length-strategy (selection-strategy)
|
---|
[3935] | 81 | ((pair-key-fn :initform #'(lambda (p q) (+ (poly-length p) (poly-length q))))
|
---|
[3942] | 82 | (pair-order-fn :initform #'<))
|
---|
[3933] | 83 | (:documentation "Make a selection strategy where a pair with the minimum combined length of both
|
---|
| 84 | polynomials is selected."))
|
---|
| 85 |
|
---|
[3938] | 86 | (defclass critical-pair-queue (selection-strategy)
|
---|
[3940] | 87 | ((pq :initform nil :initarg :pq :accessor pq :type priority-queue)))
|
---|
[3927] | 88 |
|
---|
[3951] | 89 | (defmethod initialize-instance :after ((self critical-pair-queue) &key (poly-list nil) (start 1))
|
---|
| 90 | "Initializes the priority for critical pairs. POLY-LIST is the initial list of polynomials.
|
---|
[3947] | 91 | START is the first position beyond the elements which form a partial
|
---|
| 92 | grobner basis, i.e. satisfy the Buchberger criterion."
|
---|
[3940] | 93 | (with-accessors ((pair-key-fn pair-key-fn)
|
---|
| 94 | (pair-order-fn pair-order-fn)
|
---|
| 95 | (pq pq))
|
---|
[3941] | 96 | self
|
---|
| 97 | (setf pq (make-priority-queue
|
---|
[3939] | 98 | :element-type 'critical-pair
|
---|
| 99 | :element-key #'(lambda (pair) (funcall pair-key-fn (critical-pair-first pair) (critical-pair-second pair)))
|
---|
[3945] | 100 | :test pair-order-fn))
|
---|
[3955] | 101 | ;; Add critical pairs for polynomials in POLY-LIST
|
---|
[3952] | 102 | (let* ((s (1- (length poly-list)))
|
---|
| 103 | (b (nconc (makelist (make-instance 'critical-pair :first (elt poly-list i) :second (elt poly-list j))
|
---|
[3945] | 104 | (i 0 (1- start)) (j start s))
|
---|
[3952] | 105 | (makelist (make-instance 'critical-pair :first (elt poly-list i) :second (elt poly-list j))
|
---|
[3945] | 106 | (i start (1- s)) (j (1+ i) s)))))
|
---|
[3954] | 107 | (dolist (pair b)
|
---|
[3945] | 108 | (priority-queue-insert pq pair)))
|
---|
| 109 | ))
|
---|
[3938] | 110 |
|
---|
[3944] | 111 | (defmethod print-object ((self critical-pair-queue) stream)
|
---|
| 112 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 113 | (with-accessors ((pair-key-fn pair-key-fn)
|
---|
| 114 | (pair-order-fn pair-order-fn)
|
---|
| 115 | (pq pq))
|
---|
| 116 | self
|
---|
| 117 | (format stream "PAIR-KEY-FN=~A PAIR-ORDER-FN=~A PQ=~A"
|
---|
| 118 | pair-key-fn pair-order-fn pq))))
|
---|
| 119 |
|
---|
| 120 |
|
---|
[60] | 121 |
|
---|
| 122 | (defun pair-queue-insert (b pair)
|
---|
| 123 | (priority-queue-insert b pair))
|
---|
| 124 |
|
---|
| 125 | (defun pair-queue-remove (b)
|
---|
| 126 | (priority-queue-remove b))
|
---|
| 127 |
|
---|
| 128 | (defun pair-queue-size (b)
|
---|
| 129 | (priority-queue-size b))
|
---|
| 130 |
|
---|
| 131 | (defun pair-queue-empty-p (b)
|
---|
| 132 | (priority-queue-empty-p b))
|
---|
[162] | 133 |
|
---|
[3922] | 134 |
|
---|
[162] | 135 |
|
---|
[3923] | 136 |
|
---|