Пример #1
0
        private static NeuralLink[] BuildExternalLinksExisting_AcrossParts(NeuralLinkExternalDNA dnaLink, HighestPercentResult[] partLinks, ContainerInput[] containers, Dictionary<ContainerInput, ContainerPoints> nearestNeurons, int maxIntermediateLinks, int maxFinalLinks)
        {
            List<NeuralLink> retVal = new List<NeuralLink>();

            foreach (HighestPercentResult partLink in partLinks)
            {
                #region Get containers

                ContainerInput fromContainer = containers[partLink.From.Index];
                if (!nearestNeurons.ContainsKey(fromContainer))
                {
                    nearestNeurons.Add(fromContainer, new ContainerPoints(fromContainer, maxIntermediateLinks));
                }
                ContainerPoints from = nearestNeurons[fromContainer];

                ContainerInput toContainer = containers[partLink.To.Index];
                if (!nearestNeurons.ContainsKey(toContainer))
                {
                    nearestNeurons.Add(toContainer, new ContainerPoints(toContainer, maxIntermediateLinks));
                }
                ContainerPoints to = nearestNeurons[toContainer];

                #endregion

                List<LinkIndexed> links = new List<LinkIndexed>();

                // Build links
                HighestPercentResult[] bestLinks = GetHighestPercent(from.GetNearestPoints(dnaLink.From), to.GetNearestPoints(dnaLink.To), maxIntermediateLinks, false);
                foreach (HighestPercentResult link in bestLinks)
                {
                    double[] brainChemicals = null;
                    if (dnaLink.BrainChemicalModifiers != null)
                    {
                        brainChemicals = dnaLink.BrainChemicalModifiers.
                            Take(fromContainer.BrainChemicalCount).		// if there are more, just drop them
                            Select(o => o).
                            //Select(o => o * link.Percent).		// I decided not to multiply by percent.  The weight is already reduced, no point in double reducing
                            ToArray();
                    }

                    links.Add(new LinkIndexed(link.From.Index, link.To.Index, dnaLink.Weight * link.Percent, brainChemicals));
                }

                // Convert the indices into return links
                retVal.AddRange(links.Select(o => new NeuralLink(fromContainer.Container, toContainer.Container, from.AllNeurons[o.From], to.AllNeurons[o.To], partLink.Percent * o.Weight, o.BrainChemicalModifiers)));
            }

            if (retVal.Count > maxFinalLinks)
            {
                // Prune and normalize percents
                var pruned = Prune(retVal.Select(o => o.Weight).ToArray(), maxFinalLinks);		// choose the top X weights, and tell what the new weight should be
                retVal = pruned.Select(o => new NeuralLink(retVal[o.Item1].FromContainer, retVal[o.Item1].ToContainer, retVal[o.Item1].From, retVal[o.Item1].To, o.Item2, retVal[o.Item1].BrainChemicalModifiers)).ToList();		// copy the referenced retVal items, but with the altered weights
            }

            // Exit Function
            return retVal.ToArray();
        }
Пример #2
0
 public ContainerInput(long token, INeuronContainer container, NeuronContainerType containerType, Point3D position, Quaternion orientation, double? internalRatio, Tuple<NeuronContainerType, ExternalLinkRatioCalcType, double>[] externalRatios, int brainChemicalCount, NeuralLinkDNA[] internalLinks, NeuralLinkExternalDNA[] externalLinks)
 {
     this.Token = token;
     this.Container = container;
     this.ContainerType = containerType;
     this.Position = position;
     this.Orientation = orientation;
     this.InternalRatio = internalRatio;
     this.ExternalRatios = externalRatios;
     this.BrainChemicalCount = brainChemicalCount;
     this.InternalLinks = internalLinks;
     this.ExternalLinks = externalLinks;
 }