/// <summary>
        /// Вспомогательная функция, для выбора нужного кодирования и метода скрещивания.
        /// </summary>
        public static void AssignedDelegat(int typeGA, int typeCrossing)
        {
            switch (typeGA)
            {
            case 0:

                GeneticAlgoritm = GeneticAlgoritmReal;
                switch (typeCrossing)
                {
                case 0: Crossing = CrossingRealOnePoint; break;

                case 1: Crossing = CrossingBLXalpha; break;
                }
                break;


            case 1:
                GeneticAlgoritm = GeneticAlgoritmInteger;
                switch (typeCrossing)
                {
                case 0: Crossing = CrossingIntegerOnePoint; break;

                case 1: Crossing = CrossingIntegerTwoPoint; break;

                case 2: Crossing = CrossingIntegerUniform; break;
                }
                break;
            }
        }
예제 #2
0
        public SubjectTraveller[] SelectCrossMutate(SubjectTraveller[] population, CrossingType crossType)
        {
            //SortPopulationByTravelTimeAsc();  //NIE MA TO SENSU - W TURNIEJU TO I TAK WYBIERANY JEST NAJLEPSZY POSROD KILKU
            List <SubjectTraveller> newGeneration = new List <SubjectTraveller>(Env.POP_SIZE);

            //int actualPopSize = 0;

            while (newGeneration.Count <= Env.POP_SIZE)
            {
                double           rndNum = ThreadSafeRandom.ThisThreadsRandom.NextDouble();
                SubjectTraveller child;
                SubjectTraveller mother = TournamentSelection(Env.CROSS_RATE, Env.TOURNAMENT_SIZE, population);

                if (rndNum <= Env.CROSS_RATE)
                {
                    SubjectTraveller father = TournamentSelection(Env.CROSS_RATE, Env.TOURNAMENT_SIZE, population);
                    child = crossType(mother, father);
                }
                else
                {
                    child = new SubjectTraveller(mother);
                }
                child.Mutate();
                if (!newGeneration.Contains(child))
                {
                    newGeneration.Add(child);
                }
            }

            return(newGeneration.ToArray());
        }
예제 #3
0
    public Crossing(CrossingType ct)
    {
        this.ReferencePath = new roadPiece[4];
        // Create all the end point needed by the crossing, those will be connected by the simulator to the other crossing
        // The direction is relative to the crossing,
        // Deg0
        endPoints = new roadPiece[4];
        endPoints[(int)Oriention.Degree0]             = new roadPiece((roadPiece)null);
        endPoints[(int)Oriention.Degree0].orientation = getGlobalOrientationFromLocal(global::Oriention.Degree0, this.Oriention);
        endPoints[(int)Oriention.Degree0].coordinate  = new System.Drawing.Point(10, 10);

        // Deg90
        endPoints[(int)Oriention.Degree90]             = new roadPiece((roadPiece)null);
        endPoints[(int)Oriention.Degree90].orientation = getGlobalOrientationFromLocal(global::Oriention.Degree90, this.Oriention);
        endPoints[(int)Oriention.Degree90].coordinate  = new System.Drawing.Point(10, 10);

        // Deg180
        endPoints[(int)Oriention.Degree180]             = new roadPiece((roadPiece)null);
        endPoints[(int)Oriention.Degree180].orientation = getGlobalOrientationFromLocal(global::Oriention.Degree180, this.Oriention);
        endPoints[(int)Oriention.Degree180].coordinate  = new System.Drawing.Point(10, 10);

        // Deg270
        endPoints[(int)Oriention.Degree270]             = new roadPiece((roadPiece)null);
        endPoints[(int)Oriention.Degree270].orientation = getGlobalOrientationFromLocal(global::Oriention.Degree270, this.Oriention);
        endPoints[(int)Oriention.Degree270].coordinate  = new System.Drawing.Point(10, 10);


        switch (ct)
        {
        case CrossingType.CrossingWithoutPedestrian:
            crossingWithoutPedestrian();
            break;
        }
    }
