Order constraints in optimisation












0















I have a set of many (10000+) items, from which have I have to choose exactly 20 items. I can only choose each item once. My items have profits, and costs, as well as several boolean properties (such as colour). I need to output the results in a specific order: in particular I need the first and third items to be blue, and the second and fourth items to be red.



Each item is represented as a tuple:



item = ('item name', cost, profit, is_blue, is_red)


as an example



vase = ['Ming Vase', 1000, 10000, 0, 1]

plate = ['China Plate', 10, 5, 1, 0]


and the total set of items is a list of lists:



items = [item1, item2, ..., itemN].


My profits and costs are also lists:



profits = [x[2] for x in items]
costs = [x[1] for x in items]


For each item chosen, it needs to have a minimum value, and a minimum of 5 items must have the property (is_blue) flag set to 1.



I want to choose the 20 cheapest items with the highest value, such that 5 of them have the is_blue flag set to 1, and the first and third items are blue (etc).



I'm having trouble formulating this using google OR tools.



from ortools.linear_solver import pywraplp

solver = pywraplp.Solver('SolveAssignmentProblemMIP',
pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)

x = {}

for i in range(MAX_ITEMS):
x[i] = solver.BoolVar('x[%s]' % (i))

#Define the constraints
total_chosen = 20
solver.Add(solver.Sum([x[i] for i in range(MAX_ITEMS)]) == total_chosen)

blues = [x[3] for x in items]
solver.Add(solver.Sum([blues[i] * x[i] for i in .


range(MAX_ITEMS)]) >= 5)



max_cost = 5.0

for i in range(MAX_ITEMS):
solver.Add(x[i] * cost[i] <= max_cost)

solver.Maximize(solver.Sum([profits[i] * x[i] for i in range(total_chosen)]))
sol = solver.Solve()


I can get the set of items I've chosen by:



for i in range(MAX_ITEMS):
if x[i].solution_value() > 0:
print(item[i].item_name)


This works fine - it chooses the set of 20 items which maximise the profits subject to the cost constraint, but I'm stuck on how to extend this to choosing items in way that guarantees that the first is blue etc.



Any help in formulating the constraints and objective would be really helpful. Thanks!










share|improve this question























  • Why do you care that the first is blue? You will have at least 5 blue items, so just reorder your solution with 1st and 3rd items being blue

    – juvian
    Nov 6 '18 at 16:18
















0















I have a set of many (10000+) items, from which have I have to choose exactly 20 items. I can only choose each item once. My items have profits, and costs, as well as several boolean properties (such as colour). I need to output the results in a specific order: in particular I need the first and third items to be blue, and the second and fourth items to be red.



Each item is represented as a tuple:



item = ('item name', cost, profit, is_blue, is_red)


as an example



vase = ['Ming Vase', 1000, 10000, 0, 1]

plate = ['China Plate', 10, 5, 1, 0]


and the total set of items is a list of lists:



items = [item1, item2, ..., itemN].


My profits and costs are also lists:



profits = [x[2] for x in items]
costs = [x[1] for x in items]


For each item chosen, it needs to have a minimum value, and a minimum of 5 items must have the property (is_blue) flag set to 1.



I want to choose the 20 cheapest items with the highest value, such that 5 of them have the is_blue flag set to 1, and the first and third items are blue (etc).



I'm having trouble formulating this using google OR tools.



from ortools.linear_solver import pywraplp

solver = pywraplp.Solver('SolveAssignmentProblemMIP',
pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)

x = {}

for i in range(MAX_ITEMS):
x[i] = solver.BoolVar('x[%s]' % (i))

#Define the constraints
total_chosen = 20
solver.Add(solver.Sum([x[i] for i in range(MAX_ITEMS)]) == total_chosen)

blues = [x[3] for x in items]
solver.Add(solver.Sum([blues[i] * x[i] for i in .


range(MAX_ITEMS)]) >= 5)



max_cost = 5.0

for i in range(MAX_ITEMS):
solver.Add(x[i] * cost[i] <= max_cost)

solver.Maximize(solver.Sum([profits[i] * x[i] for i in range(total_chosen)]))
sol = solver.Solve()


I can get the set of items I've chosen by:



for i in range(MAX_ITEMS):
if x[i].solution_value() > 0:
print(item[i].item_name)


This works fine - it chooses the set of 20 items which maximise the profits subject to the cost constraint, but I'm stuck on how to extend this to choosing items in way that guarantees that the first is blue etc.



Any help in formulating the constraints and objective would be really helpful. Thanks!










