GithubHelp home page GithubHelp logo

chen0040 / cs-estimation-of-distribution-algorithms Goto Github PK

View Code? Open in Web Editor NEW
1.0 4.0 0.0 452 KB

Estimation of Distribution Algorithms implemented in C#

License: MIT License

C# 100.00%
eda estimation-of-distribution numerical-optimization local-search

cs-estimation-of-distribution-algorithms's Introduction

cs-estimation-of-distribution-algorithms

Estimation of Distribution Algorithms implemented in C#

Install

Install-Package cs-estimation-of-distribution-algorithms -Version 1.0.1

Features

The current library support optimization problems in which solutions are either binary-coded or continuous vectors. The algorithms implemented for estimation-of-distribution are listed below:

  • PBIL
  • CGA (Compact Genetic Algorithm)
  • BOA (Bayesian Optimization Algorithm)
  • UMDA (Univariate Marginal Distribution Algorithm)
  • Cross Entropy Method
  • MIMIC

Usage

Solving Continuous Optimization

Running PBIL

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

CostFunction_RosenbrockSaddle f = new CostFunction_RosenbrockSaddle();
            
int popSize = 8000;
PBIL s = new PBIL(popSize, f);

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

int max_iterations = 200;
s.Minimize(f, max_iterations);

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;
	}

}

Running CGA

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

CostFunction_RosenbrockSaddle f = new CostFunction_RosenbrockSaddle();
            
int n = 1000; // sample size for the distribution 
CGA s = new CGA(n, f);

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

int max_iterations = 2000000;
s.Minimize(f, max_iterations);

Running UMDA

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

CostFunction_RosenbrockSaddle f = new CostFunction_RosenbrockSaddle();
            
int popSize = 1000; 
int selectionSize = 100;
UMDA s = new UMDA(popSize, selectionSize, f);

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

int max_iterations = 2000000;
s.Minimize(f, max_iterations);

Running MIMIC

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

CostFunction_RosenbrockSaddle f = new CostFunction_RosenbrockSaddle();
            
int n = 1000; // population size 
MIMIC s = new MIMIC(n, f);

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

int max_iterations = 2000000;
s.Minimize(f, max_iterations);

Running CrossEntropyMethod

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

CostFunction_RosenbrockSaddle f = new CostFunction_RosenbrockSaddle();
            
int sampleSize = 1000; 
int selectionSize = 100;
CrossEntropyMethod s = new CrossEntropyMethod(sampleSize, selectionSize, f);

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

int max_iterations = 2000000;
s.Minimize(f, max_iterations);

Solving Problems with Binary-encoded Solutions

Running PBIL

The samle codes below show how to solve a canonical optimization problem that look for solutions with minimum number of 1 bits in the solution:

int popSize = 8000;
int dimension = 50;
int eliteCount = 50;
PBIL s = new PBIL(popSize, dimension, eliteCount);
s.MaxIterations = 100;

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

s.Minimize((solution, constraints) =>
{
	// solution is binary-encoded
	double cost = 0;
	// minimize the number of 1 bits in the solution
	for(int i=0; i < solution.Length; ++i)
	{
		cost += solution[i]; 
	}
	return cost;
});

Running CGA

The samle codes below show how to solve a canonical optimization problem that look for solutions with minimum number of 1 bits in the solution:

int sampleSize = 8000;
int dimension = 50;
int sampleSelectionSize = 100;
CGA s = new CGA(sampleSize, dimension, sampleSelectionSize);
s.MaxIterations = 100;

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

s.Minimize((solution, constraints) =>
{
	// solution is binary-encoded
	double cost = 0;
	// minimize the number of 1 bits in the solution
	for(int i=0; i < solution.Length; ++i)
	{
		cost += solution[i]; 
	}
	return cost;
});

Running UMDA

The samle codes below show how to solve a canonical optimization problem that look for solutions with minimum number of 1 bits in the solution:

int sampleSize = 8000;
int dimension = 50;
int sampleSelectionSize = 100;
UMDA s = new UMDA(sampleSize, dimension, sampleSelectionSize);
s.MaxIterations = 100;

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

s.Minimize((solution, constraints) =>
{
	// solution is binary-encoded
	double cost = 0;
	// minimize the number of 1 bits in the solution
	for(int i=0; i < solution.Length; ++i)
	{
		cost += solution[i]; 
	}
	return cost;
});

TODO

  • BOA algorithm still has bugs, will need to be fixed in the future release.

cs-estimation-of-distribution-algorithms's People

Contributors

chen0040 avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  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.