Skip to content
Snippets Groups Projects
Commit 6b33e451 authored by François Pitois's avatar François Pitois
Browse files

SORTING speed-up

parent a6d3b303
No related branches found
No related tags found
No related merge requests found
Showing
with 250 additions and 10 deletions
#!/bin/bash #!/bin/bash
date "+%T"
cp $1 build/graph.graph cp $1 build/graph.graph
python3 src/part1/unified.py python3 src/part1/unified.py
java -classpath src/part2/GraphCompress/out/production/GraphCompress Main java -classpath src/part2/GraphCompress/out/production/GraphCompress Main
python3 src/part3/encode.py python3 src/part3/encode.py
# python3 src/part4/draw.py date "+%T"
<?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
......
...@@ -3,6 +3,5 @@ ...@@ -3,6 +3,5 @@
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../../../../.." vcs="Git" /> <mapping directory="$PROJECT_DIR$/../../../../.." vcs="Git" />
<mapping directory="$PROJECT_DIR$/../../../../../" vcs="Git" /> <mapping directory="$PROJECT_DIR$/../../../../../" vcs="Git" />
<mapping directory="$PROJECT_DIR$/../../.." vcs="Git" />
</component> </component>
</project> </project>
\ No newline at end of file
No preview for this file type
File added
No preview for this file type
File added
File added
File added
...@@ -57,4 +57,11 @@ public class CompressMath { ...@@ -57,4 +57,11 @@ public class CompressMath {
return term1 + term2 + term3; return term1 + term2 + term3;
} }
public static int arithmeticSubSum(Pic[] picList){
int s = 0;
for (Pic pic : picList){
s += pic.arithmeticSub();
}
return s;
}
} }
import java.util.ArrayList;
public class Greedy {
String path;
Graph graph;
ArrayList<Structure> structList;
ArrayList<Integer> bestStructList;
ArrayList<Score> score; // size of score should always be (size of structList - size of bestStructList)
public Greedy(String path){
this.path = path;
String graphFilename = path + "graph.graph";
String structFilename = path + "struct.txt";
this.graph = Graph.fromFile(graphFilename);
this.structList = Structure.listFromFile(structFilename);
this.bestStructList= new ArrayList<Integer>();
this.score = new ArrayList<Score>();
}
public void updateScore(){
int i;
int n = structList.size();
Structure struct;
int currentDiskSpace;
this.score.clear();
for (i = 0; i < n; i++) {
if (!bestStructList.contains(i)) {
struct = structList.get(i);
graph.undoInit();
graph.addStructure(struct);
currentDiskSpace = graph.diskSpace();
this.score.add(new Score(i, currentDiskSpace, struct));
graph.undoAction();
}
}
this.score.sort(Score.C);
}
public void addBestStructure(){
int i = 0;
int j = this.bestStructList.size();
int n = this.score.size();
Structure struct;
int structIndex;
int currentDiskSpace;
int bestCompression = graph.diskSpace();
while (i < n) {
struct = score.get(i).getStruct();
structIndex = score.get(i).getIndex();
graph.undoInit();
graph.addStructure(struct);
currentDiskSpace = graph.diskSpace();
if (currentDiskSpace < bestCompression) {
bestCompression = currentDiskSpace;
bestStructList.add(structIndex);
graph.addStructure(struct);
System.out.println("> Selecting structure " + structIndex + " as n°" + j + ", estimate size is " + graph.diskSpace() + " bits");
j++;
i++;
} else {
graph.undoAction();
i = n; // break
}
}
}
public void run(){
String partFilename = this.path + "partition.txt";
System.out.println("Part 2");
System.out.println("> Without structure, estimate size is " + graph.diskSpace() + " bits");
int prev_size = -1;
int new_size = 0;
while(prev_size < new_size){
prev_size = bestStructList.size();
System.out.println("> Updating scores...");
this.updateScore();
this.addBestStructure();
new_size = bestStructList.size();
}
FileIO.write(graph.getPartition().toFormattedString(), partFilename);
}
}
import java.io.File;
import java.sql.SQLOutput;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
public class Main { public class Main {
public static void test1() {
int n = 10;
String edgeString = "0,1;0,2;0,3;1,2;1,3;2,3;2,4;2,5;3,5;4,6";
Graph graph = new Graph(Edge.parseString(edgeString), n);
Structure s1 = new Structure("fc 0 1 2 3");
Structure s2 = new Structure("fc 2 3 4 5");
graph.addStructure(s1);
System.out.println(graph.toString());
Graph copyGraph = graph.clone();
copyGraph.undoInit();
copyGraph.addStructure(s2);
System.out.println(graph.toString());
System.out.println(copyGraph.toString());
copyGraph.undoAction();
System.out.println(graph.toString());
System.out.println(copyGraph.toString());
}
public static void test2(){
String graphName;
graphName = "as-oregon";
graphName = "choc";
String path;
path = "/home/pitois/Documents/these/thesis/prog/compress/";
path = "C:/Users/Pit/Documents/Boulot/Thèse/travail/thesis/prog/compress/";
path = "../compress/";
String graphFilename = path + "graph/" + graphName + ".graph";
String structFilename = path + "struct/" + graphName + ".txt";
Graph graph = Graph.fromFile(graphFilename);
ArrayList<Structure> structList = Structure.listFromFile(structFilename);
int i, n;
Structure struct;
n = structList.size();
for (i=0; i<n; i++){
struct = structList.get(i);
graph.addStructure(struct);
}
ArrayList<Edge> edgeList = Edge.readFile(graphFilename);
Graph graph2 = Graph.fromFile(graphFilename);
for (i=0; i<n; i++){
struct = structList.get(i);
graph2.undoInit();
graph2.addStructure(struct);
graph2.undoAction();
graph2.addStructure(struct);
System.out.println(i + ": " + graph2.diskSpace());
}
System.out.println(graph.check(edgeList));
System.out.println(graph2.check(edgeList));
int row, column;
for (Block block: graph.getBlockMatrix()){
row = block.getRow();
column = block.getColumn();
if(!graph2.getBlockMatrix().hasValue(row, column)){
System.out.println("Missing: " + row + ", " + column);
}
}
//System.out.println(i + ": " + graph.diskSpace());
//System.out.println(i + ": " + graph2.diskSpace());
//System.out.println(graph);
//System.out.println(graph.getPartition());
//System.out.println(graph2);
//System.out.println(graph2.getPartition());
/*
Iterator<Block> iterator = graph.getBlockMatrix().iterator();
Iterator<Block> iterator2 = graph2.getBlockMatrix().iterator();
Block block;
Block block2;
for (; iterator.hasNext() && iterator2.hasNext();) {
block = iterator.next();
block2 = iterator2.next();
System.out.println(block);
System.out.println(block2);
System.out.println(block.equals(block2));
}
*/
}
public static void greedy(){ public static void greedy(){
String path; String path;
path = "build/"; path = "build/";
// path = "../../../build/";
String graphFilename = path + "graph.graph"; String graphFilename = path + "graph.graph";
String structFilename = path + "struct.txt"; String structFilename = path + "struct.txt";
String partFilename = path + "partition.txt"; String partFilename = path + "partition.txt";
Graph graph = Graph.fromFile(graphFilename); Graph graph = Graph.fromFile(graphFilename);
ArrayList<Structure> structList = Structure.listFromFile(structFilename); ArrayList<Structure> structList = Structure.listFromFile(structFilename);
int i, n; int i, k, n, m;
n = structList.size(); n = structList.size();
m = n / 10;
m = n;
Structure struct; Structure struct;
ArrayList<Integer> bestStructList= new ArrayList<Integer>(); ArrayList<Integer> bestStructList= new ArrayList<Integer>();
int bestCompression = 10*graph.diskSpace(); int bestCompression = 10*graph.diskSpace();
...@@ -25,6 +108,7 @@ public class Main { ...@@ -25,6 +108,7 @@ public class Main {
int j = 0; int j = 0;
while (true) { while (true) {
bestStructure = -1; bestStructure = -1;
k = 0;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
if (!bestStructList.contains(i)) { if (!bestStructList.contains(i)) {
struct = structList.get(i); struct = structList.get(i);
...@@ -34,6 +118,13 @@ public class Main { ...@@ -34,6 +118,13 @@ public class Main {
if (currentDiskSpace < bestCompression) { if (currentDiskSpace < bestCompression) {
bestCompression = currentDiskSpace; bestCompression = currentDiskSpace;
bestStructure = i; bestStructure = i;
k = 0;
//System.out.println(i);
} else {
k++;
if (k > m){
i = n; // break
}
} }
graph.undoAction(); graph.undoAction();
} }
...@@ -41,18 +132,25 @@ public class Main { ...@@ -41,18 +132,25 @@ public class Main {
if(bestStructure >= 0){ if(bestStructure >= 0){
bestStructList.add(bestStructure); bestStructList.add(bestStructure);
graph.addStructure(structList.get(bestStructure)); graph.addStructure(structList.get(bestStructure));
System.out.println("> Selecting structure " + bestStructure + ", estimate size is " + graph.diskSpace() + " bits"); System.out.println("> Selecting structure " + bestStructure + " as n°" + j + ", estimate size is " + graph.diskSpace() + " bits");
//FileIO.write(graph.getPartition().toFormattedString(), path + "partition" + j + ".txt");
j++; j++;
} else { } else {
break; break;
} }
} }
//System.out.println(graph.check(edgeList));
FileIO.write(graph.getPartition().toFormattedString(), partFilename); FileIO.write(graph.getPartition().toFormattedString(), partFilename);
} }
public static void main(String[] args) { public static void main(String[] args) {
greedy(); /* run the old algorithm without speed-up */
// greedy();
/* run the new algorithm with SORTING speed-up */
Greedy g = new Greedy("build/");
g.run();
} }
} }
\ No newline at end of file
public class Pic {
private final int nbOne;
private final int nbPixel;
public Pic(int nbOne, int nbPixel) {
this.nbOne = nbOne;
this.nbPixel = nbPixel;
}
public int getNbOne() {
return nbOne;
}
public int getNbPixel() {
return nbPixel;
}
public int arithmeticSub(){
return CompressMath.arithmeticSub(this.getNbOne(), this.getNbPixel());
}
}
import java.util.Comparator;
public class Score {
private int index;
private int score;
private Structure struct;
public static final Comparator<? super Score> C = new Comparator<Score>(){
@Override
public int compare(Score lhs, Score rhs){
return lhs.score > rhs.score ? 1 : (lhs.score < rhs.score ? -1 : 0);
}
} ;
public Score(int index, int score, Structure struct){
this.index = index;
this.score = score;
this.struct = struct;
}
public Structure getStruct(){return struct;}
public int getIndex(){return index;}
@Override
public String toString() {
return "(" + this.index + ", " + this.score + ")";
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment