Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var a = new RouteWorker();

            a.Start();

            while (true)
            {
                Thread.Sleep(1000);
            }
        }
        /// <summary>
        /// This implementation is the knapsack style Best Closest Neighbor variant.
        /// Firstly it gets neighbors of point of interest passed by argument.
        /// Secondly it orders that points by score / weight decreasing value.
        /// The weight value is obtained by getting the edge between param node and the neighbor node.
        /// Finally it returns the first not visited node of that point list.
        /// </summary>
        /// <param name="interestPoint">Point of interest</param>
        /// <returns></returns>
        protected override InterestPointWorker GetBestNeighbor(InterestPointWorker interestPoint)
        {
            var adjPoiIds = CityMapClone.GetAdjacentNodes(interestPoint.Entity.Id);

            var tempNodes = new Collection <(int NodeKey, double Ratio)>();

            adjPoiIds.ToList().ForEach(adjPoiId =>
            {
                var node = CityMapClone[adjPoiId];
                if (node.IsVisited)
                {
                    return;
                }

                RouteWorker edge = CityMapClone.GetEdge(interestPoint.Entity.Id, adjPoiId);
                if (edge is null)
                {
                    return;
                }

                var value = node.Entity.Score.Value / edge.Weight.Invoke();
                tempNodes.Add((adjPoiId, value));
            });

            var tempNodesSorted = from node in tempNodes
                                  orderby node.Ratio descending
                                  select node.NodeKey;

            InterestPointWorker candidateNode = default;
            int candidateNodeId = tempNodesSorted.FirstOrDefault();

            if (candidateNodeId != 0)
            {
                candidateNode = CityMapClone[candidateNodeId];
            }

            return(candidateNode);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Calculates the solution cost passed as parameter using an equation of convex combination.
        /// </summary>
        /// <param name="solution">
        /// Solution to evaluate.
        /// </param>
        /// <returns>
        /// An Evaluation Object.
        /// </returns>
        private int CalculateCost(ToSolution solution)
        {
            // Calcolo del termine del gradimento.
            int scoreTerm = solution.Tour.Nodes.Sum(node => node.Entity.Score.Value);

            // Il peso che determina l'importanza dei termini dell'equazione.
            double lambda = Solver.Instance.CurrentObjectiveFunctionWeight;

            // Calcolo del termine che da peso alla distanza tra il nodo corrente e quello precedente.
            // In particolare, vengono privilegati i nodi molto vicini con un peso molto alto
            // che darà quindi un maggior gradimento.
            double distanceTerm = solution.Tour.Nodes.Sum(node =>
            {
                RouteWorker edge = solution.Tour.GetEdges(node.Entity.Id).FirstOrDefault();
                if (edge is null)
                {
                    return(0);
                }
                double distanceWeight = GetDistanceTermWeight(edge.Weight.Invoke());
                return(distanceWeight * node.Entity.Score.Value);
            });

            return((int)Math.Round(lambda * scoreTerm + (1 - lambda) * distanceTerm));
        }
Ejemplo n.º 4
0
 public static int GetFactWorkerId(this RouteWorker rw)
 {
     return(rw.AltWorkerId.HasValue ? rw.AltWorkerId.Value : rw.WorkerId);
 }