share|improve this question























  • Why do you care that the first is blue? You will have at least 5 blue items, so just reorder your solution with 1st and 3rd items being blue

    – juvian
    Nov 6 '18 at 16:18














0












0








0








I have a set of many (10000+) items, from which have I have to choose exactly 20 items. I can only choose each item once. My items have profits, and costs, as well as several boolean properties (such as colour). I need to output the results in a specific order: in particular I need the first and third items to be blue, and the second and fourth items to be red.



Each item is represented as a tuple:



item = ('item name', cost, profit, is_blue, is_red)


as an example



vase = ['Ming Vase', 1000, 10000, 0, 1]

plate = ['China Plate', 10, 5, 1, 0]


and the total set of items is a list of lists:



items = [item1, item2, ..., itemN].


My profits and costs are also lists:



profits = [x[2] for x in items]
costs = [x[1] for x in items]


For each item chosen, it needs to have a minimum value, and a minimum of 5 items must have the property (is_blue) flag set to 1.



I want to choose the 20 cheapest items with the highest value, such that 5 of them have the is_blue flag set to 1, and the first and third items are blue (etc).



I'm having trouble formulating this using google OR tools.



from ortools.linear_solver import pywraplp

solver = pywraplp.Solver('SolveAssignmentProblemMIP',
pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)

x = {}

for i in range(MAX_ITEMS):
x[i] = solver.BoolVar('x[%s]' % (i))

#Define the constraints
total_chosen = 20
solver.Add(solver.Sum([x[i] for i in range(MAX_ITEMS)]) == total_chosen)

blues = [x[3] for x in items]
solver.Add(solver.Sum([blues[i] * x[i] for i in .


range(MAX_ITEMS)]) >= 5)



max_cost = 5.0

for i in range(MAX_ITEMS):
solver.Add(x[i] * cost[i] <= max_cost)

solver.Maximize(solver.Sum([profits[i] * x[i] for i in range(total_chosen)]))
sol = solver.Solve()


I can get the set of items I've chosen by:



for i in range(MAX_ITEMS):
if x[i].solution_value() > 0:
print(item[i].item_name)


This works fine - it chooses the set of 20 items which maximise the profits subject to the cost constraint, but I'm stuck on how to extend this to choosing items in way that guarantees that the first is blue etc.



Any help in formulating the constraints and objective would be really helpful. Thanks!










share|improve this question














I have a set of many (10000+) items, from which have I have to choose exactly 20 items. I can only choose each item once. My items have profits, and costs, as well as several boolean properties (such as colour). I need to output the results in a specific order: in particular I need the first and third items to be blue, and the second and fourth items to be red.



Each item is represented as a tuple:



item = ('item name', cost, profit, is_blue, is_red)


as an example



vase = ['Ming Vase', 1000, 10000, 0, 1]

plate = ['China Plate', 10, 5, 1, 0]


and the total set of items is a list of lists:



items = [item1, item2, ..., itemN].


My profits and costs are also lists:



profits = [x[2] for x in items]
costs = [x[1] for x in items]


For each item chosen, it needs to have a minimum value, and a minimum of 5 items must have the property (is_blue) flag set to 1.



I want to choose the 20 cheapest items with the highest value, such that 5 of them have the is_blue flag set to 1, and the first and third items are blue (etc).



I'm having trouble formulating this using google OR tools.



from ortools.linear_solver import pywraplp

solver = pywraplp.Solver('SolveAssignmentProblemMIP',
pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)

x = {}

for i in range(MAX_ITEMS):
x[i] = solver.BoolVar('x[%s]' % (i))

#Define the constraints
total_chosen = 20
solver.Add(solver.Sum([x[i] for i in range(MAX_ITEMS)]) == total_chosen)

blues = [x[3] for x in items]
solver.Add(solver.Sum([blues[i] * x[i] for i in .


range(MAX_ITEMS)]) >= 5)



max_cost = 5.0

for i in range(MAX_ITEMS):
solver.Add(x[i] * cost[i] <= max_cost)

solver.Maximize(solver.Sum([profits[i] * x[i] for i in range(total_chosen)]))
sol = solver.Solve()


I can get the set of items I've chosen by:



for i in range(MAX_ITEMS):
if x[i].solution_value() > 0:
print(item[i].item_name)


This works fine - it chooses the set of 20 items which maximise the profits subject to the cost constraint, but I'm stuck on how to extend this to choosing items in way that guarantees that the first is blue etc.



Any help in formulating the constraints and objective would be really helpful. Thanks!







python-3.x optimization or-tools mixed-integer-programming






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 6 '18 at 16:07









Tom KealyTom Kealy

78211031




