You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I tried to model a constraint that sets a machine_active variable to 1 whenever at least one job is assigned.
I however don't manage to get the machine_active variable to be calculated correctly, i.e. multiple machine_activevariables are False though they should be True as jobs are assigned to the respective machine. I attached my code below as well as an alternative implementation using or-tools directly that returns the expected result.
importcpmpyascpfromcpmpyimportSolverLookupmodel=cp.Model()
model=SolverLookup.get("ortools")
num_machines=7num_jobs=5job_to_machine_assignments=cp.boolvar(shape=(num_jobs, num_machines))
machine_active=cp.boolvar(shape=(num_machines,))
# machine active if job assignedmodel+=machine_active==job_to_machine_assignments.max(axis=0)
# max one machine per jobmodel+= [job_to_machine_assignments[j].sum()<=1forjinrange(num_jobs)]
# max 2 jobs per machinemodel+= [job_to_machine_assignments[:,m].sum()<=2forminrange(num_machines)]
# Objectiveunassigned=num_jobs-job_to_machine_assignments.sum()
model.minimize(unassigned)
model.solve()
I also tried the following implementation of the max constraint, which didn't work out for me either:
this happens because the max() function used is the python built in function and not the cpmpy one. This means that it directly takes the one with the "maximum value", creating a constraint directly between 2 variables, instead of the desired constraint.
You can use the cp.max() function in order to use it with cpmpy variables, which leads to the wanted result.
That is a good point, if we recommend using numpy's numpy-array.sum() (e.g. @IgnaceBleukx in a previous question from Max), then it is surprising that numpy-array.max() does not work.
So we should look into what we recommend...
I think at this point, the most generic is to recommend max(arr[i,j,:]) and sum(arr[i,:,:]) etc...
(or if you dont do from cpmpy import * but import cpmpy as cp then cp.max() etc)
Hi!
I tried to model a constraint that sets a
machine_active
variable to1
whenever at least one job is assigned.I however don't manage to get the machine_active variable to be calculated correctly, i.e. multiple
machine_active
variables areFalse
though they should beTrue
as jobs are assigned to the respective machine. I attached my code below as well as an alternative implementation usingor-tools
directly that returns the expected result.I also tried the following implementation of the max constraint, which didn't work out for me either:
Output:
The following model in or-tools returned expected results:
Output:
The text was updated successfully, but these errors were encountered: