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, 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))) :initarg :pair-key-fn :accessor pair-key-fn)
|
---|
57 | (pair-order-fn :initform #'lex> :initarg :pair-order-fn :accessor pair-order-fn))
|
---|
58 | (:documentation "Represents the normal critical pair selection
|
---|
59 | strategy. The two ingredients of a strategy is a function
|
---|
60 | PAIR-KEY-FUNCTION which computes a key from the critical pair, which
|
---|
61 | can be any type of data, and a function PAIR-ORDER-FN used to compaire
|
---|
62 | the calculated keys to determine which pair is more promising and
|
---|
63 | should be considered first. The normal selection strategy for a given
|
---|
64 | monomial order PAIR-ORDER-FN consists in selecting the pair with the
|
---|
65 | minimal LCM of leading monomials first."))
|
---|
66 |
|
---|
67 | (defmethod print-object ((self selection-strategy) stream)
|
---|
68 | (print-unreadable-object (self stream :type t :identity t)
|
---|
69 | (with-accessors ((pair-key-fn pair-key-fn)
|
---|
70 | (pair-order-fn pair-order-fn))
|
---|
71 | self
|
---|
72 | (format stream "PAIR-KEY-FN=~A PAIR-ORDER-FN=~A"
|
---|
73 | pair-key-fn pair-order-fn))))
|
---|
74 |
|
---|
75 | (defclass min-total-degree-strategy (selection-strategy)
|
---|
76 | ((pair-key-fn :initform #'(lambda (p q) (total-degree (universal-lcm (leading-monomial p) (leading-monomial q)))))
|
---|
77 | (pair-order-fn :initform #'<))
|
---|
78 | (:documentation "Make a selection strategy where a pair with a
|
---|
79 | minimum total degree of LCM of leading monomials is selected."))
|
---|
80 |
|
---|
81 | (defclass minimal-length-strategy (selection-strategy)
|
---|
82 | ((pair-key-fn :initform #'(lambda (p q) (+ (poly-length p) (poly-length q))))
|
---|
83 | (pair-order-fn :initform #'<))
|
---|
84 | (:documentation "Make a selection strategy where a pair with the minimum combined length of both
|
---|
85 | polynomials is selected."))
|
---|
86 |
|
---|
87 | (defclass critical-pair-queue (selection-strategy)
|
---|
88 | ((pq :initform nil :initarg :pq :accessor pq :type priority-queue)))
|
---|
89 |
|
---|
90 | (defmethod initialize-instance :after ((self critical-pair-queue) &key (f nil) (start 1))
|
---|
91 | "Initializes the priority for critical pairs. F is the initial list of polynomials.
|
---|
92 | START is the first position beyond the elements which form a partial
|
---|
93 | grobner basis, i.e. satisfy the Buchberger criterion."
|
---|
94 | (with-accessors ((pair-key-fn pair-key-fn)
|
---|
95 | (pair-order-fn pair-order-fn)
|
---|
96 | (pq pq))
|
---|
97 | self
|
---|
98 | (setf pq (make-priority-queue
|
---|
99 | :element-type 'critical-pair
|
---|
100 | :element-key #'(lambda (pair) (funcall pair-key-fn (critical-pair-first pair) (critical-pair-second pair)))
|
---|
101 | :test pair-order-fn))
|
---|
102 | (let* ((s (1- (length f)))
|
---|
103 | (b (nconc (makelist (make-instance 'critical-pair :first (elt f i) :second (elt f j))
|
---|
104 | (i 0 (1- start)) (j start s))
|
---|
105 | (makelist (make-instance 'critical-pair :first (elt f i) :second (elt f j))
|
---|
106 | (i start (1- s)) (j (1+ i) s)))))
|
---|
107 | (dolist (pair b pq)
|
---|
108 | (priority-queue-insert pq pair)))
|
---|
109 | ))
|
---|
110 |
|
---|
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 |
|
---|
121 | (defun pair-queue-initialize (pq f start
|
---|
122 | &aux
|
---|
123 | (s (1- (length f)))
|
---|
124 | (b (nconc (makelist (make-instance 'critical-pair :first (elt f i) :second (elt f j))
|
---|
125 | (i 0 (1- start)) (j start s))
|
---|
126 | (makelist (make-instance 'critical-pair :first (elt f i) :second (elt f j))
|
---|
127 | (i start (1- s)) (j (1+ i) s)))))
|
---|
128 | "Initializes the priority for critical pairs. F is the initial list of polynomials.
|
---|
129 | START is the first position beyond the elements which form a partial
|
---|
130 | grobner basis, i.e. satisfy the Buchberger criterion."
|
---|
131 | (declare (type priority-queue pq) (type fixnum start))
|
---|
132 | (dolist (pair b pq)
|
---|
133 | (priority-queue-insert pq pair)))
|
---|
134 |
|
---|
135 | (defun pair-queue-insert (b pair)
|
---|
136 | (priority-queue-insert b pair))
|
---|
137 |
|
---|
138 | (defun pair-queue-remove (b)
|
---|
139 | (priority-queue-remove b))
|
---|
140 |
|
---|
141 | (defun pair-queue-size (b)
|
---|
142 | (priority-queue-size b))
|
---|
143 |
|
---|
144 | (defun pair-queue-empty-p (b)
|
---|
145 | (priority-queue-empty-p b))
|
---|
146 |
|
---|
147 |
|
---|
148 |
|
---|
149 |
|
---|