/// 最短経路計算実行 /// nStart => スタートノードのインデックス /// nCount => 検索回数 public ACONode Execute(ACONode startNode, ACONode goalNode, double alpha, double beta, int SEED, int NumOfSolve, int n) { int S = 0; ACONode ansewr = startNode; if (startNode == null) { return(null); } foreach (ACOBranch branch in _branches) //pheromoneの初期化 { branch.pheromone = 0.0; } while (S != NumOfSolve) { newagent(n); ansewr = AgentMove(startNode, goalNode, alpha, beta, SEED); UpdatePheromone(goalNode); S++; } return(ansewr); }
public ACOBranch(ACONode node1, ACONode node2) //new時の 初期化関数 { Node1 = node1; Node2 = node2; Distance = 1.0; pheromone = 0.0; p = 0.0; }
/// コンストラクタ /// nNodeCount => 全ノード数 /// class Dijkstrの初期化関数 public ACOalgo(int nNodeCount) { _nodes = new List <ACONode>(); for (int i = 0; i < nNodeCount; i++) { var c = new ACONode(); c.nodeNumber = i; _nodes.Add(c); } }
private void UpdatePheromone(ACONode goal) { foreach (Agent agent in _agent) { if (agent.SourceNode == goal) { foreach (ACOBranch branch in _branches) { if (agent.agentbranch.Contains(branch)) { branch.pheromone = 1 / agent.sumdistance; } } } } }
// Use this for initialization void Start() { //初期化 aco = new ACOalgo(count); branches = new List <ACOBranch>(); foreach (int[] bond in info) { branches.Add(new ACOBranch(aco.Nodes[bond[0]], aco.Nodes[bond[1]])); } //breanch入れていく aco.Branches = branches; answerNode = aco.Execute(aco.Nodes[0], aco.Nodes[12], a, b, seed, solve, agentnum); print(answerNode.nodeNumber); }
private ACONode AgentMove(ACONode startnode, ACONode goalnode, double alpha, double beta, int SEED) { System.Random rnd = new System.Random(SEED); ACONode ansernode = startnode; double sumdistance = 0.0; double sumpheromone = 0.0; double sump = 0.0; foreach (Agent agent in _agent) { agent.SourceNode = startnode; sumdistance = 0.0; sumpheromone = 0.0; sump = 0.0; while (agent.SourceNode.nodeNumber != goalnode.nodeNumber && agent.SourceNode.nodeNumber != 18) { sumdistance = 0.0; foreach (ACOBranch branch in _branches) { foreach (ACOBranch branch2 in _branches) { if (branch2.Node1.Equals(agent.SourceNode) == true) { sumpheromone = sumpheromone + Math.Pow(branch2.pheromone, alpha); sumdistance = sumdistance + Math.Pow(branch2.Distance, beta); print(sumpheromone); print(sumdistance); } else { continue; } } print(branch.p); branch.p = (Math.Pow(branch.pheromone, alpha) + Math.Pow(branch.Distance, beta)) / (sumpheromone + sumdistance); } foreach (ACOBranch branch in _branches) { if (branch.Node1.Equals(agent.SourceNode) == true) { sump = sump + branch.p; // print(branch.p); if (rnd.NextDouble() <= sump) { agent.agentbranch.Add(branch); //print(branch.Node2.nodeNumber); agent.SourceNode = branch.Node2; agent.sumdistance++; } } else { continue; } } if (agent.SourceNode == goalnode) { ansernode = agent.SourceNode; } } } return(ansernode); }