我正在尝试使用 microsoft 求解器基础,以便能够从一组位置中选择 3 个最分散的位置。我在我的模型中添加了两个目标,一个确保选择 3 个位置,另一个确保它们是最分散的 3 个位置。
static void Main(string[] args)
{
Location candidate1 = new Location(0, 43.432, -79.432);
Location candidate2 = new Location(1, 43.0, -79.0);
Location candidate3 = new Location(2, 23.0, 29.0);
Location candidate4 = new Location(3, 43.0, -79.0);
Location candidate5 = new Location(4, 43.0, -79.0);
Location[] candidates = {candidate1, candidate2, candidate3, candidate4, candidate5};
SolverContext solver = new SolverContext();
Model model = solver.CreateModel();
model.Name = "LocationModel";
Set items = new Set(Domain.Any, "candidates");
Decision take = new Decision(Domain.Boolean, "candidate", items);
model.AddDecision(take);
Parameter locations = new Parameter(Domain.RealNonnegative, "locations", items);
locations.SetBinding(candidates, "ID", "ID");
model.AddParameter(locations);
var dist = from l1 in candidates from l2 in candidates select new { ID1 = l1.ID, ID2 = l2.ID, dist = Geography.GetDistance(l1.Latitude, l1.Longitude, l2.Latitude, l2.Longitude) };
Parameter distance = new Parameter(Domain.RealNonnegative, "Location", items, items);
distance.SetBinding(dist, "dist", "ID1", "ID2");
model.AddParameter(distance);
Term goal = Model.Sum(Model.ForEach(items, i => Model.ForEach(items, j => take[i]*take[j]* distance[i, j])));
model.AddConstraint("LocationsQuantityMax", Model.Sum(Model.ForEach(items, item => take[item])) == 3);
model.AddGoal("Dispersion", GoalKind.Maximize, goal);
Directive directive = new HybridLocalSearchDirective();
Solution solution = solver.Solve(directive);
List<Location> locations1 = new List<Location>();
if (solution.Decisions.Any())
{
var selections = solution.Decisions.First().GetValues()
.Where(d => (bool) d[0])
.Select(d => Convert.ToInt32(d[1]));
locations1.AddRange(
from c in candidates
join s in selections on c.ID equals s
select c);
}
foreach (Location location in locations1)
{
Console.WriteLine("ID: {0}, Latitude: {1}, Longitude: {2}", location.ID, location.Latitude, location.Longitude);
}
此外,我的 Location 类如下所示:
class Location
{
public int ID { get; private set; }
public double Latitude { get; private set; }
public double Longitude { get; private set; }
public Location(int LocationID, double latitude, double longitude)
{
this.ID = LocationID;
this.Latitude = latitude;
this.Longitude = longitude;
}
}
我的距离计算是:
public static double ToRad(this double num)
{
return num * Math.PI / 180;
}
public static double GetDistance(double lat1, double lon1, double lat2, double lon2)
{
const int r = 6371; // Radius of earth in KM
// Convert to Radians
lat1 = lat1.ToRad();
lon1 = lon1.ToRad();
lat2 = lat2.ToRad();
lon2 = lon2.ToRad();
// Spherical Law of Cosines
var resultCos =
Math.Acos(
Math.Sin(lat1) * Math.Sin(lat2) +
Math.Cos(lat1) * Math.Cos(lat2) * Math.Cos(lon2 - lon1)
) * r;
return resultCos;
}
现在我的代码有两个问题,我不知道它们的来源,
首先,
我的代码目前适用于 5 个位置的大多数排列,其中三个是相同的经度纬度(即在此;例如候选 2、候选 4 和候选 5),但对于候选的某些排列,它不会返回最分散的 3 个(即对于相同的问题实例只是更改候选人声明的顺序)。我无法理解为什么。
第二,
如果您注释掉约束以选择至少 3 个位置,而不是像预期的那样选择所有位置,它不会选择任何位置。
我的目标适用于大多数情况这一事实似乎表明它是正确的,有人知道它为什么有时会动摇吗?
这不是家庭作业,非常感谢任何回复的人。