Skip to content

Instantly share code, notes, and snippets.

@jogonba2
Last active August 29, 2015 14:20
Show Gist options
  • Save jogonba2/d9abb9e32c984d3d60ee to your computer and use it in GitHub Desktop.
Save jogonba2/d9abb9e32c984d3d60ee to your computer and use it in GitHub Desktop.
Sistemas P, computación con membranas.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Overxfl0w13 #
# P Systems #
# PI = (O,mu,w1,...,wm,(R1,p1),...,(Rm,pm),i0) #
# O -> Alfabeto de objetos (catalizadores, de entrada y de salida)
# mu -> estructura de membranas en forma parentizada (estructura de árbol)
# w1,...,wm -> Cadenas representación de multiconjuntos asociadas a cada membrana
# (R1,p1)...,(Rm,pm) -> Reglas de evolución y relaciones de prioridad
# En las reglas de evolución, se eliminarán las relaciones de prioridad por simplicidad y no se permiten reglas cooperativas,
# con más de un elemento en la parte derecha de la regla #
# i0 -> Membrana de salida.
from random import randint
def p_system(PI):
O,mu,w,R,i0,winf = PI[0],PI[1],PI[2],PI[3],PI[4],[]
while True:
rules = []
for x in xrange(len(w)): # Indice Region #
for y in xrange(len(w[x])): # Indice objeto #
for rule in R[x]:
if w[x][y] in rule[0]:
rules.append((x,(" ".join(w[x]).replace(w[x][y]," ".join(rule[1])).split(" "))))
if len(rules)==0: break
randomRule = randint(0,len(rules)-1)
regla = rules[randomRule]
region_afectada = regla[0]
elim_region = False
w[region_afectada] = regla[1]
for s in xrange(len(w[region_afectada])):
if w[region_afectada][s]=="delta": elim_region=True
if "in" in w[region_afectada][s] and w[region_afectada][-1]==region_afectada+1:
w[region_afectada+1].insert(0,w[region_afectada][s][0])
w[region_afectada][s] = ""
if "here" in w[region_afectada][s]: w[region_afectada][s] = w[region_afectada][s][0]
if "out" in w[region_afectada][s]:
if region_afectada==0: winf.insert(0,w[region_afectada][s][0])
else:
w[region_afectada-1].insert(0,w[region_afectada][s][0])
w[region_afectada][s] = ""
for p in xrange(len(w)):
try:
while '' in w[p]: w[p].remove('')
except ValueError as ve: continue
if elim_region==True:
for sim in w[region_afectada]:
if sim!="delta": w[region_afectada-1].insert(0,sim)
del w[region_afectada]
del R[region_afectada]
elim_region = False
return w
if __name__ == '__main__':
O = ['a','c','b']
mu = [[]]
w = [[],['a','a','c','c','b']]
# R = [[REGLAS 1 REGION],[REGLAS 2 REGION],...,[REGLAS M REGION]]
# REGLAS I REGION = ((LHS),(RHS))
# delta -> destruye la membrana, prefijos here,inN,out #
R = [[],[(['a'],['bout','bout'])]]
i0 = 1
PI = (O,mu,w,R,i0)
print p_system(PI)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment