Commit 9d35b9c9 authored by Bowen Wu's avatar Bowen Wu
Browse files

Clean up repo

parent c6b0796d
......@@ -7,5 +7,5 @@ mutated/
*.sol
.DS_Store
benchmark/
seed/
seed*/
lsf.*
\ No newline at end of file
......@@ -59,6 +59,20 @@ def parse_args_function():
help="When having more than one mutating strategy, how to pick it. \
Available: fixed, round_robin, random."
)
parser.add_argument(
"--solver1",
choices=["Cplex", "Gurobi", "CBC"],
type=str,
default="Cplex",
help="Avaialble solvers are Cplex, Gurobi and CBC"
)
parser.add_argument(
"--solver2",
choices=["Cplex", "Gurobi", "CBC"],
type=str,
default="Gurobi",
help="Avaialble solvers are Cplex, Gurobi and CBC"
)
return parser
......
import random
from mps_util import load_mps_dict, print_mps_dict
import pysmps.pysmps.smps_loader
from generator import MPSProgram
import numpy as np
class MutantMethod(object):
......
import os
import sys
from pysmps.pysmps.smps_loader import load_mps, MPSParseError
from pysmps.pysmps.smps_loader import load_mps
from generator import MPSProgram
import glob
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
......@@ -62,27 +61,6 @@ def print_mps_dict(mps: dict):
for k in mps:
print(f"{bcolors.OKBLUE}{k}\n{bcolors.ENDC}", mps[k])
def read_benchmark_mps():
"""
Check if the current parser can handle all possible cases in the seed pool
Parameter:
maxsize: maximum size of the file interested in bytes
"""
tests = glob.glob("/Users/bowenwu/optfuzz_seeds/*.mps")
import time
for test in tests:
print("Reading ", test)
start = time.time()
try:
d = load_mps_dict(test)
MPSProgram(**d).__str__()
except MPSParseError as e:
print(test, " fails.\n", e)
print("Time taken = ", time.time() - start)
def print_load_mps_ret(path):
name, objsense, objective_name, row_names, col_names, col_types, types, c, A, rhs_names, rhs, bnd_names, bnd = load_mps(path)
print(f"{bcolors.OKBLUE}Program Name: {bcolors.ENDC}", name)
......
......@@ -14,16 +14,12 @@ from mps_mut import DenseMatMut, FlipObjSense, ScaleMut, SparseMatMut, FlipSignM
from mps_util import load_mps_dict, eprint
from optimizer import OPTIMAL, CBCSolver, CplexSolver, GurobiSolver
# TODO
# 1. Make this class checkpoint-able
# 2. Use two threads for cplex and gurobi each
# 3. Collect more stats
class OptFuzzer(object):
MAX_MUTANTS_IN_DIR = 10
def __init__(self, seed : List,
mut_method : List,
cplex : CplexSolver,
gurobi : GurobiSolver,
solver_1 : CplexSolver,
solver_2 : GurobiSolver,
schedule: str,
save_path : str,
checkpoint_freq : int,
......@@ -35,8 +31,8 @@ class OptFuzzer(object):
self.no_op_mut_method = NoOpMut()
self.next_mut = 0
self.n_mut = len(self.mut_method)
self.cplex = cplex # cplex solver
self.gurobi = gurobi # gurobi solver
self.solver_1 = solver_1
self.solver_2 = solver_2
self.save_path = save_path # path to save the mutated mps files
os.makedirs(self.save_path, exist_ok=True)
self.checkpoint_freq = checkpoint_freq
......@@ -97,11 +93,9 @@ class OptFuzzer(object):
#eprint("========== GOING TO MUTATE ============")
if mut_choice == 'random':
mps_prime = mps
for _ct in range(nr_mutations):
for _ in range(nr_mutations):
idx = random.randint(0, self.n_mut - 1)
mut_method = self.no_op_mut_method if (random.random() < 0.5) else self.mut_method[idx]
# eprint(self.mut_method)
# eprint('idx = ', idx, ' Mutating using ', mut_method)
mps_prime = mut_method(mps_prime)
else:
mps_prime = self.mut_method[idx](mps)
......@@ -117,9 +111,6 @@ class OptFuzzer(object):
self.next_id += 1
# self.queue.put(parent)
# heapq.heappush(self.queue, parent_ent)
return parent_ent, ret
def sol_equal(self, sol1 : dict, sol2 : dict, check_var = False) -> bool:
......@@ -167,11 +158,11 @@ class OptFuzzer(object):
f.write(mps_prog.__str__())
c_start = time.time()
cplex_sol = self.cplex(fname)
cplex_sol = self.solver_1(fname)
c_time = time.time() - c_start
g_start = time.time()
gurobi_sol = self.gurobi(fname)
gurobi_sol = self.solver_2(fname)
g_time = time.time() - g_start
if cplex_sol is None or gurobi_sol is None:
......@@ -188,21 +179,14 @@ class OptFuzzer(object):
new_key = scaled_time_diff * 1.0 + math.fabs(scaled_time_diff - p[0][2]) * 0
new_key = self.get_perturbed_time_diff(new_key)
heapq.heappush(self.queue, ((-new_key, p[1]["id"], scaled_time_diff), p[1]))
# eprint("time-diff = ", self.get_perturbed_time_diff(scaled_time_diff))
if not are_equal:
dfname = os.path.join(self.diff_path, f"diff_{len(self.diff)}.mps")
with open(dfname, "w") as f:
f.write(mps_prog.__str__())
self.diff.append(mut)
# print_ind_stats = False
# if(print_ind_stats):
# #Id, Name, Equal, Cplex time, Gurobi time
# eprint('{},{},{},{},{},{}'.format(mut['id']-203, mut['seed_name'].replace('seed/',''), are_equal, c_time, g_time,scaled_time_diff))
# self.all_mutant.append(mut)
self.all_mutant.append(mut)
def print_stats(self):
eprint(f"Seeds: {self.seed}")
......@@ -215,7 +199,6 @@ class OptFuzzer(object):
eprint("Difference ", i, " is derived from ", d["root"], " at round ", d["mut_no"])
def checkpoint(self):
pass
# store all the mutants that have been generated to disks
fname = os.path.join(self.checkpoint_path, f"ckpt_{self.next_id - 1}")
with open(fname, "wb") as f:
......@@ -240,10 +223,25 @@ class OptFuzzer(object):
self.checkpoint()
nround += 1
self.print_stats()
self.cplex.print_solver_stats()
self.gurobi.print_solver_stats()
self.solver_1.print_solver_stats()
self.solver_2.print_solver_stats()
self.save_states()
def get_solver(solver_name):
if solver_name == "Cplex":
solver = CplexSolver()
elif solver_name == "Gurobi":
solver = GurobiSolver()
elif solver_name == "CBC":
solver = CBCSolver()
else:
solver = None
solver.set_timeout(False)
solver.set_verbose(False)
return solver
def main():
from config import parser
args = parser.parse_args()
......@@ -271,8 +269,8 @@ def main():
import glob
seed = glob.glob(os.path.join(args.seed_path, "*.mps"))
cpx = CplexSolver(timeout=False)
grb = GurobiSolver(timeout=False)
solver_1 = get_solver(args.solver1)
solver_2 = get_solver(args.solver2)
ts = str(time.time())
eprint("Timestamp = ", ts)
......@@ -287,7 +285,7 @@ def main():
parser.print_help()
os.abort()
fuzzer = OptFuzzer(seed, mut_method, cpx, grb, schedule, save_path, checkpoint_freq, checkpoint_path, diff_path)
fuzzer = OptFuzzer(seed, mut_method, solver_1, solver_2, schedule, save_path, checkpoint_freq, checkpoint_path, diff_path)
fuzzer.fuzz(max_time, mut_choice)
if __name__ == "__main__":
......
......@@ -25,7 +25,7 @@ status_text = {
MAX_TIME_LIMIT = 20
class Solver(object):
def __init__(self, verbose = False, timeout = True) -> None:
def __init__(self, verbose = False, timeout = False) -> None:
# by default, the solvers are very talky but we can make it less verbose
self.verbose = verbose
self.timeout = timeout
......@@ -37,6 +37,12 @@ class Solver(object):
eprint("\n", self.__class__.__name__, " Stats:")
for k in self.stats:
eprint(status_text[k], " = ", self.stats[k], " times")
def set_verbose(self, verbose):
self.verbose = verbose
def set_timeout(self, timeout):
self.set_timeout = timeout
class CplexSolver(Solver):
# https://www.tu-chemnitz.de/mathematik/discrete/manuals/cplex/doc/refman/html/appendixB.html
......@@ -58,7 +64,7 @@ class CplexSolver(Solver):
119 : INF_OR_UNBD
}
def __init__(self, verbose = False, timeout = True, emphasis = 0) -> None:
def __init__(self, verbose = False, timeout = False, emphasis = 0) -> None:
super().__init__(verbose, timeout)
self.emphasis = emphasis
......@@ -153,7 +159,7 @@ class GurobiSolver(Solver):
9 : TIME_LIMIT_EXCEEDED,
}
def __init__(self, verbose = False, timeout = True) -> None:
def __init__(self, verbose = False, timeout = False) -> None:
super().__init__(verbose, timeout)
def __call__(self, mps_file: str) -> dict:
......
......@@ -2,6 +2,7 @@ from optimizer import OPTIMAL, CplexSolver, GurobiSolver, CBCSolver
import glob
import os
import sys
from mps_util import eprint
MIPEmphasisBalanced = 0
MIPEmphasisFeasibility = 1
......@@ -14,31 +15,33 @@ def replay(path):
cpx = CplexSolver(timeout=False, emphasis=MIPEmphasisBestBound)
grb = GurobiSolver(timeout=False)
diff_mps = glob.glob(os.path.join(path, "*.mps"))
print(diff_mps)
if len(diff_mps) == 0:
diff_mps = [path]
eprint(diff_mps)
for mps in diff_mps:
print(mps)
eprint(mps)
cpx_sol = cpx(mps)
print("Cplex: Status = ", cpx_sol["status"], end="")
eprint("Cplex: Status = ", cpx_sol["status"], end="")
if cpx_sol["status"] == OPTIMAL:
print(", Obj Val = ", cpx_sol["obj_val"])
eprint(", Obj Val = ", cpx_sol["obj_val"])
else:
print("")
eprint("")
grb_sol = grb(mps)
print("Gurobi: Status = ", grb_sol["status"], end="\n\n" if grb_sol["status"] != OPTIMAL else "")
eprint("Gurobi: Status = ", grb_sol["status"], end="\n\n" if grb_sol["status"] != OPTIMAL else "")
if grb_sol["status"] == OPTIMAL:
print(", Obj Val = ", grb_sol["obj_val"], "\n")
eprint(", Obj Val = ", grb_sol["obj_val"], "\n")
# cbc_solution(mps)
cbc_solution(mps)
def cbc_solution(mps):
cbc = CBCSolver()
cbc_sol = cbc(mps)
print("CBC: Status = ", cbc_sol["status"], end="\n\n" if cbc_sol["status"] != OPTIMAL else "")
eprint("CBC: Status = ", cbc_sol["status"], end="\n\n" if cbc_sol["status"] != OPTIMAL else "")
if cbc_sol["status"] == OPTIMAL:
print(", Obj Val = ", cbc_sol["obj_val"], "\n")
eprint(", Obj Val = ", cbc_sol["obj_val"], "\n")
if __name__ == "__main__":
assert len(sys.argv) >= 2, "Number of arguments must be larger than 2"
......
if [ $1 = "local" ]; then
euler_command=
elif [ $1 = "euler" ]; then
euler_command="bsub -W 270 -n 4 -R rusage[mem=2048]"
elif [ $1 = "clean" ]; then
if [ $1 = "clean" ]; then
rm -rf ckpt/
rm -rf diff/
# rm -rf diff/
rm -rf mutated/
rm clone*.log
rm lsf.*
exit
else
echo "Must specify local or euler"
exit
fi
time=14400
set -x
for mut in FlipSignMut MoreIntMut; do
if [ $mut = "FlipSignMut" ]; then
......@@ -22,11 +17,22 @@ for mut in FlipSignMut MoreIntMut; do
seed="seed2/"
fi
for policy in fifo pq; do
${euler_command} python opt_fuzz.py \
--mut_methods $mut \
--seed_path $seed \
--schedule $policy \
--max_time 14400
if [ $1 = "local" ]; then
python opt_fuzz.py \
--mut_methods $mut \
--seed_path $seed \
--schedule $policy \
--max_time $time 1>/dev/null
elif [ $1 = "euler" ]; then
bsub -W 270 -n 4 -R rusage[mem=2048] "python opt_fuzz.py \
--mut_methods $mut \
--seed_path $seed \
--schedule $policy \
--max_time $time 1>/dev/null"
else
echo "Must specify local or euler"
exit
fi
done
done
set +x
Supports Markdown
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