简体   繁体   中英

Microsoft Solver Foundation - how to use function as constraint?

I'm trying to find a solution to a problem using Microsoft Solver Foundation in C# and I'm having trouble setting up all the constraints I need. My basic model is I have a list of bays and I need to load each bay so that the total of all the bays is maximised. I'm currently doing it like this

var solver = SolverContext.GetContext();
var model = solver.CreateModel();
var decisions = 
    bays.Select(b => new Decision(Domain.IntegerNonnegative, "B"+b.bay.getShortName()));
model.AddDecisions(decisions.ToArray());

foreach (BayPositionLoading bay in bays)
{
    model.AddConstraint(
            "B" + bay.bay.getShortName() + "Cons",
            model.Decisions
                 .First(d => d.Name == "B" + bay.bay.getShortName()) <= bay.bay.maxLoad);
}

What I really would like to be able to do is add a constraint that a certain function returns true. The function would be something like this

public bool isValid (List<Bay> bays)
{
    return blah;
}

But I can't figure out how to create the list of bays to pass to this function. I would like to do something like but this keeps throwing an exception when I say ToDouble or GetDouble.

foreach(Bay b in bays)
{
    var dec= model.Decisions.First(it => it.Name == "B" + bay.bay.getShortName());
    b.actualLoad = dec.ToDouble(); // Or GetDouble
}
model.AddConstraint("func", isValid(bays) == true);

Can anyone suggest how this can be done? Thanks!

您只需要使用OML语言提供的数学运算,我认为MSF不支持您尝试使用的自定义函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM