Files @ 630c42e6d376
Branch filter:

Location: OneEye/src/statebag/__init__.py

Laman
requirements.txt
"""Theory:
We have a sequence S of valid board states s_1, ..., s_n.
We search for a picked subsequence S_p of length k and m additional moves M such that:
- S_p augmented by M forms a valid sequence of moves
- k-m is maximal for S_p picked from S
It is relatively cheap to add new items to S.

User can change detector parameters, presumably in case the previous don't fit the (current) situation.
In such a case we assume the new parameters are correct from the current position onwards.
But the change might have been appropriate even earlier (before the user detected and fixed an error).
So we try to find the correct crossover point like this:
- construct a sequence S' = s'_i, ..., s'_n by reanalyzing the positions with a new set of parameters, where s_i is the point of previous user intervention or s_0
- for each s'_j:
	- try to append it to S[:j]
	- try to append it to S'[:j]
	- remember the better variant
- linearize the fork back by discarding s'_j-s preceding the crossover and s_j-s following the crossover
"""
from .boardstate import BoardState


## Update contents of diff1 by contents of diff2.
def updateDiff(diff1,diff2):
	res=[]
	i=j=0
	m=len(diff1)
	n=len(diff2)
	while i<m and j<n:
		(r1,c1,action1,item1)=diff1[i]
		(r2,c2,action2,item2)=diff2[j]
		if (r1,c1)==(r2,c2):
			merged=transformSingle(action1,item1,action2,item2)
			if merged: res.append((r1,c1,*merged))
			i+=1
			j+=1
		elif (r1,c1)<(r2,c2):
			res.append(diff1[i])
			i+=1
		else:
			res.append(diff2[j])
			j+=1
	if i<m: res.extend(diff1[i:])
	else: res.extend(diff2[j:])
	return res


def transformSingle(action1,item1,action2,item2):
	if action1=="+":
		if action2!="-":
			return ("+",item2)
	elif action2=="-": return ("-",item2)
	elif (action1=="*" and item1==item2) or (action1=="-" and item1!=item2): return ("*",item2)
	return None


class StateBag:
	def __init__(self):
		self._states=[]

	def pushState(self,board):
		sn=BoardState(board)
		if len(self._states)>0:
			if sn==self._states[-1]: return self._states[-1] # no change
			sn.cachedDiff=sn-self._states[-1]
		else: sn.setWeight(1)

		diff=sn.cachedDiff
		for s in reversed(self._states):
			sn.tryConnect(s,diff)
			diff=updateDiff(s.cachedDiff,diff)
		self._states.append(sn)
		return sn

	def merge(self,branch):
		pass