private void btnRun_Click(object sender, EventArgs e) { try { Objects.ForEach(o => o.BgColor = Color.Orange); VisitedNodes = new ObservableCollection <object>(); VisitedNodes.CollectionChanged += _visitedNodes_CollectionChanged; BaseNetwork network = rbPredefinedNetwork.Checked ? (BaseNetwork)cbPredefinedNetworks.SelectedItem : _customNetwork; DistributedAlgorithm <int, string> algorithm = rbFromList.Checked ? (DistributedAlgorithm <int, string>)cbAlgorithms.SelectedItem : (DistributedAlgorithm <int, string>)Activator.CreateInstance((Type)((ComboBoxItem)cbClassFromDll.SelectedItem).Value); NodesManager.Instance.Nodes.Clear(); NodesManager.Instance.Nodes.AddRange(network.GetNetwork(algorithm, new NetworkSimulator(), node_OnNodeMessage)); NodesManager.Instance.Nodes.ForEach(n => ((NetworkSimulator)n.GetNetworkComponent()).CurrentNodeId = n.GetId()); var messages = txtMessage.Text.Split(';'); RedrawPanel(Objects, Connections); for (int i = 0; i < chlInitNodes.CheckedItems.Count; i++) { Objects.First(p => p.Id == ((Node <int, string>)chlInitNodes.CheckedItems[i]).GetId().Id).BgColor = Colors[i % 8]; var thread = new Thread(ExecuteInit); var message = i < messages.Length ? messages[i] : messages[messages.Length - 1]; thread.Start(new List <object> { i, message, Colors[i % 8] }); } } catch (Exception ex) { MessageBox.Show("Error has occured. More information: " + ex); } }
public void ClearExpiredAlgorithms() { lock (_lockObject) { _algorithm = (DistributedAlgorithm <TIdType, TValue>)Activator.CreateInstance(_algorithmTemplate.GetType()); } }
public Node(NodeId <TIdType> id, DistributedAlgorithm <TIdType, TValue> algorithm, NetworkComponent <TIdType, TValue> component) { _id = id; SetAlgorithm(algorithm); SetNetworkComponent(component); _neighbors = new List <NodeId <TIdType> >(); }
public void SetAlgorithm(DistributedAlgorithm <TIdType, TValue> algorithm) { _algorithmTemplate = algorithm; lock (_lockObject) { _algorithm = (DistributedAlgorithm <TIdType, TValue>)Activator.CreateInstance(_algorithmTemplate.GetType()); } }
public void ExecuteInit(Message <TValue> initMessage) { var guid = Guid.NewGuid(); _algorithmEntryTime.Add(new KeyValuePair <Guid, DateTime>(guid, DateTime.Now)); _algorithm = (DistributedAlgorithm <TIdType, TValue>)Activator.CreateInstance(_algorithmTemplate.GetType()); TriggerNodeMessage(initMessage.Value, _id); SendMessage(_algorithm.Init(initMessage, _neighbors, _id)); }
public static void StartDistributed() { var ea = new DistributedAlgorithm(20); var timer = ea.StartAlgorithm(400, 10); Console.WriteLine($"Execution time: {timer}ms"); var fittest = ea.Fittest(); Console.WriteLine($"Fittest Individ:\n{fittest}\n\n Fitness: {fittest.Fitness}"); }
public override List <Node <int, string> > GetNetwork(DistributedAlgorithm <int, string> algorithm, NetworkComponent <int, string> component, Node <int, string> .NodeMessage function) { var result = new List <Node <int, string> >(); foreach (var obj in _objs) { var node = new Node <int, string>(new NodeId <int> { Id = obj.Id }, (DistributedAlgorithm <int, string>)Activator.CreateInstance(algorithm.GetType()), (NetworkComponent <int, string>)Activator.CreateInstance(component.GetType())); node.Neighbors.AddRange(_conns.Where(d => d.Key == obj.Id).Select(d => new NodeId <int> { Id = d.Value })); node.OnNodeMessage += function; result.Add(node); } return(result); }
public override List <Node <int, string> > GetNetwork(DistributedAlgorithm <int, string> algorithm, NetworkComponent <int, string> component, Node <int, string> .NodeMessage function) { var result = new List <Node <int, string> >(); for (var i = 1; i < 8; i++) { var tempNode = new Node <int, string>(new NodeId <int> { Id = i }, (DistributedAlgorithm <int, string>)Activator.CreateInstance(algorithm.GetType()), (NetworkComponent <int, string>)Activator.CreateInstance(component.GetType())); tempNode.Neighbors.Add(new NodeId <int> { Id = i == 7 ? 1 : (i + 1) }); tempNode.Neighbors.Add(new NodeId <int> { Id = i == 1 ? 7 : (i - 1) }); tempNode.OnNodeMessage += function; result.Add(tempNode); } return(result); }
public override List <Node <int, string> > GetNetwork(DistributedAlgorithm <int, string> algorithm, NetworkComponent <int, string> component, Node <int, string> .NodeMessage function) { var result = new List <Node <int, string> >(); var tempNode = new Node <int, string>(new NodeId <int> { Id = 1 }, (DistributedAlgorithm <int, string>)Activator.CreateInstance(algorithm.GetType()), (NetworkComponent <int, string>)Activator.CreateInstance(component.GetType())); tempNode.Neighbors.Add(new NodeId <int> { Id = 2 }); tempNode.Neighbors.Add(new NodeId <int> { Id = 3 }); tempNode.OnNodeMessage += function; result.Add(tempNode); var tempNode1 = new Node <int, string>(new NodeId <int> { Id = 2 }, (DistributedAlgorithm <int, string>)Activator.CreateInstance(algorithm.GetType()), (NetworkComponent <int, string>)Activator.CreateInstance(component.GetType())); tempNode1.Neighbors.Add(new NodeId <int> { Id = 1 }); tempNode1.Neighbors.Add(new NodeId <int> { Id = 3 }); tempNode1.OnNodeMessage += function; result.Add(tempNode1); var tempNode2 = new Node <int, string>(new NodeId <int> { Id = 3 }, (DistributedAlgorithm <int, string>)Activator.CreateInstance(algorithm.GetType()), (NetworkComponent <int, string>)Activator.CreateInstance(component.GetType())); tempNode2.Neighbors.Add(new NodeId <int> { Id = 2 }); tempNode2.Neighbors.Add(new NodeId <int> { Id = 1 }); tempNode2.Neighbors.Add(new NodeId <int> { Id = 4 }); tempNode2.Neighbors.Add(new NodeId <int> { Id = 8 }); tempNode2.OnNodeMessage += function; result.Add(tempNode2); var tempNode3 = new Node <int, string>(new NodeId <int> { Id = 4 }, (DistributedAlgorithm <int, string>)Activator.CreateInstance(algorithm.GetType()), (NetworkComponent <int, string>)Activator.CreateInstance(component.GetType())); tempNode3.Neighbors.Add(new NodeId <int> { Id = 3 }); tempNode3.Neighbors.Add(new NodeId <int> { Id = 5 }); tempNode3.Neighbors.Add(new NodeId <int> { Id = 6 }); tempNode3.Neighbors.Add(new NodeId <int> { Id = 7 }); tempNode3.OnNodeMessage += function; result.Add(tempNode3); var tempNode4 = new Node <int, string>(new NodeId <int> { Id = 5 }, (DistributedAlgorithm <int, string>)Activator.CreateInstance(algorithm.GetType()), (NetworkComponent <int, string>)Activator.CreateInstance(component.GetType())); tempNode4.Neighbors.Add(new NodeId <int> { Id = 4 }); tempNode4.Neighbors.Add(new NodeId <int> { Id = 6 }); tempNode4.OnNodeMessage += function; result.Add(tempNode4); var tempNode5 = new Node <int, string>(new NodeId <int> { Id = 6 }, (DistributedAlgorithm <int, string>)Activator.CreateInstance(algorithm.GetType()), (NetworkComponent <int, string>)Activator.CreateInstance(component.GetType())); tempNode5.Neighbors.Add(new NodeId <int> { Id = 4 }); tempNode5.Neighbors.Add(new NodeId <int> { Id = 5 }); tempNode5.OnNodeMessage += function; result.Add(tempNode5); var tempNode6 = new Node <int, string>(new NodeId <int> { Id = 7 }, (DistributedAlgorithm <int, string>)Activator.CreateInstance(algorithm.GetType()), (NetworkComponent <int, string>)Activator.CreateInstance(component.GetType())); tempNode6.Neighbors.Add(new NodeId <int> { Id = 4 }); tempNode6.Neighbors.Add(new NodeId <int> { Id = 8 }); tempNode6.Neighbors.Add(new NodeId <int> { Id = 9 }); tempNode6.OnNodeMessage += function; result.Add(tempNode6); var tempNode7 = new Node <int, string>(new NodeId <int> { Id = 8 }, (DistributedAlgorithm <int, string>)Activator.CreateInstance(algorithm.GetType()), (NetworkComponent <int, string>)Activator.CreateInstance(component.GetType())); tempNode7.Neighbors.Add(new NodeId <int> { Id = 3 }); tempNode7.Neighbors.Add(new NodeId <int> { Id = 7 }); tempNode7.Neighbors.Add(new NodeId <int> { Id = 9 }); tempNode7.OnNodeMessage += function; result.Add(tempNode7); var tempNode8 = new Node <int, string>(new NodeId <int> { Id = 9 }, (DistributedAlgorithm <int, string>)Activator.CreateInstance(algorithm.GetType()), (NetworkComponent <int, string>)Activator.CreateInstance(component.GetType())); tempNode8.Neighbors.Add(new NodeId <int> { Id = 7 }); tempNode8.Neighbors.Add(new NodeId <int> { Id = 8 }); tempNode8.OnNodeMessage += function; result.Add(tempNode8); return(result); }
public abstract List <Node <int, string> > GetNetwork(DistributedAlgorithm <int, string> algorithm, NetworkComponent <int, string> component, Node <int, string> .NodeMessage function);