本文整理匯總了Python中net.Net.finished方法的典型用法代碼示例。如果您正苦於以下問題:Python Net.finished方法的具體用法?Python Net.finished怎麽用?Python Net.finished使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類net.Net
的用法示例。
在下文中一共展示了Net.finished方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: generate_iscas_faults
# 需要導入模塊: from net import Net [as 別名]
# 或者: from net.Net import finished [as 別名]
def generate_iscas_faults(file, num_mutated_gates):
net = Net(*SimpleParser(open(file)).parse())
net.generate_random_inputs()
net.calculate_outputs()
original_outputs = dict(net.outputs)
tries = 0
while tries < 100:
gates = random.sample(net.gates.values(), num_mutated_gates)
mutated_gates = []
tries += 1
for g in gates:
m = Gate(*g)
m.type = GATE_MUTATION[m.type]
mutated_gates.append(m)
original_gates = net.mutate_net(mutated_gates)
net.reset_outputs()
net.calculate_outputs()
net.mutate_net(original_gates)
if net.outputs != original_outputs:
return [file, net.input_values_compact(), mutated_gates]
print "Failed to generate fault for " + file + " within 100 tries."
net.finished()
示例2: IscasOracle
# 需要導入模塊: from net import Net [as 別名]
# 或者: from net.Net import finished [as 別名]
class IscasOracle(Description):
def __init__(self, sisc_file, inputs, outputs, **options):
super(IscasOracle, self).__init__([], **options)
self.sisc_file = sisc_file
self.comp_calls = 0
self.check_calls = 0
self.comp_time = 0
self.check_time = 0
self.scs = set()
self.setup = False
self.inputs = inputs
self.outputs = outputs
self.net = None
def setup_net(self):
if not self.setup:
self.setup = True
self.net = Net(*SimpleParser(open(self.sisc_file)).parse(), **self.options)
for c,i in enumerate(self.net.inputs):
self.net.inputs[i] = self.inputs[c] == 1
for c,i in enumerate(self.net.outputs):
self.net.outputs[i] = self.outputs[c] == 1
self.components = TwoWayDict(dict(enumerate(self.net.gates.keys())))
def set_options(self, **options):
super(IscasOracle, self).set_options(**options)
if self.net is not None:
self.net.set_options(**options)
def get_num_components(self):
self.setup_net()
return len(self.components)
def get_conflict_set(self, h):
self.setup_net()
self.comp_calls += 1
t0 = time.time()
cs = self.net.calculate_conflicts(self.numbers_to_gates(h))
t1 = time.time()
self.comp_time += t1-t0
if cs:
self.scs.add(frozenset(cs))
return self.gates_to_numbers(cs)
else:
return None
def check_consistency(self, h):
self.setup_net()
self.check_calls += 1
t0 = time.time()
sat = self.net.check_consistency(self.numbers_to_gates(h))
t1 = time.time()
self.check_time += t1-t0
return sat
def get_next_diagnosis(self, previous_diagnoses, max_card=None):
self.setup_net()
self.comp_calls += 1
t0 = time.time()
# print "get_next_diagnosis(%s,%d)"%(previous_diagnoses, max_card)
diag = self.net.calculate_next_diagnosis(map(self.numbers_to_gates,previous_diagnoses), max_card=max_card)
# print "solution:", diag
t1 = time.time()
self.comp_time += t1-t0
if diag:
return self.gates_to_numbers(diag)
else:
return None
def get_all_diagnoses(self, previous_diagnoses, max_card=None, max_solutions=None):
self.setup_net()
self.comp_calls += 1
t0 = time.time()
diag = self.net.calculate_next_diagnosis(map(self.numbers_to_gates,previous_diagnoses), max_card=max_card, find_all_sols=True)
t1 = time.time()
self.comp_time += t1-t0
if diag:
return map(self.gates_to_numbers, diag)
else:
return None
def finished(self):
self.net.finished()
def gates_to_numbers(self, gates):
return frozenset(map(lambda g: self.components.key(g), gates))
def numbers_to_gates(self, numbers):
return frozenset(map(lambda n: self.components[n], numbers))
示例3: generate_independent_faults
# 需要導入模塊: from net import Net [as 別名]
# 或者: from net.Net import finished [as 別名]
def generate_independent_faults(file, num_gates_to_mutate, exclude_gates=[]):
net = Net(*SimpleParser(open(file)).parse())
if len(net.outputs) < num_gates_to_mutate:
net.finished()
raise Exception("Cannot create %d independent mutations for net with %d outputs" % (num_gates_to_mutate, len(net.outputs)))
try_num = 1
log.info("Trying to generate %d independent fault(s) for %s" % (num_gates_to_mutate, file))
while try_num < 2*(2**len(net.inputs)): # try about two times the number of possible input combinations
try_num += 1
net.generate_random_inputs()
log.debug("=== Trying input %s" % net.inputs)
net.reset_outputs() # unconstrain all outputs at the beginning
net.calculate_outputs()
previous_outputs = dict(net.outputs)
log.debug("Original Outputs: %s" % previous_outputs)
net.reset_outputs()
gates_to_try = filter(lambda g: g.output not in exclude_gates, net.gates.values())
mutated_gates = []
immutable_outputs = set()
while len(mutated_gates) < num_gates_to_mutate:
while gates_to_try:
log.debug("Previous output: %s" % previous_outputs)
random.shuffle(gates_to_try)
gate = gates_to_try.pop()
mutated_gate = Gate(*gate)
mutated_gate.type = GATE_MUTATION[mutated_gate.type]
log.debug("Mutating %s to %s" % (gate, mutated_gate))
net.mutate_net([mutated_gate])
if not net.calculate_outputs():
log.debug("Unsatisfiable, undoing mutation.")
net.mutate_net([gate]) # restore original gate
break
new_outputs = dict(net.outputs)
log.debug("New output: %s" % new_outputs)
changed_outputs = set([g for g in new_outputs if new_outputs[g] != previous_outputs[g]])
if changed_outputs:
log.debug("found mutation")
immutable_outputs |= changed_outputs
log.debug("Immutable outputs: %s" % immutable_outputs)
mutated_gates.append(mutated_gate)
previous_outputs = new_outputs
for o in net.outputs: # unconstrain all outputs that didn't change
if o not in immutable_outputs:
net.outputs[o] = None
break
else:
log.debug("undoing mutation, trying next")
net.mutate_net([gate])
for o in net.outputs: # unconstrain all outputs that didn't change (in the last step)
if o not in immutable_outputs:
net.outputs[o] = None
# end while gates_to_try
if not gates_to_try and len(mutated_gates) < num_gates_to_mutate:
log.debug("no more gates to try :(")
break
# end while len(mutated_gates) < num_gates_to_mutate:
if not len(mutated_gates) < num_gates_to_mutate:
break
net.finished()
# end while try_num < 2*(2**len(net.inputs)):
if len(mutated_gates) < num_gates_to_mutate:
return None
return [file, net.input_values_compact(), mutated_gates]