Cecey
New Coder
Python:
def determinate(automaton, replace=False, is_determinate=False, do_complete=False):
done = 0
conserve = []
states = list(automaton.states)
next_states = copy.deepcopy(automaton.next_states)
initial_states = list(automaton.initial_states)
terminal_states = list(automaton.terminal_states)
initial_composite = ''.join(letter + (STATE_SEPARATOR if initial_states[-1] != letter else '') for letter in initial_states)
initial_next_state = []
is_terminal = any(state in terminal_states for state in initial_states)
for state in initial_states:
if state in next_states:
for _state in next_states[state]:
if _state not in initial_next_state:
initial_next_state.append(_state)
if is_determinate:
letter_next = {letter: 0 for letter in automaton.alphabet}
for _state in initial_next_state:
letter_next[_state[1]]+=1
if any([letter_next[letter]>1 for letter in letter_next]):
return False
else:
states.insert(0, initial_composite)
next_states[initial_composite] = list(initial_next_state)
if is_terminal:
terminal_states.append(initial_composite)
for state in states:
letter_next = {letter: [] for letter in automaton.alphabet}
if state in next_states:
for _state in next_states[state]:
letter_next[_state[1]].append(_state[0])
for letter in letter_next:
if len(letter_next[letter])>1:
done = 1
if is_determinate:
return False
else:
composite = set(list(_state for _state in letter_next[letter]))
matched = match(automaton, states, initial_states, composite, conserve)
if matched:
next_states[state].append([matched, letter])
else:
process_composite(automaton, states, initial_states, automaton.alphabet, next_states, composite, conserve)
next_states[state].append([match(automaton, states, initial_states, composite, conserve), letter])
if not done:
if is_determinate:
return True
else:
for state in initial_states:
if state not in conserve:
del next_states[state]
del states[states.index(state)]
if state in terminal_states:
del terminal_states[terminal_states.index(state)]
next_states[initial_composite] = [_state for _state in next_states[initial_composite] if _state not in initial_next_state]
if do_complete:
save = list(automaton.states), copy.deepcopy(automaton.next_states)
automaton.states, automaton.next_states = states, next_states
states, next_states = complete(automaton, replace=False, special=True)
automaton.states = save[0]
automaton.next_states = save[1]
if replace:
automaton.states = states
automaton.next_states = next_states
automaton.initial_states = [initial_composite]
automaton.terminal_states = terminal_states
if do_complete:
automaton.is_complete = True
automaton.is_determinate = True
automaton.rename(automaton.name + ' (déterministe' +
(' et complet' if complete else '') + ')')
automaton.is_standard = is_standard(automaton)
return(automaton)
else:
automaton = Automaton(automaton.alphabet, states,
next_states, [initial_composite],
terminal_states, tag=[automaton,
'det'+('+cpt' if complete else '')], name='', bools=[
False, True, do_complete or automaton.is_complete, False])
return(automaton)
Last edited by a moderator: