# haginile/SwiftOptimizer

A Swift Library for Solving Optimization Problems (i.e., minimization/maximization)
C++ C Swift Other
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
 Failed to load latest commit information. SwiftOptimizer.xcodeproj Jul 20, 2014 SwiftOptimizer Jul 20, 2014 swix Jul 19, 2014 LICENSE Jul 19, 2014 README.md Jul 20, 2014

# SwiftOptimizer

SwiftOptimizer allows you to solve minimization/maximization problems in Apple's Swift programming language. It is ported from QuantLib and uses the awesome `swix` library for matrix calculations.

It currently supports the `Simplex` and `BFGS` methods, but will be expanded to include least squares, etc.

## Example

First things first, subclass `CostFunction` to create a class representing the function you are trying to minimize. For example, if you are interested in minimizing the Rosenbrock Function, then you need to set up the cost function as follows:

```class RosenBrockFunction: CostFunction
{
override func value(parameters: matrix) -> Double {
return pow(1.0 - parameters[0], 2) + 100 * pow(parameters[1] - pow(parameters[0], 2), 2.0)
}
}```

The `CostFunction`, `Constraint` (if any), and the initial values together define the `Problem` you are trying to solve. You also need to specify the `EndCriteria` so that the optimizer knows when to quit:

```var costFunction = RosenBrockFunction()
var constraint = NoConstraint()
var initialValue = zeros(2)
var problem = Problem(costFunction: costFunction, constraint: constraint, initialValue: initialValue)

var myEndCriteria = EndCriteria(maxIterations: 1000,
maxStationaryStateIterations: 100,
rootEpsilon: 1.0e-8,
functionEpsilon: 1.0e-9,
Finally, this is how you run the `Simplex` optimizer:
```var solver = Simplex(lambda: 0.1)
Other optimization algorithms can be applied analogously. For example, this is how to use the `BFGS` algorithm:
```var bfgsSolver = BFGS()