Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restructuring and cleanup to prepare for open-sourcing.
- Loading branch information
Showing
28 changed files
with
553 additions
and
402 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// ViewController.swift | ||
// SwiftCMAES | ||
// | ||
// Created by Santiago Gonzalez on 4/13/19. | ||
// Copyright © 2019 Santiago Gonzalez. All rights reserved. | ||
// | ||
|
||
import Cocoa | ||
|
||
class ViewController: NSViewController { | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
// Do any additional setup after loading the view. | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Santiago Gonzalez | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# SwiftCMA | ||
## *by Santiago Gonzalez* | ||
### ***A pure-Swift implementation of Covariance Matrix Adaptation Evolutionary Strategy (CMA-ES).*** | ||
|
||
**SwiftCMA** is a *de novo* implementation of [Covariance Matrix Adaptation Evolutionary Strategy](https://en.wikipedia.org/wiki/CMA-ES) (CMA-ES). CMA-ES is a wonderful population-based optimization technique that can optimize non-convex, non-smooth, non-differentiable functions. While CMA-ES is conceptually simple, it's rather complex mathematically. **SwiftCMA** is written in pure Swift, and makes proper use of functional programming and Swift's type system. This project is provided under the MIT License (see the `LICENSE` file for more info). | ||
|
||
## Functionality | ||
|
||
### CMA-ES | ||
|
||
The specific implementation of CMA-ES is inspired from the MATLAB reference implementation on [Wikipedia](https://en.wikipedia.org/wiki/CMA-ES). The implementation supports arbitrary dimension solution spaces. | ||
|
||
The primary `CMAES` object has two slightly different implementations of the main `epoch()` method. | ||
* One takes a closure that takes an array of candidate solution vectors and returns an array of corresponding objective function values. This allows your code to potentially calculate objective function values concurrently. | ||
* Alternatively, for simplicity, you can use the flavor of `epoch()` that takes an objective evaluator. Objective functions can be represented by types that conform to the `ObjectiveEvaluator` protocol. In this case, objective function values are calculated sequentially on the same thread. | ||
|
||
### Linear Algebra API | ||
|
||
Swift isn't traditionally thought of as a good language for linear algebra code, though I feel that's mainly due to the lack of linear algebra APIs. **SwiftCMA** provides a clean API for vectors and matrices, based on top of Swift arrays, that should feel familiar if you've used Eigen / MATLAB / Octave, or similar systems. This API has not been optimized to be as fast as it could be since objective-function evaluation is the biggest bottleneck by far for what I created this library for (metalearning). Pull requests are welcome! | ||
|
||
Functionality: | ||
* Vectors and vector operations | ||
* Matrices and matrix operations | ||
* Vector-matrix operations | ||
* Eigendecomposition of matrices to get eigenvalues and an eigenbasis | ||
|
||
### Unit Tests | ||
|
||
Testing is great, so we have some unit tests as part of the Xcode project! More tests would be great, right now the tests just cover the linear algebra APIs. | ||
|
||
### Built-in Objective Functions | ||
|
||
**SwiftCMA** has some built-in objective functions. These are useful for testing / benchmarking how well the system is able to optimize some relatively well-understood functions. | ||
|
||
* N-dimensional sphere: `SphereObjectiveEvaluator` | ||
* Rastrigin function: `RastriginObjectiveEvaluator` | ||
* Ackley function: `AckleyObjectiveEvaluator` | ||
|
||
### Test App | ||
|
||
**SwiftCMA** comes with an Xcode project that builds a test app bundle. All code specific to this is in the `App/` directory. A quick note: the project builds an app bundle, rather than a basic executable, since it needs to link to `Accelerate.framework`. | ||
|
||
## Usage | ||
|
||
Everything you need to use **SwiftCMA** is in the `Sources/` directory. | ||
|
||
let startSolution: Vector = ... | ||
var fitness = MyObjectiveEvaluator() | ||
let populationSize = CMAES.populationSize(forDimensions: startSolution.count) | ||
let stepSigma: Double = ... | ||
let cmaes = CMAES(startSolution: startSolution, populationSize: populationSize, stepSigma: stepSigma) | ||
var bestSolution: (Vector, Double)? | ||
for i in 0..<1000 { | ||
cmaes.epoch(evaluator: &fitness) { newSolution, newFitness in | ||
print("Found solution with fitness \(fitness): \(solution)") | ||
} | ||
|
||
if bestSolution == nil || bestSolution!.1 > cmaes.bestSolution!.1 { | ||
bestSolution = cmaes.bestSolution | ||
} | ||
print("\(i): \(cmaes.bestSolution!.1)") | ||
} | ||
print("Best: \(bestSolution!.1): \(bestSolution!.0)") | ||
|
||
### Defining an Objective Function | ||
|
||
CMA-ES aims to find the global minimum, so your objective function must be formulated so that smaller values are better. | ||
|
||
struct SphereObjectiveEvaluator: ObjectiveEvaluator { | ||
typealias Genome = Vector | ||
|
||
func objective(genome: Vector, solutionCallback: (Vector, Double) -> ()) -> Double { | ||
let value = genome.squaredMagnitude // Distance from origin is the error. | ||
if diff < 0.01 { // We have found a solution when the difference is below a threshold. | ||
solutionCallback(genome, diff) | ||
} | ||
return genome.squared.sum | ||
} | ||
} | ||
|
||
### Dependencies | ||
|
||
The only external dependency is `LAPACK` (for eigendecomposition). On macOS, this is fulfilled by the built-in `Accelerate` framework. On Linux, you should use the [CLapacke-Linux](https://github.com/indisoluble/CLapacke-Linux) Swift wrapper around LAPACK, which is very easy to install using `APT`. | ||
|
||
|
||
## Future Work | ||
|
||
* Integrate with Swift Package Manager. | ||
* Separate linear algebra API into its own library. | ||
* Faster. | ||
* Support fun variants of CMA-ES. | ||
* More tests (unit, integration, performance). | ||
* More engaging test app that visualizes the CMA-ES process. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.