-
Notifications
You must be signed in to change notification settings - Fork 2
/
City.cs
123 lines (115 loc) · 4.13 KB
/
City.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//////////////////////////////////////////////////////////////////////////////////////////////////
// File Name: City.cs
// Permission to use, copy, modify, and distribute this Program and its documentation,
// if any, for any purpose and without fee is hereby granted, provided that:
// (i) you not charge any fee for the Program, and the Program not be incorporated
// by you in any software or code for which compensation is expected or received;
// (ii) the copyright notice listed above appears in all copies;
// (iii) both the copyright notice and this Agreement appear in all supporting documentation; and
// (iv) the name of Michael LaLena or lalena.com not be used in advertising or publicity
// pertaining to distribution of the Program without specific, written prior permission.
///////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace Tsp
{
/// <summary>
/// An individual City in our tour.
/// </summary>
public class City
{
/// <summary>
/// Constructor that provides the city location.
/// </summary>
/// <param name="x">X position of the city.</param>
/// <param name="y">Y position of the city.</param>
public City(int x, int y)
{
Location = new Point(x, y);
}
/// <summary>
/// Private copy of the location of this city.
/// </summary>
private Point location;
/// <summary>
/// The location of this city.
/// </summary>
public Point Location
{
get
{
return location;
}
set
{
location = value;
}
}
/// <summary>
/// Private copy of the distance from this city to every other city.
/// The index in this array is the number of the city linked to.
/// </summary>
private List<double> distances = new List<double>();
/// <summary>
/// The distance from this city to every other city.
/// </summary>
public List<double> Distances
{
get
{
return distances;
}
set
{
distances = value;
}
}
/// <summary>
/// Private copy of the list of the cities that are closest to this one.
/// </summary>
private List<int> closeCities = new List<int>();
/// <summary>
/// A list of the cities that are closest to this one.
/// </summary>
public List<int> CloseCities
{
get
{
return closeCities;
}
}
/// <summary>
/// Find the cities that are closest to this one.
/// </summary>
/// <param name="numberOfCloseCities">When creating the initial population of tours, this is a greater chance
/// that a nearby city will be chosen for a link. This is the number of nearby cities that will be considered close.</param>
public void FindClosestCities( int numberOfCloseCities )
{
double shortestDistance;
int shortestCity = 0;
double[] dist = new double[Distances.Count];
Distances.CopyTo(dist);
if (numberOfCloseCities > Distances.Count - 1)
{
numberOfCloseCities = Distances.Count - 1;
}
closeCities.Clear();
for (int i = 0; i < numberOfCloseCities; i++)
{
shortestDistance = Double.MaxValue;
for (int cityNum = 0; cityNum < Distances.Count; cityNum++)
{
if (dist[cityNum] < shortestDistance)
{
shortestDistance = dist[cityNum];
shortestCity = cityNum;
}
}
closeCities.Add(shortestCity);
dist[shortestCity] = Double.MaxValue;
}
}
}
}