Thin Clojure wrapper around Apache Commons Math 3's simplex LP solver. Nothing here knows what a variable represents - it takes coefficient vectors and constraint triplets and returns a point + objective value.
Thin Clojure wrapper around Apache Commons Math 3's simplex LP solver. Nothing here knows what a variable represents - it takes coefficient vectors and constraint triplets and returns a point + objective value.
(bv n i w)Creates a basis vector: an all-zero double array of length n with w at index i.
Creates a basis vector: an all-zero double array of length `n` with `w` at index `i`.
(linear-optimization target constraints)(linear-optimization target
constraints
{:keys [goal epsilon max-ulps cut-off rule non-negative?
max-iter stats?]
:or {goal :minimize
epsilon 1.0E-6
max-ulps 10
cut-off 1.0E-10
rule :dantzig
non-negative? false
max-iter Integer/MAX_VALUE}})Solves a linear problem.
Target is defined as a vector of coefficients and constant as the last value:
[a1 a2 a3 ... c] means f(x1,x2,x3) = a1*x1 + a2*x2 + a3*x3 + ... + c
Constraints are defined as a sequence of one of the following triplets:
[a1 a2 a3 ...] R n - which means a1*x1+a2*x2+a3*x3+... R n[a1 a2 a3 ... ca] R [b1 b2 b3 ... cb] - which means a1*x1+a2*x2+a3*x3+...+ca R b1*x1+b2*x2+b3*x3+...+cb
where R is a relationship and can be one of <=, >= or = as symbol or keyword. Also :leq, :geq and :eq are valid.Function returns pair of optimal point and function value. If stat? option is set to true, returns also information about number of iterations.
Possible options:
:goal - :minimize (default) or :maximize:rule - pivot selection rule, :dantzig (default) or :bland:max-iter - maximum number of iterations, maximum integer by default:non-negative? - allow non-negative variables only, default: false:epsilon - convergence value, default: 1.0e-6::max-ulps - floating point comparisons, default: 10 ulp:cut-off - pivot elements smaller than cut-off are treated as zero, default: 1.0e-10(linear-optimization [-1 4 0] [[-3 1] :<= 6
[-1 -2] :>= -4
[0 1] :>= -3])
;; => [(9.999999999999995 -3.0) -21.999999999999993]
Solves a linear problem.
Target is defined as a vector of coefficients and constant as the last value:
`[a1 a2 a3 ... c]` means `f(x1,x2,x3) = a1*x1 + a2*x2 + a3*x3 + ... + c`
Constraints are defined as a sequence of one of the following triplets:
* `[a1 a2 a3 ...] R n` - which means `a1*x1+a2*x2+a3*x3+... R n`
* `[a1 a2 a3 ... ca] R [b1 b2 b3 ... cb]` - which means `a1*x1+a2*x2+a3*x3+...+ca R b1*x1+b2*x2+b3*x3+...+cb`
where `R` is a relationship and can be one of `<=`, `>=` or `=` as symbol or keyword. Also `:leq`, `:geq` and `:eq` are valid.
Function returns pair of optimal point and function value. If `stat?` option is set to true, returns also information about number of iterations.
Possible options:
* `:goal` - `:minimize` (default) or `:maximize`
* `:rule` - pivot selection rule, `:dantzig` (default) or `:bland`
* `:max-iter` - maximum number of iterations, maximum integer by default
* `:non-negative?` - allow non-negative variables only, default: `false`
* `:epsilon` - convergence value, default: `1.0e-6`:
* `:max-ulps` - floating point comparisons, default: `10` ulp
* `:cut-off` - pivot elements smaller than cut-off are treated as zero, default: `1.0e-10`
```clojure
(linear-optimization [-1 4 0] [[-3 1] :<= 6
[-1 -2] :>= -4
[0 1] :>= -3])
;; => [(9.999999999999995 -3.0) -21.999999999999993]
```(linear-optimization-generic target constraints)(linear-optimization-generic target constraints options)Generic linear optimization that supports optional absolute sum constraint
(pass :sum-of-abs-weights in options to allow short positions).
Generic linear optimization that supports optional absolute sum constraint (pass `:sum-of-abs-weights` in options to allow short positions).
(linear-optimization-with-abs-sum target constraints sum-of-abs-weights)(linear-optimization-with-abs-sum target constraints sum-of-abs-weights options)Linear optimization with sum of absolute values constraint.
For each original variable x_i, we introduce two non-negative variables:
Where: x_i = x_i+ - x_i- and |x_i| = x_i+ + x_i-
Parameters:
Example: (linear-optimization-with-abs-sum [-1 4 0] ; objective coefficients [[-3 1] :<= 6 ; other constraints [1 1] := 100] ; sum of weights = 100 120.0 ; sum of absolute weights {})
Linear optimization with sum of absolute values constraint.
For each original variable x_i, we introduce two non-negative variables:
- x_i+ (positive part)
- x_i- (negative part)
Where: x_i = x_i+ - x_i- and |x_i| = x_i+ + x_i-
Parameters:
- target: vector of coefficients for objective function [a1 a2 ... c]
- constraints: standard linear constraints (will be transformed, should include sum-of-weights)
- sum-of-abs-weights: target for sum of absolute weights (must be positive)
- options: same as linear-optimization
Example:
(linear-optimization-with-abs-sum
[-1 4 0] ; objective coefficients
[[-3 1] :<= 6 ; other constraints
[1 1] := 100] ; sum of weights = 100
120.0 ; sum of absolute weights
{})(maximize-objective universe objective-key constraints)(maximize-objective universe objective-key constraints options)Solve for a universe (a seq of items/maps) whose objective coefficients are
(get item objective-key), maximizing sum(objective) subject to constraints.
options are the same as linear-optimization-generic (e.g. :non-negative?,
:sum-of-abs-weights); :goal is forced to :maximize.
Solve for a universe (a seq of items/maps) whose objective coefficients are `(get item objective-key)`, maximizing sum(objective) subject to `constraints`. `options` are the same as `linear-optimization-generic` (e.g. `:non-negative?`, `:sum-of-abs-weights`); `:goal` is forced to `:maximize`.
(seq->double-array vs)Convert sequence to double array. Returns input if vs is double array already.
Convert sequence to double array. Returns input if `vs` is double array already.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |