Skip to content

Commit c9aec4e

Browse files
committed
saner BEST-VALUE
If we have an evaluation that doesn't produce any upper bound for the objective, keep the previous best value and bound instead of dropping them.
1 parent a704a1c commit c9aec4e

File tree

1 file changed

+31
-34
lines changed

1 file changed

+31
-34
lines changed

screamer.lisp

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7134,48 +7134,45 @@ sufficient hooks for the user to define her own force functions.)"
71347134
(reorder-internal
71357135
variables cost-function terminate? order force-function))))
71367136

7137-
;;; FIXME: This doesn't make any sense. See branch "maybe" for an alternative
7138-
;;; expression. Also: why are we trying to increase the upper bound, and not
7139-
;;; the lower bound? Should the API also not allow us to minimize a variable
7140-
;;; towards either zero or negative infinity? --ns 2011-11-01
7137+
;;; FIXME: What about optimizing towards negative infinity or zero? What
7138+
;;; about restricting the low bound? What are the real applications for this?
7139+
;;; What about evaluations that provide eg. a low bound higher than the
7140+
;;; previous high bound, but no high bound?
71417141
(defmacro-compile-time best-value
7142-
(form1 objective-form &optional (form2 nil form2?))
7143-
"First evaluates OBJECTIVE-FORM, which should evaluate to constraint variable V.
7144-
7145-
Then repeatedly evaluates FORM1 in non-deterministic context till it fails. If
7146-
previous round of evaluation produced an upper bound B for V, the during the
7147-
next round any change to V must provide an upper bound higher than B, or that
7148-
that change fails.
7149-
7150-
If the last successful evaluation of FORM produced an upper bound for V,
7151-
returns a list of two elements: the the primary value of FORM1 from that
7152-
round, and the upper bound of V.
7153-
7154-
Otherwise if FORM2 is provided, returns the result of evaluating it, or else
7155-
calls fails.
7156-
7157-
Note: this documentation string is entirely reverse-engineered. Lacking
7158-
information on just how BEST-VALUE was intended to work, it is hard to tell
7159-
what is a bug, an accident of implementation, and what is a feature. If you
7160-
have any insight into BEST-VALUE, please send email to
7161-
nikodemus@random-state.net."
7162-
(let ((bound (gensym "BOUND-"))
7163-
(best (gensym "BEST-"))
7164-
(objective (gensym "OBJECTIVE-")))
7142+
(form objective &optional (default '(fail)))
7143+
"Evaluates OBJECTIVE, which should evaluate to real-valued
7144+
constraint variable V.
7145+
7146+
Then repeatedly evaluates FORM in non-deterministic context till it
7147+
fails. Once a successful evaluation has produced an upper bound for V,
7148+
any subsequent evaluation of FORM that restricts the upper bound of V
7149+
to less than or equal the previous upper bound fails.
7150+
7151+
If any evaluation produced an upper bound for V, returns a list of two
7152+
elements: the the primary value of FORM from the evaluation where
7153+
upper bound of V reached its maximum, and that upper bound.
7154+
7155+
Otherwise evaluates DEFAULT -- defaulting to FAIL."
7156+
(let ((bound (gensym "BOUND"))
7157+
(best (gensym "BEST"))
7158+
(objectivev (gensym "OBJECTIVE")))
71657159
`(let ((,bound nil)
71667160
(,best nil)
7167-
(,objective (variablize ,objective-form)))
7161+
(,objectivev (variablize ,objective)))
71687162
(attach-noticer!
71697163
#'(lambda ()
7170-
(let ((upper (variable-upper-bound ,objective)))
7164+
(let ((upper (variable-upper-bound ,objectivev)))
71717165
(when (and ,bound upper (<= upper ,bound))
71727166
(fail))))
7173-
,objective)
7167+
,objectivev)
71747168
(for-effects
7175-
(let ((value ,form1))
7176-
(global (setf ,bound (variable-upper-bound ,objective))
7177-
(setf ,best value))))
7178-
(if ,bound (list ,best ,bound) ,(if form2? form2 '(fail))))))
7169+
(let ((value ,form)
7170+
(tmp (variable-upper-bound ,objectivev)))
7171+
(when tmp
7172+
(global
7173+
(setf ,bound tmp)
7174+
(setf ,best value)))))
7175+
(if ,bound (list ,best ,bound) ,default))))
71797176

71807177
(defun template-internal (template variables)
71817178
(cond

0 commit comments

Comments
 (0)