私は今ソルバー財団を学んでいます。私は実際に私のプロジェクトにlpsolveを接続していますが、私の問題は、制約を最もよく表す方法の一般的な問題だと思います。
私は、かなり典型的なナップザックまたはパッキングの問題を抱えていると思います。場所のコレクションがあり、それぞれに「スコア」があります。目標の「スコア」を満たすための場所の最小数を選択したい。
(実際には、それよりも少し複雑です。各場所にはいくつかの異なるプロパティがあり、複数のプロパティをターゲットにすることがよくあります)。
ここまでは順調ですね。ただし、追加の制約があります。選択した場所の地理的な分散を最大化したいのです。その制約をどのように表すことができますか?
これが私が今持っているものの基本的な例です:
static void Main(string[] args)
{
var locations = new List<LocationWithScore>()
{
new LocationWithScore() { LocationID = 0, Latitude = 43.644274M, Longitude = -79.478703M, Score = 20 },
new LocationWithScore() { LocationID = 1, Latitude = 43.644709M, Longitude = -79.476814M, Score = 20 },
new LocationWithScore() { LocationID = 2, Latitude = 43.643063M, Longitude = -79.477458M, Score = 20 },
new LocationWithScore() { LocationID = 3, Latitude = 43.650516M, Longitude = -79.469562M, Score = 20 },
new LocationWithScore() { LocationID = 4, Latitude = 43.642659M, Longitude = -79.463124M, Score = 20 }
};
SolverContext sContext = SolverContext.GetContext();
sContext.ClearModel();
Model model = sContext.CreateModel();
model.Name = "LocationWithScore";
Set items = new Set(Domain.Any, "candidates");
Decision take = new Decision(Domain.Boolean, "candidate", items);
model.AddDecision(take);
Parameter scoreParam = new Parameter(Domain.RealNonnegative, "score", items);
scoreParam.SetBinding(locations, "Score", "LocationID");
model.AddParameters(scoreParam);
model.AddConstraint("scoreConstraint", Model.Sum(Model.ForEach(items, item => scoreParam[item] * take[item])) >= 60);
model.AddGoal("locationGoal", GoalKind.Minimize, Model.Sum(Model.ForEach(items, item => take[item])));
var solution = sContext.Solve(new LpSolveDirective());
var report = solution.GetReport();
Console.WriteLine(report.ToString());
Console.WriteLine("Done");
Console.ReadKey();
}
internal class LocationWithScore
{
public int LocationID { get; set; }
public decimal Latitude { get; set; }
public decimal Longitude { get; set; }
public double Score { get; set; }
}
これにより、最初の3つの場所が選択されるだけで、目標は60になりますが、これら3つの場所は非常に近くにクラスター化されています。私が好むのは、最初の3つ(ID 0〜2)と最後の2つ(ID 3と4)のいずれかを選択することです。
誰かがここでいくつかのガイダンスを提供できますか?よろしくお願いします。