Example #1
0
        public double DistanceTo(Genome targetGenome)
        {
            double distanceSquared = 0.0;
            for (int i = 0; i < Genes.Count; i++)
                distanceSquared += Math.Pow(Genes[i].Value - targetGenome.Genes[i].Value, 2.0);

            return Math.Sqrt(distanceSquared);
        }
Example #2
0
        private void makeChild(Genome father, Genome mother)
        {
            Genome child = new Genome();
            for (int i = 0; i < father.Genes.Count; i++)
            {
                Gene fatherGene = father.Genes[i];
                Gene motherGene = mother.Genes[i];
                Gene childGene = new Gene(fatherGene.Min, fatherGene.Max, 0.5 * (fatherGene.Value + motherGene.Value));

                if (Utils.GetRandomDouble() < MutationRate)
                {
                    childGene.Value = childGene.Value + (childGene.Max - childGene.Min) * Utils.GetRandomDouble(-MutationAmount,  MutationAmount);
                    childGene.MakeValueValid();
                }

                child.Genes.Add(childGene);
            }

            //this.computeFitness(child);
            Genomes.Add(child);
        }
        private void computeFitness(Genome genome)
        {
            for (int i = 0; i < genome.Genes.Count; i++)
            {
                double angle = genome.Genes[i].Value;

                edgePlanes[i] = new Plane(
                    edgePlanes[i].Origin,
                    edgePlanes[i].XAxis,
                    Vector3d.CrossProduct(
                        edgeYAxes[i] * Math.Sin(angle) + edgeZAxes[i] * Math.Cos(angle),
                        edgePlanes[i].XAxis)
                    );
            }

            genome.Fitness = 0.0;

            for (int i = 0; i < iSpringMesh.Triangles.Count; i++)
            {
                Triangle triangle = iSpringMesh.Triangles[i];
                Point3d intersectionPoint;
                if (Intersection.PlanePlanePlane(
                    edgePlanes[triangle.FirstEdgeIndex],
                    edgePlanes[triangle.SecondEdgeIndex],
                    edgePlanes[triangle.ThirdEdgeIndex],
                    out intersectionPoint))
                {
                    genome.Fitness += Utils.Distance(
                        trianglePlanes[i].ClosestPoint(intersectionPoint),
                        triangleCentres[i]);
                }
            }

            if (double.IsNaN(genome.Fitness))
            {
                int a = 2;
                int b = a + 1;
            }
        }