예제 #4
0
 /// <summary>
 /// If "swapped" is true, the loops A and B have been swapped.  This affects
 /// how arguments are passed to the given loop relation, since for example
 /// A.Contains(B) is not the same as B.Contains(A).
 /// </summary>
 public IndexCrosser(S2ShapeIndex a_index, S2ShapeIndex b_index, CrossingType type, EdgePairVisitor visitor, bool swapped)
 {
     a_index_           = a_index;
     b_index_           = b_index;
     visitor_           = visitor;
     min_crossing_sign_ = type == CrossingType.INTERIOR ? 1 : 0;
     swapped_           = swapped;
     b_query_           = new S2CrossingEdgeQuery(b_index_);
 }
예제 #5
0
 public Way(string name, Node[] nodes, int?lanes, RoadType roadType, Sidewalk sidewalk, int?layer, CrossingType crossing, Surface surface, bool oneWay)
 {
     Name     = name;
     Nodes    = nodes;
     Lanes    = lanes;
     RoadType = roadType;
     Sidewalk = sidewalk;
     OneWay   = oneWay;
     Layer    = layer;
     Crossing = crossing;
     Surface  = surface;
 }
예제 #6
0
    // Constrcutor
    public Crossing(Point pp, CrossingType ct, Image img) : base(pp)
    {
        this.Image      = img;
        this.Coordinate = pp;

        this.ReferencePath       = new roadPiece[4];
        this.ReferencePathLinked = new Boolean[4];
        // Create all the end point needed by the crossing, those will be connected by the simulator to the other crossing
        // The direction is relative to the crossing,
        // Deg0 Done
        EndPoints = new roadPiece[4];
        EndPoints[(int)Orientation.Degree0]             = new roadPiece(this, (roadPiece)null);
        EndPoints[(int)Orientation.Degree0].orientation = getGlobalOrientationFromLocal(global::Orientation.Degree0, this.Oriention);
        EndPoints[(int)Orientation.Degree0].coordinate  = new System.Drawing.Point(10, 55);

        // Deg90 Done
        EndPoints[(int)Orientation.Degree90]             = new roadPiece(this, (roadPiece)null);
        EndPoints[(int)Orientation.Degree90].orientation = getGlobalOrientationFromLocal(global::Orientation.Degree90, this.Oriention);
        EndPoints[(int)Orientation.Degree90].coordinate  = new System.Drawing.Point(84, 10);

        // Deg180 Done
        EndPoints[(int)Orientation.Degree180]             = new roadPiece(this, (roadPiece)null);
        EndPoints[(int)Orientation.Degree180].orientation = getGlobalOrientationFromLocal(global::Orientation.Degree180, this.Oriention);
        EndPoints[(int)Orientation.Degree180].coordinate  = new System.Drawing.Point(135, 85);

        // Deg270 Done
        EndPoints[(int)Orientation.Degree270]             = new roadPiece(this, (roadPiece)null);
        EndPoints[(int)Orientation.Degree270].orientation = getGlobalOrientationFromLocal(global::Orientation.Degree270, this.Oriention);
        EndPoints[(int)Orientation.Degree270].coordinate  = new System.Drawing.Point(56, 135);


        switch (ct)
        {
        case CrossingType.CrossingWithoutPedestrian:
            Image = TrafficLightSimulator.Properties.Resources.crossingA;
            crossingWithoutPedestrian();
            break;

        case CrossingType.CrossingWithPedestrian:
            Image = TrafficLightSimulator.Properties.Resources.crossingB;
            crossingWithPedestrian();
            break;

        default:
            System.Windows.Forms.MessageBox.Show("Error From the Crossing class");
            break;
        }
    }
