-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_spurious_experiment.py
76 lines (71 loc) · 2.78 KB
/
run_spurious_experiment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: utf-8 -*-
"""
Generate explanations with spurious features:
used to analyze the correlation and the percentage
of relevant feature changes.
"""
# Import packages
import gurobipy as GBP
import pandas as pd
import os
# Import custom functions
from src.Simulator import Simulator
from src.setup_experiment import run_sensitivity_experiment
dirname = os.path.dirname(__file__)
# Create gurobi environment
gurobiEnv = GBP.Env()
gurobiEnv.setParam('TimeLimit', 3600)
gurobiEnv.setParam('Threads', 8)
gurobiEnv.start()
# ---- Create simulation setting with synthetic data ----
experimentName = 'newsvendor'
# Experiment parameters
nbSamplesList = [50, 100, 200, 400, 600, 800]
nbTreesList = [100]
depthList = [4]
NB_REPETITIONS = 100
NB_SPURIOUS = 2
# ---- Run simulations ----
print('---- Running experiment: {} ----'.format(experimentName))
for nbSamples in nbSamplesList:
resultDataframe = pd.DataFrame()
print('\n -- Nb. samples: {}'.format(nbSamples))
for i in range(NB_REPETITIONS):
print(" Simulation: {} out of {}.".format(
i+1, NB_REPETITIONS), end='\r')
simulator = Simulator(experimentName, gurobiEnv,
nbSamples=nbSamples, verbose=False,
nbSpurious=NB_SPURIOUS)
x_init = simulator.sample()
x_alt = simulator.sample()
# ----------------- RANDOM FORESTS ---------------------
for nbTrees in nbTreesList:
for treeDepth in depthList:
z_opt, z_alt, x_r, x_a, t_r, t_a = run_sensitivity_experiment(
simulator, x_init, x_alt, simulator.get_model,
gurobiEnv, prescriptorType='rf', nbTrees=nbTrees,
max_depth=treeDepth)
partialResultDf = pd.DataFrame(
{'experiment': [experimentName],
'nbSamples': [nbSamples],
'simIndex': [i],
'method': ['rf'],
'nbTrees': [nbTrees],
'treeDepth': [treeDepth],
'x_init': [x_init],
'x_alt': [x_alt],
'z_opt': [z_opt],
'z_alt': [z_alt],
'x_rel': [x_r],
'x_abs': [x_a],
'solvetime_relative': [t_r],
'solvetime_absolute': [t_a]})
resultDataframe = pd.concat(
[resultDataframe, partialResultDf],
ignore_index=True, axis=0)
# Save results fo csv file
folderName = os.path.join(dirname, "output",)
os.makedirs(folderName, exist_ok=True)
resultDataframe.to_csv(folderName
+ '/spurious_{}_results_n_{}.csv'.format(
experimentName, nbSamples))