78211031













  • Why do you care that the first is blue? You will have at least 5 blue items, so just reorder your solution with 1st and 3rd items being blue

    – juvian
    Nov 6 '18 at 16:18



















  • Why do you care that the first is blue? You will have at least 5 blue items, so just reorder your solution with 1st and 3rd items being blue

    – juvian
    Nov 6 '18 at 16:18

















Why do you care that the first is blue? You will have at least 5 blue items, so just reorder your solution with 1st and 3rd items being blue

– juvian
Nov 6 '18 at 16:18





Why do you care that the first is blue? You will have at least 5 blue items, so just reorder your solution with 1st and 3rd items being blue

– juvian
Nov 6 '18 at 16:18












1 Answer
1






active

oldest

votes


















2














Instead of expressing chosen items with BoolVar, consider making a list of 20 IntVar with domain of 0..MAX_ITEMS. From there it should be fairly easy to do something like this:



solver.Add(chosens[0].IndexOf(all_items)[3] == 1)
solver.Add(chosens[2].IndexOf(all_items)[3] == 1)


chosens[i].IndexOf(all_items) simply means all_items[IndexOfChosen], I.E: whichever item is chosen for the Ith place. If you go with this approach, do not forget to MakeAllDifferent!






share|improve this answer


























  • @Tom Kealy Also consider using cp_sat as it is shinier, superior and in most cases, faster.

    – Burak
    Nov 19 '18 at 8:48











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53175639%2forder-constraints-in-optimisation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Instead of expressing chosen items with BoolVar, consider making a list of 20 IntVar with domain of 0..MAX_ITEMS. From there it should be fairly easy to do something like this:



solver.Add(chosens[0].IndexOf(all_items)[3] == 1)
solver.Add(chosens[2].IndexOf(all_items)[3] == 1)


chosens[i].IndexOf(all_items) simply means all_items[IndexOfChosen], I.E: whichever item is chosen for the Ith place. If you go with this approach, do not forget to MakeAllDifferent!






share|improve this answer


























  • @Tom Kealy Also consider using cp_sat as it is shinier, superior and in most cases, faster.

    – Burak
    Nov 19 '18 at 8:48
















2














Instead of expressing chosen items with BoolVar, consider making a list of 20 IntVar with domain of 0..MAX_ITEMS. From there it should be fairly easy to do something like this:



solver.Add(chosens[0].IndexOf(all_items)[3] == 1)
solver.Add(chosens[2].IndexOf(all_items)[3] == 1)


chosens[i].IndexOf(all_items) simply means all_items[IndexOfChosen], I.E: whichever item is chosen for the Ith place. If you go with this approach, do not forget to MakeAllDifferent!






share|improve this answer


























  • @Tom Kealy Also consider using cp_sat as it is shinier, superior and in most cases, faster.

    – Burak
    Nov 19 '18 at 8:48














2












2








2







Instead of expressing chosen items with BoolVar, consider making a list of 20 IntVar with domain of 0..MAX_ITEMS. From there it should be fairly easy to do something like this:



solver.Add(chosens[0].IndexOf(all_items)[3] == 1)
solver.Add(chosens[2].IndexOf(all_items)[3] == 1)


chosens[i].IndexOf(all_items) simply means all_items[IndexOfChosen], I.E: whichever item is chosen for the Ith place. If you go with this approach, do not forget to MakeAllDifferent!






share|improve this answer















Instead of expressing chosen items with BoolVar, consider making a list of 20 IntVar with domain of 0..MAX_ITEMS. From there it should be fairly easy to do something like this:



solver.Add(chosens[0].IndexOf(all_items)[3] == 1)
solver.Add(chosens[2].IndexOf(all_items)[3] == 1)


chosens[i].IndexOf(all_items) simply means all_items[IndexOfChosen], I.E: whichever item is chosen for the Ith place. If you go with this approach, do not forget to MakeAllDifferent!







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 8:40

























answered Nov 19 '18 at 8:34









BurakBurak

736




736













  • @Tom Kealy Also consider using cp_sat as it is shinier, superior and in most cases, faster.

    – Burak
    Nov 19 '18 at 8:48



















  • @Tom Kealy Also consider using cp_sat as it is shinier, superior and in most cases, faster.

    – Burak
    Nov 19 '18 at 8:48

















@Tom Kealy Also consider using cp_sat as it is shinier, superior and in most cases, faster.

– Burak
Nov 19 '18 at 8:48





@Tom Kealy Also consider using cp_sat as it is shinier, superior and in most cases, faster.

– Burak
Nov 19 '18 at 8:48




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53175639%2forder-constraints-in-optimisation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()