Commit 5853112d authored by Bowen Wu's avatar Bowen Wu
Browse files

Priority queue scheduler

parent 0c77884e
......@@ -3,9 +3,10 @@ import random
import os
import math
import pickle
import heapq
from copy import deepcopy
from typing import List
from typing import List, Tuple
from queue import Queue
from generator import MPSProgram
......@@ -45,7 +46,8 @@ class OptFuzzer(object):
self.abs_tol = abs_tol
self.diff = [] # mutants that cause differences
self.all_mutant = []
self.queue = Queue(maxsize=0) # unbounded queue for seeds
# self.queue = Queue(maxsize=0) # unbounded queue for seeds
self.queue = []
self.next_id = 0 # id assigned to the next mutant
print("Initializing seeds")
for s in self.seed:
......@@ -54,12 +56,17 @@ class OptFuzzer(object):
"parent" : [-1], # root seed, no parent
"mut_method" : "",
"mps" : load_mps_dict(s),
"name": s,
}
self.next_id += 1
self.queue.put_nowait(mutant)
# self.queue.put_nowait(mutant)
heapq.heappush(self.queue, (0.5, mutant))
print("Seeds initialized")
def get_mutant(self, mut_choice : str, nr_mutations: int = 3) -> Tuple[dict, dict]:
# parent = self.queue.get()
parent_ent = heapq.heappop(self.queue)
parent = parent_ent[1]
mps = deepcopy(parent["mps"])
idx = 0
......@@ -80,15 +87,14 @@ class OptFuzzer(object):
for _ct 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]
print(self.mut_method)
print('idx = ', idx, ' Mutating using ', mut_method)
# print(self.mut_method)
# print('idx = ', idx, ' Mutating using ', mut_method)
mps_prime = mut_method(mps_prime)
else:
mps_prime = self.mut_method[idx](mps)
ret = {
"id" : self.next_id,
"seed_name": parent["name"],
"parent" : [parent["id"]], # root seed, no parent
"mut_method" : self.mut_method[idx].__str__(),
"mps" : mps_prime
......@@ -96,9 +102,10 @@ class OptFuzzer(object):
self.next_id += 1
self.queue.put(parent)
# self.queue.put(parent)
# heapq.heappush(self.queue, parent_ent)
return ret
return parent_ent, ret
def sol_equal(self, sol1 : dict, sol2 : dict, check_var = False) -> bool:
if sol1['status'] != sol2['status']:
......@@ -127,8 +134,14 @@ class OptFuzzer(object):
return True
def get_scaled_time_diff(self, time1, time2) -> float:
return math.fabs(time1-time2) / max(time1, time2)
def get_perturbed_time_diff(self, time) -> float:
return min(1, time + random.uniform(-time,time) * 0.5)
def fuzz_once(self, mut_choice):
mut = self.get_mutant(mut_choice)
p, mut = self.get_mutant(mut_choice)
mps_prog = MPSProgram(**mut["mps"])
idx = mut["id"] % OptFuzzer.MAX_MUTANTS_IN_DIR # prevent running our of inodes
......@@ -150,16 +163,22 @@ class OptFuzzer(object):
os.abort()
are_equal = self.sol_equal(cplex_sol, gurobi_sol)
print_ind_stats = False
if(print_ind_stats):
#Id, Name, Equal, Cplex time, Gurobi time
print('{},{},{},{},{}'.format(mut['id']-203, mut['seed_name'].replace('seed/',''), are_equal, c_time, g_time))
scaled_time_diff = self.get_scaled_time_diff(c_time, g_time)
heapq.heappush(self.queue, (self.get_perturbed_time_diff(scaled_time_diff), p[1]))
# print("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
# print('{},{},{},{},{},{}'.format(mut['id']-203, mut['seed_name'].replace('seed/',''), are_equal, c_time, g_time,scaled_time_diff))
self.all_mutant.append(mut)
......
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