예제 #7
0
        /// <summary>
        /// Visits all pairs of crossing edges in the given S2ShapeIndex, terminating
        /// early if the given EdgePairVisitor function returns false (in which case
        /// VisitCrossings returns false as well).  "type" indicates whether all
        /// crossings should be visited, or only interior crossings.
        ///
        /// If "need_adjacent" is false, then edge pairs of the form (AB, BC) may
        /// optionally be ignored (even if the two edges belong to different edge
        /// chains).  This option exists for the benefit of FindSelfIntersection(),
        /// which does not need such edge pairs (see below).
        /// </summary>
        private static bool VisitCrossings(S2ShapeIndex index, CrossingType type, bool need_adjacent, EdgePairVisitor visitor)
        {
            // TODO(ericv): Use brute force if the total number of edges is small enough
            // (using a larger threshold if the S2ShapeIndex is not constructed yet).
            var count = index.GetEnumerableCount();

            for (var pos = 0; pos < count; pos++)
            {
                var shape_edges = new ShapeEdgeVector();
                var icell       = index.GetIndexCell(pos);
                var indexCell   = icell.Value.Item2;
                GetShapeEdges(index, indexCell, shape_edges);
                if (!VisitCrossings(shape_edges, type, need_adjacent, visitor))
                {
                    return(false);
                }
            }
            return(true);
        }
