close Warning: Can't synchronize with repository "(default)" (The repository directory has changed, you should resynchronize the repository with: trac-admin $ENV repository resync '(default)'). Look in the Trac log for more information.

source: branches/f4grobner/pair-queue.lisp@ 3962

Last change on this file since 3962 was 3962, checked in by Marek Rychlik, 9 years ago

* empty log message *

File size: 6.3 KB
Line 
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 "PAIR-QUEUE-INSERT"
29 "PAIR-QUEUE-REMOVE"
30 "PAIR-QUEUE-SIZE"
31 "PAIR-QUEUE-EMPTY-P"
32 "SET-PAIR-HEURISTIC"
33 )
34 (:documentation "Critical pair queue implementation. The pair queue is a list of critical
35pairs, ordered by some partial order. Pair queue is a kind of priority queue.")
36 )
37
38(in-package :pair-queue)
39
40(defclass critical-pair ()
41 ((first :initform nil :initarg :first :accessor critical-pair-first :type poly)
42 (second :initform nil :initarg :second :accessor critical-pair-second :type poly))
43 (:documentation "Represents a critical pair, i.e. a pair of two polynomials. The derived
44classes may add extra data used in computing the order of critical pairs."))
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(defclass selection-strategy ()
55 ((pair-key-fn :initform #'(lambda (p q) (universal-lcm (leading-monomial p) (leading-monomial q)))
56 :initarg :pair-key-fn
57 :accessor pair-key-fn)
58 (pair-order-fn :initform #'lex>
59 :initarg :pair-order-fn
60 :accessor pair-order-fn))
61 (:documentation "Represents the normal critical pair selection
62strategy. The two ingredients of a strategy is a function
63PAIR-KEY-FUNCTION which computes a key from the critical pair, which
64can be any type of data, and a function PAIR-ORDER-FN used to compaire
65the calculated keys to determine which pair is more promising and
66should be considered first. The normal selection strategy for a given
67monomial order PAIR-ORDER-FN consists in selecting the pair with the
68minimal LCM of leading monomials first."))
69
70(defmethod print-object ((self selection-strategy) stream)
71 (print-unreadable-object (self stream :type t :identity t)
72 (with-accessors ((pair-key-fn pair-key-fn)
73 (pair-order-fn pair-order-fn))
74 self
75 (format stream "PAIR-KEY-FN=~A PAIR-ORDER-FN=~A"
76 pair-key-fn pair-order-fn))))
77
78(defclass min-total-degree-strategy (selection-strategy)
79 ((pair-key-fn :initform #'(lambda (p q) (total-degree (universal-lcm (leading-monomial p) (leading-monomial q)))))
80 (pair-order-fn :initform #'<))
81 (:documentation "Make a selection strategy where a pair with a
82minimum total degree of LCM of leading monomials is selected."))
83
84(defclass minimal-length-strategy (selection-strategy)
85 ((pair-key-fn :initform #'(lambda (p q) (+ (poly-length p) (poly-length q))))
86 (pair-order-fn :initform #'<))
87 (:documentation "Make a selection strategy where a pair with the minimum combined length of both
88polynomials is selected."))
89
90(defclass critical-pair-queue (selection-strategy)
91 ((pq :initform nil :initarg :pq :accessor pq :type priority-queue)))
92
93(defmethod initialize-instance :after ((self critical-pair-queue) &key (poly-list nil) (start 1))
94 "Initializes the priority queue SELF of critical pairs, where POLY-LIST is the initial list of polynomials.
95and START is the first position beyond the elements which form a
96partial Grobner basis, i.e. satisfy the Buchberger criterion."
97 (with-accessors ((pair-key-fn pair-key-fn)
98 (pair-order-fn pair-order-fn)
99 (pq pq))
100 self
101 (setf pq (make-priority-queue
102 :element-type 'critical-pair
103 :element-key #'(lambda (pair) (funcall pair-key-fn (critical-pair-first pair) (critical-pair-second pair)))
104 :test pair-order-fn))
105 ;; Add critical pairs for polynomials in POLY-LIST
106 (let* ((s (1- (length poly-list)))
107 (b (nconc (makelist (make-instance 'critical-pair :first (elt poly-list i) :second (elt poly-list j))
108 (i 0 (1- start)) (j start s))
109 (makelist (make-instance 'critical-pair :first (elt poly-list i) :second (elt poly-list j))
110 (i start (1- s)) (j (1+ i) s)))))
111 (dolist (pair b)
112 (priority-queue-insert pq pair)))))
113
114(defmethod print-object ((self critical-pair-queue) stream)
115 (print-unreadable-object (self stream :type t :identity t)
116 (with-accessors ((pair-key-fn pair-key-fn)
117 (pair-order-fn pair-order-fn)
118 (pq pq))
119 self
120 (format stream "PAIR-KEY-FN=~A PAIR-ORDER-FN=~A PQ=~A"
121 pair-key-fn pair-order-fn pq))))
122
123
124
125(defgeneric enqueue (self object)
126 (:documentation "Insert an object OBJECT into a queue SELF.")
127 (:method ((self critical-pair-queue) (pair critical-pair))
128 (with-slots (pq)
129 self
130 (priority-queue-insert pq pair))))
131
132(defgeneric dequeue (self)
133 (:documentation "Remove an object from a queue SELF. Returns the removed object.")
134 (:method ((self critical-pair-queue))
135 (with-slots (pq)
136 self
137 (priority-queue-remove pq))))
138
139(defgeneric queue-size (self)
140 (:documentation "Returns the number of elements in the queue SELF.")
141 (:method ((self critical-pair-queue))
142 (with-slots (pq)
143 self
144 (priority-queue-size pq))))
145
146(defgeneric queue-empty-p (self)
147 (:documentation "Returns T if the queue SELF is empty, NIL otherwise.")
148 (:method (self)
149 (with-slots (pq)
150 self
151 (priority-queue-empty-p pq))))
152
Note: See TracBrowser for help on using the repository browser.