# Balanced Assignment¶

This example is the Balanced Assignment challenge, in which we need to create groups that are as diverse as possible.

Balanced Assignment

Given several employee categories (title, location, department, male/female, …) and a specified number of groups, assign every employee to a project group so that the groups are the same size, and they are as diverse as possible.

Together with this specification, we are given a list of 210 people and their information.

This challenge is an interesting optimization challenge. As always, we start by filling out the glossary. We create a type to represent the people, and types for every information type we have. Then, we also need a type which will represent a number of persons, and a type which will just represent a large integer.

Note here that because our list has 210 entries, it would be nearly impossible to add all of these into the Values column of the glossary. We opt here to not define the values, but instead we will point the glossary to our data table, which will tell the solver that every value in the data table is a possible value.

Type
Name Type Values
Person String See Data Table Person
Group Int [0..12]
Department string See Data Table Person
Location string See Data Table Person
Gender string See Data Table Person
Title string See Data Table Person
NbPersons int [16..19]

Next up, we introduce a function for every parameter, which will assign every person to their info. We will also need a function which maps every group on a number, representing the number of people in that group.

Function
Name Type
department of Person Department
location of Person Location
gender of Person Gender
title of Person Title
group of Person Group
number of Group Int

To finish our glossary, we introduce a constant Score which will keep track of the score of a solution.

Constant
Name Type
Score BoundNumber

With our glossary finished, we can move on to the next step. We need a way to calculate the Score value for a solution. To do this, we will check for every two people that if they have something in common, that they are in a different group. If this is the case, then we add 1 to our score.

Calculate Score
C+ Person called p1 Person called p2 department of p1 location of p1 gender of p1 title of p1 group of p1 Score
1 - - = department of p2 - - - not(group of p2) 1
2 - - - = location of p2 - - not(group of p2) 1
3 - - - - = gender of p2 - not(group of p2) 1
4 - - - - - = title of p2 not(group of p2) 1

Now, we want to count the number of people per group. Again, we can do this in a simple table. `Number in group of Person` is a combination of `Number in Group` and `group of Person`. This allows us to count the amount of people per group.

Count the number of people in a group
C+ Person Group group of Person number in Group
1 - - Group 1

Finally, we need to create the constraint that each group contains between 16 and 19 people. This is fairly straightforward using a constraint table:

Set group sizes
E* Group number in Group
1 - [16, 19]

To let the cDMN solver know that we want to find the solution with the highest score, we introduce a `Goal` table to maximize.

Goal
Maximize Score

And that’s it! We can now run the solver to find the optimal solution. It takes about four minutes, but eventually it finds the following optimal solution: Quite cool!

