Ejemplo n.º 1
0
        protected override void Finished()
        {
            List <char[]> cMasters = Masters.Select(x => x.ToCharArray()).ToList();

            foreach (var x in cMasters)
            {
                var f = new Folding();
                f.BaseType = x;
                f.print(this.GAC.Sequence, this.Log);
                //f.print(this.GAC.Sequence, new ConsoleLogger());
                //Console.WriteLine(CalculateHemming(f.BaseType, cMasters));
            }
            Console.WriteLine(CalculateAVGHemming(Masters.Select(y => y.ToCharArray())));

            var msg = String.Format("S: {0} G: {1} P: {2} M: {3} C: {4}{5}", new string(GAC.Sequence), GA.CurrentGeneration, GAC.PopulationSize, GAC.MutationRate, GAC.CrossoverRate, Environment.NewLine);

            this.Log.Write(msg);
        }
Ejemplo n.º 2
0
 public Folding(Folding folding)
 {
     this.BaseType = new char[folding.BaseType.Length];
     Buffer.BlockCopy(folding.BaseType, 0, this.BaseType, 0, folding.BaseType.Length * sizeof(char));
 }
Ejemplo n.º 3
0
        public void print(char[] reference, Logger log)
        {
            var Order = new List <Point>();

            int Orientation  = 0;
            var Field        = new Dictionary <Point, List <printhelper> >();
            var currentPoint = new Point {
                X = 0, Y = 0
            };

            var MaxP = new Point {
                X = 0, Y = 0
            };
            var MinP = new Point {
                X = 0, Y = 0
            };

            double cNeighbour = 0;
            double cOverlapp  = 0;

            bool bFirst = true;

            int seqLength = this.BaseType.Length;

            if (seqLength != reference.Length - 1)
            {
                throw new ArgumentOutOfRangeException("Sequence lengths do not match");
            }
            // Create Field
            for (int i = 0; i < reference.Length; i++)
            {
                Direction currentDirection;
                if (i == reference.Length - 1)
                {
                    currentDirection = Direction.U;
                }
                else
                {
                    currentDirection = cD[this.BaseType[i]];
                }
                var currentType = cT[reference[i]];

                var iDirection = (int)currentDirection;

                List <printhelper> elems;
                if (Field.TryGetValue(currentPoint, out elems))
                {
                    //overlapp
                    cOverlapp++;

                    Field[currentPoint].Add(new printhelper {
                        isFirst = false, dir = currentDirection, type = currentType, orient = Orientation
                    });
                    Order.Add(currentPoint);
                }
                else
                {
                    var p = new List <printhelper>();
                    p.Add(new printhelper {
                        isFirst = bFirst, dir = currentDirection, type = currentType, orient = Orientation
                    });
                    Field[currentPoint] = p;
                    Order.Add(currentPoint);
                    if (bFirst)
                    {
                        bFirst = false;
                    }
                }

                //check neighbours
                if (currentType == FoldType.Hydrophobic)
                {
                    List <Direction> dirs = currentDirection.GetNeighbours();
                    foreach (Direction dir in dirs)
                    {
                        var neighbour = new Point();
                        neighbour.X = currentPoint.X + (OrientMapping[0][Orientation][(int)dir] * 2);
                        neighbour.Y = currentPoint.Y + (OrientMapping[1][Orientation][(int)dir] * 2);

                        if (Field.TryGetValue(neighbour, out elems))
                        {
                            cNeighbour += elems.Count(x => x.type == FoldType.Hydrophobic);
                        }
                    }
                }

                currentPoint.X += (OrientMapping[0][Orientation][iDirection]) * 2;
                currentPoint.Y += (OrientMapping[1][Orientation][iDirection]) * 2;

                MinP.X = Math.Min(currentPoint.X, MinP.X);
                MinP.Y = Math.Min(currentPoint.Y, MinP.Y);

                MaxP.X = Math.Max(currentPoint.X, MaxP.X);
                MaxP.Y = Math.Max(currentPoint.Y, MaxP.Y);

                Orientation = ((iDirection - 2) + Orientation).mod(4);
            }

            var Fitness = Folding.ScaleFitness(cOverlapp, cNeighbour);

            log.Write("\n");

            var PDiffX = (MaxP.X - MinP.X);
            var PDiffY = (MaxP.Y - MinP.Y);

            log.Write(new string('-', PDiffX + 1) + Environment.NewLine);
            var grid = new char[(PDiffX) + 1, (PDiffY) + 1];

            //add first to grid
            grid[Order[0].X - MinP.X, Order[0].Y - MinP.Y] = reference[0];

            for (var i = 1; i < Order.Count; ++i)
            {
                var x = (Order[i].X - MinP.X);
                var y = (Order[i].Y - MinP.Y);

                if (grid[x, y] == 0)
                {
                    grid[x, y] = reference[i];
                }
                else
                {
                    grid[x, y] = 'X';
                }

                var DiffX = (Order[i].X - Order[i - 1].X) / 2;
                var DiffY = (Order[i].Y - Order[i - 1].Y) / 2;

                var connector = '-';
                if (DiffY != 0)
                {
                    connector = '|';
                }

                grid[x - DiffX, y - DiffY] = connector;
            }

            for (var y = MaxP.Y - MinP.Y; y >= 0; --y)
            {
                for (var x = 0; x <= MaxP.X - MinP.X; ++x)
                {
                    char value = grid[x, y];
                    if (value == 0)
                    {
                        value = ' ';
                    }
                    log.Write(value.ToString());
                }
                log.Write(Environment.NewLine);
            }

            var result = string.Format("F: {0} N: {1} O: {2} S: {3}{4}", Fitness, cNeighbour, cOverlapp, new string(this.BaseType), Environment.NewLine);

            log.Write(new string('-', PDiffX + 1) + Environment.NewLine);
            log.Write(result);
        }
