private void PaintConnection(Graphics g, ModelConnection con)
        {
            Point srcPos = DocToViewport(con.SourceNeuron.Position);
            Point tgtPos = DocToViewport(con.TargetNeuron.Position);

            // Connections leave from the base of the neuron and enter the top.
            srcPos.Y += (int)(neuronDiameterHalfed * 0.9);              //  to hide line ends behind neurons.
            tgtPos.Y -= (int)(neuronDiameterHalfed * 0.9);

            // Is any part of the connection within the viewport area?
            if (!IsPointWithinViewport(srcPos) && !IsPointWithinViewport(tgtPos))
            {                   // Don't waste time painting this connection.
                return;
            }

            //----- Modify a pen for this connection.
            // Color and width based on connection weight.
            penConnection.Width = (float)Math.Max(1.0, (Math.Abs(con.Weight) / connectionWidthFactor) * 3.0 * zoomFactor);

            // Color hue. 80% gives rangle of red through to blue.
            float temp = (float)((con.Weight - connectionWeightMin) / connectionWeightRange);

            penConnection.Color = ColorUtilities.FromBlueRedScale(temp);
            // ColorUtilities.FromHls(hue, 1.0F, 0.5F);

            //----- Draw the line.
            if (tgtPos.Y <= srcPos.Y)
            {                   // Target is behind source. Draw a back-connection.
                PaintBackConnection(g, penConnection,
                                    srcPos, tgtPos,
                                    (ConnectionPoints)con.SourceNeuron.AuxPaintingData,
                                    (ConnectionPoints)con.TargetNeuron.AuxPaintingData);
            }
            else
            {                   // Target is ahead of the source. Draw a straight line.
                g.DrawLine(penConnection, srcPos, tgtPos);
            }
        }
		static public NetworkModel DecodeToNetworkModel(NeatGenome.NeatGenome g)
		{
			ModelNeuronList masterNeuronList = new ModelNeuronList();

			// loop all neurons and build a table keyed on id.
			HybridDictionary neuronTable = new HybridDictionary(g.NeuronGeneList.Count);
			foreach(NeuronGene neuronGene in g.NeuronGeneList)
			{
				ModelNeuron modelNeuron = new ModelNeuron(neuronGene.NeuronType, neuronGene.InnovationId,neuronGene.ActivationFunction);
				neuronTable.Add(modelNeuron.Id, modelNeuron);
				masterNeuronList.Add(modelNeuron);
			}

			// Loop through all of the connections.
			// Now we have a neuron table keyed on id we can attach the connections
			// to their source and target neurons.
			foreach(ConnectionGene connectionGene in g.ConnectionGeneList)
			{
				ModelConnection modelConnection = new ModelConnection();
				modelConnection.Weight = connectionGene.Weight;
				modelConnection.SourceNeuron = (ModelNeuron)neuronTable[connectionGene.SourceNeuronId];
				modelConnection.TargetNeuron = (ModelNeuron)neuronTable[connectionGene.TargetNeuronId];

				modelConnection.SourceNeuron.OutConnectionList.Add(modelConnection);
				modelConnection.TargetNeuron.InConnectionList.Add(modelConnection);
			}

            //Sebastian. Build Model connections
            foreach (ModuleGene mg in g.ModuleGeneList)
            {
                foreach (uint sourceID in mg.InputIds)
                {
                    foreach (uint targetID in mg.OutputIds)
                    {
                        ModelConnection modelConnection = new ModelConnection();
                        modelConnection.Weight = 1.0; //TODO  connectionGene.Weight;
                        modelConnection.SourceNeuron = (ModelNeuron)neuronTable[sourceID];
                        modelConnection.TargetNeuron = (ModelNeuron)neuronTable[targetID];

                        modelConnection.SourceNeuron.OutConnectionList.Add(modelConnection);
                        modelConnection.TargetNeuron.InConnectionList.Add(modelConnection);
                    }
                }
            }

			return new NetworkModel(masterNeuronList);
		}
		static public NetworkModel DecodeToNetworkModel(ConcurrentNetwork network)
		{
			ModelNeuronList masterNeuronList = new ModelNeuronList();
			
			// loop all neurons and build a table keyed on id.
			Hashtable neuronTable = new Hashtable(network.MasterNeuronList.Count);
			foreach(Neuron neuron in network.MasterNeuronList)
			{	
				ModelNeuron modelNeuron = new ModelNeuron(neuron.NeuronType, neuron.Id,ActivationFunctionFactory.GetActivationFunction("NullFn"));
				neuronTable.Add(modelNeuron.Id, modelNeuron);
				masterNeuronList.Add(modelNeuron);
			}

			// Loop through all of the connections (within the neurons)
			// Now we have a neuron table keyed on id we can attach the connections
			// to their source and target neurons.
			foreach(Neuron neuron in network.MasterNeuronList)
			{
				foreach(Connection connection in neuron.ConnectionList)
				{
					ModelConnection modelConnection = new ModelConnection();
					modelConnection.Weight = connection.Weight;
					modelConnection.SourceNeuron = (ModelNeuron)neuronTable[connection.SourceNeuronId];
					modelConnection.TargetNeuron = (ModelNeuron)neuronTable[connection.TargetNeuronId];

					modelConnection.SourceNeuron.OutConnectionList.Add(modelConnection);
					modelConnection.TargetNeuron.InConnectionList.Add(modelConnection);
				}
			}

			return new NetworkModel(masterNeuronList);
		}
		private void PaintConnection(Graphics g, ModelConnection con)
		{
			Point srcPos = DocToViewport(con.SourceNeuron.Position);
			Point tgtPos = DocToViewport(con.TargetNeuron.Position);

			// Connections leave from the base of the neuron and enter the top.
			srcPos.Y += (int)(neuronDiameterHalfed*0.9);	//  to hide line ends behind neurons.
			tgtPos.Y -= (int)(neuronDiameterHalfed*0.9);

			// Is any part of the connection within the viewport area?
			if(!IsPointWithinViewport(srcPos) && !IsPointWithinViewport(tgtPos))
			{	// Don't waste time painting this connection.
				return;
			}

		//----- Modify a pen for this connection.
			// Color and width based on connection weight. 
			penConnection.Width = (float)Math.Max(1.0, (Math.Abs(con.Weight)/connectionWidthFactor)*3.0*zoomFactor);

			// Color hue. 80% gives rangle of red through to blue.
			float temp = (float)((con.Weight-connectionWeightMin)/connectionWeightRange);
			penConnection.Color = ColorUtilities.FromBlueRedScale(temp);
									// ColorUtilities.FromHls(hue, 1.0F, 0.5F);

		//----- Draw the line.
			if(tgtPos.Y <= srcPos.Y)
			{	// Target is behind source. Draw a back-connection.
				PaintBackConnection(g, penConnection, 
										srcPos, tgtPos, 
										(ConnectionPoints)con.SourceNeuron.AuxPaintingData,
										(ConnectionPoints)con.TargetNeuron.AuxPaintingData);
			}
			else
			{	// Target is ahead of the source. Draw a straight line.
				g.DrawLine(penConnection, srcPos, tgtPos);
			}
		}