-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Matrix sparsity and typed matrix sparsity implementation
- Loading branch information
Showing
4 changed files
with
145 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
metrics/src/main/java/hu/bme/mit/ga/metrics/impl/simple/MatrixSparsity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package hu.bme.mit.ga.metrics.impl.simple; | ||
|
||
import hu.bme.mit.ga.adapters.GraphAdapter; | ||
import hu.bme.mit.ga.adapters.GraphIndexer; | ||
import hu.bme.mit.ga.base.data.ScalarData; | ||
import hu.bme.mit.ga.metrics.AbstractGraphMetric; | ||
import org.ejml.data.DMatrixRMaj; | ||
import org.ejml.data.DMatrixSparseCSC; | ||
import org.ejml.data.DMatrixSparseTriplet; | ||
import org.ejml.ops.ConvertDMatrixStruct; | ||
import org.ejml.simple.SimpleMatrix; | ||
import org.ejml.sparse.csc.CommonOps_DSCC; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class MatrixSparsity extends AbstractGraphMetric<ScalarData<Double>> { | ||
public MatrixSparsity() { | ||
super("MatrixSparsity", new ScalarData<>()); | ||
} | ||
@Override | ||
protected <N, T> void evaluateAll(GraphAdapter<N, T> adapter) { | ||
GraphIndexer<N, T> indexer = adapter.getIndexer(); | ||
int size = indexer.getSize(); | ||
DMatrixSparseTriplet triplets = indexer.getAdjacencyMatrixEjmlUntyped(); | ||
DMatrixSparseCSC A = ConvertDMatrixStruct.convert(triplets, (DMatrixSparseCSC) null); | ||
DMatrixRMaj rowSum = new DMatrixRMaj(size, 1); | ||
CommonOps_DSCC.sumRows(A,rowSum); | ||
double d = SimpleMatrix.wrap(rowSum).elementSum(); | ||
double n = size*size; | ||
data.setValue(d/n); | ||
} | ||
|
||
@Override | ||
public List<Map<String, Object>> getTsvMaps(String[] header) { | ||
final List<Map<String, Object>> values = new ArrayList<>(); | ||
Map<String, Object> value = new HashMap<>(); | ||
value.put(header[0], "MatrixSparsity"); | ||
value.put(header[1], null); | ||
value.put(header[2], null); | ||
value.put(header[3], data.getValue()); | ||
values.add(value); | ||
return values; | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
metrics/src/main/java/hu/bme/mit/ga/metrics/impl/typed/TypedMatrixSparsity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package hu.bme.mit.ga.metrics.impl.typed; | ||
|
||
import hu.bme.mit.ga.adapters.GraphIndexer; | ||
import hu.bme.mit.ga.base.data.MapData; | ||
import hu.bme.mit.ga.adapters.GraphAdapter; | ||
import hu.bme.mit.ga.metrics.AbstractGraphMetric; | ||
import org.ejml.data.DMatrixRMaj; | ||
import org.ejml.data.DMatrixSparseCSC; | ||
import org.ejml.data.DMatrixSparseTriplet; | ||
import org.ejml.ops.ConvertDMatrixStruct; | ||
import org.ejml.simple.SimpleMatrix; | ||
import org.ejml.sparse.csc.CommonOps_DSCC; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class TypedMatrixSparsity extends AbstractGraphMetric<MapData<String, Double>> { | ||
|
||
public TypedMatrixSparsity() { | ||
super("TypedMatrixSparsity", new MapData<>()); | ||
} | ||
|
||
@Override | ||
protected <N, T> void evaluateAll(GraphAdapter<N, T> adapter) { | ||
for (T type : adapter.getIndexer().getTypes()) { | ||
evaluateT(adapter, type); | ||
} | ||
} | ||
|
||
protected <N, T> void evaluateT(GraphAdapter<N, T> adapter, T type) { | ||
GraphIndexer indexer = adapter.getIndexer(); | ||
int n = adapter.getIndexer().getSize(); | ||
double nonzero = (double)indexer.getTypedEdges().get(type)*2; | ||
double size = n*n; | ||
data.put(type.toString(), nonzero / size); | ||
} | ||
|
||
@Override | ||
public List<Map<String, Object>> getTsvMaps(String[] header) { | ||
final List<Map<String, Object>> values = new ArrayList<>(); | ||
for (String type : data.getValues().keySet()) { | ||
Double value = data.getValues().get(type); | ||
Map<String, Object> row = new HashMap<>(); | ||
row.put(header[0], "TypedMatrixSparsity"); | ||
row.put(header[1], type); | ||
row.put(header[2], null); | ||
row.put(header[3], value); | ||
values.add(row); | ||
} | ||
return values; | ||
} | ||
} |