コード例 #1
0
    def _mp_gk_calc(self, model_data, Outcomes, cds, x_bar_new, new_x_non_zero,
                    time, btime):
        gkt = 0

        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[model_data._data['product'][None].index(
                                i)] + 1 > j and real == 1:
                            breal += 1
                        elif OC[model_data._data['product'][None].index(
                                i)] < j and real == 0:
                            breal += 1

                    if breal == len(self.Branch_Def[b]):
                        sbranch = b
                        break

                sinf = SC.scenario(OC, model_data._data['probability'],
                                   model_data._data['product'][None],
                                   model_data._data['trial'][None])
                sqrtterm = 0
                for (i, j, t) in cds[sbranch]:
                    ltup = [
                        tup for tup in new_x_non_zero if tup[0] == i
                        and tup[1] == j and tup[2] == t and tup[3] == OC
                    ]
                    if len(ltup) == 1:
                        sqrtterm += (round(ltup[0][5], 3) - round(
                            x_bar_new[sbranch][model_data._data['product']
                                               [None].index(i)][t - 1], 3))**2
                    else:
                        print('ERROR!!!')

                gkt += sinf.probability * sqrt(sqrtterm)
        return gkt
コード例 #2
0
ファイル: EOSSBound.py プロジェクト: zztcok/SNAC_PSNAC
def Scen_Solve(S_List, MD, FD):
    ENPV_Set = 0
    ### Build Initial Model
    model = build_model(MD)
    #print(FD)
    for s in S_List:

        if s != None:
            ss = tuple(s)
            #print(ss)
            ### Definition
            product = MD._data['product'][None]
            trials = MD._data['trial'][None]

            ## Scenario Specific Data
            sinf = SC.scenario(ss, MD._data['probability'], product, trials)

            Success = {}
            for i in product:
                model.Success[i] = sinf.success[product.index(i)]

            ### Fix Fixed Variables
            for (i, j, t, dec) in FD:
                for rlzn in FD[(i, j, t, dec)]:
                    if rlzn_check(ss, rlzn, product):
                        #print(i,j,t+1,dec)
                        model.Decision_X[i, j, t + 1].value = dec
                        model.Decision_X[i, j, t + 1].fixed = True

            ### Solve Model
            opt = SolverFactory("cplex")
            results = opt.solve(model)
            model.solutions.load_from(results)

            ENPV_Set = ENPV_Set + sinf.probability * float(
                results['Problem'][0]['Lower bound'])

            model.unfix_all_vars()

    return ENPV_Set
コード例 #3
0
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
コード例 #4
0
ファイル: Full_not_solve.py プロジェクト: zztcok/SNAC_PSNAC
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()
コード例 #5
0
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()
コード例 #6
0
    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]
コード例 #7
0
    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]