Giter VIP home page Giter VIP logo

cs-swarm-intelligence's Introduction

cs-swarm-intelligence

Swam intelligence for numerical optimization implemented in .NET

Features

The current library support optimization problems in which solutions are either discrete or continuous vectors. The algorithms implemented for swarm-intelligence are listed below:

  • Particle Swarm Optimization (PSO)
  • Bees Algorithm
  • Ant Colony System

Install

Install-Package cs-swarm-intelligence -Version 1.0.1

Usage

Running PSO

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

int maxIterations = 2000;
int dimension = 2;
int popSize = 200;
double[] lowerBounds = new double[] { -2.048, -2.048 };
double[] upperBounds = new double[] { 2.048, 2.048 };
SimpleParticle finalSolution;

ParticleSwarm<SimpleParticle>.Solve(popSize, dimension, (solution, constraints) =>
{
	// this is the Rosenbrock Saddle cost function
	double[] positions = solution.Positions;
	double x0 = positions[0];
	double x1 = positions[1];

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

Running Bees Algorithm

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

int maxIterations = 2000;
int dimension = 2;
int displayEvery = 10;
double[] lowerBounds = new double[] { -2.048, -2.048 };
double[] upperBounds = new double[] { 2.048, 2.048 };
SimpleBee finalSolution;

BeeSwarm<SimpleBee>.Solve(dimension, (solution) =>
{
	// this is the Rosenbrock Saddle cost function
	
	double x0 = solution[0];
	double x1 = solution[1];

	double cost = 100 * Math.Pow(x0 * x0 - x1, 2) + Math.Pow(1 - x0, 2);
	return cost;
}, out finalSolution, lowerBounds, upperBounds, maxIterations, displayEvery);

Ant Colony System

The sample codes belows show to solve the Travelling Salesman Problem (TSP) using Ant Colony System:

int populationSize = 100;

SimpleAnt bestSolution;
TspBenchmark tsp = Tsp.get(Tsp.Instance.bayg29);
int problemSize = tsp.ProblemSize();
int displayEvery = 10;
int maxIterations = 1000;
AntColonySystem<SimpleAnt>.SolveByAntColonySystem(populationSize, problemSize
, solution => // this returns the cost of the solution which in the case of the TSP is the total distance of visiting every cities exactly once using the route represented by the solution 
{
	double cost = 0;
	for(int i=0; i < solution.Length; ++i)
	{
		int j = (i + 1) % solution.Length;
		int v = solution[i];
		int w = solution[j];
		cost += tsp.Distance(v, w);
	}
	return cost;
}, (state1, state2) => // this returns the heuristic value for a move from state1 to state2
{ 
	return 1.0 / (1.0 + tsp.Distance(state1, state2));
}, displayEvery, out bestSolution, null, maxIterations);

Ant System

The sample codes belows show to solve the Travelling Salesman Problem (TSP) using Ant System:

int populationSize = 100;
            
SimpleAnt bestSolution;
TspBenchmark tsp = Tsp.get(Tsp.Instance.bayg29);
int problemSize = tsp.ProblemSize();
int displayEvery = 10;
int maxIterations = 1000;
AntSystem<SimpleAnt>.SolveByAntSystem(populationSize, problemSize, solution =>
{
	double cost = 0;
	for(int i=0; i < solution.Length; ++i)
	{
		int j = (i + 1) % solution.Length;
		int v = solution[i];
		int w = solution[j];
		cost += tsp.Distance(v, w);
	}
	return cost;
}, (state1, state2) =>
{
	return 1.0 / (1.0 + tsp.Distance(state1, state2));
}, displayEvery, out bestSolution, null, maxIterations);

cs-swarm-intelligence'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.