'How do I Implement a Utility Function from a Preference Table

I'm working with a facility location/resource allocation problem with a dataset that looks like this:

Agents_df

Agent First Preference Second Preference Third Preference
1 Office A Office B Office C
2 Office C Office B Office A
3 Office A Office C Office B

I want to build a utility function based off this table where an agent derives 'utils' from their office assignment according to where that assignment is ranked.

Essentially what I want to ask is how can I write this function into DOcplex? This is what I have so far for further context:

### Decision Variables
nb_offices = len(offices_df) #number of offices
Open = mdl.integer_var_list(nb_offices, 0, 1, "open") #is the office open (1) or not (0)

nb_agents = len(agents_df) #number of agents
assignment = mdl.integer_var_list(nb_agents, 0, nb_offices - 1, "assigned") #whether an agent is assigned to an office (1) or not (0)

### Add constraint stating that the office of every assigned employee must be open
for a in assignment:
    mdl.add(mdl.element(Open, a) == 1)
    
### Add constraint stating that the number of employees assigned to each office must not exceed its capacity
for o in range(nb_offices):
    mdl.add(mdl.count(assignment, o) <= offices_df['Capacity'][o])
    
### Add constraint that there may only be a maximum of 2 offices opened
total_offices = mdl.sum(Open)
mdl.add(total_offices <= 2)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source