def setup_algorithms(training_data, dist, delta): start = time.time() n_x = dist.n_x n_a = dist.n_a n_y = dist.n_y statistical_approximation_prior = StatisticalApproximator( n_x, n_a, n_y, training_data, smoothing_mode='gaussian') constraint_upper = Constraint(training_data, n_a, n_y, approximator=statistical_approximation_prior, delta=delta, bound='upper') constraint_lower = Constraint(training_data, n_a, n_y, approximator=statistical_approximation_prior, delta=delta, bound='lower') constraint_exact = TrueConstraint( dist, approximator=statistical_approximation_prior, delta=delta) algorithms = [ #ConstrainedDynamicProgramming(n_x, n_a, n_y, training_data, constraint_upper, statistical_approximation_prior, name="Dynamic Programming Upper Bound", label="CDP_U"), #ConstrainedDynamicProgramming(n_x, n_a, n_y, training_data, constraint_lower, statistical_approximation_prior, name="Dynamic Programming Lower bound", label="CDP_L"), #ConstrainedDynamicProgramming(n_x, n_a, n_y, training_data, constraint_exact, statistical_approximation_prior, name="Dynamic Programming Exact Bound", label="CDP_E"), ConstrainedGreedy(n_x, n_a, n_y, training_data, constraint_upper, statistical_approximation_prior, name="Greedy Upper Bound", label="CG_U"), ConstrainedGreedy(n_x, n_a, n_y, training_data, constraint_lower, statistical_approximation_prior, name="Greedy Lower Bound", label="CG_L"), ConstrainedGreedy(n_x, n_a, n_y, training_data, constraint_exact, statistical_approximation_prior, name="Greedy Exact Bound", label="CG_E"), ] print("Setting up algorithms took {:.3f} seconds".format(time.time() - start)) return algorithms
def setup_algorithms(dist, training_data, n_x, n_a, n_y, delta): start = time.time() statistical_approximation = StatisticalApproximator(n_x, n_a, n_y, training_data, smoothing_mode='gaussian') function_approximation = FunctionApproximation(n_x, n_a, n_y, training_data) doctor_approximation = DoctorApproximator(n_x, n_a, n_y, training_data) print("Initializing Constraint") start = time.time() constraintStatUpper = Constraint(training_data, n_a, n_y, approximator=statistical_approximation, delta=delta, bound='upper') constraintFuncApprox = Constraint(training_data, n_a, n_y, approximator=function_approximation, delta=delta) constraint_exact_func = TrueConstraint(dist, approximator=function_approximation, delta=delta) print("Initializing the constraint took {:.3f} seconds".format(time.time() - start)) print("Initializing algorithms") algorithms = [ #ConstrainedGreedy(n_x, n_a, n_y, training_data, constraintStatUpper, statistical_approximation, # name='Constrained Greedy', label='CG'), # ConstrainedGreedy(n_x, n_a, n_y, split_training_data, constraintStatLower, statistical_approximation, # name='Constrained Greedy Lower', label='CG_L'), ConstrainedGreedy(n_x, n_a, n_y, training_data, constraintFuncApprox, function_approximation, name="Constrained Greedy FuncApprox", label="CG_F"), #ConstrainedDynamicProgramming(n_x, n_a, n_y, training_data, constraintStatUpper, # statistical_approximation), ConstrainedDynamicProgramming(n_x, n_a, n_y, training_data, constraintFuncApprox, function_approximation, name="Constrained Dynamic Programming FuncApprox", label="CDP_F"), #NaiveGreedy(n_x, n_a, n_y, function_approximation, max_steps=n_a), #NaiveGreedy(n_x, n_a, n_y, function_approximation, max_steps=n_a), NaiveDynamicProgramming(n_x, n_a, n_y, training_data, constraintStatUpper, reward=-0.35), Doctor(), EmulatedDoctor(n_x, n_a, n_y, training_data, approximator=doctor_approximation) ] return algorithms
def setup_algorithms(training_data, dist, delta, train=True): start = time.time() n_x = dist.n_x n_a = dist.n_a n_y = dist.n_y statistical_approximation_prior = StatisticalApproximator( n_x, n_a, n_y, training_data, smoothing_mode='gaussian') constraint_prior = Constraint(training_data, n_a, n_y, approximator=statistical_approximation_prior, delta=delta) constraint_prior = TrueConstraint( dist, approximator=statistical_approximation_prior, delta=delta) algorithms = [ ConstrainedDynamicProgramming(n_x, n_a, n_y, training_data, constraint_prior, statistical_approximation_prior, name="Constrained Dynamic Programming", label="CDP"), ConstrainedGreedy(n_x, n_a, n_y, training_data, constraint_prior, statistical_approximation_prior, name="Constrained Greedy", label="CG"), NaiveGreedy(n_x, n_a, n_y, statistical_approximation_prior, round(delta * (n_a - 1)) + 1, name='Naive Greedy', label='NG'), NaiveDynamicProgramming(n_x, n_a, n_y, training_data, statistical_approximation_prior, reward=-(delta + 0.0001), name='Naive Dynamic Programming', label='NDP'), ] print("Setting up algorithms took {:.3f} seconds".format(time.time() - start)) return algorithms
def setup_algorithms(training_data, dist, delta, train=True): start = time.time() n_x = dist.n_x n_a = dist.n_a n_y = dist.n_y statistical_approximation_prior = StatisticalApproximator( n_x, n_a, n_y, training_data, smoothing_mode='gaussian') true_approximation = ExactApproximator(dist) constraint_prior = Constraint(training_data, n_a, n_y, approximator=statistical_approximation_prior, delta=delta) constraint_true = Constraint(training_data, n_a, n_y, approximator=true_approximation, delta=delta) algorithms = [ ConstrainedDynamicProgramming( n_x, n_a, n_y, training_data, constraint_prior, statistical_approximation_prior, name="Dynamic Programming Historical Prior", label="CDP_H"), ConstrainedDynamicProgramming(n_x, n_a, n_y, training_data, constraint_true, true_approximation, name="Dynamic Programming True", label="CDP_T"), ConstrainedGreedy(n_x, n_a, n_y, training_data, constraint_prior, statistical_approximation_prior, name="Greedy Historical Prior", label="CG_H"), ] print("Setting up algorithms took {:.3f} seconds".format(time.time() - start)) return algorithms
def setup_algorithms(training_data, dist, delta): start = time.time() n_x = dist.n_x n_a = dist.n_a n_y = dist.n_y statistical_approximation_none = StatisticalApproximator(n_x, n_a, n_y, training_data, smoothing_mode='none') statistical_approximation_prior = StatisticalApproximator(n_x, n_a, n_y, training_data, smoothing_mode='gaussian') function_approximation = FunctionApproximation(n_x, n_a, n_y, training_data) true_approximation = ExactApproximator(dist) constraint_none = Constraint(training_data, n_a, n_y, approximator=statistical_approximation_none, delta=delta) constraint_prior = Constraint(training_data, n_a, n_y, approximator=statistical_approximation_prior, delta=delta) constraint_func = Constraint(training_data, n_a, n_y, approximator=function_approximation, delta=delta) constraint_true = Constraint(training_data, n_a, n_y, approximator=true_approximation, delta=delta) algorithms = [ ConstrainedGreedy(n_x, n_a, n_y, training_data, constraint_none, statistical_approximation_none, name="Greedy Uniform Prior", label="CG_U"), ConstrainedGreedy(n_x, n_a, n_y, training_data, constraint_prior, statistical_approximation_prior, name="Greedy Historical Prior", label="CG_H"), ConstrainedGreedy(n_x, n_a, n_y, training_data, constraint_func, function_approximation, name="Greedy Function Approximation", label="CG_F"), ConstrainedGreedy(n_x, n_a, n_y, training_data, constraint_true, true_approximation, name="Greedy True Approximation", label="CG_T"), ] print("Setting up algorithms took {:.3f} seconds".format(time.time() - start)) return algorithms
def setup_algorithms(training_data, dist, delta): start = time.time() n_x = dist.n_x n_a = dist.n_a n_y = dist.n_y statistical_approximation_prior = StatisticalApproximator( n_x, n_a, n_y, training_data, smoothing_mode='gaussian') statistical_approximation_none = StatisticalApproximator( n_x, n_a, n_y, training_data, smoothing_mode='none') true_approximation = ExactApproximator(dist) constraint_prior = Constraint(training_data, n_a, n_y, approximator=statistical_approximation_prior, delta=delta) constraint_none = Constraint(training_data, n_a, n_y, approximator=statistical_approximation_none, delta=delta) constraint_true = Constraint(training_data, n_a, n_y, approximator=true_approximation, delta=delta) algorithms = [ ConstrainedDynamicProgramming(n_x, n_a, n_y, training_data, constraint_true, true_approximation, name="Dynamic Programming True", label="CDP_T"), ConstrainedDynamicProgramming(n_x, n_a, n_y, training_data, constraint_prior, statistical_approximation_prior, name="Constrained Dynamic Programming", label="CDP_H"), ConstrainedDynamicProgramming( n_x, n_a, n_y, training_data, constraint_none, statistical_approximation_none, name="Constrained Dynamic Programming Uninformed", label="CDP_U"), ConstrainedGreedy(n_x, n_a, n_y, training_data, constraint_prior, statistical_approximation_prior, name="Constrained Greedy", label="CG"), #NaiveGreedy(n_x, n_a, n_y, statistical_approximation_prior, round(delta * (n_a-1))+1, name='Naive Greedy', label='NG'), #NaiveDynamicProgramming(n_x, n_a, n_y, training_data, statistical_approximation_prior, reward=-(delta+0.0001), name='Naive Dynamic Programming', label='NDP'), ] print("Setting up algosrithms took {:.3f} seconds".format(time.time() - start)) return algorithms
def setup_algorithms(training_data, dist, delta, train=True): start = time.time() n_x = dist.n_x n_a = dist.n_a n_y = dist.n_y statistical_approximation_prior = StatisticalApproximator( n_x, n_a, n_y, training_data, smoothing_mode='gaussian') function_approximation = FunctionApproximation(n_x, n_a, n_y, training_data) constraint_prior = Constraint(training_data, n_a, n_y, approximator=statistical_approximation_prior, delta=delta) constraintFuncApprox = Constraint(training_data, n_a, n_y, approximator=function_approximation, delta=delta) algorithms = [ ConstrainedDynamicProgramming(n_x, n_a, n_y, training_data, constraint_prior, statistical_approximation_prior, name="Constrained Dynamic Programming", label="CDP"), ConstrainedGreedy(n_x, n_a, n_y, training_data, constraint_prior, statistical_approximation_prior, name="Constrained Greedy", label="CG"), ConstrainedGreedy(n_x, n_a, n_y, training_data, constraintFuncApprox, function_approximation, name="Constrained Greedy FuncApprox", label="CG_F"), # ConstrainedDynamicProgramming(n_x, n_a, n_y, training_data, constraintStatUpper, # statistical_approximation), ConstrainedDynamicProgramming( n_x, n_a, n_y, training_data, constraintFuncApprox, function_approximation, name="Constrained Dynamic Programming FuncApprox", label="CDP_F"), NaiveDynamicProgramming(n_x, n_a, n_y, training_data, statistical_approximation_prior, reward=-(delta / 2 + 0.0001), name='Naive Dynamic Programming', label='NDP'), ] print("Setting up algorithms took {:.3f} seconds".format(time.time() - start)) return algorithms
n_y, approximator=function_approximation, delta=delta, epsilon=epsilon) print( "Initializing the constraint took {:.3f} seconds".format(time.time() - start)) print("Initializing algorithms") algorithms = [ # GreedyShuffled(n_x, n_a, n_y, split_training_data, delta, epsilon), # ConstrainedGreedy(n_x, n_a, n_y, split_training_data, constraintTrue, true_approximation, name="Constrained Greedy True", label="CGT"), ConstrainedGreedy(n_x, n_a, n_y, split_training_data, constraintStatUpper, statistical_approximation, name='Constrained Greedy Upper', label='CGU'), ConstrainedGreedy(n_x, n_a, n_y, split_training_data, constraintStatLower, statistical_approximation, name='Constrained Greedy Lower', label='CGL'), # ConstrainedGreedy(n_x, n_a, n_y, split_training_data, constraintFuncApprox, function_approximation, # name="Constrained Greedy FuncApprox"), # ConstrainedDynamicProgramming(n_x, n_a, n_y, split_training_data, constraintTrue, true_approximation, name="Dynamic Programming True", label="CDPT"), #ConstrainedDynamicProgramming(n_x, n_a, n_y, split_training_data, constraintStatUpper, statistical_approximation,
delta=delta, epsilon=epsilon) constraintFuncApprox = Constraint(split_training_data, n_a, n_y, approximator=function_approximation, delta=delta, epsilon=epsilon) print( "Initializing the constraint took {:.3f} seconds".format(time.time() - start)) print("Initializing algorithms") algorithms = [ #ConstrainedGreedy(n_x, n_a, n_y, split_training_data, constraintTrue, true_approximation, name="Constrained Greedy True", label="CGT"), ConstrainedGreedy(n_x, n_a, n_y, split_training_data, constraintFuncApprox, function_approximation), #ConstrainedGreedy(n_x, n_a, n_y, split_training_data, constraintFuncApprox, function_approximation, name="Constrained Greedy FuncApprox"), #ConstrainedDynamicProgramming(n_x, n_a, n_y, split_training_data, constraintTT, true_approximation, name="Dynamic Programming Exact", label="CDP_E"), #ConstrainedDynamicProgramming(n_x, n_a, n_y, split_training_data, constraintCT, statistical_approximation, name="Dynamic Programming True Stat", label="CDP_TS"), #ConstrainedDynamicProgramming(n_x, n_a, n_y, split_training_data, constraintStat, statistical_approximation), #ConstrainedDynamicProgramming(n_x, n_a, n_y, split_training_data, constraintStatL, statistical_approximation, name="CDP_L", label='CDP_L'), #ConstrainedDynamicProgramming(n_x, n_a, n_y, split_training_data, constraintCT, statistical_approximation, name="CDP_E", label='CDP_E'), #ConstrainedDynamicProgramming(n_x, n_a, n_y, split_training_data, constraintStat, function_approximation, name="Dynamic Programming Func", label="CDPF"), #ConstrainedDynamicProgramming(n_x, n_a, n_y, split_training_data, constraintFuncApprox, function_approximation,name="Constrained Dynamic Programming FuncApprox"), #NaiveGreedy(n_x, n_a, n_y, statistical_approximation, n_a-1), #DistAlgWrapper(dist, name="Distribution", label="dist"), #NaiveDynamicProgramming(n_x, n_a, n_y, split_training_data, statistical_approximation, reward=reward) #QLearner(n_x, n_a, n_y, split_training_data, reward=reward, learning_time=training_episodes, learning_rate=0.01, discount_factor=1), #QLearnerConstrained(n_x, n_a, n_y, split_training_data, constraint, learning_time=training_episodes, learning_rate=0.01, discount_factor=1), #OnlineQLearner(n_x, n_a, n_y, dist, constraint, learning_time=training_episodes),
statistical_approximationNone = StatisticalApproximator(n_x, n_a, n_y, training_data_set, smoothing_mode='none') function_approximation = FunctionApproximation(n_x, n_a, n_y, training_data_set) print("Initializing Constraint") start = time.time() constraintNone = Constraint(training_data_set, n_a, n_y, approximator=statistical_approximationNone, delta=delta, epsilon=epsilon) constraintPrior = Constraint(training_data_set, n_a, n_y, approximator=statistical_approximationPrior, delta=delta, epsilon=epsilon) constraintFunc = Constraint(training_data_set, n_a, n_y, approximator=function_approximation, delta=delta, epsilon=epsilon) constraintTrue = Constraint(training_data_set, n_a, n_y, approximator=true_approximation, delta=delta, epsilon=epsilon) print("Initializing the constraint took {:.3f} seconds".format(time.time()-start)) print("Initializing algorithms") algorithms = [ ConstrainedDynamicProgramming(n_x, n_a, n_y, training_data_set, constraintNone, statistical_approximationNone, name="CDP_U", label="CDP_U"), ConstrainedDynamicProgramming(n_x, n_a, n_y, training_data_set, constraintPrior, statistical_approximationPrior, name="CDP_H", label="CDP_H"), ConstrainedGreedy(n_x, n_a, n_y, training_data_set, constraintPrior, statistical_approximationPrior, name="CG_H"), ConstrainedDynamicProgramming(n_x, n_a, n_y, training_data_set, constraintTrue, true_approximation, name="CDP_T", label="CDP_T"), ] n_algorithms = len(algorithms) for alg in algorithms: start = time.time() print("Training {}".format(alg.name)) alg.learn() if alg.name not in evaluations_data_amount: evaluations_data_amount[alg.name] = {outcome_name: [], time_name: []} total_outcome = 0 total_time = 0 for i in range(n_test_samples): interventions = alg.evaluate(test_data[i])
constraintNone = Constraint(split_training_data, n_a, n_y, approximator=statistical_approximationNone, delta=delta, epsilon=epsilon) constraintPrior = Constraint(split_training_data, n_a, n_y, approximator=statistical_approximationPrior, delta=delta, epsilon=epsilon) constraintFunc = Constraint(split_training_data, n_a, n_y, approximator=function_approximation, delta=delta, epsilon=epsilon) constraintATrue = Constraint(split_training_data, n_a, n_y, approximator=true_approximation, delta=delta, epsilon=epsilon) constraintTrue = TrueConstraint(dist, approximator=true_approximation, delta=delta, epsilon=epsilon) print("Initializing the constraint took {:.3f} seconds".format(time.time()-start)) print("Initializing algorithms") algorithms = [ #ConstrainedDynamicProgramming(n_x, n_a, n_y, split_training_data, constraintNone, statistical_approximationNone, name="Dynamic Programming Uniform Prior", label="CDP_U"), ConstrainedDynamicProgramming(n_x, n_a, n_y, split_training_data, constraintPrior, statistical_approximationPrior, name="Dynamic Programming Historical Prior", label="CDP_H"), #ConstrainedDynamicProgramming(n_x, n_a, n_y, split_training_data, constraintFunc, function_approximation, name="Dynamic Programming Function Approximation", label="CDP_F"), ConstrainedDynamicProgramming(n_x, n_a, n_y, split_training_data, constraintATrue, true_approximation, name="Dynamic Programming True", label="CDP_T"), #ConstrainedGreedy(n_x, n_a, n_y, split_training_data, constraintNone, statistical_approximationNone, name="Greedy Uniform Prior", label="CG_U"), ConstrainedGreedy(n_x, n_a, n_y, split_training_data, constraintPrior, statistical_approximationPrior, name="Greedy Historical Prior", label="CG_H"), #ConstrainedGreedy(n_x, n_a, n_y, split_training_data, constraintFunc, function_approximation, name="Greedy Function Approximation", label="CG_F"), ] assert len(algorithms) == n_algorithms time_name = 'time' outcome_name = 'outcome' evaluations_delta = {} for delta in deltas: for alg in algorithms: if alg.name not in evaluations_delta: evaluations_delta[alg.name] = {outcome_name: [], time_name: []} try: alg.constraint.delta = delta alg.constraint.better_treatment_constraint_dict = {}
def setup_algorithms(training_data, dist, delta): start = time.time() n_x = dist.n_x n_a = dist.n_a n_y = dist.n_y statistical_approximation_none = StatisticalApproximator( n_x, n_a, n_y, training_data, smoothing_mode='none') statistical_approximation_prior = StatisticalApproximator( n_x, n_a, n_y, training_data, smoothing_mode='gaussian') function_approximation = FunctionApproximation(n_x, n_a, n_y, training_data) true_approximation = ExactApproximator(dist) constraint_none = Constraint(training_data, n_a, n_y, approximator=statistical_approximation_none, delta=delta) constraint_prior = Constraint(training_data, n_a, n_y, approximator=statistical_approximation_prior, delta=delta) constraint_func = Constraint(training_data, n_a, n_y, approximator=function_approximation, delta=delta) constraint_true = Constraint(training_data, n_a, n_y, approximator=true_approximation, delta=delta) algorithms = [ ConstrainedDynamicProgramming( n_x, n_a, n_y, training_data, constraint_prior, statistical_approximation_prior, name="Dynamic Programming Historical Prior", label="CDP_H"), ConstrainedGreedy(n_x, n_a, n_y, training_data, constraint_func, function_approximation, name="Constrained Greedy Historical Prior", label="CG_H"), NaiveDynamicProgramming( n_x, n_a, n_y, training_data, statistical_approximation_prior, reward=-(delta + 0.0001), name='Naive Dynamic Programming Historical Prior', label='NDP_H'), NaiveGreedy(n_x, n_a, n_y, approximator=statistical_approximation_prior, max_steps=4), ] print("Setting up algorithms took {:.3f} seconds".format(time.time() - start)) return algorithms