lifebeyondfife / Decider

An Open Source .Net Constraint Programming Solver
MIT License
150 stars 21 forks source link

Creating Maximization and Minimization objectives #34

Closed therollingbird closed 3 years ago

therollingbird commented 3 years ago

Hi i am pretty new to using your library and i have been playing around with it, this might be a dumb question but i couldn't see how i could do or have an objective and that objective be maximized or minimized, i.e. i would want to maximize or minimize profit using your solver how would i model it using your solver ? i.e for the 2 following example

image

image

or x = Variable(0, 50, 'x') y = Variable(0, 45, 'y') z = Variable(0, 37, 'z')

model.Maximize(2x + 2y + 3*z)

con 1 = Constraint(2x + 7y + 3z <= 50) con 2 = Constraint(3x - 5y + 7z <= 45) con 3 = Constraint(5x + 2y - 6*z <= 37)

solver = Solve()/Search

lifebeyondfife commented 3 years ago

Thanks for the question, and the interest in Decider.

Specify a variable to maximise (or minimise by creating a variable like minVar = var * -1), by using the following StartSearch method: https://github.com/lifebeyondfife/Decider/blob/master/Csp/BaseTypes/State.cs#L30

Note the second parameter is a constrained integer variable, which is the one to be optimised i.e. made as large as possible.

You can see a full example in this file, see how the variable optimise is passed as the second argument: https://github.com/lifebeyondfife/Decider/blob/master/Examples/Optimisation/Optimisation.cs#L45

Hopefully that makes things clear. Best of luck.

therollingbird commented 3 years ago

so to maximize i just create var optimise = new VariableInteger("optimise", 0, 3000); and to minimize i just create var optimise = new VariableInteger("optimise", -3000, 0); would that be a correct assumption ?

and thank you heaps

therollingbird commented 3 years ago

so to maximize i just create var optimise = new VariableInteger("optimise", 0, 3000); and to minimize i just create var optimise = new VariableInteger("optimise", -3000, 0); would that be a correct assumption ?

and thank you heaps

I think I am still a bit lost

On Friday, January 8, 2021, Iain notifications@github.com wrote:

Thanks for the question, and the interest in Decider.

Specifying a variable to maximise (or minimise by creating a variable like minVar = var * -1), by using the following StartSearch method: https://github.com/lifebeyondfife/Decider/blob/ master/Csp/BaseTypes/State.cs#L30

Note the second parameter is a constrained integer variable, which is the one to be optimised i.e. made as large as possible.

You can see a full example in this file, see how the variable optimise is passed as the second argument: https://github.com/ lifebeyondfife/Decider/blob/master/Examples/Optimisation/ Optimisation.cs#L45

Hopefully that makes things clear. Best of luck.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/lifebeyondfife/Decider/issues/34#issuecomment-756152291, or unsubscribe https://github.com/notifications/unsubscribe-auth/AISTBRJNH2EJUPU7FWPDKX3SYXAZFANCNFSM4VY5YRRA .

lifebeyondfife commented 3 years ago

You would create something like this:

var x = new VariableInteger("a", 0, 50);
var y = new VariableInteger("b", 0, 45);
var z = new VariableInteger("c", 0, 37);
var optimise = new VariableInteger("optimise", 0, 100000);

var constraint = new ConstraintInteger(2 * x + 2 * y + 3 * z == optimise);

IState<int> state = new StateInteger(new[] { x, y, z, optimise }, new List<IConstraint> { constraint });
state.StartSearch(out StateOperationResult searchResult, optimise, out IDictionary<string, IVariable<int>> solution);
therollingbird commented 3 years ago

Yes for maximisation got it :)

For minimisation would it be

var minimize = new VariableInteger("optimise", -10000, 0);

Or var optimise = new VariableInteger("optimise", -100000, -10000);

ConstraintInteger(2 x - 2 y - 3 * z == optimise);

Minimisation is that part that im lost sorry if I didn't make the question clear earlier on.

And thanks for all the help you're giving me

On Saturday, January 9, 2021, Iain notifications@github.com wrote:

You would create something like this:

var x = new VariableInteger("a", 0, 50); var y = new VariableInteger("b", 0, 45); var z = new VariableInteger("c", 0, 37); var optimise = new VariableInteger("optimise", 0, 100000);

var constraint = new ConstraintInteger(2 x + 2 y + 3 * z == optimise);

IState state = new StateInteger(new[] { x, y, z, optimise }, new List { constraint }); state.StartSearch(out StateOperationResult searchResult, optimise, out IDictionary<string, IVariable> solution);

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/lifebeyondfife/Decider/issues/34#issuecomment-756776643, or unsubscribe https://github.com/notifications/unsubscribe-auth/AISTBRMDXKKENG6UOCPYHLLSY4HN3ANCNFSM4VY5YRRA .

lifebeyondfife commented 3 years ago

In this example, minimising would instantly find x = 0, y = 0, and z = 0, unless you allow those variables to be less than zero. But the way I would create a minimise variable is:

var optimise = new VariableInteger("optimise", -100000, 0);

var constraint = new ConstraintInteger(2 * x + 2 * y + 3 * z == optimise * -1);