This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-04-27
Channels
- # announcements (9)
- # aws (1)
- # aws-lambda (12)
- # babashka (18)
- # beginners (37)
- # calva (27)
- # clerk (15)
- # clojure (16)
- # clojure-conj (13)
- # clojure-europe (44)
- # clojure-germany (3)
- # clojure-norway (27)
- # clojure-uk (1)
- # cursive (6)
- # data-science (24)
- # datahike (7)
- # datomic (40)
- # fulcro (5)
- # hoplon (33)
- # hyperfiddle (9)
- # introduce-yourself (6)
- # jobs (1)
- # lsp (22)
- # nbb (2)
- # off-topic (15)
- # pathom (37)
- # pedestal (3)
- # polylith (7)
- # portal (1)
- # re-frame (7)
- # releases (1)
- # remote-jobs (1)
- # rewrite-clj (6)
- # sci (1)
- # scittle (1)
- # xtdb (7)
Anyone know of a generic optimisation library for clojure? I want the equivalent of a data solver like in excel
Some sort of convex optimisation or gradient decent thing, I’m ignorant about how they work
also root finder (for univariate functions): https://generateme.github.io/fastmath/fastmath.solver.html
Let me know if you hit any problems (some things can be tricky, also avoid :bfgs
which is buggy)
Is this able to optimise to a specific value? Or would you like wrap the desired function in another one where the desired value is a minimum or something?
To answer questions like, find a set of values x y … such that f(x,y,..) = 3838 or something
generally optimizers search for minimum/maximum, so there is no option to find a specific value
for your case you should reach for solver, however there is only univariate one. Checking if it's possible to make multivariate solver (I rely on Apache Commons Math)
made a little wrapper function that defines the squared error of a value function from a value that seems to work for what i needed when minimising
(fn [j v]
(fn [& rst]
(let [error (- (apply j rst) v)]
(* error error))))
> To answer questions like, find a set of values x y … such that f(x,y,..) = 3838 or something for satisfiability problems like these there are other options, which, owing to the intrinsic computational complexity of these problems, will be much slower than gradient descent because they look for exact solutions • https://github.com/clojure/core.logic • https://github.com/Engelberg/rolling-stones • https://torvaney.github.io/projects/flow-solver.html
Google OR-Tools maybe: https://developers.google.com/optimization It’s java, but you can wrap it with Clojure. I did for my purposes.
It's not hard to write your own root finder. I think excel uses a variation of newton-raphson method. here is my code
(ns aziz.newton
(:require [clojure.math :as math]))
(defn newton-raphson
[f f-derivative x0 tolerance max-iterations]
(loop [x x0
x_prev (+ x0 0.0001)
iterations 0]
(let [fx (f x)
fpx (f-derivative x x_prev)
x1 (- x (/ fx fpx))
error (Math/abs (- x1 x))]
(if (or (< error tolerance) (>= iterations max-iterations))
x1
(recur x1 x (inc iterations))))))
(defn f
"this is the function we want to find the root of, g need to be defined in a way that g(x)=0.
for example, to calculate xirr, we need to define npv function such that x is the rate of return and
npv(x) = 0"
[g guess]
(g guess))
(defn f'
[g guess previous-guess]
(/ (- (g guess) (g previous-guess)) (- guess previous-guess)))
(comment
(defn fun [x]
(- x (math/pow 48 (/ 1 3))))
(newton-raphson fun (partial f' fun) 1 0.0001 1000))
in your example of f(x,y,..) = 3838, you convert it into f(x,y,..) - 3838 = 0
I wrote this for a quick side analysis that I needed and I didn't know the libraries mentioned aboveoh yes you're right, my solution is not good
@U01MV1EPVK8 Let me know how OR-Tools works for you. I had some fun, writing Clojure wrappers for the CP-SAT Solver, to support linear expressions in data structures. See https://docs.google.com/presentation/d/1YD1qHaSveI58gFHoe-pc3c9fQMn9cCwYnSW9XjeeF4o/edit#slide=id.g13f7fc68ac4_0_5 (ignore the DSL approach-- I don’t like it): TL:DR
(let [domain {"x" [:range 0 20]
"y" [:range 0 20]}
eqs2 [
["=" ["y"] [[2 "x"] 3]] ; y = 2x + 3
["<" ["x"] [4]] ; (x < 4)
]]
(q/solve-equations domain eqs2))
It’s called goal-seek in excel, or this plugin: https://www.solver.com/excel-solver-how-load-or-start-solver