| 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 :utils)
 | 
|---|
| 24 |   (:export "CRITICAL-PAIR"
 | 
|---|
| 25 |            "CRITICAL-PAIR-FIRST"
 | 
|---|
| 26 |            "CRITICAL-PAIR-SECOND"
 | 
|---|
| 27 |            "MAKE-PAIR-QUEUE"
 | 
|---|
| 28 |            "PAIR-QUEUE-INITIALIZE"
 | 
|---|
| 29 |            "PAIR-QUEUE-INSERT"
 | 
|---|
| 30 |            "PAIR-QUEUE-REMOVE"
 | 
|---|
| 31 |            "PAIR-QUEUE-SIZE"
 | 
|---|
| 32 |            "PAIR-QUEUE-EMPTY-P"
 | 
|---|
| 33 |            "SET-PAIR-HEURISTIC"
 | 
|---|
| 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."))
 | 
|---|
| 45 | 
 | 
|---|
| 46 | (defmethod print-object ((self critical-pair) stream)
 | 
|---|
| 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 | 
 | 
|---|
| 54 | (defun make-pair-queue (pair-key-fn pair-order-fn)
 | 
|---|
| 55 |   "Constructs a priority queue for critical pairs."
 | 
|---|
| 56 |   (make-priority-queue
 | 
|---|
| 57 |    :element-type 'critical-pair
 | 
|---|
| 58 |    :element-key #'(lambda (pair) (funcall pair-key-fn (critical-pair-first pair) (critical-pair-second pair)))
 | 
|---|
| 59 |    :test pair-order-fn))
 | 
|---|
| 60 | 
 | 
|---|
| 61 | (defun pair-queue-initialize (pq f start
 | 
|---|
| 62 |                               &aux
 | 
|---|
| 63 |                               (s (1- (length f)))
 | 
|---|
| 64 |                               (b (nconc (makelist (make-instance 'critical-pair :first (elt f i) :second (elt f j))
 | 
|---|
| 65 |                                                  (i 0 (1- start)) (j start s))
 | 
|---|
| 66 |                                         (makelist (make-instance 'critical-pair :first (elt f i) :second (elt f j))
 | 
|---|
| 67 |                                                  (i start (1- s)) (j (1+ i) s)))))
 | 
|---|
| 68 |   "Initializes the priority for critical pairs. F is the initial list of polynomials.
 | 
|---|
| 69 | START is the first position beyond the elements which form a partial
 | 
|---|
| 70 | grobner basis, i.e. satisfy the Buchberger criterion."
 | 
|---|
| 71 |   (declare (type priority-queue pq) (type fixnum start))
 | 
|---|
| 72 |   (dolist (pair b pq)
 | 
|---|
| 73 |     (priority-queue-insert pq pair)))
 | 
|---|
| 74 | 
 | 
|---|
| 75 | (defun pair-queue-insert (b pair)
 | 
|---|
| 76 |   (priority-queue-insert b pair))
 | 
|---|
| 77 | 
 | 
|---|
| 78 | (defun pair-queue-remove (b)
 | 
|---|
| 79 |   (priority-queue-remove b))
 | 
|---|
| 80 | 
 | 
|---|
| 81 | (defun pair-queue-size (b)
 | 
|---|
| 82 |   (priority-queue-size b))
 | 
|---|
| 83 | 
 | 
|---|
| 84 | (defun pair-queue-empty-p (b)
 | 
|---|
| 85 |   (priority-queue-empty-p b))
 | 
|---|
| 86 | 
 | 
|---|
| 87 | (defclass selection-strategy ()
 | 
|---|
| 88 |   ((pair-key-fn   :initform nil :initarg :pair-key-fn :accessor pair-key-fn)
 | 
|---|
| 89 |    (pair-order-fn :initform nil :initarg :pair-order-fn :accessor pair-order-fn))
 | 
|---|
| 90 |   (:documentation "Sets up variables *PAIR-KEY-FUNCTION* and *PAIR-ORDER* used
 | 
|---|
| 91 | to determine the priority of critical pairs in the priority queue."))
 | 
|---|
| 92 | 
 | 
|---|
| 93 | (defun make-normal-strategy (monomial-order-fn)
 | 
|---|
| 94 |   "Make a normal selection strategy for a given monomial order MONOMIAL-ORDER-FN. A pair
 | 
|---|
| 95 | with the minimal LCM of leading monomials is selected."
 | 
|---|
| 96 |   (make-instance 'selection-strategy
 | 
|---|
| 97 |                  :pair-key-fn #'(lambda (p q) (universal-lcm (leading-monomial p) (leading-monomial q)))
 | 
|---|
| 98 |                  :pair-order-fn monomial-order-fn))
 | 
|---|
| 99 | 
 | 
|---|
| 100 | (defun make-min-total-degree-strategy ()
 | 
|---|
| 101 |   "Make a selection strategy where a pair with a minimum total degree of LCM 
 | 
|---|
| 102 | of leading monomials is selected."
 | 
|---|
| 103 |   (make-instance 'selection-strategy
 | 
|---|
| 104 |                  :pair-key-fn #'(lambda (p q) (total-degree (universal-lcm (leading-monomial p) (leading-monomial q))))
 | 
|---|
| 105 |                  :pair-order* #'<))
 | 
|---|
| 106 | 
 | 
|---|
| 107 | (defun make-minimal-length-strategy ()
 | 
|---|
| 108 |   "Make a selection strategy where a pair with the minimum combined length of both
 | 
|---|
| 109 | polynomials is selected."
 | 
|---|
| 110 |   (make-instance 'selection-strategy
 | 
|---|
| 111 |                  :pair-key-fn #'(lambda (p q) (+ (poly-length p) (poly-length q)))
 | 
|---|
| 112 |                  :pair-order #'<))
 | 
|---|
| 113 | 
 | 
|---|