Skip to content

Commit

Permalink
Fix bug in getBestMultiplyMatrix
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandrodemiquel committed May 4, 2020
1 parent a7f1e06 commit 61e7507
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 24 deletions.
39 changes: 24 additions & 15 deletions Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3326,7 +3326,7 @@ def generateMosaic(matrix, ops, factor):
return m

# Only if the factor is squared
def getBestMultiplyMatrix(t):
def getBestMultiplyMatrix(t, falseColor):
def getFullMatrix(matrix, color):
return np.full(matrix.shape, color, dtype=np.uint8)
# Possible operations on the matrix
Expand All @@ -3342,16 +3342,22 @@ def getFullMatrix(matrix, color):
ops.append(partial(rotate, angle=270))
if all([n==2 for n in t.nInColors]):
ops.append(partial(switchColors))
# TODO You can to better than that, come on
falseColor = 0

# Conditions
def trueCondition(matrix, pixel):
return True
def maxColor(matrix, pixel):
return pixel==max(matrix.colorCount, key=matrix.colorCount.get)
x = [k for k, v in sorted(matrix.colorCount.items(), key=lambda item: item[1])]
if len(x)<2 or matrix.colorCount[x[0]]!=matrix.colorCount[x[1]]:
return pixel==max(matrix.colorCount, key=matrix.colorCount.get)
else:
return False
def minColor(matrix,pixel):
return pixel==min(matrix.colorCount, key=matrix.colorCount.get)
x = [k for k, v in sorted(matrix.colorCount.items(), key=lambda item: item[1])]
if len(x)<2 or matrix.colorCount[x[-1]]!=matrix.colorCount[x[-2]]:
return pixel==min(matrix.colorCount, key=matrix.colorCount.get)
else:
return False
def isColor(matrix, pixel, color):
return pixel==color
def nonZero(matrix, pixel):
Expand Down Expand Up @@ -3384,11 +3390,11 @@ def zero(matrix, pixel):
opCond = (op, cond)
if score==0:
return opCond
return opCond
return (opCond, falseColor)

def doBestMultiplyMatrix(matrix, opCond):
def doBestMultiplyMatrix(matrix, opCond, falseColor):
factor = matrix.shape
m = np.zeros(tuple(s * f for s, f in zip(matrix.shape, factor)), dtype=np.uint8)
m = np.full(tuple(s * f for s, f in zip(matrix.shape, factor)), falseColor, dtype=np.uint8)
for i,j in np.ndindex(factor):
if opCond[1](matrix, matrix.m[i,j]):
m[i*matrix.shape[0]:(i+1)*matrix.shape[0], j*matrix.shape[1]:(j+1)*matrix.shape[1]] = \
Expand Down Expand Up @@ -4166,8 +4172,8 @@ def getPossibleOperations(t, c):
# CNNs

#x.append(getBestCNN(candTask))
if candTask.sameNSampleColors and all(["predictCNN" not in str(op.func) for op in c.ops]):
x.append(getBestSameNSampleColorsCNN(candTask))
#if candTask.sameNSampleColors and all(["predictCNN" not in str(op.func) for op in c.ops]):
# x.append(getBestSameNSampleColorsCNN(candTask))