예제 #8
0
        /// <summary>
        /// Given a vector of edges within an S2ShapeIndexCell, visit all pairs of
        /// crossing edges (of the given CrossingType).
        /// </summary>
        private static bool VisitCrossings(ShapeEdgeVector shape_edges, CrossingType type, bool need_adjacent, EdgePairVisitor visitor)
        {
            var min_crossing_sign = type == CrossingType.INTERIOR ? 1 : 0;
            var num_edges         = shape_edges.Count;

            for (int i = 0; i + 1 < num_edges; i++)
            {
                var a = shape_edges[i];
                var j = i + 1;

                // A common situation is that an edge AB is followed by an edge BC.  We
                // only need to visit such crossings if "need_adjacent" is true (even if
                // AB and BC belong to different edge chains).
                if (!need_adjacent && a.V1 == shape_edges[j].V0)
                {
                    j++;
                    if (j >= num_edges)
                    {
                        break;
                    }
                }

                var crosser = new S2EdgeCrosser(a.V0, a.V1);
                for (; j < num_edges; j++)
                {
                    var b = shape_edges[j];
                    if (crosser.C == S2Point.Empty || crosser.C != b.V0)
                    {
                        crosser.RestartAt(b.V0);
                    }
                    var sign = crosser.CrossingSign(b.V1);
                    if (sign >= min_crossing_sign)
                    {
                        if (!visitor(a, b, sign == 1))
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
예제 #9
0
    // Properties

    // Constructor
    public TrafficLight(CrossingType ct)
    {
        switch (ct)
        {
        case CrossingType.CrossingWithPedestrian:
            // For pedstrian
            pedstrianTrafficCordinates[(int)Orientation.Degree0]   = new Point(30, 30);
            pedstrianTrafficCordinates[(int)Orientation.Degree90]  = new Point(120, 10);
            pedstrianTrafficCordinates[(int)Orientation.Degree180] = new Point(30, 140);
            pedstrianTrafficCordinates[(int)Orientation.Degree270] = new Point(104, 140);
            // For Cards
            trafficCordinates[(int)Orientation.Degree0]  = new Point(120, 30);
            trafficCordinates[(int)Orientation.Degree90] = new Point(30, 120);
            break;

        //
        case CrossingType.CrossingWithoutPedestrian:
            trafficCordinates[(int)Orientation.Degree0]   = new Point(32, 32);
            trafficCordinates[(int)Orientation.Degree90]  = new Point(120, 30);
            trafficCordinates[(int)Orientation.Degree180] = new Point(32, 120);
            trafficCordinates[(int)Orientation.Degree270] = new Point(110, 120);
            break;
        }
    }
예제 #10
0
        public void StartTTP(int sizeOfPopulation, int coutOfGenerations, int itemChooseMethod, double mutProp, int crossType)
        {
            foreach (City c in _container._Cities)
            {
                c.SortItemsBy(itemChooseMethod);
            }

            SubjectTraveller[] _population;
            _population = new SubjectTraveller[sizeOfPopulation];
            InitPopulation(_population, mutProp);

            //CHECK POPULATION
            foreach (SubjectTraveller subTrav in _population)
            {
                subTrav.TravelTime = FittnesFunction(subTrav);

                if (subTrav.TravelTime > _bestG)
                {
                    _bestG = subTrav.TravelTime;
                }
                if (subTrav.TravelTime < _worstG)
                {
                    _worstG = subTrav.TravelTime;
                }
                _sumG += subTrav.TravelTime;

                //Console.WriteLine(subTrav); //print to check
            }
            _avgG = _sumG / _population.Length;


            CrossingType ctD;

            if (crossType == 1)
            {
                ctD = new CrossingType(CrossOverInKindOfUniform);
            }
            else if (crossType == 2)
            {
                ctD = new CrossingType(CrossOverOX1);
            }
            else
            {
                ctD = new CrossingType(CrossOverOX1);
            }

            for (int i = 0; i < Env.GENERATION_COUNT; i++)
            {
                _sumG = 0;
                double bestFittnesInGeneration  = Double.MinValue;
                double worstFittnesInGeneration = Double.MaxValue;
                //SELECT CANDIDATS TO CROSSING
                //CROSS
                //MUTATE

                _population = SelectCrossMutate(_population, ctD);

                //CHECK NEW POPULATION
                foreach (SubjectTraveller subTrav in _population)
                {
                    subTrav.TravelTime = FittnesFunction(subTrav);
                    if (subTrav.TravelTime > bestFittnesInGeneration)
                    {
                        bestFittnesInGeneration = subTrav.TravelTime;
                    }
                    if (subTrav.TravelTime < worstFittnesInGeneration)
                    {
                        worstFittnesInGeneration = subTrav.TravelTime;
                    }
                    if (subTrav.TravelTime > _bestG)
                    {
                        _bestG = subTrav.TravelTime;
                    }
                    if (subTrav.TravelTime < _worstG)
                    {
                        _worstG = subTrav.TravelTime;
                    }
                    _sumG += subTrav.TravelTime;
                }
                _avgG = _sumG / _population.Length;
            }
        }
예제 #11
0
 public void ShouldExposeTypeSpecificValuesThroughAnEnumerable()
 {
     Assert.That(CrossingType.Has(CrossingType.Pelican));
     Assert.That(CrossingType.Has(CrossingType.Zebra));
 }
예제 #12
0
 public void ShouldNotBeCaseSensitiveWhenParsingTinyType()
 {
     Assert.That(CrossingType.From("PelIcan"), Is.EqualTo(CrossingType.Pelican));
 }
예제 #13
0
 public void ShouldIdentifyATinyTypeFromStringValue()
 {
     Assert.That(CrossingType.From("pelican"), Is.EqualTo(CrossingType.Pelican));
     Assert.That(CrossingType.From("zebra"), Is.EqualTo(CrossingType.Zebra));
 }
예제 #14
0
        private void canvas_pic_MouseClick(object sender, MouseEventArgs e)
        {
            if (!KnotIsClosed)
            {
                Point newP = new Point(e.X, e.Y);
                newP = new Point(e.X, e.Y);
                MovablePoint movPoint = new MovablePoint(newP);
                //movPoint.MouseMove += MovPoint_MouseMove;

                if (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right)
                {
                    CrossingType crossType = e.Button == MouseButtons.Left ? CrossingType.Under : CrossingType.Over;
                    //char charType = e.Button == MouseButtons.Left ? 'U' : 'O';
                    points.Add(movPoint);
                    canvas_pic.Controls.Add(movPoint);

                    if (points.Count >= 2)
                    {
                        MovablePoint beforeLast  = points.Skip(points.Count - 2).First();
                        Line         line        = new Line(beforeLast, movPoint);
                        MovablePoint last        = points.Last();
                        List <Point> interPoints = new List <Point>();

                        bool doNotCross = true;


                        for (int i = 0; i < lines.Count; i++)
                        {
                            Point?p = line.Intersect(lines[i]);
                            if (p.HasValue)
                            {
                                if (!lines[i].Intersections.Any(ip => ip.Distance(p.Value) <= 5))
                                {
                                    interPoints.Add(p.Value);

                                    IntersectionPoint p1 = new IntersectionPoint(p.Value, crossType);
                                    IntersectionPoint p2 = new IntersectionPoint(p.Value, crossType == CrossingType.Over ? CrossingType.Under : CrossingType.Over);
                                    p1.gaussCross = crossType == CrossingType.Over ? indexCross : -indexCross;
                                    p2.gaussCross = -p1.gaussCross;
                                    indexCross++;
                                    line.Intersections.Add(p1);
                                    lines[i].Intersections.Add(p2);
                                    lines[i].OrderPoints();
                                    doNotCross = false;

                                    int indexOfLine = renderingOrder.IndexOf(lines[i]);
                                    if (p1.CrossingType == CrossingType.Over)
                                    {
                                        renderingOrder.Insert(indexOfLine + 1, p1);
                                        renderingOrder.Add(line);
                                    }
                                    else
                                    {
                                        renderingOrder.Insert(indexOfLine, p1);
                                        renderingOrder.Insert(indexOfLine, line);
                                    }
                                }
                            }
                        }

                        if (doNotCross)
                        {
                            renderingOrder.Add(line);
                        }

                        line.OrderPoints();
                        lines.Add(line);

                        //if (interPoints.Count > 0)
                        //    gaussCode_txt.Text += new string(charType, interPoints.Count);
                    }
                }

                this.Invalidate();
            }
        }
예제 #15
0
 public IntersectionPoint(Point p, CrossingType cross)
 {
     Point        = p;
     CrossingType = cross;
 }
예제 #16
0
        /// <summary>
        /// Like the above, but visits all pairs of crossing edges where one edge comes
        /// from each S2ShapeIndex.
        ///
        /// CAVEAT: Crossings may be visited more than once.
        /// </summary>
        public static bool VisitCrossingEdgePairs(S2ShapeIndex a_index, S2ShapeIndex b_index, CrossingType type, EdgePairVisitor visitor)
        {
            // We look for S2CellId ranges where the indexes of A and B overlap, and
            // then test those edges for crossings.

            // TODO(ericv): Use brute force if the total number of edges is small enough
            // (using a larger threshold if the S2ShapeIndex is not constructed yet).
            var ai = new RangeEnumerator(a_index);
            var bi = new RangeEnumerator(b_index);
            var ab = new IndexCrosser(a_index, b_index, type, visitor, false);  // Tests A against B
            var ba = new IndexCrosser(b_index, a_index, type, visitor, true);   // Tests B against A

            while (!ai.Done() || !bi.Done())
            {
                if (ai.RangeMax < bi.RangeMin)
                {
                    // The A and B cells don't overlap, and A precedes B.
                    ai.SeekTo(bi);
                }
                else if (bi.RangeMax < ai.RangeMin)
                {
                    // The A and B cells don't overlap, and B precedes A.
                    bi.SeekTo(ai);
                }
                else
                {
                    // One cell contains the other.  Determine which cell is larger.
                    var ab_relation = ai.Id.LowestOnBit() - bi.Id.LowestOnBit();
                    if (ab_relation > 0)
                    {
                        // A's index cell is larger.
                        if (!ab.VisitCrossings(ai, bi))
                        {
                            return(false);
                        }
                    }
                    else if (ab_relation < 0)
                    {
                        // B's index cell is larger.
                        if (!ba.VisitCrossings(bi, ai))
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        // The A and B cells are the same.
                        if (ai.Cell.NumEdges() > 0 && bi.Cell.NumEdges() > 0)
                        {
                            if (!ab.VisitCellCellCrossings(ai.Cell, bi.Cell))
                            {
                                return(false);
                            }
                        }
                        ai.MoveNext();
                        bi.MoveNext();
                    }
                }
            }
            return(true);
        }
예제 #17
0
        /// <summary>
        /// Visits all pairs of crossing edges in the given S2ShapeIndex, terminating
        /// early if the given EdgePairVisitor function returns false (in which case
        /// VisitCrossings returns false as well).  "type" indicates whether all
        /// crossings should be visited, or only interior crossings.
        ///
        /// CAVEAT: Crossings may be visited more than once.
        /// </summary>
        public static bool VisitCrossingEdgePairs(S2ShapeIndex index, CrossingType type, EdgePairVisitor visitor)
        {
            var needAdjacent = type == CrossingType.ALL;

            return(VisitCrossings(index, type, needAdjacent, visitor));
        }
예제 #18
0
        public void StartGeneticTTP(int sizeOfPopulation, int coutOfGenerations, int itemChooseMethod, double mutProp, int crossType)
        {
            //sort items in Cities 1- sortByBestRatio(value/weight)
            foreach (City c in _container._Cities)
            {
                c.SortItemsBy(itemChooseMethod);
            }

            //INIT POPULATION
            SubjectTraveller[] _population;
            _population = new SubjectTraveller[sizeOfPopulation];
            //SubjectTraveller[] population = new SubjectTraveller[sizeOfPopulation];
            InitPopulation(_population, mutProp);

            //CHECK POPULATION
            foreach (SubjectTraveller subTrav in _population)
            {
                //FixSubjectTraveller(subTrav); //no need to fix

                subTrav.TravelTime = FittnesFunction(subTrav);
                //jeśli FittnesFunctionF to musi być odwrotnie znak nierówności

                if (subTrav.TravelTime > _bestG)
                {
                    _bestG = subTrav.TravelTime;
                }
                if (subTrav.TravelTime < _worstG)
                {
                    _worstG = subTrav.TravelTime;
                }
                _sumG += subTrav.TravelTime;

                //Console.WriteLine(subTrav); //print to check
            }
            _avgG = _sumG / _population.Length;
            _generationMaxFittnesMap.Add(0, _bestG);
            _generationMinFittnesMap.Add(0, _worstG);
            _generationAvgFittnesMap.Add(0, _avgG);


            CrossingType ctD;

            if (crossType == 1)
            {
                ctD = new CrossingType(CrossOverInKindOfUniform);
            }
            else if (crossType == 2)
            {
                ctD = new CrossingType(CrossOverOX1);
            }
            else
            {
                ctD = new CrossingType(CrossOverOX1);
            }

            //Console.WriteLine(_bestTravelTime);

            for (int i = 0; i < Env.GENERATION_COUNT; i++)
            {
                _sumG = 0;
                double bestFittnesInGeneration  = Double.MinValue;
                double worstFittnesInGeneration = Double.MaxValue;
                //SELECT CANDIDATS TO CROSSING
                //CROSS
                //MUTATE

                _population = SelectCrossMutate(_population, ctD);

                //CHECK NEW POPULATION
                foreach (SubjectTraveller subTrav in _population)
                {
                    subTrav.TravelTime = FittnesFunction(subTrav);
                    if (subTrav.TravelTime > bestFittnesInGeneration)
                    {
                        bestFittnesInGeneration = subTrav.TravelTime;
                    }
                    if (subTrav.TravelTime < worstFittnesInGeneration)
                    {
                        worstFittnesInGeneration = subTrav.TravelTime;
                    }
                    if (subTrav.TravelTime > _bestG)
                    {
                        _bestG = subTrav.TravelTime;
                    }
                    if (subTrav.TravelTime < _worstG)
                    {
                        _worstG = subTrav.TravelTime;
                    }
                    _sumG += subTrav.TravelTime;
                }
                _avgG = _sumG / _population.Length;
                _generationMaxFittnesMap.Add(i + 1, bestFittnesInGeneration);
                _generationMinFittnesMap.Add(i + 1, worstFittnesInGeneration);
                _generationAvgFittnesMap.Add(i + 1, _avgG);
            }

            //PrintMinMaxAvgForEachGen();

            Console.WriteLine($"The Best is: {_bestG}");
            Console.WriteLine($"The Worst is: {_worstG}");
        }