Commit 539e077a authored by Marc Serramia's avatar Marc Serramia

Code ported to Python3

parent f5046d56
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 2.7" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.7" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
......
......@@ -8,24 +8,24 @@ SHELL = 'zsh' #'bash'
BUILD = True
SOLVE = False
NUM_NORMS = [1000]
NUM_NORMS = [500]
NUM_VALUES = [50]
HEIGHTS = [0,1,2,3]
WIDTHS = [0,1,2,3]
PROB_INDIF_VALUE = 0.2
INCOMPATIBILITY_PROB = 0.2
INCOMPATIBILITY_PROB = 0.1
PROB_VALUE_NORMS = 0.2
PROB_TREE = 1
ROOTS = None
GENERALISATION_PROB = 0.2
ROUNDS = 10
ROUNDS = 100
SOLVE_TIME_LIMIT = 3600
#-----------------PLOTTING PARAMETERS-------------------
PLOT_TYPE = "RATIO"
RATIO_TYPE = "D"
PLOT_NORMS = 1000
PLOT_NORMS = 500
CUT = False
QUARTILE_CUT = 0
SHOW_X_RANGE = [0, 1e55]
......
......@@ -40,7 +40,7 @@ class Plot:
elif PARAMETERS.RATIO_TYPE == "I":
x.append(float(num_igen) / float(num_inc))
else:
print "INVALID RATIO TYPE"
print("INVALID RATIO TYPE")
y.append(float(time))
size -= len(line)
plotfile.close()
......@@ -77,13 +77,16 @@ class Plot:
plt.close(fig)
def main():
plot_data_file = os.getcwd()+"/PLOTDATA/plot"+str(PARAMETERS.PLOT_NORMS)+"N.txt"
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":
pl.plotratio()
else:
print "INEXISTENT PLOT FILE"
print("INEXISTENT PLOT FILE")
if __name__ == "__main__":
main()
if sys.version > str(3):
main()
else:
print("Run with Python3")
......@@ -20,16 +20,16 @@ class Problem:
self.values = {}
self.normvalues = {}
self.va = {}
self.num_incomp = 0
self.direct_gen = 0
self.indirect_gen = 0
self.num_incomp = None
self.direct_gen = None
self.indirect_gen = None
self.lp = None
def treeCalculator(self):
norms_in_each_tree = 0
for i in range(self.height + 1):
norms_in_each_tree += pow(self.width, i)
trees = self.num_norms/norms_in_each_tree
trees = self.num_norms//norms_in_each_tree
singles = self.num_norms - trees * norms_in_each_tree
return trees, norms_in_each_tree, singles
......@@ -102,7 +102,7 @@ class Problem:
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"
f = open(filename, "w")
print "NOT IMPLEMENTED"
print("NOT IMPLEMENTED")
# TODO: GENERATE GENERALISATION in self.norms
# TODO: MAKE ANCESTORS
......@@ -110,8 +110,8 @@ class Problem:
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"
f = open(filename, "w")
for n1 in self.norms.keys()[:-1]:
for n2 in self.norms.keys()[n1 + 1:]:
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:
......@@ -121,7 +121,7 @@ class Problem:
def readIncompatibilities(self, cats_filename):
self.num_incomp = 0
print "NOT IMPLEMENTED"
print("NOT IMPLEMENTED")
#TODO: Not yet implemented
def generateValues(self):
......@@ -145,38 +145,47 @@ class Problem:
f.write(towrite)
def generateValuePreferences(self):
left = range(self.num_values)
values = []
left = list(range(self.num_values))
random.shuffle(left)
self.values[left.pop()] = 1
v = left.pop()
values.append([v, 1])
last = 1
while left:
v = left.pop()
equal = random.random() < PARAMETERS.PROB_INDIF_VALUE
if equal:
self.values[v] = last
values.append([v, last])
else:
self.values[v] = last + 1
last = self.values[v]
values.append([v, last +1])
last += 1
values.sort()
for pair in values:
v = pair[0]
w = pair[1]
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"
f = open(filename, "w")
self.generateValuePreferences()
print "NOT IMPLEMENTED"
print("NOT IMPLEMENTED")
#TODO: Not yet implemented
f.close()
def generate(self):
print "Generating: " + str(self.num_norms) + "N " + str(self.height) + "H " + str(self.width) + "W " + str(self.round) + "R"
print("Generating: " + str(self.num_norms) + "N " + str(self.height) + "H " + str(self.width) + "W " + str(self.round) + "R")
for n in range(self.num_norms):
self.norms[n] = []
self.ancestors[n] = []
self.indirect_gen = 0
self.direct_gen = 0
self.generateTreeGeneralisation()
self.generateIncomp()
self.generateValues()
def build(self):
print "Building: " + str(self.num_norms) + "N " + str(self.height) + "H " + str(self.width) + "W " + str(self.round) + "R"
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"
lp_file = open(self.lp, "w")
......@@ -215,13 +224,13 @@ class Problem:
lp_file.write("\nSubject To\n")
# Incompatibility constraints
lp_file.write('\Incompatibility constraints:\n')
lp_file.write('\\Incompatibility constraints:\n')
for pair in self.inc_constraints:
lp_file.write('n_' + str(pair[0]) + ' + n_' + str(pair[1]) + ' <= 1\n')
lp_file.flush()
#Generalisation constraints
lp_file.write('\Generalisation constraints:\n')
lp_file.write('\\Generalisation constraints:\n')
for norm in self.norms:
for a in self.ancestors[norm]:
self.gen_constraints.append([a, norm])
......@@ -231,7 +240,7 @@ class Problem:
lp_file.flush()
#Non-alignes norm constraints
lp_file.write('\Non-aligned norm constraints:\n')
lp_file.write("\\Non-aligned norm constraints:\n")
for norm in self.norms:
if self.va[norm] <= 0:
lp_file.write('n_' + str(norm) + ' = 0\n')
......@@ -248,9 +257,19 @@ class Problem:
lp_file.close()
def solve(self, plotfile):
print "Solving: " + str(self.num_norms) + "N " + str(self.height) + "H " + str(self.width) + "W " + str(self.round) + "R"
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"
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()
f.close()
if self.direct_gen is None:
self.direct_gen = int(lines[7].split(" ")[-2])
if self.indirect_gen is None:
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"
try:
m = cplex.Cplex(self.lp)
......@@ -260,8 +279,8 @@ class Problem:
start_time = time.time()
m.solve()
final_time = time.time() - start_time
print "Solved in "+str(final_time)+"s"
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))
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
print("UNABLE TO SOLVE "+self.lp)
\ No newline at end of file
from Problem import Problem
import PARAMETERS
import os
import sys
class ProblemIterator:
......@@ -9,6 +10,7 @@ class ProblemIterator:
def iter(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")
......@@ -23,7 +25,9 @@ class ProblemIterator:
problem.build()
if PARAMETERS.SOLVE:
problem.solve(f)
f.close()
f.flush()
if f:
f.close()
def hwcases(self):
cases = []
......@@ -43,4 +47,7 @@ def main():
ProblemIterator()
if __name__ == "__main__":
main()
\ No newline at end of file
if sys.version > str(3):
main()
else:
print("Run with Python3")
\ No newline at end of file
......@@ -84,7 +84,7 @@ class ProblemGraphVisualizer:
self.roots.remove(generalized)
structFile.close()
else:
print "ERROR: The path: " + filepath + " does not exist."
print("ERROR: The path: " + filepath + " does not exist.")
def isGood(self, node):
if isinstance(node, int):
......@@ -202,7 +202,7 @@ class ProblemGraphVisualizer:
look = True
for norm in nodesSelected:
actualCount = 0
queue = range(len(self.roots)-1, -1, -1)
queue = list(range(len(self.roots)-1, -1, -1))
appendPos = len(queue)
while queue:
currentNorm = queue.pop()
......@@ -249,4 +249,7 @@ def main():
ProblemGraphVisualizer.loadFromParameters()
if __name__ == "__main__":
main()
if sys.version > str(3):
main()
else:
print("Run with Python3")
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