"""
if t.backgroundColor != -1:
Expand Down Expand Up @@ -4338,8 +4344,13 @@ def getPossibleOperations(t, c):

if all([s.inMatrix.shape[0]**2 == s.outMatrix.shape[0] and \
s.inMatrix.shape[1]**2 == s.outMatrix.shape[1] for s in candTask.trainSamples]):
opCond = getBestMultiplyMatrix(candTask)
x.append(partial(doBestMultiplyMatrix, opCond=opCond))
totalColorCount = Counter()
for sample in t.trainSamples:
for color in sample.outMatrix.colorCount.keys():
totalColorCount[color] += sample.outMatrix.colorCount[color]
falseColor = max(totalColorCount.items(), key=operator.itemgetter(1))[0]
opCond = getBestMultiplyMatrix(candTask, falseColor)
x.append(partial(doBestMultiplyMatrix, opCond=opCond, falseColor=falseColor))


if hasattr(candTask, 'outShapeFactor'):
Expand Down Expand Up @@ -4472,13 +4483,11 @@ def getPossibleOperations(t, c):
for attrs in [set(['LaSh'])]:
x.append(partial(cropShape, attributes=attrs, backgroundColor=0, singleColor=True, diagonals=True))

x.append(partial(cropAllBackground))
"""
#x.append(partial(cropAllBackground))
if all([len(s.inMatrix.multicolorShapes)==1 for s in candTask.trainSamples+candTask.testSamples]):
x.append(partial(cropOnlyMulticolorShape, diagonals=False))
if all([len(s.inMatrix.multicolorDShapes)==1 for s in candTask.trainSamples+candTask.testSamples]):
x.append(partial(cropOnlyMulticolorShape, diagonals=True))
"""
if all([len(sample.inMatrix.fullFrames)==1 for sample in candTask.trainSamples+candTask.testSamples]):
x.append(partial(cropFullFrame))
x.append(partial(cropFullFrame, includeBorder=False))
Expand Down
24 changes: 20 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@
valid_tasks['aa300dc3']['train'][1]['output'][1][7] = 5
valid_tasks['aa300dc3']['train'][1]['input'][8][2] = 5
valid_tasks['aa300dc3']['train'][1]['output'][8][2] = 5
# ad7e01d0
valid_tasks['ad7e01d0']['train'][0]['output'][6][7] = 0



Expand Down Expand Up @@ -394,6 +396,20 @@ def recoverOriginalColors(matrix, rel):
m[i,j] = rel[matrix[i,j]][0]
return m

def hasRepeatedOutputs(t):
nonRepeated = []
for i in range(t.nTrain):
seen = False
for j in range(i+1, t.nTrain):
if np.array_equal(t.trainSamples[i].outMatrix.m, t.trainSamples[j].outMatrix.m):
seen = True
if not seen:
nonRepeated.append(t.trainSamples[i].outMatrix.m.copy())
if len(nonRepeated)==t.nTrain:
return False, []
else:
return True, nonRepeated

def ignoreGrid(t, task, inMatrix=True, outMatrix=True):
for s in range(t.nTrain):
if inMatrix:
Expand Down Expand Up @@ -458,7 +474,7 @@ def tryOperations(t, c, firstIt=False):
cTask["test"][s]["input"] = Utils.correctFixedColors(\
c.t.testSamples[s].inMatrix.m,\
np.array(cTask["test"][s]["input"]),\
c.t.fixedColors).tolist()
c.t.fixedColors).tolist()
cScore = sum([Utils.incorrectPixels(np.array(cTask["train"][s]["input"]), \
t.trainSamples[s].outMatrix.m) for s in range(t.nTrain)])
#changedPixels = sum([Utils.incorrectPixels(c.t.trainSamples[s].inMatrix.m, \
Expand Down Expand Up @@ -512,7 +528,7 @@ def __init__(self, index, taskId, ops):
taskId = index[idx]
task = allTasks[taskId]
originalT = Task.Task(task, taskId, submission=False)

if needsRecoloring(originalT):
task, trainRels, trainInvRels, testRels, testInvRels = orderTaskColors(originalT)
t = Task.Task(task, taskId, submission=False)
Expand Down Expand Up @@ -562,15 +578,15 @@ def __init__(self, index, taskId, ops):
for opI in range(len(c.ops)):
newX = c.ops[opI](Task.Matrix(x))
if t2.sameIOShapes and len(t2.fixedColors) != 0:
x = Utils.correctFixedColors(x, newX, t.fixedColors)
x = Utils.correctFixedColors(x, newX, t2.fixedColors)
else:
x = newX.copy()
if t.hasUnchangedGrid and (t.gridCellsHaveOneColor or t.outGridCellsHaveOneColor):
x = recoverGrid(t, x)
if needsRecoloring(originalT):
x = recoverOriginalColors(x, testRels[s])
plot_sample(originalT.testSamples[s], x)
if Utils.incorrectPixels(x, t.testSamples[s].outMatrix.m) == 0:
if Utils.incorrectPixels(x, originalT.testSamples[s].outMatrix.m) == 0:
#print(idx)
print(idx, c.ops)
plot_task(task)
Expand Down
12 changes: 7 additions & 5 deletions submissionFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5939,9 +5939,9 @@ def getPossibleOperations(t, c):

###########################################################################
# Evolve
#if candTask.sameIOShapes and all([len(x)==1 for x in candTask.changedInColors]) and\
#len(candTask.commonChangedInColors)==1 and candTask.sameNSampleColors:
# x.append(getBestEvolve(candTask))
if candTask.sameIOShapes and all([len(x)==1 for x in candTask.changedInColors]) and\
len(candTask.commonChangedInColors)==1 and candTask.sameNSampleColors:
x.append(getBestEvolve(candTask))

if candTask.sameIOShapes and all([len(x)==1 for x in candTask.changedInColors]) and\
len(candTask.commonChangedInColors)==1:
Expand Down Expand Up @@ -6107,6 +6107,7 @@ def getPossibleOperations(t, c):
x.append(partial(cropOnlyMulticolorShape, diagonals=False))
if all([len(s.inMatrix.multicolorDShapes)==1 for s in candTask.trainSamples+candTask.testSamples]):
x.append(partial(cropOnlyMulticolorShape, diagonals=True))
"""
if all([len(sample.inMatrix.fullFrames)==1 for sample in candTask.trainSamples+candTask.testSamples]):
x.append(partial(cropFullFrame))
x.append(partial(cropFullFrame, includeBorder=False))
Expand All @@ -6115,6 +6116,7 @@ def getPossibleOperations(t, c):
x.append(partial(cropFullFrame, bigOrSmall="small"))
x.append(partial(cropFullFrame, bigOrSmall="big", includeBorder=False))
x.append(partial(cropFullFrame, bigOrSmall="small", includeBorder=False))
"""

return x

Expand Down Expand Up @@ -6440,8 +6442,8 @@ def tryOperations(t, c, firstIt=False):
for op in c.ops:
if x is not None:
newX = op(Matrix(x))
if t.sameIOShapes and len(t.unchangedColors) != 0:
x = correctFixedColors(x, newX, t.unchangedColors)
if t2.sameIOShapes and len(t2.fixedColors) != 0:
x = correctFixedColors(x, newX, t2.fixedColors)
else:
x = newX.copy()
if t.hasUnchangedGrid and (t.gridCellsHaveOneColor or t.outGridCellsHaveOneColor):
Expand Down

0 comments on commit 61e7507

Please sign in to comment.