Microsoft Solver Foundation - how to use function as constraint?

1.2k views Asked by At

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!

1

There are 1 answers

2
den.run.ai On

you need to use the math provided with OML language only, I do not think custom functions like you try to use are supported in MSF.