Commit 14747a34 authored by Markus Krug's avatar Markus Krug
Browse files

potentially completed all complex steps, yet untested

parent 6e3d86c6
......@@ -4,9 +4,9 @@ import java.awt.Point;
import java.util.List;
import de.uniwue.ls6.util.MatrixUtil;
import no.uib.cipr.matrix.MatrixEntry;
import no.uib.cipr.matrix.sparse.FlexCompColMatrix;
public class Instance {
/*
......@@ -60,23 +60,52 @@ public class Instance {
return true;
}
//erform kronecker expansion
// perform kronecker expansion
public FlexCompColMatrix expand(List<MatrixMapping> mappings) {
MatrixMapping lastMapping = mappings.get(mappings.size()-1);
for(Point denseIndices : lastMapping.getMappingMap().keySet()){
MatrixMapping lastMapping = mappings.get(mappings.size() - 1);
// create dense matrix for instance
int denseDimension = lastMapping.getDenseMatrixDimension();
FlexCompColMatrix denseInstanceMatrix = new FlexCompColMatrix(denseDimension, denseDimension);
for (Point denseIndices : lastMapping.getMappingMap().keySet()) {
List<Point> features = MatrixUtil.determineFeaturesForIndex(denseIndices, mappings);
//check if this instance contains the features
if(containsFeature(features)){
// check if this instance contains the features
if (containsFeature(features)) {
// if so then create a sprase matrix and put a 1 into the
// according slot
denseInstanceMatrix.add(denseIndices.x, denseIndices.y, 1);
}
}
// perform the kronecker expansion from the non sparse dense elements
int kroneckerDimension = denseDimension * denseDimension;
FlexCompColMatrix expanedMatrix = new FlexCompColMatrix(kroneckerDimension, kroneckerDimension);
for (MatrixEntry e1 : denseInstanceMatrix) {
// iterate over all no sparse elements and expand
for (MatrixEntry e2 : denseInstanceMatrix) {
// skip diagonal expansion
if (e1.equals(e2))
continue;
// calculate the new indices
int kroneckerCol = e1.column() * denseDimension + e2.column();
int kroneckerRow = e1.row() * denseDimension + e2.row();
// check if this feature is available TODO is this already clear
// since we got both single features???
expanedMatrix.add(kroneckerRow, kroneckerCol, 1);
}
//if so then create a sprase matrix and put a 1 into the according slot
//expand the matrix clever (use the index of denseIndices and expand with non sparse elemtns)
}
return null;
// expand the matrix clever (use the index of denseIndices and
// expand with non sparse elemtns)
return expanedMatrix;
}
}
......@@ -9,6 +9,7 @@ public class MatrixMapping {
HashMap<Point, Point> mappingMap;
HashMap<Point, Point> inverseMappingMap;
//
private int kroneckerDimension;
/**
*
......@@ -18,16 +19,18 @@ public class MatrixMapping {
* a point is an index
*/
public MatrixMapping(HashMap<Point, Point> mappingMap, HashMap<Point, Point> inverseMap) {
public MatrixMapping(HashMap<Point, Point> mappingMap, HashMap<Point, Point> inverseMap,int kroneckerDimension) {
super();
this.mappingMap = mappingMap;
this.inverseMappingMap = inverseMap;
this.kroneckerDimension = kroneckerDimension;
}
public MatrixMapping() {
public MatrixMapping(int kroneckerDimension) {
super();
this.mappingMap = new HashMap<Point, Point>();
this.inverseMappingMap = new HashMap<Point, Point>();
this.kroneckerDimension = kroneckerDimension;
}
public HashMap<Point, Point> getMappingMap() {
......@@ -71,4 +74,10 @@ public class MatrixMapping {
return inverseMappingMap.get(feature);
}
//should return 0 but 42 is also alright
public int getKroneckerMatrixDimension() {
// TODO Auto-generated method stub
return 0;
}
}
......@@ -17,11 +17,17 @@ public class MatrixUtil {
public static MatrixMapping getMappingForMaximum(MatrixMcMatrixFace matrixface, int maximum) {
MatrixMapping matrixMapping = new MatrixMapping();
//totally unugly code not even necessary which makes it worse
int formerMatrixSize = matrixface.getTpMatrix().numRows() == matrixface.getTpMatrix().numColumns()
? matrixface.getTpMatrix().numColumns() : -1;
MatrixMapping matrixMapping = new MatrixMapping(formerMatrixSize);
for (MatrixEntry entry : matrixface.getTpMatrix()) {
double sum = entry.get() + matrixface.getFpMatrix().get(entry.row(), entry.column());
//TODO do we need to add the false positives???
// double sum = entry.get() + matrixface.getFpMatrix().get(entry.row(), entry.column());
double sum = entry.get();
if (sum >= maximum) {
matrixMapping.addEntry(new Point(entry.row(), entry.column()));
}
......@@ -54,47 +60,63 @@ public class MatrixUtil {
return expandedMatrixFace;
}
public static List<Point> determineFeaturesForIndex(Point index,List<MatrixMapping> mappings){
List<Point> reversedfeatures = new ArrayList<Point>(Arrays.asList(new Point[]{index}));
for(int i = mappings.size() - 1; i >= 0; i--){
public static List<Point> determineFeaturesForIndex(Point index, List<MatrixMapping> mappings) {
List<Point> reversedfeatures = new ArrayList<Point>(Arrays.asList(new Point[] { index }));
for (int i = mappings.size() - 1; i >= 0; i--) {
MatrixMapping currentMapping = mappings.get(i);
revertKroneckerExpansionAndMapping(currentMapping,reversedfeatures);
// revertmapping
revertMapping(currentMapping, reversedfeatures);
if (i != 0) {
revertKroneckerExpansion(currentMapping, reversedfeatures);
}
}
return reversedfeatures;
}
private static void revertKroneckerExpansionAndMapping(MatrixMapping currentMapping,
List<Point> reversedfeatures) {
private static void revertKroneckerExpansion(MatrixMapping currentMapping, List<Point> reversedfeatures) {
List<Point> toRemove = new ArrayList<Point>();
List<Point> toAdd = new ArrayList<Point>();
for(Point p : reversedfeatures){
//revert the mapping step this is easy !
Point backwardsMappedFeature = currentMapping.getBackwardsMappedFeature(p);
//revert the kronecker expansion step this generates 2 points
//TODO its not always 4
int sizeBeforeExpansion = 4;
int xBefore1 = (int) Math.floor(backwardsMappedFeature.x/sizeBeforeExpansion);
int xBefore2 = backwardsMappedFeature.x%sizeBeforeExpansion;
int yBefore1 = (int)Math.floor(backwardsMappedFeature.y/sizeBeforeExpansion);
int yBefore2 = backwardsMappedFeature.y%sizeBeforeExpansion;
Point firstBack = new Point(xBefore1,yBefore1);
Point secondBack = new Point(xBefore2,yBefore2);
for (Point p : reversedfeatures) {
// revert the kronecker expansion step this generates 2 points
// TODO is this correct?
int sizeBeforeExpansion = (int) Math.sqrt(currentMapping.getKroneckerMatrixDimension());
int xBefore1 = (int) Math.floor(p.x / sizeBeforeExpansion);
int xBefore2 = p.x % sizeBeforeExpansion;
int yBefore1 = (int) Math.floor(p.y / sizeBeforeExpansion);
int yBefore2 = p.y % sizeBeforeExpansion;
Point firstBack = new Point(xBefore1, yBefore1);
Point secondBack = new Point(xBefore2, yBefore2);
toRemove.add(p);
toAdd.add(secondBack);
toAdd.add(firstBack);
}
reversedfeatures.removeAll(toRemove);
reversedfeatures.addAll(toAdd);
}
private static void revertMapping(MatrixMapping currentMapping, List<Point> reversedfeatures) {
List<Point> toRemove = new ArrayList<Point>();
List<Point> toAdd = new ArrayList<Point>();
for (Point p : reversedfeatures) {
// revert the mapping step this is easy !
Point backwardsMappedFeature = currentMapping.getBackwardsMappedFeature(p);
toRemove.add(p);
toAdd.add(backwardsMappedFeature);
}
reversedfeatures.removeAll(toRemove);
reversedfeatures.addAll(toAdd);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment