(Simulated Bee Colony, SBC) . , SBC, , , , (Traveling Salesman Problem) SBC.
SBC - , . 1. SBC 20 ( A T) , . , A, T , 19,0 .
. 1. SBC
SBC 100 , . , 95,0 . SBC ( ), , . SBC 16 20, 26,5 - , .
SBC -, , . SBC . , SBC , SBC . , SBC , .
, . C#, , . , , SBC .
, Apis mellifera ( ), , . 5000 20 000 . ( 20 40 ), , (foragers). : , - .
, , .
( 50 ) . 10% - .
- . . , - , , - (waggle dance) . , . .
, , , .
(Traveling Salesman Problem, TSP) - . TSP, , .
. 1 , , - SBC 20 , A T. 20 , . 20! = 2 432 902 008 176 640 000 .
. , c1 < c2, c1 c2 (ordinal distance) . c1 > c2, 1,5 c1 c2. A B 1,0 , B A - 1,5 , A C - 2,0 . . , , , - A -> B-> C -> ... -> T, - (20-1) * 1,0 = 19,0.
TSP . - . TSP , , - . , TSP , c1 c2 c2 c1, .
, <> . , 20 , , 20! 77 000 . SBC.
TSP CitiesData, . 2. SBC code.msdn.microsoft.com/mag0411BeeColony.
. 2. CitiesData
- class CitiesData {
- public char[] cities;
-
- public CitiesData(int numberCities) {
- this.cities = new char[numberCities];
- this.cities[0] = 'A';
- for (int i = 1; i < this.cities.Length; ++i)
- this.cities[i] = (char)(this.cities[i - 1] + 1);
- }
-
- public double Distance(char firstCity, char secondCity) {
- if (firstCity < secondCity)
- return 1.0 * ((int)secondCity - (int)firstCity);
- else
- return 1.5 * ((int)firstCity - (int)secondCity);
- }
-
- public double ShortestPathLength() {
- return 1.0 * (this.cities.Length - 1);
- }
-
- public long NumberOfPossiblePaths() {
- long n = this.cities.Length;
- long answer = 1;
- for (int i = 1; i <= n; ++i)
- checked { answer *= i; }
- return answer;
- }
-
- public override string ToString() {
- string s = "";
- s += "Cities: ";
- for (int i = 0; i < this.cities.Length; ++i)
- s += this.cities[i] + " ";
- return s;
- }
- }
- , , , . , SBC, , , (jagged array of arrays).
CitiesData A, - B . .
Distance , , , .
ShortestPathLength Distance. SBC , , .
NumberOfPossiblePaths n!, n - . TSP n-1! ( ), - n/2! ( ).
ToString StringBuilder . , .
, , , , . , NumberOfPossiblePaths , long.MaxValue.
SBC, . 1, C#. . 3. , SBC- , System. : Program ( Main), Hive ( SBC) CitiesData ( ). Hive (Hive), TravelingSalesmanHive, SBC , .
. 3.
- using System;
- namespace SimulatedBeeColony {
- class Program {
- static void Main(string[] args) {
- Console.WriteLine("\nBegin Simulated Bee Colony algorithm demo\n");
- . . .
- Console.WriteLine("End Simulated Bee Colony demo");
- }
- }
-
- class Hive {
- public class Bee {
- . . .
- }
-
- // Hive data fields here
-
- public override string ToString() { . . . }
-
- public Hive(int totalNumberBees, int numberInactive,
- int numberActive, int numberScout, int maxNumberVisits,
- int maxNumberCycles, CitiesData citiesData) {
- . . .
- }
-
- public char[] GenerateRandomMemoryMatrix() { . . . }
-
- public char[] GenerateNeighborMemoryMatrix(char[] memoryMatrix) { . . . }
-
- public double MeasureOfQuality(char[] memoryMatrix) { . . . }
-
- public void Solve() { . . . }
-
- private void ProcessActiveBee(int i) { . . . }
-
- private void ProcessScoutBee(int i) { . . . }
-
- private void ProcessInactiveBee(int i) { . . . }
-
- private void DoWaggleDance(int i) { . . . }
- }
-
- class CitiesData {
- . . .
- }
-
- } // ns
Main . CitiesData:
- Console.WriteLine(
- "Loading cities data for SBC Traveling Salesman Problem analysis");
- CitiesData citiesData = new CitiesData(20);
- Console.WriteLine(citiesData.ToString());
- Console.WriteLine("Number of cities = " + citiesData.cities.Length);
- Console.WriteLine("Number of possible paths = " +
- citiesData.NumberOfPossiblePaths().ToString("#,###"));
- Console.WriteLine("Best possible solution (shortest path) length = " +
- citiesData.ShortestPathLength().ToString("F4"));
SBC , SQL, .
Hive :
- int totalNumberBees = 100;
- int numberInactive = 20;
- int numberActive = 50;
- int numberScout = 30;
- int maxNumberVisits = 100;
- int maxNumberCycles = 3460;
-
- Hive hive = new TravelingSalesmanHive(totalNumberBees,
- numberInactive, numberActive, numberScout, maxNumberVisits,
- maxNumberCycles, citiesData);
, SBC : , . <> , Hive, .
totalNumberBees , . . , , . , : 75% , 10% 15% .
100, maxNumberVisits, , (neighbor solutions), .
maxNumberCycles , Solve; , SBC , . ( , .) maxNumberCycles 3460 , SBC . , , SBC , , <> .
, . Hive () , , .
Hive Main ToString , Hive, Solve , , :
- ...
- Console.WriteLine("\nInitial random hive");
- Console.WriteLine(hive);
-
- bool doProgressBar = true;
- hive.Solve(doProgressBar);
-
- Console.WriteLine("\nFinal hive");
- Console.WriteLine(hive);
- Console.WriteLine("End Simulated Bee Colony demo");
- }
. 3, Hive Bee. . 4.
. 4. Bee
- public class Bee {
- public int status;
- public char[] memoryMatrix;
- public double measureOfQuality;
- public int numberOfVisits;
-
- public Bee(int status, char[] memoryMatrix,
- double measureOfQuality, int numberOfVisits) {
- this.status = status;
- this.memoryMatrix = new char[memoryMatrix.Length];
- Array.Copy(memoryMatrix, this.memoryMatrix, memoryMatrix.Length);
- this.measureOfQuality = measureOfQuality;
- this.numberOfVisits = numberOfVisits;
- }
-
- public override string ToString() {
- string s = "";
- s += "Status = " + this.status + "\n";
- s += " Memory = " + "\n";
- for (int i = 0; i < this.memoryMatrix.Length-1; ++i)
- s += this.memoryMatrix[i] + "->";
- s += this.memoryMatrix[this.memoryMatrix.Length-1] + "\n";
- s += " Quality = " + this.measureOfQuality.ToString("F4");
- s += " Number visits = " + this.numberOfVisits;
- return s;
- }
- }
Bee , SBC, , . memoryMatrix. SBC - . TSP, , char. , , , , .
status int, Bee: , . , status .
measureOfQuality double, memoryMatrix Bee. TSP , memoryMatrix. , , measureOfQuality . SBC - . double.
Bee - numberOfVisits. int, , Bee , . , , . , , , numberOfVisits , ( ).
Bee - status, memoryMatrix, measureOfQuality numberOfVisits. , Bee measureOfQuality, Bee memoryMatrix; , CitiesData.
Bee ToString, . Bee.ToString , SBC WriteLine.
Hive . 5. 14 , , SBC.
. 5. 14 Hive
- static Random random = null;
-
- public CitiesData citiesData;
-
- public int totalNumberBees;
- public int numberInactive;
- public int numberActive;
- public int numberScout;
-
- public int maxNumberCycles;
- public int maxNumberVisits;
-
- public double probPersuasion = 0.90;
- public double probMistake = 0.01;
-
- public Bee[] bees;
- public char[] bestMemoryMatrix;
- public double bestMeasureOfQuality;
- public int[] indexesOfInactiveBees;
WriteLine 14 . , .
, random, Random. SBC , random. random Hive.
- CitiesData. SBC . , , . , CitiesData , .
int, , , . , , , . .
, maxNumberCycles, , Solve. .
, maxNumberVisits, , . Solve, , numberOfVisits 1. numberOfVisits Bee maxNumberVisits, .
, probPersuasion, . , , , , .
probPersuasion <> 0.90, . . 90% . probPersuasion, 0.90, , . SBC , , , .
, probMistake, , , , . . ( ), , , . probMistake <> 0.01, , 1% .
, bees, - Bee. , (, , ), (memoryMatrix), (measureOfQuality) (numberOfVisits). Bee , bees Bee.
, bestMemoryMatrix, - char, bees. : SBC - , . , - . , SBC, , SBC .
, bestMeasureOfQuality, () bestMemoryMatrix.
, indexesOfInactiveBees, - int. . , , - . SBC , , , bees status .
Hive . 6. (hive) 10 : , . 2, 7 4 bees. CitiesData . :
A->B->E->C-D
:
1.0 + (3 * 1.0) + (2 * 1.5) + 1.0 = 8.0
, citiesData CitiesData, .
. 6. Hive
. 7. . , (citiesData). totalNumberBees , numberInactive, numberActive numberScout, .
. 7. Hive
- public Hive(int totalNumberBees, int numberInactive,
- int numberActive, int numberScout, int maxNumberVisits,
- int maxNumberCycles, CitiesData citiesData) {
-
- random = new Random(0);
-
- this.totalNumberBees = totalNumberBees;
- this.numberInactive = numberInactive;
- this.numberActive = numberActive;
- this.numberScout = numberScout;
- this.maxNumberVisits = maxNumberVisits;
- this.maxNumberCycles = maxNumberCycles;
-
- this.citiesData = citiesData;
-
- this.bees = new Bee[totalNumberBees];
- this.bestMemoryMatrix = GenerateRandomMemoryMatrix();
- this.bestMeasureOfQuality =
- MeasureOfQuality(this.bestMemoryMatrix);
-
- this.indexesOfInactiveBees = new int[numberInactive];
-
- for (int i = 0; i < totalNumberBees; ++i) {
- int currStatus;
- if (i < numberInactive) {
- currStatus = 0; // inactive
- indexesOfInactiveBees[i] = i;
- }
- else if (i < numberInactive + numberScout)
- currStatus = 2; // scout
- else
- currStatus = 1; // active
-
- char[] randomMemoryMatrix = GenerateRandomMemoryMatrix();
- double mq = MeasureOfQuality(randomMemoryMatrix);
- int numberOfVisits = 0;
-
- bees[i] = new Bee(currStatus,
- randomMemoryMatrix, mq, numberOfVisits);
-
- if (bees[i].measureOfQuality < bestMeasureOfQuality) {
- Array.Copy(bees[i].memoryMatrix, this.bestMemoryMatrix,
- bees[i].memoryMatrix.Length);
- this.bestMeasureOfQuality = bees[i].measureOfQuality;
- }
- }
- }
random, , (seed value), 0. . Hive. Hive 14 ; probPersuasion probMistake <> .
Hive citiesData citiesData. - , :
- int n = citiesData.cities.Length;
- this.citiesData = new CitiesData(n);
, . , , .
Hive bees null. (. . ) :
- this.bestMemoryMatrix = GenerateRandomMemoryMatrix();
- this.bestMeasureOfQuality =
- MeasureOfQuality(this.bestMemoryMatrix);
GenerateRandomMemoryMatrix , MeasureOfQuality . .
Hive indexesOfInactiveBees bees, numberInactive. 0.
Bee bees Bee. , numberInactive bees , numberScout - , - .
, , , bees 10 , 0 1 , 2-4 - 5-9 - . , indexesOfInactiveBees 2 0 1.
, , , 0 Bee:
- char[] randomMemoryMatrix = GenerateRandomMemoryMatrix();
- double mq = MeasureOfQuality(randomMemoryMatrix);
- int numberOfVisits = 0;
- bees[i] = new Bee(currStatus, randomMemoryMatrix,
- mq, numberOfVisits);
, , . , bestMemoryMatrix bestMeasureOfQuality. , , , , TSP .
bestMemoryMatrix - . .
SBC , , . .
- . , - . , GenerateRandomMemoryMatrix, :
- public char[] GenerateRandomMemoryMatrix() {
- char[] result = new char[this.citiesData.cities.Length];
- Array.Copy(this.citiesData.cities, result,
- this.citiesData.cities.Length);
- for (int i = 0; i < result.Length; i++) {
- int r = random.Next(i, result.Length);
- char temp = result[r];
- result[r] = result[i];
- result[i] = temp;
- }
- return result;
- }
TSP - char, char , GenerateRandomMemoryMatrix . result CitiesData , , Hive, CitiesData. result random, , - (Fisher-Yates shuffle algorithm) ( ) (Knuth shuffle).
, GenerateRandomMemoryMatrix Bee. , , ( - CitiesData), .
, GenerateNeighborMemoryMatrix, . 8.
. 8.
- public char[] GenerateNeighborMemoryMatrix(char[] memoryMatrix) {
- char[] result = new char[memoryMatrix.Length];
- Array.Copy(memoryMatrix, result, memoryMatrix.Length);
-
- int ranIndex = random.Next(0, result.Length);
- int adjIndex;
- if (ranIndex == result.Length - 1)
- adjIndex = 0;
- else
- adjIndex = ranIndex + 1;
-
- char tmp = result[ranIndex];
- result[ranIndex] = result[adjIndex];
- result[adjIndex] = tmp; return result;
- }
SBC - , ( ), , . , . TSP, , , , , - .
, TSP - A,B,C,D,E, A,C,B,D,E. , ( ) . A,D,C,B,E ? SBC , , .
, SBC. , . , - .
GenerateNeighborMemoryMatrix memoryMatrix ( ) result. result, random, . , ; , .
maxNumberVisits. , maxNumberVisits , . , (A,B,C), , ( A B, B C, A C). 20 maxNumberVisits 20 * 5 = 100.
, , MeasureOfQuality, :
- public double MeasureOfQuality(char[] memoryMatrix) {
- double answer = 0.0;
- for (int i = 0; i < memoryMatrix.Length - 1; ++i) {
- char c1 = memoryMatrix[i];
- char c2 = memoryMatrix[i + 1];
- double d = this.citiesData.Distance(c1, c2);
- answer += d;
- }
- return answer;
- }
SBC , . , . .
MeasureOfQuality , memoryMatrix, Distance CitiesData . , , , (ordinal distance) . - . SBC MeasureOfQuality , . , .
Solve , -, . : ProcessActiveBee, ProcessScoutBee ProcessInactiveBee. - DoWaggleDance. Solve . 9.
. 9. Solve
- public void Solve(bool doProgressBar) {
- bool pb = doProgressBar;
- int numberOfSymbolsToPrint = 10;
- int increment = this.maxNumberCycles / numberOfSymbolsToPrint;
- if (pb) Console.WriteLine("\nEntering SBC Traveling Salesman Problem algorithm main processing loop\n");
- if (pb) Console.WriteLine("Progress: |==========|");
- if (pb) Console.Write(" ");
- int cycle = 0;
-
- while (cycle < this.maxNumberCycles) {
- for (int i = 0; i < totalNumberBees; ++i) {
- if (this.bees[i].status == 1)
- ProcessActiveBee(i);
- else if (this.bees[i].status == 2)
- ProcessScoutBee(i);
- else if (this.bees[i].status == 0)
- ProcessInactiveBee(i);
- }
- ++cycle;
-
- if (pb && cycle % increment == 0)
- Console.Write("^");
- }
-
- if (pb) Console.WriteLine("");
- }
ProcessActiveBee, ProcessScoutBee ProcessInactiveBee. , Solve, , . SBC . , Solve .
pb , . numberOfSymbolsToPrint 10, 10% , maxNumberCycles ( increment , , 10% ).
cycle, , while. ( for.) for bees Bee . , doProgressBar true, % , ( <^>).
SBC ( ). ProcessActiveBee . 10.
. 10. ProcessActiveBee
- private void ProcessActiveBee(int i) {
- char[] neighbor = GenerateNeighborMemoryMatrix(bees[i].memoryMatrix);
- double neighborQuality = MeasureOfQuality(neighbor);
- double prob = random.NextDouble();
- bool memoryWasUpdated = false;
- bool numberOfVisitsOverLimit = false;
-
- if (neighborQuality < bees[i].measureOfQuality) { // better
- if (prob < probMistake) { // mistake
- ++bees[i].numberOfVisits;
- if (bees[i].numberOfVisits > maxNumberVisits)
- numberOfVisitsOverLimit = true;
- }
- else { // No mistake
- Array.Copy(neighbor, bees[i].memoryMatrix, neighbor.Length);
- bees[i].measureOfQuality = neighborQuality;
- bees[i].numberOfVisits = 0;
- memoryWasUpdated = true;
- }
- }
- else { // Did not find better neighbor
- if (prob < probMistake) { // Mistake
- Array.Copy(neighbor, bees[i].memoryMatrix, neighbor.Length);
- bees[i].measureOfQuality = neighborQuality;
- bees[i].numberOfVisits = 0;
- memoryWasUpdated = true;
- }
- else { // No mistake
- ++bees[i].numberOfVisits;
- if (bees[i].numberOfVisits > maxNumberVisits)
- numberOfVisitsOverLimit = true;
- }
- }
-
- if (numberOfVisitsOverLimit == true) {
- bees[i].status = 0;
- bees[i].numberOfVisits = 0;
- int x = random.Next(numberInactive);
- bees[indexesOfInactiveBees[x]].status = 1;
- indexesOfInactiveBees[x] = i;
- }
- else if (memoryWasUpdated == true) {
- if (bees[i].measureOfQuality < this.bestMeasureOfQuality) {
- Array.Copy(bees[i].memoryMatrix, this.bestMemoryMatrix,
- bees[i].memoryMatrix.Length);
- this.bestMeasureOfQuality = bees[i].measureOfQuality
- }
- DoWaggleDance(i);
- }
- else {
- return;
- }
- }
ProcessActiveBee , i, bees. , , memoryMatrix, :
- char[] neighbor =
- GenerateNeighborMemoryMatrix(bees[i].memoryMatrix);
- double neighborQuality = MeasureOfQuality(neighbor);
, :
- double prob = random.NextDouble();)
- bool memoryWasUpdated = false;
- bool numberOfVisitsOverLimit = false;
prob 0.0 1.0 probMistake, , , . . .
memoryWasUpdated , , ( true) ( false). numberOfVisitsOverLimit maxNumberVisits, , , . , .
, , , . , , , , , , .
, , (probMistake = 0.01). , , , SBC, .
, , maxNumberVisits. , , indexesOfInactiveBees. , . , , DoWaggleDance, .
DoWaggleDance , . DoWaggleDance:
- private void DoWaggleDance(int i) {
- for (int ii = 0; ii < numberInactive; ++ii) {
- int b = indexesOfInactiveBees[ii];
- if (bees[i].measureOfQuality < bees[b].measureOfQuality) {
- double p = random.NextDouble();
- if (this.probPersuasion > p) {
- Array.Copy(bees[i].memoryMatrix, bees[b].memoryMatrix,
- bees[i].memoryMatrix.Length);
- bees[b].measureOfQuality = bees[i].measureOfQuality;
- }
- }
- }
- }
i - , . . ( probPersuasion = 0.90), .
, , , . , for DoWaggleDance :
- if (bees[b].status != 0) throw new Exception(. . .);
:
- if (bees[b].numberOfVisits != 0) throw new Exception(. . .);
ProcessScoutBee, Solve, -, . ProcessScoutBee . 11.
. 11. ProcessScoutBee
- private void ProcessScoutBee(int i) {
- char[] randomFoodSource = GenerateRandomMemoryMatrix();
- double randomFoodSourceQuality =
- MeasureOfQuality(randomFoodSource);
- if (randomFoodSourceQuality < bees[i].measureOfQuality {
- Array.Copy(randomFoodSource, bees[i].memoryMatrix,
- randomFoodSource.Length);
- bees[i].measureOfQuality = randomFoodSourceQuality;
- if (bees[i].measureOfQuality < bestMeasureOfQuality) {
- Array.Copy(bees[i].memoryMatrix, this.bestMemoryMatrix,
- bees[i].memoryMatrix.Length);
- this.bestMeasureOfQuality = bees[i].measureOfQuality;
- }
- DoWaggleDance(i);
- }
- }
i - bees. - , , , , , . , . - , , .
, SBC - , . - SBC .
ProcessInactiveBee :
- `private void ProcessInactiveBee(int i) {
- return;
- }
- private void ProcessInactiveBee(int i) {
- return;
- }
SBC , ProcessInactiveBee - , . - .
, SBC . SBC , . - ( ), - (neighbor solution) .
, , , - . , ( , ). - (NP-complete) - (NP-hard) ( - ), SBC.
SBC . , (genetic algorithms, GA), , (ant colony optimization, ACO), , (simulated annealing, SA), .
. , , , , (solution convergence speed) . SBC , , , , , .
SBC , .
(Dr. James McCaffrey) Volt Information Sciences Inc. Microsoft, ( ). , Internet Explorer MSN Search. <.NET Test Automation Recipes> (Apress, 2006). jammc@microsoft.com.
Microsoft Research (Dan Liebling) (Anne Loomis Thompson)
10.11.2021 - 12:37: - Personalias -> WHO IS WHO - - _.
10.11.2021 - 12:36: - Conscience -> . ? - _.
10.11.2021 - 12:36: , , - Upbringing, Inlightening, Education -> ... - _.
10.11.2021 - 12:35: - Ecology -> - _.
10.11.2021 - 12:34: , - War, Politics and Science -> - _.
10.11.2021 - 12:34: , - War, Politics and Science -> . - _.
10.11.2021 - 12:34: , , - Upbringing, Inlightening, Education -> , - _.
10.11.2021 - 09:18: - New Technologies -> , 5G- - _.
10.11.2021 - 09:18: - Ecology -> - _.
10.11.2021 - 09:16: - Ecology -> - _.
10.11.2021 - 09:15: , , - Upbringing, Inlightening, Education -> - _.
10.11.2021 - 09:13: , , - Upbringing, Inlightening, Education -> - _.