```Model 1
==========
department_of_Person := {BIW->NNE, KRS->WSW, TLR->NNW, VAA->NNW, JRT->NNE, AMR->SSE, MES->NNE, JAD->NNE, MJR->NNE, JRS->NNE, HCN->SSE, DAN->NNE, CWT->NNE, DCN->NNE, SWR->WSW, TBG->NNE, LVO->NNE, ECA->NNE, MAI->NNE, JCO->SSE, DWO->SSE, AJH->NNE, CAE->SSE, MRL->WSW, FTR->NNE, SJO->NNE, DHY->NNE, DHE->NNE, CWU->NNW, EAZ->NNE, RFS->NNE, JWS->WSW, RVY->NNE, PMO->SSE, RGR->NNE, LLR->NNE, FEE->NNW, ADS->NNE, CFS->NNW, DBN->SSE, PSN->NNE, REN->NNE, FWI->NNW, MPO->SSE, JDS->NNW, GCY->NNE, WMS->NNE, JMS->NNE, SWS->NNW, PMY->NNE, WAD->NNE, TAR->NNE, RPR->NNE, PWS->NNE, EJD->WSW, CPR->NNE, AVI->NNE, MTH->NNE, CGH->NNE, MKN->WSW, HFE->NNW, SCS->NNE, DGR->NNE, IGN->NNE, CAR->NNW, JAE->NNE, OGZ->NNE, RLN->SSE, AGI->SSE, ACI->NNE, CAH->SSE, LVN->NNE, DLT->SSE, RCR->NNE, CTS->NNE, AJS->SSE, NHI->NNE, RHA->NNW, RMN->NNE, RHD->SSE, SBM->NNW, PSO->NNE, RDL->NNW, DET->NNE, KWR->NNE, PSL->SSE, TEN->NNW, SME->NNE, WLB->NNE, RHE->NNE, RJY->SSE, SJH->NNE, DHR->SSE, HJO->NNE, RLS->NNE, KLN->WSW, SBK->NNE, HKA->NNE, KWY->NNE, WFS->NNW, MBM->SSE, RCG->SSE, APT->NNE, TMN->NNE, FJT->WSW, KKI->NNE, BCG->NNE, MKA->NNE, KKA->NNE, SGA->NNW, BLE->NNE, TWD->SSE, EGR->NNE, ABE->NNW, SCR->NNE, DSF->NNW, RDN->NNE, JWB->NNW, VMA->SSE, MLE->NNE, CEH->NNE, MRH->NNE, JMA->NNE, JMT->NNE, CGY->NNE, RJZ->NNW, HMN->WSW, DJA->NNE, NMA->NNE, MMY->NNE, MRE->NNE, SMA->NNE, MMA->NNE, JMQ->NNE, LSR->NNW, CMY->NNE, MNA->NNE, ANV->SSE, SNN->NNE, DMN->NNW, FJL->NNE, SOL->NNE, RGF->NNE, EGL->NNE, RJE->NNE, GMR->NNE, MBY->NNE, SML->NNE, AJT->NNE, PAS->NNE, API->NNE, SDN->NNE, BAN->NNE, JJR->SSE, GGH->NNW, MBT->NNE, GDE->NNE, MSR->SSE, LMN->NNW, PST->NNE, KRA->NNE, ESO->NNE, ASO->NNE, PSA->NNE, UJT->NNE, MRZ->WSW, SER->NNW, RSN->NNE, RSR->NNW, JPY->NNE, DLD->NNE, ASA->SSE, HAN->SSE, JAP->SSE, MFR->NNE, MMH->NNE, AJU->SSE, MSL->NNW, PSY->NNE, FSM->NNE, PSZ->SSE, CSN->NNE, TJY->WSW, MDI->NNE, TTI->NNE, ETN->NNE, MRK->NNE, RMS->NNE, REZ->NNE, TBR->NNE, HWG->NNE, SCG->NNW, JLN->NNE, JSG->NNE, EHR->NNE, LHS->NNE, JWR->NNE, SAS->NNE, CGS->NNE, MET->NNE, KWN->WSW, AWN->NNE, HRR->NNE, WHF->NNE, RGE->SSE, HMG->NNE, GWZ->NNE, XYF->NNE, JEN->NNE, BLW->NNE}.
location_of_Person := {BIW->Peoria, KRS->Springfield, TLR->Peoria, VAA->Peoria, JRT->Springfield, AMR->Peoria, MES->Peoria, JAD->Peoria, MJR->Springfield, JRS->Springfield, HCN->Peoria, DAN->Springfield, CWT->Springfield, DCN->Peoria, SWR->Peoria, TBG->Springfield, LVO->Peoria, ECA->Springfield, MAI->Peoria, JCO->Macomb, DWO->Peoria, AJH->Peoria, CAE->Peoria, MRL->Peoria, FTR->Peoria, SJO->Peoria, DHY->Urbana, DHE->Peoria, CWU->Peoria, EAZ->Peoria, RFS->Peoria, JWS->Peoria, RVY->Peoria, PMO->Peoria, RGR->Peoria, LLR->Peoria, FEE->Springfield, ADS->Peoria, CFS->Joliet, DBN->Peoria, PSN->Peoria, REN->Peoria, FWI->Peoria, MPO->Peoria, JDS->Peoria, GCY->Peoria, WMS->Springfield, JMS->Springfield, SWS->Springfield, PMY->Peoria, WAD->Springfield, TAR->Peoria, RPR->Peoria, PWS->Peoria, EJD->Peoria, CPR->Peoria, AVI->Springfield, MTH->Joliet, CGH->Springfield, MKN->Peoria, HFE->Carbondale, SCS->Peoria, DGR->Springfield, IGN->Springfield, CAR->Peoria, JAE->Springfield, OGZ->Peoria, RLN->Peoria, AGI->Peoria, ACI->Peoria, CAH->Peoria, LVN->Springfield, DLT->Peoria, RCR->Peoria, CTS->Peoria, AJS->Peoria, NHI->Carbondale, RHA->Carbondale, RMN->Springfield, RHD->Peoria, SBM->Peoria, PSO->Peoria, RDL->Joliet, DET->Springfield, KWR->Peoria, PSL->Peoria, TEN->Springfield, SME->Springfield, WLB->Peoria, RHE->Peoria, RJY->Springfield, SJH->Cairo, DHR->Peoria, HJO->Carbondale, RLS->Peoria, KLN->Peoria, SBK->Cairo, HKA->Carbondale, KWY->Peoria, WFS->Peoria, MBM->Peoria, RCG->Peoria, APT->Peoria, TMN->Peoria, FJT->Peoria, KKI->Carbondale, BCG->Urbana, MKA->Carbondale, KKA->Peoria, SGA->Springfield, BLE->Peoria, TWD->Peoria, EGR->Peoria, ABE->Peoria, SCR->Peoria, DSF->Springfield, RDN->Peoria, JWB->Peoria, VMA->Peoria, MLE->Macomb, CEH->Springfield, MRH->Peoria, JMA->Carbondale, JMT->Peoria, CGY->Springfield, RJZ->Peoria, HMN->Springfield, DJA->Peoria, NMA->Carbondale, MMY->Peoria, MRE->Peoria, SMA->Joliet, MMA->Carbondale, JMQ->Carbondale, LSR->Peoria, CMY->Peoria, MNA->Carbondale, ANV->Peoria, SNN->Macomb, DMN->Peoria, FJL->Springfield, SOL->Evansville, RGF->Springfield, EGL->Peoria, RJE->Macomb, GMR->Peoria, MBY->Peoria, SML->Springfield, AJT->Peoria, PAS->Peoria, API->Springfield, SDN->Peoria, BAN->Peoria, JJR->Springfield, GGH->Peoria, MBT->Peoria, GDE->Peoria, MSR->Peoria, LMN->Peoria, PST->Springfield, KRA->Peoria, ESO->Springfield, ASO->Carbondale, PSA->Springfield, UJT->Springfield, MRZ->Peoria, SER->Peoria, RSN->Joliet, RSR->Peoria, JPY->Peoria, DLD->Urbana, ASA->Peoria, HAN->Peoria, JAP->Peoria, MFR->Springfield, MMH->Joliet, AJU->Springfield, MSL->Springfield, PSY->Springfield, FSM->Springfield, PSZ->Peoria, CSN->Joliet, TJY->Springfield, MDI->Peoria, TTI->Carbondale, ETN->Peoria, MRK->Peoria, RMS->Peoria, REZ->Evansville, TBR->Peoria, HWG->Peoria, SCG->Joliet, JLN->Peoria, JSG->Peoria, EHR->Peoria, LHS->Peoria, JWR->Springfield, SAS->Peoria, CGS->Springfield, MET->Peoria, KWN->Springfield, AWN->Springfield, HRR->Macomb, WHF->Peoria, RGE->Springfield, HMG->Joliet, GWZ->Joliet, XYF->Peoria, JEN->Peoria, BLW->Peoria}.
gender_of_Person := {BIW->M, KRS->F, TLR->F, VAA->M, JRT->M, AMR->M, MES->M, JAD->M, MJR->M, JRS->M, HCN->M, DAN->M, CWT->M, DCN->M, SWR->M, TBG->M, LVO->M, ECA->M, MAI->F, JCO->M, DWO->M, AJH->M, CAE->M, MRL->M, FTR->M, SJO->M, DHY->M, DHE->M, CWU->M, EAZ->M, RFS->M, JWS->M, RVY->M, PMO->M, RGR->M, LLR->M, FEE->M, ADS->M, CFS->M, DBN->M, PSN->M, REN->F, FWI->M, MPO->M, JDS->M, GCY->M, WMS->M, JMS->M, SWS->M, PMY->M, WAD->M, TAR->M, RPR->M, PWS->M, EJD->F, CPR->M, AVI->F, MTH->M, CGH->M, MKN->M, HFE->M, SCS->M, DGR->M, IGN->M, CAR->M, JAE->M, OGZ->M, RLN->M, AGI->M, ACI->F, CAH->F, LVN->F, DLT->F, RCR->M, CTS->M, AJS->M, NHI->M, RHA->M, RMN->M, RHD->M, SBM->M, PSO->M, RDL->M, DET->M, KWR->M, PSL->F, TEN->M, SME->M, WLB->M, RHE->M, RJY->M, SJH->M, DHR->M, HJO->M, RLS->M, KLN->M, SBK->M, HKA->M, KWY->M, WFS->M, MBM->M, RCG->M, APT->M, TMN->M, FJT->M, KKI->M, BCG->M, MKA->M, KKA->M, SGA->F, BLE->M, TWD->M, EGR->F, ABE->M, SCR->M, DSF->M, RDN->M, JWB->F, VMA->M, MLE->M, CEH->M, MRH->M, JMA->M, JMT->F, CGY->M, RJZ->M, HMN->F, DJA->M, NMA->F, MMY->M, MRE->M, SMA->M, MMA->M, JMQ->F, LSR->M, CMY->M, MNA->M, ANV->M, SNN->F, DMN->M, FJL->M, SOL->M, RGF->M, EGL->M, RJE->M, GMR->M, MBY->F, SML->M, AJT->M, PAS->M, API->M, SDN->M, BAN->M, JJR->M, GGH->M, MBT->M, GDE->M, MSR->M, LMN->F, PST->M, KRA->M, ESO->M, ASO->F, PSA->M, UJT->M, MRZ->F, SER->M, RSN->M, RSR->M, JPY->M, DLD->F, ASA->M, HAN->M, JAP->M, MFR->M, MMH->M, AJU->M, MSL->M, PSY->M, FSM->M, PSZ->M, CSN->M, TJY->M, MDI->M, TTI->M, ETN->M, MRK->M, RMS->M, REZ->M, TBR->M, HWG->M, SCG->M, JLN->F, JSG->M, EHR->M, LHS->M, JWR->M, SAS->F, CGS->M, MET->F, KWN->M, AWN->M, HRR->M, WHF->M, RGE->M, HMG->F, GWZ->M, XYF->M, JEN->M, BLW->M}.