module(...,package.seeall) janelaNull = 0 resoMax = 0 --calculo do ajuste geral function AjusteGeral(ajustesMultiResolucao) local somaPonderada = 0 local somaPesos = 0 local k = 0.1 -- peso dado a janelas de certo tamanho local ajusteTotal for i, janela in ipairs(ajustesMultiResolucao) do somaPonderada = somaPonderada + janela.fitWindow*math.exp((-1)*k*(janela.sizeWindow-1)) somaPesos = somaPesos + math.exp((-1)*k*(janela.sizeWindow-1)) end ajusteTotal = somaPonderada/somaPesos fileTemp = io.open("AjustesNoTempo","a+") fileTemp:write(tostring(2001),",",tostring(ajusteTotal),"\n") io.close(fileTemp) print("Ajuste Total: ",ajusteTotal) end ------------------------------------------------------------------------------------------ -- inicio: calculo de media e variancia em janelas ------------------------------------------------------------------------------------------ cellMath = {} -- calcula a media dos valores dentro de uma janela de amostragem function cellMath.mediaWindow(cs,sizeWindow,cellPosition,attr) local total = 0 local amount = 0 local position = 0 local idx -- armazena o indice da celula retornado for i = 0 , sizeWindow , 1 do for j = 0 , sizeWindow , 1 do idx = coord2index(cellPosition.x + j, cellPosition.y + i,resoMax) cell = cs.cells[idx] if cell ~= null then total = total + cell[attr] amount = amount + 1 end end end if amount ~= 0 then return total/amount else return 0 end end -- calcula a variancia dos valores dentro de uma janela de amostragem function cellMath.varianceWindow(cs,sizeWindow,cellPosition,attr,media) local total = 0 local amount = 0 local position local idx -- armazena o indice da celula retornado for i = 0 , sizeWindow , 1 do for j = 0 , sizeWindow , 1 do idx = coord2index(cellPosition.x + j, cellPosition.y + i, resoMax) cell = cs.cells[idx] if cell ~= null then total = total + (cell[attr] - media)^2 amount = amount + 1 end end end return math.sqrt(total/(amount-1)) end ------------------------------------------------------------------------------------------ -- final: calculo de media e variancia em janelas ------------------------------------------------------------------------------------------ -- verifica a maior dimensão do espaço celular function selectBiggestAxisCell(cs) local BiggestCell = {["x"] = 0, ["y"] = 0} forEachCell(cs, function(cell) if BiggestCell.x < cell.x then BiggestCell.x = cell.x end if BiggestCell.y < cell.y then BiggestCell.y = cell.y end end ) local Dimension if BiggestCell.x == BiggestCell.y then Dimension = BiggestCell.x elseif BiggestCell.x > BiggestCell.y then Dimension = BiggestCell.x else Dimension = BiggestCell.y end return Dimension end -------------------------------funções------------------------------- -- calcula o ajuste em uma determinada janela de tamanho "size_window" function WindowfitCalculator(cs1,attr1,cs2, attr2, size_window, cellPosition, amount_window) local ssd = 0 local visited_cells = 0 local term1, term2 local mediaCell1, mediaCell2 local varCell1, varCell2 --cellMath = require "StatisticCell" if size_window ~= 0 then mediaCell1 = cellMath.mediaWindow(cs1,size_window,cellPosition,attr1) mediaCell2 = cellMath.mediaWindow(cs2,size_window,cellPosition,attr2) varCell1 = cellMath.varianceWindow(cs1,size_window,cellPosition,attr1,mediaCell1) varCell2 = cellMath.varianceWindow(cs2,size_window,cellPosition,attr2,mediaCell2) else mediaCell1 = 1 mediaCell2 = 1 end if mediaCell1 ~= 0 and mediaCell2 ~= 0 then for i = 0 , size_window , 1 do for j = 0 , size_window , 1 do idx = coord2index(cellPosition.x + j, cellPosition.y + i, 33) cell_1 = cs1.cells[idx] if cell_1 ~= nil then -- então cell_2 também será visited_cells = visited_cells + 1 cell_2 = cs2.cells[idx] if (cell_1[attr1] ~= 0 and cell_2[attr2]~=0) then if size_window == 0 then ssd = (cell_1[attr1] - cell_2[attr2])^2 else term1 = (cell_1[attr1] - mediaCell1)/varCell1 term2 = (cell_2[attr2] - mediaCell2)/varCell2 -- ssd = sum of squares of differences ssd = ssd + (term1 - term2)^2 end end end end end end if visited_cells == 0 then janelaNull = janelaNull + 1 return 0, amount_window else amount_window = amount_window + 1 return math.sqrt(ssd),amount_window end end -- função que faz transcorrer a janela de amostragem pelo espaço celular function WindowRun(cs1, attr1, cs2, attr2, size_window, reso_max) local i, j local cell_pointer, horizontal_limit, vertical_limit local fits_sum = 0 local amount_window = 0 local fit_add local janelas = 0 local resoStartI = 0 local resoMaxI = reso_max local resoStartJ = 0 local resoMaxJ = reso_max -- o metodo retorna x como sendo coluna e y como sendo linha -- então x irá receber j, que faz a coluna andar -- y recebera i, que faz a linha andar -- local resoStartI = 5 -- local resoMaxI = 20 -- -- local resoStartJ = 8 -- local resoMaxJ = 21 for i = resoStartI, resoMaxI, 1 do for j = resoStartJ, resoMaxJ, 1 do cell_pointer = {x = j , y = i} fit_add, amount_window = WindowfitCalculator(cs1, attr1, cs2, attr2, size_window - 1, cell_pointer, amount_window) fits_sum = fits_sum + fit_add -- reconhece que encostou na lateral vertical_limit = j + size_window if vertical_limit > resoMaxJ then break end end -- reconhece que encostou no fundo horizontal_limit = i + size_window -- reconhece que chegou ao fim do espaço celular, e retorna o tamanho da janela com -- a média dos ajustes das janelas if horizontal_limit > resoMaxI then print("|",size_window,"|", amount_window,"|", janelaNull,"|") janelaNull = 0 return size_window, fits_sum/amount_window end end end -- gera o grafico dos ajustes function graphGenerator(tableOfFits) local cell_fit = Cell{size = 0 , fit = 0} cell_fit:createObserver( OBSERVERS.GRAPHIC, {"fit","size"}, {"Multiple Resolution","Fit's Curve","Window Size","Fit"} ) print("\n","\n") print("| Window Size","| Window fit |") print("-----------------------------------------") for i, window in ipairs(tableOfFits) do cell_fit.size = window.sizeWindow cell_fit.fit = window.fitWindow print("|",window.sizeWindow,"|", tostring(window.fitWindow),"|") cell_fit:notifyObservers() end end function multipleResolutionMethod(cs1, attr1, cs2, attr2) --verifica compatibilidade entre tamanho dos espaços celulares if cs1:size() ~= cs2:size() then print("Error: Data bases with diferrent sizes") return nil end resoMax = selectBiggestAxisCell(cs1) -- armazena tamanho do maior eixo do cs local tableOfFits = {} -- tabela que armazena os coeficientes de correspondencia local size_window local size,fit -- recebe o tamanho da janela de amostragem atual e o coeficiente correspondente print("Estimating agreement between celular spaces. Please, wait a moment...") -- resolução maxima é reso_max+1 pois a contagem da posição das celulas inicia -- de 0 nos dois eixos. assim o número de tamanhos de janelas de comparação é resolução mais -- 1 que corresponde à posição 0 no cs. temp = 0 print("|size_window","|amount_window","|janelaNull","|") print("-----------------------------------------------") for size_window = 1, resoMax + 1 , 1 do size, fit = WindowRun(cs1, attr1, cs2, attr2, size_window, resoMax) tableOfFits[size_window] = {sizeWindow = size, fitWindow = fit} end graphGenerator(tableOfFits) AjusteGeral(tableOfFits) return tableOfFits end