Adding a constraint
回答済みI had a question on posting some kinds of constraints.
I have a basic dataset (matrix) of students and projects. The idea is to assign students to projects in a fair manner for student satisfaction.
Each row is a student. There are 6 datapoints per row representing project preferences (in order of desirability for the student).
There are many projects offered by many potential supervisors.
Each project offered by a supervisor has a quota of students.
Each supervisor also has an overall quota of students they can take on (from amongst all projects they offer, they can offer more than one and each project will have its own quota - obviously the overall quota for a supervisor is a hard constraint as well as the individual project supervision quotas).
I have decision variables for the student/project choice combos (tuples of ('student', 'project') as well as gap variables per student in the event it's not possible to assign them a project.
I can add a constraint restricting a student at most one project (it's possible some may be assigned to 0) and I can add a constraint that respects the individual project quotas (no more than X student for this particular project).
My problem is adding a constraint for the OVERALL quota a supervisor may have. That would involve inputing a constraint across all projects they offer and all students that have put that project in the list. Any suggestions or is there a workaround on this?
In essence, I'm doing somethign like this which feels pretty dodgy.
for s in supervisors:
oq = get_quota(s)
my_sum = None
for a in supervisors[s].areas: # areas = projects in pratice
if my_sum is None:
my_sum = x.sum('*', a)
else:
my_sum += x.sum('*', a)
m.addConstr(my_sum <= oq)
-
You approach looks fine. You could alternatively just sum over all students of a specific supervisor' area directly
for s in supervisors:
oq = get_quota(s)
m.addConstr(gurobipy.quicksum(x[i,a] for i in students for a in supervisors[s].areas) <= oq)Best regards,
Jaromił0 -
Thanks so much for your reply.
Could I ask for advice as to how to post some additional constraints? (Different problem). It's a sports scheduling problem and a deviation variable needs to be calculated to determine (for the objective function calculation) how far a soft constraint has been deviated from. See below. Any suggestions appreciated.
0 -
You can use the addGenConstrAbs method to model equalities BA1 and addGenConstrMax to model qualities BA2. Note that all general constraints only accept Var objects as input. This means that you will have to introduce an auxiliary variable for every term \(g_{ij} - \sum_{s\in P}x_{ijs}\) and \(\sum_{j\in U\backslash \{i\}}(x_{ijs} + x_{jis})+1\).
0 -
The auxiliary variables would be the deviation variables, yes?
0 -
The auxiliary variables \(w_{ij}, z_{is}\) would be
\[\begin{align*}
w_{ij} &= g_{ij} - \sum_{s\in P}x_{ijs} \quad \forall i,j\in U, i\neq j\\
z_{is} &= \sum_{j\in U\backslash \{i\}}(x_{ijs} + x_{jis})+1 \quad \forall i\in U, s\in P
\end{align*}\]0 -
OK, I think I see now. The w and z variables basically correspond to the "d" ones in the original equation.
Thanks so much for your fast replies.
0 -
The \(w\) and \(z\) variables don't really correspond to your \(d\) variables. They are required to work with Gurobi's general constraints. What you have to model in Gurobi is
\[\begin{align*}
w_{ij} &= g_{ij} - \sum_{s\in P}x_{ijs} &\forall i,j\in U, i\neq j\\
z_{is} &= \sum_{j\in U\backslash \{i\}}(x_{ijs} + x_{jis})+1 &\forall i\in U, s\in P\\
d_{ij} &= |w_{ij}| &\forall i,j\in U, i\neq j\\
d_{is} &= \max(0, z_{is}) &\forall i\in U, s\in P
\end{align*}\]
This is because all general constraints only accept single Var objects as input.0
サインインしてコメントを残してください。
コメント
7件のコメント