Ejemplo n.º 4
0
        public override double CalculateFitness(char[] reference)
        {
            int Orientation = 0;
            var positions   = new Point[reference.Length];
            var types       = new FoldType[reference.Length];

            var currentPoint = new Point {
                X = 0, Y = 0
            };

            double cNeighbour = 0;
            double cOverlapp  = 0;

            int seqLength = this.BaseType.Length;

            if (seqLength != reference.Length - 1)
            {
                throw new Exception("Sequenzelengths do not match");
            }
            // Create Field
            for (int i = 0; i < reference.Length; i++)
            {
                Direction currentDirection;
                if (i == reference.Length - 1)
                {
                    currentDirection = Direction.U;
                }
                else
                {
                    currentDirection = cD[this.BaseType[i]];
                }
                var currentType = types[i] = cT[reference[i]];

                var iDirection = (int)currentDirection;

                positions[i] = currentPoint;

                currentPoint.X += OrientMapping[0][Orientation][iDirection];
                currentPoint.Y += OrientMapping[1][Orientation][iDirection];

                Orientation = ((iDirection - 2) + Orientation).mod(4);
            }

            int refL = reference.Length - 1;

            for (int leftI = 0; leftI < refL; leftI++)
            {
                for (int rightI = reference.Length - 1; rightI > leftI; rightI--)
                {
                    var posRight = positions[rightI];
                    var posLeft  = positions[leftI];
                    var distance = posRight.distance(positions[leftI]);
                    //Console.WriteLine("{0},{1} -> {2},{3} {4},{5} D: {6}", leftI, rightI, positions[leftI].X, positions[leftI].Y, positions[rightI].X, positions[rightI].Y, distance);
                    if (distance == 0)
                    {
                        //overlapp
                        //Console.WriteLine("Overlapp");
                        cOverlapp++;
                    }
                    else if (distance == 1)
                    {
                        if (types[leftI] == FoldType.Hydrophobic && types[rightI] == FoldType.Hydrophobic)
                        {
                            if (positions[leftI + 1].distance(posRight) != 0)   //nextPoint is not the next Point in chain
                            //Console.WriteLine("{0},{1},{2}", nextX, nextY, nextPos.distance(posRight));
                            //Console.WriteLine("{0},{1} near {2},{3}", positions[leftI].X, positions[leftI].Y, positions[rightI].X, positions[rightI].Y);
                            {
                                cNeighbour++;
                            }
                        }
                    }
                    else
                    {
                        rightI -= (distance - 2);
                    }
                }
            }

            return(Folding.ScaleFitness(cOverlapp, cNeighbour));
        }
Ejemplo n.º 5
0
        protected override void Evaluate()
        {
            double MaxFitness = GA.MaxFitness;
            double AvgFitness = GA.AvgFitness;

            if (MaxFitness > MasterFitness)
            {
                MasterFitness = MaxFitness;
                Masters.Clear();
                Masters.AddRange(GA.Cache.Where(x => x.Fitness >= MaxFitness).Select(x => new string(x.GAElement.BaseType)));
            }
            else if (MaxFitness >= MasterFitness)
            {
                Masters.AddRange(GA.Cache.Where(x => x.Fitness >= MaxFitness).Select(x => new string(x.GAElement.BaseType)));
            }
            string msg;

            if (this.CalculateHamming)
            {
                msg = string.Format("[{0}] Max: {1} MaxF: {2} Avg: {3} AvgF: {4} {5}", GA.CurrentGeneration, MaxFitness, Folding.Neighbours(MaxFitness), AvgFitness, Folding.Neighbours(AvgFitness), CalculateAVGHemming(GA.Cache.Select(x => x.GAElement.BaseType)));
            }
            else
            {
                msg = string.Format("[{0}] Max: {1} MaxF: {2} Avg: {3} AvgF: {4}", GA.CurrentGeneration, MaxFitness, Folding.Neighbours(MaxFitness), AvgFitness, Folding.Neighbours(AvgFitness));
            }
            Log.Log(msg);
        }