Commit 65bfcccd authored by Bowen Wu's avatar Bowen Wu
Browse files

Add arg parse, change sol compare for MIP

parent 0b1c2070
......@@ -6,4 +6,5 @@ mutated/
*.log
*.sol
.DS_Store
benchmark/
\ No newline at end of file
benchmark/
seed/
\ No newline at end of file
import argparse
def parse_args_function():
parser = argparse.ArgumentParser()
parser.add_argument(
"--mut_methods",
nargs="+",
default=["ScaleMut", "SparseMatMut", "DenseMatMut", "FlipSignMut"],
help="Mutation strategies to be used. \
Available: ScaleMut, SparseMatMut, DenseMatMut, FlipSignMut"
)
parser.add_argument(
"--ckpt_freq",
type=int,
default=10000,
help="Number of iterations per checkpoint"
)
parser.add_argument(
"--max_time",
type=int,
default=1,
help="Maximum fuzzing time in seconds."
)
parser.add_argument(
"--seed_path",
type=str,
default="seed/",
help="Place where you store the seeds for fuzzing."
)
parser.add_argument(
"--save_path",
type=str,
default="mutated/",
help="Place where you want to store the temporary mutants generated."
)
parser.add_argument(
"--ckpt_path",
type=str,
default="ckpt/",
help="Place where you want to store the checkpoints."
)
parser.add_argument(
"--diff_path",
type=str,
default="diff/",
help="Place where you want to store the mutants that cause differences."
)
parser.add_argument(
"--mut_choice",
type=str,
default="fixed",
help="When having more than one mutating strategy, how to pick it. \
Available: fixed, round_robin, random."
)
return parser
parser = parse_args_function()
......@@ -7,10 +7,8 @@ import pickle
from typing import List
from queue import Queue
from numpy import diff
from generator import MPSProgram
from mps_mut import DenseMatMut, SparseMatMut, FlipSignMut
from mps_mut import DenseMatMut, ScaleMut, SparseMatMut, FlipSignMut
from mps_util import load_mps_dict
from optimizer import OPTIMAL, CplexSolver, GurobiSolver
......@@ -19,6 +17,7 @@ from optimizer import OPTIMAL, CplexSolver, GurobiSolver
# 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,
save_path : str, checkpoint_freq : int, checkpoint_path : str, diff_path: str, rel_tol = 1e-6) -> None:
self.seed = seed # a list of seed mps in dict format
......@@ -79,7 +78,7 @@ class OptFuzzer(object):
return ret
def sol_equal(self, sol1 : dict, sol2 : dict) -> bool:
def sol_equal(self, sol1 : dict, sol2 : dict, check_var = False) -> bool:
if sol1['status'] != sol2['status']:
return False
......@@ -89,18 +88,20 @@ class OptFuzzer(object):
return False
# check if the variable values are the same
for v1 in sol1['var_val']:
var1 = sol1['var_val'][v1]
# Gurobi omits variables if 0
var2 = 0
try:
var2 = sol2['var_val'][v1]
except KeyError:
# not applicable to MIP where continuous var values can be approx.
if check_var:
for v1 in sol1['var_val']:
var1 = sol1['var_val'][v1]
# Gurobi omits variables if 0
var2 = 0
if not math.isclose(var1, var2, rel_tol=self.rel_tol):
return False
try:
var2 = sol2['var_val'][v1]
except KeyError:
var2 = 0
if not math.isclose(var1, var2, rel_tol=self.rel_tol):
return False
return True
......@@ -108,7 +109,8 @@ class OptFuzzer(object):
mut = self.get_mutant(mut_choice)
mps_prog = MPSProgram(**mut["mps"])
fname = os.path.join(self.save_path, "mutant_" + str(mut["id"])+".mps")
idx = mut["id"] % OptFuzzer.MAX_MUTANTS_IN_DIR # prevent running our of inodes
fname = os.path.join(self.save_path, "mutant_" + str(idx)+".mps")
with open(fname, "w") as f:
f.write(mps_prog.__str__())
......@@ -143,9 +145,10 @@ class OptFuzzer(object):
def save_states(self):
self.checkpoint()
fname = os.path.join(self.diff_path, "diff.pickle")
with open(fname, "wb") as f:
pickle.dump(self.diff, f)
if len(self.diff) > 0:
fname = os.path.join(self.diff_path, "diff.pickle")
with open(fname, "wb") as f:
pickle.dump(self.diff, f)
def fuzz(self, max_time, mut_choice = "fixed"):
elapse = 0 # in seconds
......@@ -163,19 +166,37 @@ class OptFuzzer(object):
self.save_states()
def main():
dmm = DenseMatMut(0.5)
smm = SparseMatMut(0.5)
fsm = FlipSignMut(0.5)
seed = ["benchmark/markshare_4_0.mps"]
mut_method = [dmm]
from config import parser
args = parser.parse_args()
print(args)
mut_method = []
for m in args.mut_methods:
c = None
if m == "ScaleMut":
c = ScaleMut(0.5)
elif m == "SparseMatMut":
c = SparseMatMut(0.5)
elif m == "DenseMatMut":
c = DenseMatMut(0.5)
elif m == "FlipSignMut":
c = FlipSignMut(0.5)
mut_method.append(c)
import glob
seed = glob.glob(os.path.join(args.seed_path, "*.mps"))
cpx = CplexSolver()
grb = GurobiSolver()
save_path = "mutated/"
checkpoint_freq = 100
checkpoint_path = "ckpt/"
diff_path = "diff/"
max_time = 1
mut_choice = "fixed"
ts = str(time.time())
save_path = os.path.join(args.save_path, ts)
checkpoint_freq = args.ckpt_freq
checkpoint_path = os.path.join(args.ckpt_path, ts)
diff_path = os.path.join(args.diff_path, ts)
max_time = args.max_time
mut_choice = args.mut_choice
fuzzer = OptFuzzer(seed, mut_method, cpx, grb, save_path, checkpoint_freq, checkpoint_path, diff_path)
fuzzer.fuzz(max_time, mut_choice)
......
if [ $1 = "local" ]; then
euler_command=
elif [ $1 = "euler" ]; then
euler_command="bsub"
elif [ $1 = "clean" ]; then
rm -rf ckpt/
rm -rf diff/
rm -rf mutated/
rm clone*.log
exit
else
echo "Must specify local or euler"
exit
fi
set -x
${euler_command} python opt_fuzz.py \
--mut_methods FlipSignMut \
--max_time 30
set +x
\ No newline at end of file
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