コード例 #1
0
        private void Optimize(int begin, int end, IntervalCollection meta, CostDelegate cost_del)
        {
            Interval set;

            set.contiguous = false;

            int    best_set_begin = -1;
            int    best_set_end   = -1;
            double best_set_cost  = 0;

            for (int i = begin; i <= end; ++i)
            {
                set.low = this[i].low;

                double cost = 0.0;
                for (int j = i; j <= end; ++j)
                {
                    set.high = this[j].high;
                    cost    += cost_del(this[j]);

                    double set_cost = cost_del(set);
                    if (set_cost < cost && cost > best_set_cost)
                    {
                        best_set_begin = i;
                        best_set_end   = j;
                        best_set_cost  = cost;
                    }
                }
            }

            if (best_set_begin < 0)
            {
                // didn't find an optimal set: add original members

                for (int i = begin; i <= end; ++i)
                {
                    meta.Add(this[i]);
                }
            }
            else
            {
                // found set: add it ...

                set.low  = this[best_set_begin].low;
                set.high = this[best_set_end].high;

                meta.Add(set);

                // ... and optimize to the left and right

                if (best_set_begin > begin)
                {
                    Optimize(begin, best_set_begin - 1, meta, cost_del);
                }
                if (best_set_end < end)
                {
                    Optimize(best_set_end + 1, end, meta, cost_del);
                }
            }
        }
コード例 #2
0
        public IntervalCollection GetMetaCollection(CostDelegate cost_del)
        {
            IntervalCollection intervalCollection = new IntervalCollection();

            Normalize();
            Optimize(0, Count - 1, intervalCollection, cost_del);
            intervalCollection.intervals.Sort();
            return(intervalCollection);
        }
コード例 #3
0
        public IntervalCollection GetMetaCollection(CostDelegate cost_del)
        {
            IntervalCollection meta = new IntervalCollection();

            Normalize();
            Optimize(0, Count - 1, meta, cost_del);
            meta.intervals.Sort();

            return(meta);
        }
コード例 #4
0
        private void Optimize(int begin, int end, IntervalCollection meta, CostDelegate cost_del)
        {
            Interval i = default(Interval);

            i.contiguous = false;
            int    num  = -1;
            int    num2 = -1;
            double num3 = 0.0;

            for (int j = begin; j <= end; j++)
            {
                Interval interval = this[j];
                i.low = interval.low;
                double num4 = 0.0;
                for (int k = j; k <= end; k++)
                {
                    Interval interval2 = this[k];
                    i.high = interval2.high;
                    num4  += cost_del(this[k]);
                    double num5 = cost_del(i);
                    if (num5 < num4 && num4 > num3)
                    {
                        num  = j;
                        num2 = k;
                        num3 = num4;
                    }
                }
            }
            if (num < 0)
            {
                for (int l = begin; l <= end; l++)
                {
                    meta.Add(this[l]);
                }
                return;
            }
            Interval interval3 = this[num];

            i.low = interval3.low;
            Interval interval4 = this[num2];

            i.high = interval4.high;
            meta.Add(i);
            if (num > begin)
            {
                Optimize(begin, num - 1, meta, cost_del);
            }
            if (num2 < end)
            {
                Optimize(num2 + 1, end, meta, cost_del);
            }
        }
コード例 #5
0
ファイル: interval.cs プロジェクト: nlhepler/mono
		private void Optimize (int begin, int end, IntervalCollection meta, CostDelegate cost_del) {
			Interval set;
			set.contiguous = false;
		
			int best_set_begin = -1;
			int best_set_end = -1;
			double best_set_cost = 0;

			for (int i = begin; i <= end; ++ i) {
				set.low = this[i].low;

				double cost = 0.0;
				for (int j = i; j <= end; ++ j) {
					set.high = this[j].high;
					cost += cost_del (this[j]);
					
					double set_cost = cost_del (set);
					if (set_cost < cost && cost > best_set_cost) {
						best_set_begin = i;
						best_set_end = j;
						best_set_cost = cost;
					}
				}
			}

			if (best_set_begin < 0) {
				// didn't find an optimal set: add original members

				for (int i = begin; i <= end; ++ i)
					meta.Add (this[i]);
			}
			else {
				// found set: add it ...

				set.low = this[best_set_begin].low;
				set.high = this[best_set_end].high;
				
				meta.Add (set);

				// ... and optimize to the left and right

				if (best_set_begin > begin)
					Optimize (begin, best_set_begin - 1, meta, cost_del);
				if (best_set_end < end)
					Optimize (best_set_end + 1, end, meta, cost_del);
			}
		}
コード例 #6
0
ファイル: interval.cs プロジェクト: nlhepler/mono
		public IntervalCollection GetMetaCollection (CostDelegate cost_del) {
			IntervalCollection meta = new IntervalCollection ();
		
			Normalize ();
			Optimize (0, Count - 1, meta, cost_del);
			meta.intervals.Sort ();

			return meta;
		}
コード例 #7
0
        // All sorting related data is immutable.

        public static Path <T> Solve <T>(T start, T goal, NeighbourDelegate <T> neighboursFunction,
                                         CostDelegate <T> costFunction,
                                         HeuristicDelegate <T> heuristicFunction)
            where T : ISatellite
        {
            var nodeMap       = new Dictionary <T, Node <T> >();
            var priorityQueue = new PriorityQueue <Node <T> >();

            var nStart = new Node <T>(start, 0, heuristicFunction.Invoke(start, goal), null, false);

            nodeMap[start] = nStart;
            priorityQueue.Enqueue(nStart);
            float cost = 0;

            while (priorityQueue.Count > 0)
            {
                Node <T> current = priorityQueue.Dequeue();
                if (current.Closed)
                {
                    continue;
                }
                current.Closed = true;
                if (current.Item.Equals(goal))
                {
                    // Return path and cost
                    var reversePath = new List <T>();
                    for (Node <T> node = current; node != null; node = node.From)
                    {
                        reversePath.Add(node.Item);
                        cost += node.Cost;
                    }
                    reversePath.Reverse();
                    return(new Path <T>(reversePath, cost));
                }

                foreach (T item in neighboursFunction.Invoke(current.Item))
                {
                    float new_cost = current.Cost + costFunction.Invoke(current.Item, item);
                    // If the item has a node, it will either be in the closedSet, or the openSet
                    if (nodeMap.ContainsKey(item))
                    {
                        Node <T> n = nodeMap[item];
                        if (new_cost <= n.Cost)
                        {
                            // Cost via current is better than the old one, discard old node, queue new one.
                            var new_node = new Node <T>(n.Item, new_cost, n.Heuristic, current, false);
                            n.Closed      = true;
                            nodeMap[item] = new_node;
                            priorityQueue.Enqueue(new_node);
                        }
                    }
                    else
                    {
                        // It is not in the openSet, create a node and add it
                        var new_node = new Node <T>(item, new_cost,
                                                    heuristicFunction.Invoke(item, goal), current,
                                                    false);
                        priorityQueue.Enqueue(new_node);
                        nodeMap[item] = new_node;
                    }
                }
            }
            return(Path.Empty <T>(start));
        }