|
| 1 | +(ns provisdom.solvers.common |
| 2 | + "Common utilities, specs, and anomaly categories for solvers. |
| 3 | + Consolidates shared functionality used across multiple solver implementations." |
| 4 | + (:require |
| 5 | + [clojure.spec.alpha :as s] |
| 6 | + [clojure.spec.gen.alpha :as gen] |
| 7 | + [provisdom.math.core :as m] |
| 8 | + [provisdom.math.vector :as vector])) |
| 9 | + |
| 10 | +;;;SOLVER ANOMALY CATEGORIES |
| 11 | +;; These extend the base anomaly system with solver-specific categories. |
| 12 | +;; Use these as the value for ::anomalies/category in anomaly maps. |
| 13 | +(s/def ::solver-category |
| 14 | + #{::bad-supplied-function ; user-provided function has issues (wrong arity, throws, returns invalid values) |
| 15 | + ::max-iterations-exceeded ; solver hit iteration limit without converging |
| 16 | + ::not-bracketed ; root not bracketed (for bracketing root solvers) |
| 17 | + ::singular-matrix ; matrix is singular or ill-conditioned |
| 18 | + ::out-of-bounds ; query point outside valid interpolation range |
| 19 | + ::diverged ; algorithm diverged (rounding errors, etc.) |
| 20 | + ::invalid-input}) ; input violates solver requirements |
| 21 | + |
| 22 | +;;;CONSTANTS |
| 23 | +(def accu-precision |
| 24 | + "Minimum precision for accuracy parameters. Values below this are treated as zero by some solvers |
| 25 | + (e.g., Hipparchus)." |
| 26 | + 9e-16) |
| 27 | + |
| 28 | +(def mdl |
| 29 | + "Max dimension length for generators." |
| 30 | + 6) |
| 31 | + |
| 32 | +;;;ARRAY HELPERS |
| 33 | +(defn double-array? |
| 34 | + "Tests if a value is a double array." |
| 35 | + [x] |
| 36 | + (instance? (Class/forName "[D") x)) |
| 37 | + |
| 38 | +(defn ->array2D |
| 39 | + "Creates a 2D double array from nested collections." |
| 40 | + [coll2D] |
| 41 | + (into-array (Class/forName "[D") |
| 42 | + (map #(into-array Double/TYPE %) coll2D))) |
| 43 | + |
| 44 | +(defn vec->double-array |
| 45 | + "Converts a vector to a double array." |
| 46 | + [v] |
| 47 | + (double-array v)) |
| 48 | + |
| 49 | +;;;COMMON SPECS |
| 50 | +(s/def ::double-array |
| 51 | + (s/with-gen |
| 52 | + double-array? |
| 53 | + #(gen/fmap double-array (gen/vector (s/gen ::m/double) 0 mdl)))) |
| 54 | + |
| 55 | +(s/def ::max-iter |
| 56 | + (s/with-gen (s/nilable ::m/int+) |
| 57 | + #(gen/one-of [(s/gen (s/int-in 100 1000)) |
| 58 | + (gen/return nil)]))) |
| 59 | + |
| 60 | +(s/def ::max-evaluations |
| 61 | + (s/with-gen (s/nilable ::m/int+) |
| 62 | + #(gen/one-of [(s/gen (s/int-in 100 1000)) |
| 63 | + (gen/return nil)]))) |
| 64 | + |
| 65 | +(s/def ::rel-accu |
| 66 | + (s/with-gen (s/and ::m/finite+ |
| 67 | + (fn [x] (>= x accu-precision))) |
| 68 | + #(s/gen (s/double-in :min accu-precision |
| 69 | + :max 1.0 |
| 70 | + :NaN? false)))) |
| 71 | + |
| 72 | +(s/def ::abs-accu |
| 73 | + (s/with-gen (s/and ::m/finite+ |
| 74 | + (fn [x] (>= x accu-precision))) |
| 75 | + #(s/gen (s/double-in :min accu-precision |
| 76 | + :max 1.0 |
| 77 | + :NaN? false)))) |
| 78 | + |
| 79 | +(s/def ::goal #{:min :max}) |
| 80 | +(s/def ::value ::m/number) |
| 81 | +(s/def ::vector-point ::vector/vector) |
| 82 | +(s/def ::value-and-vector-point (s/keys :req [::vector-point ::value])) |
| 83 | + |
| 84 | +(s/def ::vars-guess |
| 85 | + (s/and ::vector/vector-finite |
| 86 | + (fn [v] (pos? (count v))))) |
| 87 | + |
| 88 | +(s/def ::var-lower-bounds ::vector/vector-finite) |
| 89 | +(s/def ::var-upper-bounds ::vector/vector-finite) |
| 90 | + |
| 91 | +(s/def ::check-by-objective? boolean?) |
| 92 | + |
| 93 | +;;; Function type specs - use fn? to avoid orchestra issues with fspecs |
| 94 | +(s/def ::array->number fn?) |
| 95 | +(s/def ::array->vector fn?) |
| 96 | +(s/def ::array->nilable-matrix (s/nilable fn?)) |
| 97 | + |
| 98 | +;;;INTERPOLATION-RELATED SPECS |
| 99 | +(s/def ::strictly-ascending-vector-finite |
| 100 | + (s/with-gen |
| 101 | + (s/and (s/coll-of ::m/finite |
| 102 | + :kind clojure.core/vector? |
| 103 | + :into [] |
| 104 | + :min-count 2) |
| 105 | + (fn [v] |
| 106 | + (let [dv (map double v)] |
| 107 | + (= dv (sort (distinct dv)))))) |
| 108 | + #(gen/bind |
| 109 | + (gen/vector-distinct |
| 110 | + (s/gen ::m/finite) |
| 111 | + {:min-elements 2 |
| 112 | + :max-elements 5}) |
| 113 | + (fn [v] |
| 114 | + (gen/return (vec (sort v))))))) |
| 115 | + |
| 116 | +(s/def ::x-vals ::strictly-ascending-vector-finite) |
| 117 | +(s/def ::y-vals ::strictly-ascending-vector-finite) |
| 118 | +(s/def ::z-vals ::strictly-ascending-vector-finite) |
| 119 | + |
| 120 | +(s/def ::f-vals |
| 121 | + (s/with-gen |
| 122 | + (s/coll-of ::m/finite |
| 123 | + :kind clojure.core/vector? |
| 124 | + :into [] |
| 125 | + :min-count 2) |
| 126 | + #(gen/vector (s/gen ::m/finite) 2 5))) |
| 127 | + |
| 128 | +(s/def ::x-vals-with-f-vals |
| 129 | + (s/with-gen |
| 130 | + (s/and (s/keys :req [::x-vals ::f-vals]) |
| 131 | + (fn [{::keys [x-vals f-vals]}] |
| 132 | + (= (count f-vals) (count x-vals)))) |
| 133 | + #(gen/bind |
| 134 | + (s/gen ::x-vals) |
| 135 | + (fn [x] |
| 136 | + (gen/bind |
| 137 | + (gen/vector (s/gen ::m/num) |
| 138 | + (count x)) |
| 139 | + (fn [f-v] |
| 140 | + (gen/return {::x-vals x |
| 141 | + ::f-vals f-v}))))))) |
| 142 | + |
| 143 | +(s/def ::bandwidth-for-loess |
| 144 | + (s/with-gen ::m/open-prob |
| 145 | + #(s/gen (s/double-in :infinite? false |
| 146 | + :NaN? false |
| 147 | + :min 0.25 |
| 148 | + :max 0.5)))) |
0 commit comments