コード例 #1
0
        public TSPState(TSPState template)
        {
            this.p = template.p;
            this.currentPosition = template.currentPosition;
            this.notVisited = new HashSet<int>(template.notVisited);
			this.path = new List<int>(template.path);
        }
コード例 #2
0
        public override void visualize(State s)
        {
            this.problem = (TSPProblem)s.p;
            TSPState state = (TSPState)s;
            int maxValueX = problem.positions.Max(x => x.x),
                maxValueY = problem.positions.Max(x => x.y);

            if (clear)
                g.Clear(Color.WhiteSmoke);
            xStretch = width / (maxValueX + 20);
            yStretch = height / (maxValueY + 20);
            if (problem.citiesCount > 2000)
                nodeSize--;
            if (problem.citiesCount > 10000)
                nodeSize--;

			for (int i = 0; i < state.path.Count-1; i++)
			{
				drawLine(state.path[i], state.path[i + 1]);
			}

            for (int i = 0; i < problem.citiesCount; i++)
            {
                drawNode(i);
                if (state.notVisited.Contains(i))
                    fillNode(i, Color.Red);
                else
                    fillNode(i, Color.Black);
            }
            fillNode(state.currentPosition, Color.Green);
            screen.Refresh();
        }
コード例 #3
0
        public override void getSuccessors(List<StateCostPair> result)
        {
            result.Clear();
            TSPProblem p = (TSPProblem)this.p;
            foreach (var item in notVisited)
            {
                TSPState succ = (TSPState)this.clone();
                succ.notVisited.Remove(item);
                succ.currentPosition = item;
				succ.path.Add(item);
                result.Add(new StateCostPair(succ, p.getDistance(this.currentPosition, succ.currentPosition)));
            }
        }
コード例 #4
0
        public override bool Equals(object obj)
        {
            if (!(obj is TSPState))
                return false;
            TSPState state = (TSPState)obj;
            if (state.currentPosition != this.currentPosition)
                return false;
            if (this.notVisited.Count != state.notVisited.Count)
                return false;
            foreach (var item in this.notVisited)
            {
                if (!state.notVisited.Contains(item))
                    return false;
            }
			for (int i = 0; i < path.Count; i++)
			{
				if (path[i] != state.path[i])
					return false;
			}
            return true;
        }
コード例 #5
0
 public override void readFromFile(string filePath)
 {
     positions = new List<intPair>();
     using(var reader = new System.IO.StreamReader(filePath))
     {
         this.citiesCount = int.Parse(reader.ReadLine());
         for (int i = 0; i < citiesCount; i++)
         {
             positions.Add(new intPair(int.Parse(reader.ReadLine()), int.Parse(reader.ReadLine())));
         }
     }
     TSPState s = new TSPState(this);
     s.notVisited = new HashSet<int>();
     s.currentPosition = 0;
     for (int i = 0; i < citiesCount; i++)
     {
         s.notVisited.Add(i);
     }
     this.initialState = s;
     computeDistances();
 }