public virtual Variable SelectVariable()
{
Variable v = null;
if (IsOption(Minimize) || IsOption(Maximize))
{
v = InfVariable();
// v = MinimumSizeVariable();
}
// try to select the variable with highest weight on soft constraints
// if there are soft constraints
if (IsSoftConstraintsUsed())
{
// var varsAlreadyAssigned = (network.Variables.Cast<Variable>()
// Where(vars => vars.Domain.Size() == 1 && vars.IsValueType == false)
// Select(vars => new { var0 = vars }));//((IntDomain)vars.Domain).Min()));
var variablesWhichHavePreferencesAndVar0HasNotBeenAssigned =
//softConstraints
// .Where(soft => soft is Equals).Cast<Equals>() // the constraint is soft equal
// .Where(vars => vars.Vars[0].Domain.Size() > 1) // the first var is not assigned yet
// .Select(doms => new { var0 = doms.Vars[0], var1 = doms.Vars[1], weight = doms.Weight })
// .Distinct()
// .Union(
// softConstraints
// .Where(soft => soft is NotEquals).Cast<NotEquals>() // the constraint is soft not equal
// .Where(vars => vars.Vars[0].Domain.Size() > 1) // the first var is not assigned yet
// .Select(doms => new { var0 = doms.Vars[0], var1 = doms.Vars[1], weight = doms.Weight })
// .Distinct())
// .OrderBy(w => w.weight).ToArray(); // the most weighted first
_softConstraints
.Where(soft => soft is Equals).Cast<Equals>() // the constraint is soft equal
.Where(vars => vars.Vars[0].Domain.Size() > 1) // the first var is not assigned yet
.Select(doms => new { var0 = doms.Vars[0], type = "Equal", weight = doms.Weight })
.Union(
_softConstraints
.Where(soft => soft is NotEquals).Cast<NotEquals>() // the constraint is soft not equal
.Where(vars => vars.Vars[0].Domain.Size() > 1) // the first var is not assigned yet
.Select(doms => new { var0 = doms.Vars[0], type = "NotEqual", weight = doms.Weight })
)
.GroupBy(vars => new { vars.var0, vars.type })
.Select(vars => new {vars.Key.var0, vars.Key.type, count = vars.Count()})
.OrderByDescending(w => w.count).ToArray(); // the most weighted first
if (variablesWhichHavePreferencesAndVar0HasNotBeenAssigned.Count() > 0)
{
v = variablesWhichHavePreferencesAndVar0HasNotBeenAssigned.First().var0;
}
}
return v ?? MinimumSizeVariable();
}