def _Calculate_Value(X, Z,model_data,scenario_name): ### Use M2S Object to calculate parameters import Core.Solvers.MTSSP.M2S_item as M2S_item duration = model_data._data['trial_duration'] gammaL = {} gammaD = {} revenue_max = {} prod = model_data._data['product'][None] sg = model_data._data['trial'][None] ts = model_data._data['time_step'][None] resource_type = model_data._data['resource_type'][None] trial_cost = model_data._data['trial_cost'] success = {} for i in builtins.MD_GLOBAL._data['product'][None]: if builtins.Tree_Graph_GLOBAL.G.node[scenario_name]['Outcome'][builtins.MD_GLOBAL._data['product'][None].index(i)] == len(sg): success[i] = 1 else: success[i] = 0 for items in model_data._data['gammaL']: gammaL[items[0]] = model_data._data['gammaL'][items] for items in model_data._data['gammaD']: gammaD[items[0]] = model_data._data['gammaD'][items] for items in model_data._data['maximum_revenue']: revenue_max[items[0]] = model_data._data['maximum_revenue'][items] last_trial = sg[-1] ## Calculate running rev rev_run = M2S_item.calc_rr(revenue_max,gammaL,duration, prod, sg, ts) ##Calculate open rev rev_open = M2S_item.calc_openrev(revenue_max,gammaL,duration, prod, sg, ts, len(ts)) ##Calculate Discounting Factor discounting_factor = M2S_item.calc_discounting_factor(revenue_max,gammaL,trial_cost, prod, sg, len(ts)) cost = sum((1 - 0.025 * (t - 1)) * trial_cost[(i,j)]*X[prod.index(i)][sg.index(j)][ts.index(t)] for i in prod for j in sg for t in ts) revenue = sum( success[i] * revenue_max[i]*X[prod.index(i)][sg.index(last_trial)][ts.index(t)] for i in prod for t in ts) - sum(success[i]*gammaD[i]*Z[prod.index(i)][sg.index(j)][ts.index(t)] for i in prod for j in sg if j>1 for t in ts) - sum(gammaL[i]*success[i]*(t + duration[(i,len(sg))])*X[prod.index(i)][sg.index(last_trial)][ts.index(t)] for i in prod for t in ts) FRV1 = sum(success[i] * rev_open[(i,j)] * discounting_factor[(i,j)] * Z[prod.index(i)][sg.index(j)][ts.index(len(ts))] for i in prod for j in sg) FRV2 = sum(success[i] * rev_run[(i,j,t)] * discounting_factor[(i,j+1)] * X[prod.index(i)][sg.index(j)][ts.index(t)] for i in prod for j in sg if j < last_trial for t in ts if t > len(ts) - duration[(i,j)]) Free_revenue = FRV1 + FRV2 value = revenue + Free_revenue - cost return value
def deterministic_PRDP_solve_with_return(mipgap, model_data, output_directory): ### Start Solution Timer start_time = timer.clock() init_mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss ##Solver Choice opt = SolverFactory("cplex") options = Options() opt.options.mip_tolerances_mipgap = mipgap ########################################## ### Generate Scenario ########################################## #### Problem Info For Scenario Generation num_product = len(model_data._data['product'][None]) prod = model_data._data['product'][None] num_trial = len(model_data._data['trial'][None]) sg = model_data._data['trial'][None] prob = model_data._data['probability'] num_ts = len(model_data._data['time_step'][None]) ### Generate all possible outcomes Outcomes = itertools.product(range(num_trial + 1), repeat = num_product) Outcomes = tuple(Outcomes) ### From Outcomes Name and Generate Scenarios scenario = 1 List_of_Scenarios = {} SS=[] for items in Outcomes: scenario_name = scenario List_of_Scenarios[scenario_name] = scenario_class.scenario(items,prob, prod,sg) SS.append(scenario_name) scenario += 1 ########################################################## ### Input Parameters to Solver ########################################################## rev_max = {} gammaL = {} gammaD = {} duration = {} trial_cost = {} revenue_max = {} success = {} rev_run = {} rev_open = {} discounting_factor ={} ##Set product product = model_data._data['product'][None] ##Set stage_gate stage_gate = model_data._data['trial'][None] ## Set time step time_step = model_data._data['time_step'][None] ##Set resource type resource_type = model_data._data['resource_type'][None] ## Set duration duration = model_data._data['trial_duration'] ## Set trial cost trial_cost = model_data._data['trial_cost'] ## Set Discount Values for items in model_data._data['gammaL']: gammaL[items[0]] = model_data._data['gammaL'][items] for items in model_data._data['gammaD']: gammaD[items[0]] = model_data._data['gammaD'][items] ## Set Maximum Revenue for items in model_data._data['maximum_revenue']: revenue_max[items[0]] = model_data._data['maximum_revenue'][items] ## Set Last Trial last_trial = len(stage_gate) last_time_step = len(time_step) ##Calculate Success matrix success = M2S_item.calc_success(product, num_trial, List_of_Scenarios) ## Calculate running rev rev_run = M2S_item.calc_rr(revenue_max,gammaL,duration, product, stage_gate, time_step) ##Calculate open rev rev_open = M2S_item.calc_openrev(revenue_max,gammaL,duration, product, stage_gate, time_step, last_time_step) ##Calculate Discounting Factor discounting_factor = M2S_item.calc_discounting_factor(revenue_max,gammaL,trial_cost, product, stage_gate, last_time_step) ## Set Probabilities and Outcomes pb = {} outcome = {} for s in SS: pb[s] = List_of_Scenarios[s].probability outcome[s] = List_of_Scenarios[s].outcome resource_max = {} for items in model_data._data['max_resource']: resource_max[items[0]] = model_data._data['max_resource'][items] resource_required = {} resource_required = model_data._data['resource_requirement'] ####################################################################### ### Generate Non-Anticipativity Constraints ####################################################################### OC = {} for s in SS: OC[s] = [] for i in prod: OC[s].append(List_of_Scenarios[s].outcome[prod.index(i)]) phi= {} phii= {} phij ={} for s in SS: for sp in SS: if sp > s: for i in prod: OCtest = list(OC[s]) OCtest[prod.index(i)] += 1 OCtest2 = list(OC[s]) OCtest2[prod.index(i)] += -1 if OCtest == OC[sp]: trl = OC[s][prod.index(i)] + 1 phi[(s,sp)] = 1 phii[(s,sp)] = i phij[(s,sp)] = trl if OCtest2 == OC[sp]: trl = OC[sp][prod.index(i)] + 1 phi[(s,sp)] = 1 phii[(s,sp)] = i phij[(s,sp)] = trl ############################################ ### Solve Model ############################################ model = defunction.de(prod,sg,time_step,resource_type,SS,resource_max,gammaL,gammaD,duration,trial_cost,resource_required, revenue_max,pb, success,last_time_step, last_trial, rev_run, rev_open, discounting_factor, phi, phii, phij, outcome) sttmr = timer.clock() results= opt.solve(model) fttmr = timer.clock() fin_mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss model.solutions.load_from(results) Scenario_Results = {} for t in time_step: for s in SS: for i in product: for j in stage_gate: if model.Decision_X[i,j,t,s].value == 1: index = product.index(i) jndex = stage_gate.index(j) tndx = time_step.index(t) try: Scenario_Results[(i,j,t)] except: Scenario_Results[(i,j,t)] = 1 ### Make Output Directory if not os.path.exists(output_directory): os.makedirs(output_directory) save_file = "Deterministic_Solution" results.write(filename = os.path.join(output_directory, save_file)) Finish_Time = timer.clock() Total_Solve_Time = fttmr - sttmr Total_Time = Finish_Time - start_time Objective_Value = results['Problem'][0]['Lower bound'] ### Generate New File Name save_file = "Output" ### Open save file f = open(os.path.join(output_directory, save_file), "w") ### Generate file contents algorithm_time = 'Total Solve Time:' + ' ' + str(Total_Solve_Time) f.write(algorithm_time + '\n') algorithm_time = 'Total Time:' + ' ' + str(Total_Time) f.write(algorithm_time + '\n') objective = "ENPV:" + " " + str(Objective_Value) f.write(objective + '\n') total_resource = "Total Memory:" + " " + str(fin_mem-init_mem) f.write(total_resource + "\n") f.write(str(Scenario_Results) + "\n") f.close() from Core.Solvers.MSSP.MSSP_Results_Object import MSSP_Results_Object return_object = MSSP_Results_Object(Objective_Value, Total_Solve_Time,(fin_mem-init_mem), Total_Time) return return_object
def main(): ### Location and date information current_directory = os.path.dirname(os.path.realpath(__file__)) current_date = time.strftime('%m_%d_%Y', time.gmtime()) ### Set Results File Output Directory output_directory = current_directory + '/Solutions/' + 'SAA_DE' + '_' + current_date + '/' ### Loop Info # files = ['modeldata.dat','modeldatab.dat','modeldata3.dat'] # files = ['modeldata_4.dat','modeldata_10.dat','modeldata3.dat','modeldata3_5.dat','modeldata4_3.dat','modeldata4.dat','modeldata4_5.dat','modeldata5.dat'] # files = ['modeldata4_3.dat','modeldata4.dat','modeldata4_5.dat','modeldata5.dat','modeldata6.dat'] # files = ['modeldata_4.dat','modeldata_10.dat'] files = ['modeldata4_5.dat'] # Specify sample size # Number_of_Scenarios = {'modeldatab.dat': [.3, .5, .7, 1.0], 'modeldata.dat': [.3, .5, .7, 1.0], # 'modeldata3.dat': [0.05, 0.10, 0.15, 0.20, 0.25, .3, .4, .5, 0.6, 0.8, 1.0]} # Number_of_Scenarios = {'modeldata6.dat':[.125, 0.25]} # Number_of_Scenarios = {'modeldata4.dat':[0.5]} # 'modeldata_4.dat':[.75] # Number_of_Scenarios = {'modeldata_4.dat':[.75],'modeldata_10.dat':[.24],'modeldata3.dat':[.09375],'modeldata3_5.dat':[.192],'modeldata4_3.dat':[.148148148],'modeldata4.dat':[.5],'modeldata4_5.dat':[.0384],'modeldata5.dat':[0.25],'modeldata6.dat':[.125, 0.5, 0.75]} Number_of_Scenarios = { 'modeldata_4.dat': [.75], 'modeldata_10.dat': [.24], 'modeldata3.dat': [.09375], 'modeldata3_5.dat': [.192], 'modeldata4_3.dat': [.148148148], 'modeldata4.dat': [.5], 'modeldata4_5.dat': [.0384, 0.25], 'modeldata5.dat': [.0625, 0.25], 'modeldata6.dat': [.125, 0.5, 0.75] } # Solve Count -- The specified number of time we solve the model. # scount = {'modeldata4.dat': 1} scount = { 'modeldata.dat': 1, 'modeldata_4.dat': 1, 'modeldata_10.dat': 1, 'modeldata3.dat': 1, 'modeldata3_5.dat': 1, 'modeldata4_3.dat': 1, 'modeldata4.dat': 1, 'modeldata4_5.dat': 1, 'modeldata5.dat': 1, 'modeldata6.dat': 1 } # import_record_sceanrios = 'record_subset_scenarios_10_29_2020.json' # with open(import_record_sceanrios) as f: # Subset_Scenarios_record = dict([tuple((tuple(x[0]), x[1])) for x in json.loads(f.read())]) for fl in files: print("Starting File " + str(fl)) ##### Import the data into the system model_data = get_datafile(fl) ### Define number of products and number of trials NP = len(model_data._data['product'][None]) NT = len(model_data._data['trial'][None]) Product = model_data._data['product'][None] Trials = model_data._data['trial'][None] Probability = model_data._data['probability'] ### Generate all possible outcomes Outcomes = itertools.product(range(NT + 1), repeat=NP) Outcomes = list(Outcomes) ### Generate Full Scenario Set scenario = 1 Scenario_Objects = {} Scenario_Outcomes = {} Scenario_List = [] for s in Outcomes: Scenario_Objects[scenario] = scenario_class.scenario( s, Probability, Product, Trials) Scenario_Outcomes[scenario] = s Scenario_List.append(scenario) scenario += 1 Full_Sample_Size = len(Outcomes) Full_Sample_Size = math.ceil(Full_Sample_Size) n = 0 nmax = 5 sub_model_Max = 0 sub_model_AVG = 0 sub_model_Max_alter = 0 sub_model_AVG_alter = 0 full_model_Max = 0 full_model_AVG = 0 reducedfull_model_Max = 0 reducedfull_model_AVG = 0 NAC_AVG = 0 NAC_MAX = 0 NAC_AVG_alter = 0 NAC_MAX_alter = 0 Full_AVG = 0 Full_Max = 0 while n < nmax: sub_model_NAC_count = 0 sub_model_NAC_count_alter = 0 full_model_NAC_count = 0 reducedfull_model_NAC_count = 0 for ns in Number_of_Scenarios[fl]: ### Determine Sample Size Sample_Size = ns * len(Outcomes) Sample_Size = math.ceil(Sample_Size) try: import_record_sceanrios = 'record_subset_scenarios_10_29_2020.json' with open(import_record_sceanrios) as f: Subset_Scenarios_record = dict([ tuple((tuple(x[0]), x[1])) for x in json.loads(f.read()) ]) ### Select Scenarios Subset_Scenarios = Subset_Scenarios_record[fl, n, ns] print(len(Subset_Scenarios)) except: pass try: import_record_sceanrios = 'record_subset_scenarios_11_10_2020.json' with open(import_record_sceanrios) as f: Subset_Scenarios_record = dict([ tuple((tuple(x[0]), x[1])) for x in json.loads(f.read()) ]) ### Select Scenarios Subset_Scenarios = Subset_Scenarios_record[fl, n, ns] print(len(Subset_Scenarios)) except: pass try: import_record_sceanrios = 'record_subset_scenarios_11_19_2020.json' with open(import_record_sceanrios) as f: Subset_Scenarios_record = dict([ tuple((tuple(x[0]), x[1])) for x in json.loads(f.read()) ]) ### Select Scenarios Subset_Scenarios = Subset_Scenarios_record[fl, n, ns] print(len(Subset_Scenarios)) except: pass try: import_record_sceanrios = 'record_subset_scenarios_11_21_2020.json' with open(import_record_sceanrios) as f: Subset_Scenarios_record = dict([ tuple((tuple(x[0]), x[1])) for x in json.loads(f.read()) ]) ### Select Scenarios Subset_Scenarios = Subset_Scenarios_record[fl, n, ns] print(len(Subset_Scenarios)) except: pass ### Normalize Probability total_probability = 0 for s in Subset_Scenarios: total_probability += Scenario_Objects[s].probability S_Probability = {} for s in Subset_Scenarios: S_Probability[s] = Scenario_Objects[ s].probability / total_probability Pb = {} for s in Scenario_List: Pb[s] = Scenario_Objects[s].probability ### Generate NACs For Subset # print('Generating NACs for Scenarios' + str(Subset_Scenarios)) Uncertain_Parameters = [] for i in Product: Param = UP(str(i), 'endogenous', 'gradual', range(NT + 1), [], range(NT + 1)) Uncertain_Parameters.append(Param) Uncertain_Parameters = tuple(Uncertain_Parameters) ######################################################################################################## ################################### NAC GENERATION 3 ################################################### ### Generate NACs for Full Set Ptime = time.process_time() FullSet_Start = time.time() fullsetIter = 0 while fullsetIter < scount[fl]: phi_ij = {} OC = {} for s in Scenario_List: OC[s] = list(Scenario_Objects[s].outcome) for s in Scenario_List: for sp in Scenario_List: for i in Product: if s > sp: if OC[s][Product.index(i)] == OC[sp][ Product.index(i)]: pass else: if OC[s][Product.index(i)] > OC[sp][ Product.index(i)]: trl = OC[sp][Product.index(i)] + 1 try: phi_ij[(s, sp)].append( (i, trl)) except: phi_ij[(s, sp)] = [(i, trl)] else: trl = OC[s][Product.index(i)] + 1 try: phi_ij[(s, sp)].append( (i, trl)) except: phi_ij[(s, sp)] = [(i, trl)] fullsetIter += 1 FullSet_Total = time.time() - FullSet_Start Full_process_time = time.process_time() - Ptime ## record NAC pairs Full_AVG += len(phi_ij) / 1 if len(phi_ij) > Full_Max: Full_Max = len(phi_ij) ######################################################################################################## ################################### NAC GENERATION 3 ################################################### ### Generate NACs for Full Set #################################################################### ### Generate Model #################################################################### Success = {} for s in Scenario_List: for i in Product: Success[( i, s)] = Scenario_Objects[s].success[Product.index(i)] GammaD = {} GammaL = {} revenue_max = {} resource_max = {} for items in model_data._data['max_resource']: resource_max[ items[0]] = model_data._data['max_resource'][items] ## Set Discount Values for items in model_data._data['gammaL']: GammaL[items[0]] = model_data._data['gammaL'][items] for items in model_data._data['gammaD']: GammaD[items[0]] = model_data._data['gammaD'][items] ## Set Maximum Revenue for items in model_data._data['maximum_revenue']: revenue_max[ items[0]] = model_data._data['maximum_revenue'][items] ## Calculate running rev rev_run = M2S_item.calc_rr(revenue_max, GammaL, model_data._data['trial_duration'], Product, Trials, model_data._data['time_step'][None]) ##Calculate open rev rev_open = M2S_item.calc_openrev( revenue_max, GammaL, model_data._data['trial_duration'], Product, Trials, model_data._data['time_step'][None], model_data._data['time_step'][None][-1]) ##Calculate Discounting Factor discounting_factor = M2S_item.calc_discounting_factor( revenue_max, GammaL, model_data._data['trial_cost'], Product, Trials, model_data._data['time_step'][None][-1]) ######################################################################################################## ################################## Create Model 1 ###################################################### opt = SolverFactory("cplex") ### generate number of NAC constriants ######################################################################################################## ################################## Create Model 1 alternative ###################################################### ######################################################################################################## ################################## Create Model 2 ###################################################### Ptime = time.process_time() Model2_StartTime = time.time() m2 = 0 while m2 < scount[fl]: full_model = SAA(Product, Trials, model_data._data['time_step'][None], model_data._data['resource_type'][None], Subset_Scenarios, \ resource_max, GammaL, GammaD, model_data._data['trial_duration'], model_data._data['trial_cost'], \ model_data._data['resource_requirement'], revenue_max, S_Probability, \ Success, len(model_data._data['time_step'][None]), Trials[-1], rev_run, rev_open, discounting_factor, \ phi_ij, Scenario_Outcomes) m2 += 1 Model2TotalTime = time.time() - Model2_StartTime model_process_time = time.process_time() - Ptime ##### Solve Model opt = SolverFactory("cplex") ### generate number of NAC constriants full_model_NAC_count = len(full_model.NAC_Constraint) + len( full_model.NAC2_Constraint) + len( full_model.NAC3_Constraint) print('full_model_NAC_count', full_model_NAC_count) pai = len(full_model.NAC3_Constraint) / ( len(Product) * len(Trials) * (len(model_data._data['time_step'][None]) - 1)) xxx = len(Product) * len(Trials) * ( len(model_data._data['time_step'][None]) - 1) # print('pai',pai,xxx) if full_model_NAC_count > full_model_Max: full_model_Max = full_model_NAC_count print('full_model_Max', full_model_Max) ######################################################################################################## ######################################################################################################## ### Generate New File Name save_file = 'Full_' + str(fl) + "_" + str(ns) + "_" + str( nmax) + "_iterations" ### Open save file if not os.path.exists(output_directory): os.makedirs(output_directory) if n == 0: f = open(os.path.join(output_directory, save_file), "w") Header = "%-30s %-30s %-30s %-30s %-30s" % ( 'Full_scenario_pair', 'Full_model_AVG', 'Full_model_Max', 'Full_Count_AVG', 'Full_Count_MAX') f.write(Header) f.write('\n') else: f = open(os.path.join(output_directory, save_file), "a") ### Generate file contents RESULTS = "%-30s %-30s %-30s %-30s %-30s" % ( str(round(pai, 4)), str(round(Full_AVG, 4)), str(round(Full_Max, 4)), str(round(full_model_NAC_count, 4)), str(round(full_model_Max, 4)), ) f.write(RESULTS) f.write('\n') n += 1 f.write('\n') f.write('----------' + import_record_sceanrios + '---------------') f.write('\n') f.close()
def main(): ### Location and date information current_directory = os.path.dirname(os.path.realpath(__file__)) current_date = time.strftime('%m_%d_%Y', time.gmtime()) ### Set Results File Output Directory output_directory = current_directory + '/Solutions/' + 'SAA_DE' + '_' + current_date + '/' ### Loop Info # files = ['modeldata.dat','modeldatab.dat','modeldata3.dat'] files = [ 'modeldata_4.dat', 'modeldata_10.dat', 'modeldata3.dat', 'modeldata3_5.dat', 'modeldata4_3.dat', 'modeldata4.dat', 'modeldata4_5.dat', 'modeldata5.dat', 'modeldata6.dat' ] # files = ['modeldata4_3.dat','modeldata4.dat','modeldata4_5.dat','modeldata5.dat','modeldata6.dat'] # files = ['modeldata6.dat'] # files = ['modeldata_4.dat'] # Specify sample size # Number_of_Scenarios = {'modeldatab.dat': [.3, .5, .7, 1.0], 'modeldata.dat': [.3, .5, .7, 1.0], # 'modeldata3.dat': [0.05, 0.10, 0.15, 0.20, 0.25, .3, .4, .5, 0.6, 0.8, 1.0]} # Number_of_Scenarios = {'modeldata6.dat':[.125, 0.25]} # Number_of_Scenarios = {'modeldata4.dat':[0.5]} # 'modeldata_4.dat':[.75] # Number_of_Scenarios = {'modeldata_4.dat':[.75],'modeldata_10.dat':[.24],'modeldata3.dat':[.09375],'modeldata3_5.dat':[.192],'modeldata4_3.dat':[.148148148],'modeldata4.dat':[.5],'modeldata4_5.dat':[.0384],'modeldata5.dat':[.0625, 0.25, 0.5, 0.75, 1],'modeldata6.dat':[.125, 0.5, 0.75]} Number_of_Scenarios = { 'modeldata_4.dat': [.75], 'modeldata_10.dat': [.24], 'modeldata3.dat': [.09375], 'modeldata3_5.dat': [.192], 'modeldata4_3.dat': [.148148148], 'modeldata4.dat': [.5], 'modeldata4_5.dat': [.0384], 'modeldata5.dat': [.0625], 'modeldata6.dat': [.125] } # Solve Count -- The specified number of time we solve the model. # scount = {'modeldata4.dat': 1} scount = { 'modeldata.dat': 1, 'modeldata_4.dat': 1, 'modeldata_10.dat': 1, 'modeldata3.dat': 1, 'modeldata3_5.dat': 1, 'modeldata4_3.dat': 1, 'modeldata4.dat': 1, 'modeldata4_5.dat': 1, 'modeldata5.dat': 1, 'modeldata6.dat': 1 } import_record_sceanrios = 'record_subset_scenarios_10_29_2020.json' with open(import_record_sceanrios) as f: Subset_Scenarios_record = dict( [tuple((tuple(x[0]), x[1])) for x in json.loads(f.read())]) for fl in files: print("Starting File " + str(fl)) ##### Import the data into the system model_data = get_datafile(fl) ### Define number of products and number of trials NP = len(model_data._data['product'][None]) NT = len(model_data._data['trial'][None]) Product = model_data._data['product'][None] Trials = model_data._data['trial'][None] Probability = model_data._data['probability'] ### Generate all possible outcomes Outcomes = itertools.product(range(NT + 1), repeat=NP) Outcomes = list(Outcomes) ### Generate Full Scenario Set scenario = 1 Scenario_Objects = {} Scenario_Outcomes = {} Scenario_List = [] for s in Outcomes: Scenario_Objects[scenario] = scenario_class.scenario( s, Probability, Product, Trials) Scenario_Outcomes[scenario] = s Scenario_List.append(scenario) scenario += 1 Full_Sample_Size = len(Outcomes) Full_Sample_Size = math.ceil(Full_Sample_Size) n = 0 nmax = 30 sub_model_Max = 0 sub_model_AVG = 0 sub_model_Max_alter = 0 sub_model_AVG_alter = 0 full_model_Max = 0 full_model_AVG = 0 reducedfull_model_Max = 0 reducedfull_model_AVG = 0 NAC_AVG = 0 NAC_MAX = 0 NAC_AVG_alter = 0 NAC_MAX_alter = 0 while n < nmax: sub_model_NAC_count = 0 sub_model_NAC_count_alter = 0 full_model_NAC_count = 0 reducedfull_model_NAC_count = 0 for ns in Number_of_Scenarios[fl]: ### Determine Sample Size Sample_Size = ns * len(Outcomes) Sample_Size = math.ceil(Sample_Size) ### Select Scenarios Subset_Scenarios = Subset_Scenarios_record[fl, n, ns] print(len(Subset_Scenarios)) ### Normalize Probability total_probability = 0 for s in Subset_Scenarios: total_probability += Scenario_Objects[s].probability S_Probability = {} for s in Subset_Scenarios: S_Probability[s] = Scenario_Objects[ s].probability / total_probability Pb = {} for s in Scenario_List: Pb[s] = Scenario_Objects[s].probability ### Generate NACs For Subset # print('Generating NACs for Scenarios' + str(Subset_Scenarios)) Uncertain_Parameters = [] for i in Product: Param = UP(str(i), 'endogenous', 'gradual', range(NT + 1), [], range(NT + 1)) Uncertain_Parameters.append(Param) Uncertain_Parameters = tuple(Uncertain_Parameters) ######################################################################################################## ################################### NAC GENERATION 1 ################################################### ######################################################################################################## ################################### NAC GENERATION alternative ################################################### Ptime = time.process_time() Snac_StartTime = time.time() snacIter = 0 while snacIter < scount[fl]: NAC_Set_alter = NAC(Uncertain_Parameters, Subset_Scenarios, Scenario_Outcomes) NAC_Set_alter = list(NAC_Set_alter) phI_IJ_alter = {} for (s, sp) in NAC_Set_alter: i = 0 phI_IJ_alter[(s, sp)] = [] while i < len(Product): phList = [ Scenario_Outcomes[s][i], Scenario_Outcomes[sp][i] ] if phList[0] != phList[1]: phList.sort() Differentiation = (Product[i], phList[0] + 1) phI_IJ_alter[(s, sp)].append(Differentiation) i += 1 snacIter += 1 Snac_Total_alter = time.time() - Snac_StartTime Snac_process_time = time.process_time() - Ptime ## record NAC pairs NAC_AVG_alter += len(NAC_Set_alter) / 1 if len(NAC_Set_alter) > NAC_MAX_alter: NAC_MAX_alter = len(NAC_Set_alter) print('NAC_AVG_alter', NAC_AVG_alter, 'NAC_MAX_alter', NAC_MAX_alter) list_phijj_alter = [x for x in phI_IJ_alter] print('list_phijj_alter', len(list_phijj_alter)) ######################################################################################################## ################################### NAC GENERATION 2 ################################################### ######################################################################################################## ################################### NAC GENERATION 3 ################################################### ### Generate NACs for Full Set #################################################################### ### Generate Model #################################################################### Success = {} for s in Scenario_List: for i in Product: Success[( i, s)] = Scenario_Objects[s].success[Product.index(i)] GammaD = {} GammaL = {} revenue_max = {} resource_max = {} for items in model_data._data['max_resource']: resource_max[ items[0]] = model_data._data['max_resource'][items] ## Set Discount Values for items in model_data._data['gammaL']: GammaL[items[0]] = model_data._data['gammaL'][items] for items in model_data._data['gammaD']: GammaD[items[0]] = model_data._data['gammaD'][items] ## Set Maximum Revenue for items in model_data._data['maximum_revenue']: revenue_max[ items[0]] = model_data._data['maximum_revenue'][items] ## Calculate running rev rev_run = M2S_item.calc_rr(revenue_max, GammaL, model_data._data['trial_duration'], Product, Trials, model_data._data['time_step'][None]) ##Calculate open rev rev_open = M2S_item.calc_openrev( revenue_max, GammaL, model_data._data['trial_duration'], Product, Trials, model_data._data['time_step'][None], model_data._data['time_step'][None][-1]) ##Calculate Discounting Factor discounting_factor = M2S_item.calc_discounting_factor( revenue_max, GammaL, model_data._data['trial_cost'], Product, Trials, model_data._data['time_step'][None][-1]) ######################################################################################################## ################################## Create Model 1 ###################################################### opt = SolverFactory("cplex") ### generate number of NAC constriants ######################################################################################################## ################################## Create Model 1 alternative ###################################################### Ptime = time.process_time() Model1_StartTime = time.time() m1 = 0 while m1 < scount[fl]: sub_model_alter = SAA(Product, Trials, model_data._data['time_step'][None], model_data._data['resource_type'][None], Subset_Scenarios, \ resource_max, GammaL, GammaD, model_data._data['trial_duration'], model_data._data['trial_cost'], \ model_data._data['resource_requirement'], revenue_max, S_Probability, \ Success, len(model_data._data['time_step'][None]), Trials[-1], rev_run, rev_open, discounting_factor, \ phI_IJ_alter, Scenario_Outcomes) m1 += 1 Model1TotalTime_alter = time.time() - Model1_StartTime model_process_time = time.process_time() - Ptime ##### Solve Sub Model Model Ptime = time.process_time() Model1Solve_StartTime = time.time() s1 = 0 while s1 < scount[fl]: sub_results = opt.solve(sub_model_alter) s1 += 1 Model1Solve_TotalTime_alter = time.time( ) - Model1Solve_StartTime model_solve_process_time = time.process_time() - Ptime sub_model_alter.solutions.load_from(sub_results) ### generate number of NAC constriants sub_model_NAC_count_alter = len( sub_model_alter.NAC_Constraint) + len( sub_model_alter.NAC2_Constraint) + len( sub_model_alter.NAC3_Constraint) print('sub_model_NAC_count_alter', sub_model_NAC_count_alter) sub_model_AVG_alter += sub_model_NAC_count_alter / 1 if sub_model_NAC_count_alter > sub_model_Max_alter: sub_model_Max_alter = sub_model_NAC_count_alter print('sub_model_Max_alter', sub_model_Max_alter, 'sub_model_AVG_alter', sub_model_AVG_alter) ### generate number of NAC constriants ######################################################################################################## ################################## Create Model 2 ###################################################### ######################################################################################################## ######################################################################################################## ### Generate New File Name save_file = 'SNAC_Alter_' + str(fl) + "_" + str( ns) + "_" + str(nmax) + "_iterations" ### Open save file if not os.path.exists(output_directory): os.makedirs(output_directory) if n == 0: f = open(os.path.join(output_directory, save_file), "w") Header = "%-30s %-30s %-30s %-30s %-30s %-30s %-30s %-30s %-30s %-30s %-30s" % ( 'SAA_par_ENPV', 'SNAC_par_Time', 'SNAC_par_Model_Time', 'SNAC_par_Time(P)', 'SNAC_par_Model_Time(P)', 'SNAC_par_sol_Time', 'SNAC_par_sol_Time(P)', 'sub_model_par_AVG', 'sub_model_par_Max', 'NAC_Count_AVG_par', 'NAC_Count_MAX_par') f.write(Header) f.write('\n') else: f = open(os.path.join(output_directory, save_file), "a") ### Generate file contents RESULTS = "%-30s %-30s %-30s %-30s %-30s %-30s %-30s %-30s %-30s %-30s %-30s" % ( str(round(sub_model_alter.Expected_NPV(), 4)), str(round(Snac_Total_alter, 4)), str(round(Model1TotalTime_alter, 4)), str(round(Snac_process_time, 4)), str(round(model_process_time, 4)), str(round(Model1Solve_TotalTime_alter, 4)), str(model_solve_process_time), str(round(sub_model_AVG_alter, 4)), str(round(sub_model_Max_alter, 4)), str(NAC_AVG_alter), str(NAC_MAX_alter)) f.write(RESULTS) f.write('\n') n += 1 f.write('\n') f.write('----------' + import_record_sceanrios + '---------------') f.write('\n') f.close()
def _second_PH_mpfunc(self, model_data, Outcomes, btime, x_bar_z, cds, branch_probability_total, x_bar, x_non_zero, time, rho): ### Optimization things opt = SolverFactory("cplex") opt.options['threads'] = 6 ### Thread Specific Variables x_non_zero_t = [] x_bar_thread = copy.deepcopy(x_bar_z) tENPV = 0 ### Generate non-scenario specific model variables rev_max = {} gammaL = {} gammaD = {} duration = {} trial_cost = {} revenue_max = {} success = {} rev_run = {} rev_open = {} discounting_factor = {} ##Set product product = model_data._data['product'][None] ##Set stage_gate stage_gate = model_data._data['trial'][None] ## Set time step time_step = model_data._data['time_step'][None] ##Set resource type resource_type = model_data._data['resource_type'][None] ## Set duration duration = model_data._data['trial_duration'] ## Set trial cost trial_cost = model_data._data['trial_cost'] ## Set Discount Values for items in model_data._data['gammaL']: gammaL[items[0]] = model_data._data['gammaL'][items] for items in model_data._data['gammaD']: gammaD[items[0]] = model_data._data['gammaD'][items] ## Set Maximum Revenue for items in model_data._data['maximum_revenue']: revenue_max[items[0]] = model_data._data['maximum_revenue'][items] ## Set Last Trial last_trial = len(stage_gate) Last_Time_Step = len(time_step) ## Calculate running rev running_revenue = M2S_item.calc_rr(revenue_max, gammaL, duration, product, stage_gate, time_step) ##Calculate open rev open_revenue = M2S_item.calc_openrev(revenue_max, gammaL, duration, product, stage_gate, time_step, Last_Time_Step) ##Calculate Discounting Factor discounting_factor = M2S_item.calc_discounting_factor( revenue_max, gammaL, trial_cost, product, stage_gate, Last_Time_Step) resource_max = {} for items in model_data._data['max_resource']: resource_max[items[0]] = model_data._data['max_resource'][items] resource_required = {} resource_required = model_data._data['resource_requirement'] for OC in Outcomes: if OC != None: ### Determine branch for b in self.Branches[time]: breal = 0 for (i, j, real) in self.Branch_Def[b]: if OC[product.index(i)] + 1 > j and real == 1: breal += 1 elif OC[product.index(i)] < j and real == 0: breal += 1 if breal == len(self.Branch_Def[b]): sbranch = b break ## Scenario Specific sinf = SC.scenario(OC, model_data._data['probability'], product, model_data._data['trial'][None]) Success = {} for i in product: Success[i] = sinf.success[product.index(i)] ### Convert Xbar to Appropriate Format for Pyomo x_bar_func = {} w = {} for (i, j, t) in cds[sbranch]: x_bar_func[(i, j, t)] = x_bar[sbranch][product.index(i)][t - 1] ### Update w for the two cases ### Case Non-Zero for (i, j, t, ss, wval0, dec1) in x_non_zero: if ss == OC: w[(i, j, t)] = wval0 + rho * ( dec1 - x_bar[sbranch][product.index(i)][t - 1]) ### Generate PH model model = MSSP.SS_PH(rho, w, x_bar_func, cds[sbranch], product, stage_gate, time_step, resource_type, resource_max, gammaL, gammaD, duration, trial_cost, resource_required, revenue_max, sinf.probability, Success, Last_Time_Step, last_trial, running_revenue, open_revenue, discounting_factor, OC) ### Fix Decisions for t in self.Decisions[sbranch]: for (i, j, tm, tau, dec) in self.Decisions[sbranch][t]: model.Decision_X[i, j, tm + 1].value = dec model.Decision_X[i, j, t + 1].fixed = True ### Solve results = opt.solve(model) model.solutions.load_from(results) if str(results['Solver'][0] ['Termination condition']) != 'optimal': print('Solution Not Optimal') print(results['Solver'][0]['Termination condition']) for (i, j, t) in cds[sbranch]: x_bar_thread[sbranch][product.index(i)][ t - 1] += sinf.probability / branch_probability_total[ sbranch] * abs(model.Decision_X[i, j, t].value) ### Update old non-zero x's for (i, j, t) in cds[b]: wval1 = w[(i, j, t)] dec1 = abs(model.Decision_X[i, j, t].value) x_non_zero_t.append((i, j, t, OC, wval1, dec1)) ### Calculate Equivalent ENPV for this scenario with the decisions tENPV += sinf.probability * self._evaluate_ENPV( model.Decision_X, model.Decision_Z, model_data, sinf) return [x_non_zero_t, x_bar_thread, tENPV]
def _first_PH_mpfunc(self, model_data, Outcomes, btime, x_bar_z, cds, branch_probability_total, time): ### Optimization things opt = SolverFactory("cplex") opt.options['threads'] = 6 ### Thread Specific Variables x_non_zero_t = [] x_bar_thread = copy.deepcopy(x_bar_z) ### Generate non-scenario specific model variables rev_max = {} gammaL = {} gammaD = {} duration = {} trial_cost = {} revenue_max = {} success = {} rev_run = {} rev_open = {} discounting_factor = {} ##Set product product = model_data._data['product'][None] ##Set stage_gate stage_gate = model_data._data['trial'][None] ## Set time step time_step = model_data._data['time_step'][None] ##Set resource type resource_type = model_data._data['resource_type'][None] ## Set duration duration = model_data._data['trial_duration'] ## Set trial cost trial_cost = model_data._data['trial_cost'] ## Set Discount Values for items in model_data._data['gammaL']: gammaL[items[0]] = model_data._data['gammaL'][items] for items in model_data._data['gammaD']: gammaD[items[0]] = model_data._data['gammaD'][items] ## Set Maximum Revenue for items in model_data._data['maximum_revenue']: revenue_max[items[0]] = model_data._data['maximum_revenue'][items] ## Set Last Trial last_trial = len(stage_gate) Last_Time_Step = len(time_step) ## Calculate running rev running_revenue = M2S_item.calc_rr(revenue_max, gammaL, duration, product, stage_gate, time_step) ##Calculate open rev open_revenue = M2S_item.calc_openrev(revenue_max, gammaL, duration, product, stage_gate, time_step, Last_Time_Step) ##Calculate Discounting Factor discounting_factor = M2S_item.calc_discounting_factor( revenue_max, gammaL, trial_cost, product, stage_gate, Last_Time_Step) resource_max = {} for items in model_data._data['max_resource']: resource_max[items[0]] = model_data._data['max_resource'][items] resource_required = {} resource_required = model_data._data['resource_requirement'] for OC in Outcomes: if OC != None: ### Determine branch for b in self.Branches[time]: breal = 0 for (i, j, real) in self.Branch_Def[b]: if OC[product.index(i)] + 1 > j and real == 1: breal += 1 elif OC[product.index(i)] < j and real == 0: breal += 1 if breal == len(self.Branch_Def[b]): sbranch = b break ## Scenario Specific sinf = SC.scenario(OC, model_data._data['probability'], product, model_data._data['trial'][None]) Success = {} for i in product: Success[i] = sinf.success[product.index(i)] ### Create Model model = MSSP.SingleScenario( product, stage_gate, time_step, resource_type, resource_max, gammaL, gammaD, duration, trial_cost, resource_required, revenue_max, sinf.probability, Success, Last_Time_Step, last_trial, running_revenue, open_revenue, discounting_factor, OC) ### Fix Decisions for t in self.Decisions[sbranch]: for (i, j, tm, tau, dec) in self.Decisions[sbranch][t]: model.Decision_X[i, j, tm + 1].value = dec model.Decision_X[i, j, t + 1].fixed = True ### Solve results = opt.solve(model) model.solutions.load_from(results) ### Record xbar --- abs is to get rid of negative zeros ??? for (i, j, t) in cds[sbranch]: x_bar_thread[sbranch][product.index(i)][ t - 1] += sinf.probability / branch_probability_total[ sbranch] * abs(model.Decision_X[i, j, t].value) ### Record non-zero x's for (i, j, t) in cds[sbranch]: wval0 = 0 dec1 = abs(model.Decision_X[i, j, t].value) x_non_zero_t.append((i, j, t, OC, wval0, dec1)) del sbranch return [x_non_zero_t, x_bar_thread]
def model_constructor(scenario_name, node_names): ### Build the Scenario Model m = ConcreteModel() ##Sets## m.I = Set(initialize=builtins.MD_GLOBAL._data['product'][None]) m.J = Set(initialize=builtins.MD_GLOBAL._data['trial'][None]) m.T = Set(initialize=builtins.MD_GLOBAL._data['time_step'][None]) m.R = Set(initialize=builtins.MD_GLOBAL._data['resource_type'][None]) ### Fixed Parameters### m.Resource_Max = {} for r in builtins.MD_GLOBAL._data['resource_type'][None]: m.Resource_Max[r] = builtins.MD_GLOBAL._data['max_resource'][(r,)] ## Set Discount Values m.GammaL = {} for items in builtins.MD_GLOBAL._data['gammaL']: m.GammaL[items[0]] = builtins.MD_GLOBAL._data['gammaL'][items] m.GammaD = {} for items in builtins.MD_GLOBAL._data['gammaD']: m.GammaD[items[0]] = builtins.MD_GLOBAL._data['gammaD'][items] ## Set Maximum Revenue m.Revenue_Max = {} for items in builtins.MD_GLOBAL._data['maximum_revenue']: m.Revenue_Max[items[0]] = builtins.MD_GLOBAL._data['maximum_revenue'][items] m.Duration = builtins.MD_GLOBAL._data['trial_duration'] m.Trial_Cost = builtins.MD_GLOBAL._data['trial_cost'] m.Resources_Required = builtins.MD_GLOBAL._data['resource_requirement'] m.LastTimeStep = builtins.MD_GLOBAL._data['time_step'][None][-1] m.Last_Trial = builtins.MD_GLOBAL._data['trial'][None][-1] ## Calculate running rev m.RR = M2S_item.calc_rr(m.Revenue_Max,m.GammaL,m.Duration, m.I, m.J, m.T) ##Calculate open rev m.OR = M2S_item.calc_openrev(m.Revenue_Max,m.GammaL,m.Duration, m.I, m.J, m.T, m.LastTimeStep) ##Calculate Discounting Factor m.DF = M2S_item.calc_discounting_factor(m.Revenue_Max,m.GammaL,m.Trial_Cost, m.I, m.J, m.LastTimeStep) ### Scenario Specific Parameters### m.Success = {} for i in builtins.MD_GLOBAL._data['product'][None]: if builtins.Tree_Graph_GLOBAL.G.node[scenario_name]['Outcome'][builtins.MD_GLOBAL._data['product'][None].index(i)] == len(m.J): m.Success[i] = 1 else: m.Success[i] = 0 def FS_init(node): rval = [] t = builtins.Tree_Graph_GLOBAL.G.node[node]['Active Periods'] tc = 0 ### If a node isn't a isn't a 'up' node then all variables are first stage if node.startswith('up'): ## Find Last Pseudonode for nds in reversed(node_names): if 'Constraints' in builtins.Tree_Graph_GLOBAL.G.node[nds]: last_pseudo = nds try: cvars = builtins.Tree_Graph_GLOBAL.G.node[last_pseudo]['Constraints'] except: ### Error thrown relates to not having a last pseudo pdb.set_trace() while tc < len(cvars): for trial in builtins.MD_GLOBAL._data['trial'][None]: if builtins.MD_GLOBAL._data['trial'][None].index(trial) <= cvars[tc]: if (builtins.MD_GLOBAL._data['product'][None][tc],trial,t,0) not in builtins.Tree_Graph_GLOBAL.G.node[node]['Fixed Variables'] and (builtins.MD_GLOBAL._data['product'][None][tc],trial,t,1) not in builtins.Tree_Graph_GLOBAL.G.node[node]['Fixed Variables']: rval.append((builtins.MD_GLOBAL._data['product'][None][tc],trial,t)) tc += 1 return tuple(rval) else: for i in builtins.MD_GLOBAL._data['product'][None]: for j in builtins.MD_GLOBAL._data['trial'][None]: if (i,j,t,0) not in builtins.Tree_Graph_GLOBAL.G.node[node]['Fixed Variables'] and (i,j,t,1) not in builtins.Tree_Graph_GLOBAL.G.node[node]['Fixed Variables']: rval.append((i,j,t)) return tuple(rval) def Fixed_init(node): rval = [] for (i,j,t,r) in builtins.Tree_Graph_GLOBAL.G.node[node]['Fixed Variables']: rval.append((i,j,t)) return tuple(rval) def SS_init(node): rval = [] t = builtins.Tree_Graph_GLOBAL.G.node[node]['Active Periods'] tc = 0 ### If a node isn't a isn't a 'up' node then all variables are first stage if node.startswith('up'): ## Find Last Pseudonode for nds in reversed(node_names): if 'Constraints' in builtins.Tree_Graph_GLOBAL.G.node[nds]: last_pseudo = nds cvars = builtins.Tree_Graph_GLOBAL.G.node[last_pseudo]['Constraints'] while tc < len(cvars): if cvars[tc] < len(builtins.MD_GLOBAL._data['trial'][None]): trl = builtins.MD_GLOBAL._data['trial'][None][cvars[tc]] for trial in builtins.MD_GLOBAL._data['trial'][None]: if builtins.MD_GLOBAL._data['trial'][None].index(trial) > cvars[tc]: if (builtins.MD_GLOBAL._data['product'][None][tc],trial,t,0) not in builtins.Tree_Graph_GLOBAL.G.node[node]['Fixed Variables'] and (builtins.MD_GLOBAL._data['product'][None][tc],trial,t,1) not in builtins.Tree_Graph_GLOBAL.G.node[node]['Fixed Variables']: rval.append((builtins.MD_GLOBAL._data['product'][None][tc],trial,t)) tc += 1 return tuple(rval) else: return(tuple(rval)) def scen_init(node): ### all decisions after t tend = builtins.Tree_Graph_GLOBAL.G.node[node_names[-2]]['Active Periods'] rval = [] for t in builtins.MD_GLOBAL._data['time_step'][None]: if t > tend: for i in builtins.MD_GLOBAL._data['product'][None]: for j in builtins.MD_GLOBAL._data['trial'][None]: rval.append((i,j,t)) return tuple(rval) def pseudo_scen_init(node): rval = [] for i in builtins.MD_GLOBAL._data['product'][None]: for j in builtins.MD_GLOBAL._data['trial'][None]: rval.append((i,j,builtins.Tree_Graph_GLOBAL.G.node[node]['Active Periods'])) return rval nodeallset = {} nodevar = {} nodeset = {} nodeallvar = {} for nn in node_names: nodeset[nn] = {} nodevar[nn] = {} if nn.startswith('s'): m.add_component(str(nn) + 'SetFixed', Set(within = m.I * m.J * m.T, initialize = set())) m.add_component(str(nn) + 'SetX', Set(within = m.I * m.J * m.T, initialize = set())) m.add_component(str(nn) + 'SetXX', Set(within = m.I * m.J * m.T, initialize = scen_init(nn))) elif nn.startswith('pn'): m.add_component(str(nn) + 'SetFixed', Set(within = m.I * m.J * m.T, initialize = set())) m.add_component(str(nn) + 'SetX', Set(within = m.I * m.J * m.T, initialize = set())) m.add_component(str(nn) + 'SetXX', Set(within = m.I * m.J * m.T, initialize = pseudo_scen_init(nn))) else: m.add_component(str(nn) + 'SetFixed', Set(within = m.I * m.J * m.T, initialize = Fixed_init(nn))) m.add_component(str(nn) + 'SetX', Set(within = m.I * m.J * m.T, initialize = FS_init(nn))) m.add_component(str(nn) + 'SetXX', Set(within = m.I * m.J * m.T, initialize = SS_init(nn))) for nn in node_names: for comp in list(m.component_objects()): if comp._name.startswith(str(nn) + 'Set'): if not comp._name.endswith('_domain') and not comp._name.endswith('_domain_index_0'): nodeset[nn][str(comp._name)]= comp if nn.startswith('s') or nn.startswith('pn'): if comp._name.startswith(str(nn) + 'SetXX'): m.add_component(str(nn) + 'VarXX', Var(comp,bounds=(0,1))) else: if comp._name.startswith(str(nn) + 'SetXX'): m.add_component(str(nn) + 'VarXX', Var(comp,bounds=(0,1))) elif comp._name.startswith(str(nn) + 'SetX'): m.add_component(str(nn) + 'VarX', Var(comp,bounds=(0,1))) for nn in nodeset: setAll = set() nodeallset[nn] = {} for sets in nodeset[nn]: setNew = setAll.union(set(nodeset[nn][sets].value)) setAll = setNew m.add_component(str(nn) + '_setALL', Set(within=m.I*m.J*m.T,initialize = copy.deepcopy(setAll))) for nn in node_names: for comp in list(m.component_objects()): if comp._name == str(nn) + '_setALL': nodeallset[nn][str(comp._name)] = comp m.add_component(str(nn)+'VarY',Var(comp,bounds=(0,1))) m.add_component(str(nn) + 'VarZ', Var(comp,bounds=(0,1))) for nn in node_names: nodeallvar[nn] = {} for comp in list(m.component_objects()): if comp._name == str(nn) + 'VarY': nodeallvar[nn]['Y'] = comp elif comp._name == str(nn) + 'VarZ': nodeallvar[nn]['Z'] = comp for nn in node_names: for comp in list(m.component_objects()): if comp._name.startswith(str(nn) + 'VarX') or comp._name.startswith(str(nn) + 'VarXX') : nodevar[nn][str(comp._name)] = comp if builtins.Tree_Graph_GLOBAL.debug == True: pdb.set_trace() def construct_stage_cost(model,ndn): cst = 0 ### Cost to start trials for Fixed Variables, Constrained Variables, and Second Stage Variables #pdb.set_trace() for (i,j,t) in nodeallset[ndn][str(ndn)+'_setALL']: if (i,j,t) in nodeset[ndn][str(ndn)+'SetX']: ### Cost to start trials cst += -(1 - (0.025 * (t - 1))) * model.Trial_Cost[i,j] * nodevar[ndn][str(ndn)+'VarX'][i,j,t] ### Revenue From Starting Final Trials if j == m.Last_Trial: cst += m.Success[i]*(m.Revenue_Max[i] * nodevar[ndn][str(ndn)+'VarX'][i,j,t]) ### Losses for the time to develop drug if j == m.Last_Trial: cst -= m.Success[i]*m.GammaL[i]*(t + m.Duration[i, j]) * nodevar[ndn][str(ndn)+'VarX'][i,j,t] ### Revenue not yet realized if j < m.Last_Trial and t > m.LastTimeStep - m.Duration[i,j]: cst += m.Success[i] * m.RR[i,j,t] * m.DF[i,j+1]*nodevar[ndn][str(ndn)+'VarX'][i,j,t] elif (i,j,t) in nodeset[ndn][str(ndn)+'SetFixed']: ### Cost to start trials cst += -(1 - (0.025 * (t - 1))) * model.Trial_Cost[i,j] * return_fixed_values(i,j,t,ndn) ### Revenue From Starting Final Trials if j == m.Last_Trial: cst += m.Success[i]*(m.Revenue_Max[i] * return_fixed_values(i,j,t,ndn)) ### Losses for the time to develop drug if j == m.Last_Trial: cst += -m.Success[i]*m.GammaL[i]*(t + m.Duration[i, j]) * return_fixed_values(i,j,t,ndn) ### Revenue not yet realized if j < m.Last_Trial and t > m.LastTimeStep - m.Duration[i,j]: cst += m.Success[i] * m.RR[i,j,t] * m.DF[i,j+1] * return_fixed_values(i,j,t,ndn) else: ### Cost to start trials cst += -(1 - (0.025 * (t - 1))) * model.Trial_Cost[i,j] * nodevar[ndn][str(ndn)+'VarXX'][i,j,t] ### Revenue From Starting Final Trials if j == m.Last_Trial: cst += m.Success[i]*(m.Revenue_Max[i] * nodevar[ndn][str(ndn)+'VarXX'][i,j,t]) ### Losses for the time to develop drug if j == m.Last_Trial: cst -= m.Success[i]*m.GammaL[i]*(t + m.Duration[i, j]) * nodevar[ndn][str(ndn)+'VarXX'][i,j,t] ### Revenue not yet realized if j < m.Last_Trial and t > m.LastTimeStep - m.Duration[i,j]: cst += m.Success[i] * m.RR[i,j,t] * m.DF[i,j+1]*nodevar[ndn][str(ndn)+'VarXX'][i,j,t] if ndn.startswith('s'): tstart = builtins.Tree_Graph_GLOBAL.G.node[ndn]['Active Periods'] ### Losses For Decisions Not Made cst += sum(-m.Success[i]*m.GammaD[i] * sum(nodeallvar[ndn]['Z'][i,j,t] for j in m.J if j > 1) for i in m.I for t in m.T if t >= tstart) if m.LastTimeStep >= tstart: cst += sum(m.Success[i] * m.OR[i,j] * m.DF[i,j]* nodeallvar[ndn]['Z'][i,j,m.LastTimeStep] for i in m.I for j in m.J) else: t = builtins.Tree_Graph_GLOBAL.G.node[ndn]['Active Periods'] ### Losses For Decisions Not Made cst += sum(-m.Success[i]*m.GammaD[i] * sum(nodeallvar[ndn]['Z'][i,j,t] for j in m.J if j > 1) for i in m.I) ### Revenue Yet to Be Realized if t == m.LastTimeStep: cst += sum(m.Success[i] * m.OR[i,j] * m.DF[i,j]* nodeallvar[ndn]['Z'][i,j,t] for i in m.I for j in m.J) return -cst ## Add stage cost for each node stage_covered = [] for node in node_names: if node.startswith('s'): ### This is the scenario stage match_node = scenario_name stage_covered.append('Ts') m.add_component('TsStageCost', Expression(expr=construct_stage_cost(m, match_node))) else: ### Determine stage tstage = builtins.Tree_Graph_GLOBAL.G.node[node]['Active Periods'] stage_name = 'T' + str(tstage) stage_covered.append(stage_name) try: m.add_component(str(stage_name) + 'StageCost', Expression(expr=construct_stage_cost(m, node))) except Exception as e: print(e) pdb.set_trace() m.add_component(str(stage_name) + 'StageCost', Expression(expr=construct_stage_cost(m, node))) for stage in builtins.Tree_Graph_GLOBAL.stage_names: if stage not in stage_covered: m.add_component(str(stage) + 'StageCost', Expression(expr=0.0)) m.obj = Objective(expr = sum(comp for comp in list(m.component_objects()) if comp._name.endswith('StageCost'))) ### Constraint-- Ensures resources are managed correctly def Resource_Constraint_rule(m,r,t): LHS = 0 for i in m.I: for j in m.J: for tprime in m.T: if tprime > (t - m.Duration[i,j]) and tprime <= t: ##identify node match_node = [n for n in node_names if builtins.Tree_Graph_GLOBAL.G.node[n]['Active Periods'] == tprime] if len(match_node) > 1 or len(match_node) == 0: if len(match_node) == 0: if tprime > builtins.Tree_Graph_GLOBAL.G.node[node_names[-1]]['Active Periods']: match_node = node_names[-1] else: print('You done bunked it') else: match_node = match_node[0] if match_node.startswith('s') or match_node.startswith('pn') : LHS += m.Resources_Required[i,j,r] * nodevar[match_node][str(match_node)+'VarXX'][i,j,tprime] else: if (i,j,tprime) in nodeset[match_node][str(match_node)+'SetX']: LHS += m.Resources_Required[i,j,r] * nodevar[match_node][str(match_node)+'VarX'][i,j,tprime] elif (i,j,tprime) in nodeset[match_node][str(match_node)+'SetFixed']: LHS += m.Resources_Required[i,j,r] * return_fixed_values(i,j,tprime,match_node) else: LHS += m.Resources_Required[i,j,r] * nodevar[match_node][str(match_node)+'VarXX'][i,j,tprime] return LHS <= m.Resource_Max[r] m.Resource_Constraint = Constraint(m.R, m.T, rule= Resource_Constraint_rule) ### Constraint-- Must Start Previous Trial Before it can be finished def Constraint_4_rule(m,i,j,t): if j > 1: previous_trial = j-1 LHS = 0 for tprime in m.T: if tprime <= t: ### Determine the node match_node = [n for n in node_names if builtins.Tree_Graph_GLOBAL.G.node[n]['Active Periods'] == tprime] if len(match_node) > 1 or len(match_node) == 0: if len(match_node) == 0: if tprime >= builtins.Tree_Graph_GLOBAL.G.node[node_names[-1]]['Active Periods']: match_node = node_names[-1] else: print('You done bunked it') else: match_node = match_node[0] if match_node.startswith('s') or match_node.startswith('pn'): LHS += nodevar[match_node][str(match_node)+'VarXX'][i,j,tprime] else: if (i,j,tprime) in nodeset[match_node][str(match_node)+'SetX']: LHS += nodevar[match_node][str(match_node)+'VarX'][i,j,tprime] elif (i,j,tprime) in nodeset[match_node][str(match_node)+'SetFixed']: LHS += return_fixed_values(i,j,tprime,match_node) else: LHS += nodevar[match_node][str(match_node)+'VarXX'][i,j,tprime] ### Determine node for t match_node = [n for n in node_names if builtins.Tree_Graph_GLOBAL.G.node[n]['Active Periods'] == t] if len(match_node) > 1 or len(match_node) == 0: if len(match_node) == 0: if tprime >= builtins.Tree_Graph_GLOBAL.G.node[node_names[-1]]['Active Periods']: match_node = node_names[-1] else: print('You done bunked it') else: match_node = match_node[0] decY = nodeallvar[match_node]['Y'][i,j-1,t] return LHS <= decY else: return Constraint.Skip m.Constraint_4 = Constraint(m.I, m.J, m.T, rule=Constraint_4_rule) ### Constraint--You can only start each trial once def Constraint_3_rule(model,i,j): LHS = 0 for t in m.T: ### Determine the node match_node = [n for n in node_names if builtins.Tree_Graph_GLOBAL.G.node[n]['Active Periods'] == t] if len(match_node) > 1 or len(match_node) == 0: if len(match_node) == 0: if t >= builtins.Tree_Graph_GLOBAL.G.node[node_names[-1]]['Active Periods']: match_node = node_names[-1] else: print('You done bunked it') else: match_node = match_node[0] if match_node.startswith('s') or match_node.startswith('pn'): LHS += nodevar[match_node][str(match_node)+'VarXX'][i,j,t] else: if (i,j,t) in nodeset[match_node][str(match_node)+'SetX']: LHS += nodevar[match_node][str(match_node)+'VarX'][i,j,t] elif (i,j,t) in nodeset[match_node][str(match_node)+'SetFixed']: LHS += return_fixed_values(i,j,t,match_node) else: LHS += nodevar[match_node][str(match_node)+'VarXX'][i,j,t] return LHS <= 1 m.Constraint_3 = Constraint(m.I,m.J,rule=Constraint_3_rule) def Calculate_Z_rule(m,i,j,t): ### Determine the node match_node = [n for n in node_names if builtins.Tree_Graph_GLOBAL.G.node[n]['Active Periods'] == t] if len(match_node) > 1 or len(match_node) == 0: if len(match_node) == 0: if t >= builtins.Tree_Graph_GLOBAL.G.node[node_names[-1]]['Active Periods']: match_node = node_names[-1] else: print('You done bunked it') else: match_node = match_node[0] ### Determine X,Z,Zpd and Xpd if match_node.startswith('s') or match_node.startswith('pn'): decX = nodevar[match_node][str(match_node)+'VarXX'][i,j,t] decZ = nodeallvar[match_node]['Z'][i,j,t] ### Find previous nodes for Xpd if j-1 > 0: if t-m.Duration[i,j-1] > 0: if t-m.Duration[i,j-1] >= builtins.Tree_Graph_GLOBAL.G.node[match_node]['Active Periods']: ### This means that Xpd is inside the set for S decXpd = nodevar[match_node][str(match_node)+'VarXX'][i,j-1,t-m.Duration[i,j-1]] else: for node in node_names: if t-m.Duration[i,j-1] == builtins.Tree_Graph_GLOBAL.G.node[node]['Active Periods']: ### The node is the node now check FD, FSD or SSD if (i,j-1,t-m.Duration[i,j-1]) in nodeset[node][str(node)+'SetX']: decXpd = nodevar[node][str(node)+'VarX'][i,j-1,t-m.Duration[i,j-1]] elif (i,j-1,t-m.Duration[i,j-1]) in nodeset[node][str(node)+'SetFixed']: decXpd = return_fixed_values(i,j-1,t-m.Duration[i,j-1],node) else: decXpd = nodevar[node][str(node)+'VarXX'][i,j-1,t-m.Duration[i,j-1]] if t-1 > 0: if t-1 >= builtins.Tree_Graph_GLOBAL.G.node[match_node]['Active Periods']: ### This means that Zpt is inside the set for S decZpt = nodeallvar[match_node]['Z'][i,j,t-1] else: for node in node_names: if t-1 == builtins.Tree_Graph_GLOBAL.G.node[node]['Active Periods']: ### The node is Z decZpt = nodeallvar[node]['Z'][i,j,t-1] else: if (i,j,t) in nodeset[match_node][str(match_node)+'SetX']: decX = nodevar[match_node][str(match_node)+'VarX'][i,j,t] elif (i,j,t) in nodeset[match_node][str(match_node)+'SetFixed']: decX = return_fixed_values(i,j,t,match_node) else: decX = nodevar[match_node][str(match_node)+'VarXX'][i,j,t] decZ = nodeallvar[match_node]['Z'][i,j,t] for node in node_names: if j-1 > 0: if t-m.Duration[i,j-1] > 0: if t-m.Duration[i,j-1] == builtins.Tree_Graph_GLOBAL.G.node[node]['Active Periods']: ### The node is the node now check FD, FSD or SSD if (i,j-1,t-m.Duration[i,j-1]) in nodeset[node][str(node)+'SetX']: decXpd = nodevar[node][str(node)+'VarX'][i,j-1,t-m.Duration[i,j-1]] elif (i,j-1,t-m.Duration[i,j-1]) in nodeset[node][str(node)+'SetFixed']: decXpd = return_fixed_values(i,j-1,t-m.Duration[i,j-1],node) else: decXpd = nodevar[node][str(node)+'VarXX'][i,j-1,t-m.Duration[i,j-1]] if t - 1 > 0: if t-1 == builtins.Tree_Graph_GLOBAL.G.node[node]['Active Periods']: ### The node is Z decZpt = nodeallvar[node]['Z'][i,j,t-1] decZ1 = nodeallvar['R']['Z'][i,j,1] if j == 1: if t > 1: return decZ == decZpt - decX else: return decZ1 == 1 - decX else: if t- m.Duration[i,j-1] >0 and t>1: return decZ == decZpt + decXpd - decX elif t-m.Duration[i,j-1] <= 0 and t > 1: return decZ == decZpt - decX elif t==1: return decZ == -decX m.Calculate_Z = Constraint(m.I,m.J,m.T, rule= Calculate_Z_rule) ### Constraint-- The trial is complete if the trial was started a duration ago def Trial_Finish_rule(m,i,j,t): past_duration = t - m.Duration[i,j] ### Determine the node match_node = [n for n in node_names if builtins.Tree_Graph_GLOBAL.G.node[n]['Active Periods'] == t] if len(match_node) > 1 or len(match_node) == 0: if len(match_node) == 0: if t >= builtins.Tree_Graph_GLOBAL.G.node[node_names[-1]]['Active Periods']: match_node = node_names[-1] else: print('You done bunked it') else: match_node = match_node[0] ##decXpd and decYpt ### Find previous nodes for Xpd if t-m.Duration[i,j] > 0: if t-m.Duration[i,j] >= builtins.Tree_Graph_GLOBAL.G.node[match_node]['Active Periods']: ### This means that Xpd is inside the set for S decXpd = nodevar[match_node][str(match_node)+'VarXX'][i,j,t-m.Duration[i,j]] else: for node in node_names: if t-m.Duration[i,j] == builtins.Tree_Graph_GLOBAL.G.node[node]['Active Periods']: ### The node is the node now check FD, FSD or SSD if node.startswith('pn'): decXpd = nodevar[node][str(node)+'VarXX'][i,j,t-m.Duration[i,j]] else: if (i,j,t-m.Duration[i,j]) in nodeset[node][str(node)+'SetX']: decXpd = nodevar[node][str(node)+'VarX'][i,j,t-m.Duration[i,j]] elif (i,j,t-m.Duration[i,j]) in nodeset[node][str(node)+'SetFixed']: decXpd = return_fixed_values(i,j,t-m.Duration[i,j],node) else: decXpd = nodevar[node][str(node)+'VarXX'][i,j,t-m.Duration[i,j]] if t-1 > 0: if t-1 >= builtins.Tree_Graph_GLOBAL.G.node[match_node]['Active Periods']: ### This means that Zpt is inside the set for S decYpt = nodeallvar[match_node]['Y'][i,j,t-1] else: for node in node_names: if t-1 == builtins.Tree_Graph_GLOBAL.G.node[node]['Active Periods']: ### The node is Z decYpt = nodeallvar[node]['Y'][i,j,t-1] if t==1: return nodeallvar['R']['Y'][i,j,1] == 0 elif t > 1 and past_duration < 1 : return nodeallvar[match_node]['Y'][i,j,t] == decYpt else: return nodeallvar[match_node]['Y'][i,j,t] == decYpt + decXpd m.Trial_Finish = Constraint(m.I, m.J, m.T, rule=Trial_Finish_rule) def trial_start_rule(m,i,j,t): jp = builtins.MD_GLOBAL._data['trial'][None].index(j) earliest_start = sum(m.Duration[i,jt] for jt in builtins.MD_GLOBAL._data['trial'][None] if builtins.MD_GLOBAL._data['trial'][None].index(jt) < jp) ### Determine the node match_node = [n for n in node_names if builtins.Tree_Graph_GLOBAL.G.node[n]['Active Periods'] == t] if len(match_node) > 1 or len(match_node) == 0: if len(match_node) == 0: if t >= builtins.Tree_Graph_GLOBAL.G.node[node_names[-1]]['Active Periods']: match_node = node_names[-1] else: print('You done bunked it') else: match_node = match_node[0] if t < earliest_start: if match_node.startswith('s') or match_node.startswith('pn'): return nodevar[match_node][str(match_node)+'VarXX'][i,j,t] == 0 else: if (i,j,t) in nodeset[match_node][str(match_node)+'SetX']: return nodevar[match_node][str(match_node)+'VarX'][i,j,t] == 0 elif (i,j,t) in nodeset[match_node][str(match_node)+'SetFixed']: return Constraint.Skip else: return nodevar[match_node][str(match_node)+'VarXX'][i,j,t] == 0 else: return Constraint.Skip m.Trial_Start = Constraint(m.I,m.J,m.T, rule=trial_start_rule) return m, nodeset, nodevar, nodeallvar
def build_model(MD): from Core.Solvers.EOSS.SingleScenario_Model import SingleScenario as SS import Core.Solvers.MTSSP.M2S_item as M2S_item ##Set product product = MD._data['product'][None] ##Set stage_gate stage_gate = MD._data['trial'][None] ## Set time step time_step = MD._data['time_step'][None] ##Set resource type resource_type = MD._data['resource_type'][None] ## Set duration duration = MD._data['trial_duration'] ## Set trial cost trial_cost = MD._data['trial_cost'] ## Set Discount Values gammaL = {} for items in MD._data['gammaL']: gammaL[items[0]] = MD._data['gammaL'][items] gammaD = {} for items in MD._data['gammaD']: gammaD[items[0]] = MD._data['gammaD'][items] ## Set Maximum Revenue revenue_max = {} for items in MD._data['maximum_revenue']: revenue_max[items[0]] = MD._data['maximum_revenue'][items] ## Set Last Trial last_trial = len(stage_gate) last_time_step = len(time_step) ## Calculate running rev rev_run = M2S_item.calc_rr(revenue_max, gammaL, duration, product, stage_gate, time_step) ##Calculate open rev rev_open = M2S_item.calc_openrev(revenue_max, gammaL, duration, product, stage_gate, time_step, last_time_step) ##Calculate Discounting Factor discounting_factor = M2S_item.calc_discounting_factor( revenue_max, gammaL, trial_cost, product, stage_gate, last_time_step) resource_max = {} for items in MD._data['max_resource']: resource_max[items[0]] = MD._data['max_resource'][items] resource_required = {} resource_required = MD._data['resource_requirement'] success = {} for i in product: success[i] = 0 model = SS(product,stage_gate,time_step,resource_type,\ resource_max,gammaL,gammaD,duration,\ trial_cost,resource_required, revenue_max,\ success,last_time_step, last_trial,\ rev_run, rev_open, discounting_factor) return model