Git Product home page Git Product logo

swiftoptimizer's Introduction

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, 
                                gradientNormEpsilon: 1.0e-5)

Finally, this is how you run the Simplex optimizer:

var solver = Simplex(lambda: 0.1)
var solved = solver.minimize(&problem, endCriteria: myEndCriteria)
problem.currentValue    // return matrix([1.000, 1.000])

Other optimization algorithms can be applied analogously. For example, this is how to use the BFGS algorithm:

var bfgsSolver = BFGS()
var bfgsSolved = bfgsSolver.minimize(&problem, endCriteria: myEndCriteria)
problem.currentValue    // return matrix([1.000, 1.000])

swiftoptimizer's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.