Commit f8c9d8a8 authored by Marc Serramia's avatar Marc Serramia

New experiment types and plots

parent 539e077a
......@@ -6,26 +6,28 @@ SHELL = 'zsh' #'bash'
#--------------DATA GENERATION PARAMETERS-----------------
EXPERIMENT_TYPE = "RANGE_NORMS" #Has to be either: "FIXED", "RANGE_NORMS" or "RANGE_RATIO"
BUILD = True
SOLVE = False
NUM_NORMS = [500]
NUM_VALUES = [50]
SOLVE = True
NUM_NORMS = range(40,1000,40)
NUM_VALUES = [500]
PERC_VALUES = 0.1
HEIGHTS = [0,1,2,3]
WIDTHS = [0,1,2,3]
PROB_INDIF_VALUE = 0.2
INCOMPATIBILITY_PROB = 0.1
INCOMPATIBILITY_PROB = [0,0.2,0.4,0.6,0.8,1]
PROB_VALUE_NORMS = 0.2
PROB_TREE = 1
ROOTS = None
GENERALISATION_PROB = 0.2
ROUNDS = 100
SOLVE_TIME_LIMIT = 3600
ROUNDS = 1
SOLVE_TIME_LIMIT = 60
#-----------------PLOTTING PARAMETERS-------------------
PLOT_TYPE = "RATIO"
PLOT_TYPE = "SCALABILITY" #Has to be either: "RATIO" or "SCALABILITY"
RATIO_TYPE = "D"
PLOT_NORMS = 500
PLOT_NORMS = 100
CUT = False
QUARTILE_CUT = 0
SHOW_X_RANGE = [0, 1e55]
......
import os
import sys
import PARAMETERS
import numpy as np
import matplotlib.pyplot as plt
......@@ -20,7 +21,7 @@ class Plot:
y[i] = y[i + 1]
y[i + 1] = temp
def loadScatterPoints(self):
def loadScatterPointsRatio(self):
size = os.path.getsize(self.plot_filename)
plotfile = open(self.plot_filename, "r")
x = []
......@@ -53,7 +54,58 @@ class Plot:
fig.suptitle('Effect of the problem structure on the execution time', size=16)
ax.set_xlabel('Ratio (Generalisation relations / Exclusivity relations)', size=12)
ax.set_ylabel('Time (sec)', size=12)
x, y = self.loadScatterPoints()
x, y = self.loadScatterPointsRatio()
self.sortData(x,y)
if PARAMETERS.CUT:
toRemove = []
upper_cut = np.percentile(y, 100-PARAMETERS.QUARTILE_CUT)
for i in range(len(x)):
if x[i] < PARAMETERS.SHOW_X_RANGE[0] or x[i] > PARAMETERS.SHOW_X_RANGE[1]:
toRemove.append(i)
if y[i] < PARAMETERS.SHOW_Y_RANGE[0] or y[i] > PARAMETERS.SHOW_Y_RANGE[1]:
toRemove.append(i)
if y[i] > upper_cut:
toRemove.append(i)
toRemove.sort(reverse=True)
for i in toRemove:
x.pop(i)
y.pop(i)
plt.scatter(x, y, color=PARAMETERS.POINT_COLOR, s=PARAMETERS.POINT_SIZE)
if PARAMETERS.SAVE:
plt.savefig(plot_fig_path)
if PARAMETERS.SHOW:
plt.show()
plt.close(fig)
def loadScatterPointsNormRange(self):
size = os.path.getsize(self.plot_filename)
plotfile = open(self.plot_filename, "r")
x = []
y = []
while size:
line = plotfile.readline()
data = line.strip('\n').split(',')
num_norms = data[0]
height = data[1]
width = data[2]
num_inc = data[3]
num_dgen = data[4]
num_igen = data[5]
time = data[6]
x.append(num_norms)
y.append(float(time))
size -= len(line)
plotfile.close()
return x, y
def plotnormrange(self):
plot_fig_path = os.getcwd()+"/PLOTDATA/PlotNormRange"
fig = plt.figure()
ax = fig.add_subplot(111)
fig.suptitle('Scalability of the optimisation', size=16)
ax.set_xlabel('Number of norms', size=12)
ax.set_ylabel('Time (sec)', size=12)
x, y = self.loadScatterPointsNormRange()
self.sortData(x,y)
if PARAMETERS.CUT:
toRemove = []
......@@ -77,13 +129,22 @@ class Plot:
plt.close(fig)
def main():
plot_data_file = os.getcwd()+"/PLOTDATA/"+str(PARAMETERS.PLOT_NORMS)+"N.txt"
if os.path.exists(plot_data_file):
pl = Plot(plot_data_file)
if PARAMETERS.PLOT_TYPE == "RATIO":
if PARAMETERS.PLOT_TYPE == "RATIO":
plot_data_file = os.getcwd()+"/PLOTDATA/"+str(PARAMETERS.PLOT_NORMS)+"N.txt"
if os.path.exists(plot_data_file):
pl = Plot(plot_data_file)
pl.plotratio()
else:
print("INEXISTENT PLOT FILE")
elif PARAMETERS.PLOT_TYPE == "SCALABILITY":
plot_data_file = os.getcwd() + "/PLOTDATA/NORMRANGE.txt"
if os.path.exists(plot_data_file):
pl = Plot(plot_data_file)
pl.plotnormrange()
else:
print("INEXISTENT PLOT FILE")
else:
print("INEXISTENT PLOT FILE")
print("INVALID PLOT TYPE")
if __name__ == "__main__":
if sys.version > str(3):
......
......@@ -7,7 +7,7 @@ import cplex
class Problem:
def __init__(self, num_norms, num_values, height, width, round):
def __init__(self, num_norms, num_values, height, width, round, prob_inc = None):
self.num_norms = num_norms
self.num_values = num_values
self.round = round
......@@ -15,6 +15,10 @@ class Problem:
self.width = width
self.norms = {}
self.ancestors = {}
if prob_inc is None:
self.prob_inc = PARAMETERS.INCOMPATIBILITY_PROB
else:
self.prob_inc = prob_inc
self.inc_constraints = []
self.gen_constraints = []
self.values = {}
......@@ -25,6 +29,25 @@ class Problem:
self.indirect_gen = None
self.lp = None
def getFilename(self, type):
if type == "LP":
extension = ".lp"
elif type == "SOL":
extension = ".sol"
else:
extension = ".txt"
filename = None
if PARAMETERS.EXPERIMENT_TYPE == "FIXED":
filename = os.getcwd() + "/" + str(self.num_norms) + "N/"+type+"/"+type+"_" + str(self.num_norms) + "N_" + str(self.height) + "H_" + str(self.width) + "W_" + str(self.round) + extension
elif PARAMETERS.EXPERIMENT_TYPE == "RANGE_RATIO":
prob = round(self.prob_inc*100)
filename = os.getcwd() + "/" + str(self.num_norms) + "N/" + type + "/" + type + "_" + str(self.num_norms) + "N_" + str(self.height) + "H_" + str(self.width) + "W_" + str(prob) +"PROB_"+str(self.round) + extension
elif PARAMETERS.EXPERIMENT_TYPE == "RANGE_NORMS":
prob = round(self.prob_inc * 100)
filename = os.getcwd() + "/NORMRANGE/"+type+"/"+type+"_" + str(self.num_norms) + "N_" + str(self.height) + "H_" + str(self.width) + "W_" + str(prob) +"PROB_"+str(self.round) + extension
return filename
def treeCalculator(self):
norms_in_each_tree = 0
for i in range(self.height + 1):
......@@ -79,8 +102,8 @@ class Problem:
self.ancestors = newancestors
def generateTreeGeneralisation(self):
dfilename = os.getcwd() + "/" + str(self.num_norms) + "N/GEN/GEN_" + str(self.num_norms) + "N_" + str(self.height) + "H_" + str(self.width) + "W_" + str(self.round) + ".txt"
ifilename = os.getcwd() + "/" + str(self.num_norms) + "N/ANC/ANC_" + str(self.num_norms) + "N_" + str(self.height) + "H_" + str(self.width) + "W_" + str(self.round) + ".txt"
dfilename = self.getFilename("GEN")
ifilename = self.getFilename("ANC")
fdir = open(dfilename, "w")
find = open(ifilename, "w")
trees, norms_in_each_tree, singles = self.treeCalculator()
......@@ -100,7 +123,8 @@ class Problem:
find.write(towrite[:-1]+"\n")
def generateRandomGeneralisation(self):
filename = os.getcwd() + "/" + str(self.num_norms) + "N/GEN/GEN_" + str(self.num_norms) + "N_" + str(self.height) + "H_" + str(self.width) + "W_" + str(self.round) + ".txt"
dfilename = self.getFilename("GEN")
ifilename = self.getFilename("ANC")
f = open(filename, "w")
print("NOT IMPLEMENTED")
# TODO: GENERATE GENERALISATION in self.norms
......@@ -108,13 +132,13 @@ class Problem:
def generateIncomp(self):
self.num_incomp = 0
filename = os.getcwd()+"/"+str(self.num_norms)+"N/INC/INC_"+str(self.num_norms)+"N_" + str(self.height) + "H_" + str(self.width) + "W_" + str(self.round) + ".txt"
filename = self.getFilename("INC")
f = open(filename, "w")
for n1 in list(self.norms.keys())[:-1]:
for n2 in list(self.norms.keys())[n1 + 1:]:
if not n1 in self.ancestors[n2] and not n2 in self.ancestors[n1]:
ran = random.random()
if ran < PARAMETERS.INCOMPATIBILITY_PROB:
if ran < self.prob_inc:
f.write(str(n1)+", "+str(n2)+"\n")
self.inc_constraints.append([n1, n2])
self.num_incomp += 1
......@@ -125,7 +149,7 @@ class Problem:
#TODO: Not yet implemented
def generateValues(self):
filename = os.getcwd() + "/" + str(self.num_norms) + "N/VAL/VAL_" + str(self.num_norms) + "N_" + str(self.height) + "H_" + str(self.width) + "W_" + str(self.round) + ".txt"
filename = self.getFilename("VAL")
f = open(filename, "w")
self.generateValuePreferences()
for v in self.values:
......@@ -166,7 +190,7 @@ class Problem:
self.values[v] = w
def readValues(self, cats_val_filename):
filename = os.getcwd() + "/" + str(self.num_norms) + "N/VAL/VAL_" + str(self.num_norms) + "N_" + str(self.height) + "H_" + str(self.width) + "W_" + str(self.round) + ".txt"
filename = self.getFilename("VAL")
f = open(filename, "w")
self.generateValuePreferences()
print("NOT IMPLEMENTED")
......@@ -186,7 +210,7 @@ class Problem:
def build(self):
print("Building: " + str(self.num_norms) + "N " + str(self.height) + "H " + str(self.width) + "W " + str(self.round) + "R")
self.lp = os.getcwd() + "/" + str(self.num_norms) + "N/LP/LP_" + str(self.num_norms) + "N_" + str(self.height) + "H_" + str(self.width) + "W_" + str(self.round) + ".lp"
self.lp = self.getFilename("LP")
lp_file = open(self.lp, "w")
#Header
......@@ -259,7 +283,7 @@ class Problem:
def solve(self, plotfile):
print("Solving: " + str(self.num_norms) + "N " + str(self.height) + "H " + str(self.width) + "W " + str(self.round) + "R")
if not self.lp:
self.lp = os.getcwd() + "/" + str(self.num_norms) + "N/LP/LP_" + str(self.num_norms) + "N_" + str(self.height) + "H_" + str(self.width) + "W_" + str(self.round) + ".lp"
self.lp = self.getFilename("LP")
if self.num_incomp is None or self.direct_gen is None or self.indirect_gen is None:
f = open(self.lp, "r")
lines = f.readlines()
......@@ -270,7 +294,7 @@ class Problem:
self.indirect_gen = int(lines[8].split(" ")[-2])
if self.num_incomp is None:
self.num_incomp = int(lines[9].split(" ")[-2])
self.sol = os.getcwd() + "/" + str(self.num_norms) + "N/SOL/SOL_" + str(self.num_norms) + "N_" + str(self.height) + "H_" + str(self.width) + "W_" + str(self.round) + ".sol"
self.sol = self.getFilename("SOL")
try:
m = cplex.Cplex(self.lp)
m.parameters.timelimit.set(PARAMETERS.SOLVE_TIME_LIMIT)
......@@ -281,6 +305,9 @@ class Problem:
final_time = time.time() - start_time
print("Solved in "+str(final_time)+"s")
m.solution.write(self.sol)
plotfile.write(str(self.height)+","+str(self.width)+","+str(self.num_incomp)+","+str(self.direct_gen)+","+str(self.indirect_gen)+","+str(final_time)+"\n")
if PARAMETERS.EXPERIMENT_TYPE == "RANGE_NORMS":
plotfile.write(str(self.num_norms)+ "," +str(self.height) + "," + str(self.width) + "," + str(self.num_incomp) + "," + str(self.direct_gen) + "," + str(self.indirect_gen) + "," + str(final_time) + "\n")
else:
plotfile.write(str(self.height)+","+str(self.width)+","+str(self.num_incomp)+","+str(self.direct_gen)+","+str(self.indirect_gen)+","+str(final_time)+"\n")
except cplex.exceptions.CplexError:
print("UNABLE TO SOLVE "+self.lp)
\ No newline at end of file
......@@ -6,20 +6,25 @@ import sys
class ProblemIterator:
def __init__(self):
self.iter()
pass
def iter(self):
def iterFIXED(self):
for num_norms in PARAMETERS.NUM_NORMS:
f = None
if PARAMETERS.SOLVE:
filename = os.getcwd() + "/PLOTDATA/" + str(num_norms) + "N.txt"
f = open(filename, "w")
for num_values in PARAMETERS.NUM_VALUES:
if PARAMETERS.PERC_VALUES:
num_values = num_norms * PARAMETERS.PERC_VALUES
valueslist = round(num_values)
else:
valueslist = PARAMETERS.NUM_VALUES
for num_values in valueslist:
for case in self.hwcases():
height = case[0]
width = case[1]
for round in range(PARAMETERS.ROUNDS):
problem = Problem(num_norms, num_values, height, width, round)
for r in range(PARAMETERS.ROUNDS):
problem = Problem(num_norms, num_values, height, width, r)
if PARAMETERS.BUILD:
problem.generate()
problem.build()
......@@ -29,6 +34,62 @@ class ProblemIterator:
if f:
f.close()
def iterRANGERATIO(self):
for num_norms in PARAMETERS.NUM_NORMS:
f = None
if PARAMETERS.SOLVE:
filename = os.getcwd() + "/PLOTDATA/" + str(num_norms) + "N.txt"
f = open(filename, "w")
if PARAMETERS.PERC_VALUES:
num_values = num_norms*PARAMETERS.PERC_VALUES
valueslist = round(num_values)
else:
valueslist = PARAMETERS.NUM_VALUES
for num_values in valueslist:
for case in self.hwcases():
height = case[0]
width = case[1]
for prob in PARAMETERS.INCOMPATIBILITY_PROB:
prob = float(prob)/float(100)
for r in range(PARAMETERS.ROUNDS):
problem = Problem(num_norms, num_values, height, width, r, prob)
if PARAMETERS.BUILD:
problem.generate()
problem.build()
if PARAMETERS.SOLVE:
problem.solve(f)
f.flush()
if f:
f.close()
def iterRANGENORMS(self):
f = None
if PARAMETERS.SOLVE:
filename = os.getcwd() + "/PLOTDATA/NORMRANGE.txt"
f = open(filename, "w")
for num_norms in PARAMETERS.NUM_NORMS:
if PARAMETERS.PERC_VALUES:
num_values = num_norms * PARAMETERS.PERC_VALUES
valueslist = round(num_values)
else:
valueslist = PARAMETERS.NUM_VALUES
for num_values in valueslist:
for case in self.hwcases():
height = case[0]
width = case[1]
for prob in PARAMETERS.INCOMPATIBILITY_PROB:
prob = float(prob) / float(100)
for r in range(PARAMETERS.ROUNDS):
problem = Problem(num_norms, num_values, height, width, r, prob)
if PARAMETERS.BUILD:
problem.generate()
problem.build()
if PARAMETERS.SOLVE:
problem.solve(f)
f.flush()
if f:
f.close()
def hwcases(self):
cases = []
for h in PARAMETERS.HEIGHTS:
......@@ -44,7 +105,15 @@ class ProblemIterator:
return cases
def main():
ProblemIterator()
pi = ProblemIterator()
if PARAMETERS.EXPERIMENT_TYPE == "FIXED":
pi.iterFIXED()
elif PARAMETERS.EXPERIMENT_TYPE == "RANGE_RATIO":
pi.iterRANGERATIO()
elif PARAMETERS.EXPERIMENT_TYPE == "RANGE_NORMS":
pi.iterRANGENORMS()
else:
print("INVALID EXPERIMENT_TYPE")
if __name__ == "__main__":
if sys.version > str(3):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment