Variables return wrong values
回答済みI have added my variables as seen below however instead of getting a list for each of the variables, I only get the following results:
Variable x
-------------------------
C131 7000
C257 1858.76
Can someone suggest what I am doing wrong?
Thank you
m = gp.Model('OptimisationModel')
centers = np.array([(0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2)])
facilities = np.array([(0,0), (0,1), (0,2)])
# Indices for the facilities
facilitiesIndex = [*range(1,3)] #this needs to reflect the count from db
# Indices for the counties / later to be post codes/ as in GPs
countiesIndex = [*range(1,9)] #this needs to reflect count the db
#time period - 14 days
period = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14])
periodIndex = [*range(1,14)] #this needs to reflect count the db
#minimum amount of dose made by one reactor per day (one minute run per day)
minDosePerR = 600
#max number of hours every reactor can run for (8 hours a day)
maxRunTime = 8*60
#reactor count max
rMax = 8
#county population - DEMAND
CountyPopulation = 7000
#nurse number of admissions per day
nurse_ratePerDay = 28
#max number of nurses in each clinic to administrate vaccines
nurse_max = 8
#cost per day per nurse
costPerNurse = 105
#cost to set up one reactor + storage staff and the rest for 14 days
costPerFacilitySetup = 10000
#cost of travel per mile by vehicle
costPerMile = 10
maxDr = 288600
#capacitytoStoreAtGP
capacityToStorePerGP = 399
#capacityToStoreAtHospital
capacityTOStorePerHospital = 2000
#storagecostPerUnitAtGP
storageCostPerDoseGP = 0.90
#StoragecostPerUnitAtHospital
storageCostPerDoseHospital = 0.79
budget = 200000
# Compute key parameters of MIP model formulation
num_facilities = len(facilities)
num_customers = len(centers)
num_period = len(period)
cartesian_prod_fc = list(product(range(num_facilities), range(num_customers)))
#Select whether facility is selected to be a factory
select = m.addVars(num_facilities, vtype=GRB.BINARY, name='Select')
#number of reactors at facility f at period t [1,.....f-1]
rn = m.addVars(num_facilities, vtype=GRB.CONTINUOUS)
#number of allocated doses from facility f for center c at period t [[num_customers lenght],....,[num_customers lenght-1]]
allocList = m.addVars(num_period,num_customers, vtype=GRB.CONTINUOUS)
#number of nurses at location c [[num_customers lenght],....,[num_customers lenght-1]]
ncList = m.addVars(num_period,num_customers, vtype=GRB.CONTINUOUS)
m.addConstr((allocList.sum() <= CountyPopulation), name='demandConstraints')
m.addConstr(((allocList.sum()*0.69) +
(ncList.sum()*105) +
(rn.sum()* maxRunTime*0.70) +
(rn.sum()* 10000)
<= budget), name='budgetConstraints')
# for f in facilitiesIndex:
# m.addConstrs((rn[i] <= rMax for i in range(num_facilities)), name='Setup2ship')
m.setObjective((rn.sum()*maxRunTime*600)-
(allocList.sum())-
(ncList.sum()*28), GRB.MINIMIZE)
m.optimize()
-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
I solved your model and it looks like all variables are equal to zero except for the two in your printout:
C131 7000
C257 1.8587619047619048e+03It looks like you are probably using Model.printAttr('X') to view your variable solution values. For brevity, this function prints all non-zero variable values. This is why you are only seeing the two, non-zero values --- rather than the full list of variables.
You can confirm by viewing the values of your full variable list. This can be done in a few different ways. My two other favorites are:
- Write a SOL file by running the following after you optimize the model:
m.write('results.sol') - Creating a dataframe with the results by running the following after you optimize:
import pandas as pd
# Store Variable info
varInfo = [(v.varName, v.X, v.LB, v.UB) for v in m.getVars() ]
df = pd.DataFrame(varInfo)
df.columns=['Variable Name','Solution Value', 'LB','UB']
0 - Write a SOL file by running the following after you optimize the model:
-
@..., thank you for your response. Yes makes sense. Any suggestion why they are zero? Because they shouldn't be! Thank you very much
0 -
Can you tell me a bit about why you would expect more to be non-zero? Are there specific variables you would expect to be non-zero?
From what I can tell, within each of the variable tupledicts there is no differentiation between variables. This can lead to some redundancy.
For example, let's look at the \(\texttt{allocList}\) tupledict. Each one of the 100+ \(\texttt{allocList}\) variables, has the same objective coefficient: -1. Each of the \(\texttt{allocList}\) variables participates in two constraints, again each with the same coefficient:
m.addConstr((allocList.sum() <= 7000), name='demandConstraints')
m.addConstr(((allocList.sum()*0.69) +
(ncList.sum()*105) +
(rn.sum()* maxRunTime*0.70) +
(rn.sum()* 10000)
<= budget), name='budgetConstraints')Lastly, there are no upper or lower bounds on any of the variables in the \(\texttt{allocList}\) list. So, as far as the math program is concerned all of the \(\texttt{allocList}\) variables are identical. The only thing that limits them is the demand constraint. Therefore, the following solution values of \(\texttt{allocList}\) would all be equivalent:
Solution 1 Solution 2 Solution 3 allocList[0,0] 7,000 - 1,000 allocList[0,1] - 7,000 1,000 allocList[0,2] - - 1,000 allocList[0,3] - - 4,000 Total 7,000 7,000 7,000 This means that there could be MANY values of \(\texttt{allocList}\) that would satisfy the constraints and get the same objective. This makes me think there is something missing from your model that will lead to a solution that is a bit more realistic and closer to what you would expect. This could include using your three \(\texttt{Select}\) binary variables. Or, it may mean creating constraints that are associated with some of your unused constants like \(\texttt{capacityToStorePerGP}\), \(\texttt{capacityTOStorePerHospital}\), \(\texttt{nurse_ratePerDay}\), and \(\texttt{nurse_max}\).
0
投稿コメントは受け付けていません。
コメント
4件のコメント