Giter Club home page Giter Club logo

cs-optimization-continuous-solutions's Introduction

cs-optimization-continuous-solutions

Local searches for continuous optimization implemented in C#

Install

Install-Package MathNet.Numerics -Version 2.4.0
Install-Package cs-optimization-continuous-solutions -Version 1.0.2

Features

The package includes various local search and metaheuristics algorithms for continuous optimization:

Numerical Methods

  • Adaptive Random Search
  • BFGS
  • Conjugate Gradient Descent Search
  • Fibonacci Search
  • Golden Section Search
  • Gradient Descent
  • Newton Method
  • Powell Method
  • Random Search
  • SLOP

These algorithms can be found in the "ContinuousOptimization.LocalSearch" directory / namespace

Meta-Heuristics

  • Differential Evolution
  • Evlutionary Programming
  • Evolution Strategy
  • GRASP
  • Iterated Local Search
  • Nelder Mead
  • Stochastic Hill Climbing
  • Tabu Search
  • Variable Neighbhorhood Search

These algorithms can be found in the "ContinuousOptimization.MetaHeuristics" directory / namespace

Usage

Local Search Methods on Rosenbrock Optimization Problem

The sample codes below shows how to solve the "Rosenbrock Saddle" continuous optmization problem using ConjugateGradientSearch:

CostFunction_RosenbrockSaddle f = new CostFunction_RosenbrockSaddle();
ConjugateGradientSearch s = new ConjugateGradientSearch();
            
double[] x_0 = f.CreateRandomSolution(); // initial solution 

s.SolutionUpdated += (best_solution, step) =>
{
	Console.WriteLine("Step {0}: Fitness = {1}", step, best_solution.Cost);
};

int max_iteration = 1000;
ContinuousSoluton finalSolution = s.Minimize(x_0, f, max_iteration);

Where the CostFunction_RosenbrockSaddle is the cost function that is defined as below:

public class CostFunction_RosenbrockSaddle : CostFunction
{
	public CostFunction_RosenbrockSaddle()
		: base(2, -2.048, 2.048) // 2 is the dimension of the continuous solution, -2.048 and 2.048 is the lower and upper bounds for the two dimensions 
	{

	}

	protected override void _CalcGradient(double[] solution, double[] grad) // compute the search gradent given the solution 
	{
		double x0 = solution[0];
		double x1 = solution[1];
		grad[0] = 400 * (x0 * x0 - x1) * x0 - 2 * (1 - x0);
		grad[1] = -200 * (x0 * x0 - x1);
	}

	// Optional: if not overriden, the default gradient esimator will be provided for gradient computation
	protected override double _Evaluate(double[] solution) // compute the cost of problem given the solution 
	{
		double x0 = solution[0];
		double x1 = solution[1];

		double cost =100 * Math.Pow(x0 * x0 - x1, 2) + Math.Pow(1 - x0, 2);
		return cost;
	}

}

To replace the ConjugateGradientSearch with another search method, simply change it to another name while other part of the implementation remain the same. For example the sample code below use BFGS to solve the same problem:

CostFunction_RosenbrockSaddle f = new CostFunction_RosenbrockSaddle();
BFGS s = new BFGS();
            
double[] x_0 = f.CreateRandomSolution(); // initial solution 

s.SolutionUpdated += (best_solution, step) =>
{
	Console.WriteLine("Step {0}: Fitness = {1}", step, best_solution.Cost);
};

int max_iteration = 1000;
ContinuousSoluton finalSolution = s.Minimize(x_0, f, max_iteration);

Meta-Heuristics on Rosenbrock

The sample code below shows how to solve the Rosenbrock using differential evolution.

CostFunction_RosenbrockSaddle f = new CostFunction_RosenbrockSaddle();

int maxIterations = 200;
DifferentialEvolution s = new DifferentialEvolution(f);

s.SolutionUpdated += (best_solution, step) =>
{
	Console.WriteLine("Step {0}: Fitness = {1}", step, best_solution.Cost);
};

ContinuousSolution finalSolution = s.Minimize(f, maxIterations);

The sample code below shows how to solve the Rosenbrock using evolutionary programming.

 CostFunction_RosenbrockSaddle f = new CostFunction_RosenbrockSaddle();

int maxIterations = 200;
int popSize = 100; // population Size
EvolutionaryProgramming s = new EvolutionaryProgramming(f, popSize);

s.SolutionUpdated += (best_solution, step) =>
{
	Console.WriteLine("Step {0}: Fitness = {1}", step, best_solution.Cost);
};

ContinuousSolution finalSolution = s.Minimize(f, maxIterations);

The sample code below show how to solve the Rosenbrock using evolution strategy.

CostFunction_RosenbrockSaddle f = new CostFunction_RosenbrockSaddle();

int maxIterations = 200;
int mu = 30;
int lambda = 20;
EvolutionStrategy s = new EvolutionStrategy(f, mu, lambda);

s.SolutionUpdated += (best_solution, step) =>
{
	Console.WriteLine("Step {0}: Fitness = {1}", step, best_solution.Cost);
};

ContinuousSolution finalSolution = s.Minimize(f, maxIterations);

More

For more examples, please refers to the cs-optimization-continuous-solutions-samples project.

cs-optimization-continuous-solutions's People

Contributors

chen0040 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

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.