Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: AEminium/java2aeminium
base: 30c36bf55d
...
head fork: AEminium/java2aeminium
compare: 8f1e3a9f5e
  • 6 commits
  • 11 files changed
  • 0 commit comments
  • 2 contributors
View
1  .gitignore
@@ -8,3 +8,4 @@
!/compiler/
!/runtime/
!/tests/
+!/signatures/
View
6 build.xml
@@ -10,14 +10,14 @@
<!-- aeminium.runtime -->
<property name="runtime.dir" value="runtime/src" />
- <property name="runtime.jar" value="runtime/AeminiumRuntime.jar" />
+ <property name="runtime.jar" value="runtime/dist/AeminiumRuntime.jar" />
<!-- aeminium.tests -->
<property name="tests.src.dir" value="tests/src" />
<property name="tests.target.dir" value="tests/target" />
<property name="tests.build.dir" value="tests/build" />
<property name="tests.lib.dir" value="tests/lib" />
- <property name="tests.class" value="aeminium.compiler.tests.FFT" />
+ <property name="tests.class" value="Knapsack" />
<property name="tests.jar" value="tests/test.jar" />
<!-- aeminium.tests -->
@@ -66,7 +66,7 @@
</target>
<target name="compile.runtime" depends="">
- <ant dir="runtime" target="jar-release"/>
+ <ant dir="runtime" target="jar"/>
</target>
<target name="translate.tests" depends="compile.compiler,compile.runtime">
View
176 compiler/src/aeminium/compiler/east/EForStatement.java
@@ -0,0 +1,176 @@
+package aeminium.compiler.east;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
+
+import org.eclipse.jdt.core.dom.AST;
+import org.eclipse.jdt.core.dom.Assignment;
+import org.eclipse.jdt.core.dom.Block;
+import org.eclipse.jdt.core.dom.CompilationUnit;
+import org.eclipse.jdt.core.dom.Expression;
+import org.eclipse.jdt.core.dom.FieldAccess;
+import org.eclipse.jdt.core.dom.ForStatement;
+import org.eclipse.jdt.core.dom.IfStatement;
+import org.eclipse.jdt.core.dom.Statement;
+import org.eclipse.jdt.core.dom.WhileStatement;
+
+import aeminium.compiler.DependencyStack;
+import aeminium.compiler.signature.Signature;
+import aeminium.compiler.signature.SignatureItemRead;
+import aeminium.compiler.task.Task;
+
+public class EForStatement extends EStatement
+{
+ protected final EExpression expr;
+ protected final EStatement body;
+ private List<EExpression> initializers = new ArrayList<EExpression>();
+ private List<EExpression> updaters = new ArrayList<EExpression>();
+
+ public EForStatement(EAST east, ForStatement original, EASTDataNode scope,
+ EMethodDeclaration method)
+ {
+ super(east, original, scope, method);
+
+ for (Object o : original.initializers())
+ {
+ this.initializers.add(EExpression.create(east, (Expression) o,
+ scope));
+ }
+ for (Object o : original.updaters())
+ {
+ this.updaters.add(EExpression.create(east, (Expression) o, scope));
+ }
+ this.expr = EExpression.create(east, original.getExpression(), scope);
+ this.body = EStatement.create(east, original.getBody(), scope, method);
+ }
+
+ /* factory */
+ public static EForStatement create(EAST east, ForStatement original,
+ EASTDataNode scope, EMethodDeclaration method)
+ {
+ return new EForStatement(east, original, scope, method);
+ }
+
+ @Override
+ public WhileStatement getOriginal()
+ {
+ return (WhileStatement) this.original;
+ }
+
+ @Override
+ public void checkSignatures()
+ {
+ for (EExpression stmt : this.initializers)
+ stmt.checkSignatures();
+ for (EExpression stmt : this.updaters)
+ stmt.checkSignatures();
+ this.expr.checkSignatures();
+ this.body.checkSignatures();
+
+ this.signature.addItem(new SignatureItemRead(this.expr.getDataGroup()));
+ }
+
+ @Override
+ public Signature getFullSignature()
+ {
+ Signature sig = new Signature();
+
+ sig.addAll(this.signature);
+ sig.addAll(this.expr.getFullSignature());
+ sig.addAll(this.body.getFullSignature());
+ for (EExpression stmt : this.initializers)
+ sig.addAll(stmt.getFullSignature());
+ for (EExpression stmt : this.updaters)
+ sig.addAll(stmt.getFullSignature());
+ return sig;
+ }
+
+ @Override
+ public void checkDependencies(DependencyStack stack)
+ {
+ this.expr.checkDependencies(stack);
+ this.strongDependencies.add(this.expr);
+
+ for (EExpression stmt : this.initializers)
+ {
+ stmt.checkDependencies(stack);
+ this.strongDependencies.add(stmt);
+ }
+
+ Set<EASTExecutableNode> deps = stack.getDependencies(this,
+ this.signature);
+
+ for (EASTExecutableNode node : deps)
+ if (!node.equals(this.expr))
+ this.weakDependencies.add(node);
+
+ DependencyStack copy = stack.fork();
+ this.body.checkDependencies(copy);
+
+ stack.join(copy, this);
+
+ this.children.add(this.body);
+
+ // TODO: this is only valid for the sequential translation used bellow
+ this.children.add(this);
+ }
+
+ @Override
+ public int optimize()
+ {
+ int sum = 0;
+
+ sum += this.expr.optimize();
+ sum += this.body.optimize();
+
+ sum += super.optimize();
+
+ return sum;
+ }
+
+ @Override
+ public void preTranslate(Task parent)
+ {
+ if (this.inlineTask)
+ this.task = parent;
+ else
+ this.task = parent.newSubTask(this, "for");
+
+ this.expr.preTranslate(this.task);
+ this.body.preTranslate(this.task);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public List<Statement> build(List<CompilationUnit> out)
+ {
+ AST ast = this.getAST();
+
+ IfStatement stmt = ast.newIfStatement();
+ stmt.setExpression(this.expr.translate(out));
+
+ Block block = ast.newBlock();
+ block.statements().addAll(this.body.translate(out));
+
+ FieldAccess task_access = ast.newFieldAccess();
+ task_access.setExpression(ast.newThisExpression());
+ task_access.setName(ast.newSimpleName("ae_" + this.task.getName()));
+
+ Assignment assign = ast.newAssignment();
+ assign.setLeftHandSide(task_access);
+ assign.setRightHandSide(this.task.create());
+
+ block.statements().add(ast.newExpressionStatement(assign));
+
+ stmt.setThenStatement(block);
+
+ return Arrays.asList((Statement) stmt);
+ }
+
+ public EASTExecutableNode getBody()
+ {
+ return this.body;
+ }
+}
View
3  compiler/src/aeminium/compiler/east/EStatement.java
@@ -38,6 +38,9 @@ public static EStatement create(EAST east, Statement stmt, EASTDataNode scope, E
if (stmt instanceof WhileStatement)
return EWhileStatement.create(east, (WhileStatement) stmt, scope, method);
+ if (stmt instanceof ForStatement)
+ return EForStatement.create(east, (ForStatement) stmt, scope, method);
+
System.err.println("Not implemented error: " + stmt.getClass().getName());
return null;
2  runtime
@@ -1 +1 @@
-Subproject commit 459f455d6a31298c73f41a91ada58d8feae64739
+Subproject commit 0b989a437f79e062dc88be453f4246ee1633f765
View
47 signatures/signatures
@@ -0,0 +1,47 @@
+9
+Ljava/lang/Math;.cos(D)D
+2
+R P 0
+W R
+
+Ljava/lang/Math;.sin(D)D
+2
+R P 0
+W R
+
+Ljava/lang/Math;.hypot(DD)D
+2
+R P 0
+W R
+
+Ljava/lang/Math;.atan2(DD)D
+2
+R P 0
+W R
+
+Ljava/lang/Math;.exp(D)D
+2
+R P 0
+W R
+
+Ljava/lang/Math;.cosh(D)D
+2
+R P 0
+W R
+
+Ljava/lang/Math;.sinh(D)D
+2
+R P 0
+W R
+
+Ljava/util/Random;.nextDouble()D
+3
+R T
+W T
+W R
+
+Ljava/io/PrintStream;.println(Ljava/lang/Object;)V
+2
+R P 0
+W T
+
View
15 tests/src/ComparatorOnFitness.java
@@ -0,0 +1,15 @@
+import java.util.Comparator;
+
+public class ComparatorOnFitness implements Comparator<Integer> {
+
+ double[] popFit;
+ public ComparatorOnFitness(double[] ft) {
+ popFit = ft;
+ }
+
+ public int compare(Integer a, Integer b) {
+ if (popFit[b] == popFit[a]) return 0;
+ if (popFit[b] - popFit[a] > 0 ) return 1;
+ return -1;
+ }
+}
View
25 tests/src/Indiv.java
@@ -0,0 +1,25 @@
+public class Indiv implements Comparable<Indiv> {
+ public boolean[] has;
+
+ public double fitness = 0;
+ public int size = 0;
+
+ public Indiv(int size) {
+ has = new boolean[size];
+ }
+
+ public void add(int w, boolean h) {
+ has[size] = h;
+ size++;
+ }
+
+ public int compareTo(Indiv other) {
+ if (this.fitness == other.fitness) {
+ return 0;
+ } else if (this.fitness > other.fitness) {
+ return 1;
+ } else {
+ return -1;
+ }
+ }
+}
View
13 tests/src/Item.java
@@ -0,0 +1,13 @@
+
+public class Item {
+
+ public Item(String n, Float w, Float v) {
+ this.name = n;
+ this.weight = w;
+ this.value = v;
+ }
+
+ public String name;
+ public Float weight;
+ public Float value;
+}
View
202 tests/src/Knapsack.java
@@ -0,0 +1,202 @@
+import java.io.BufferedReader;
+import java.io.DataInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStreamReader;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Random;
+import java.util.StringTokenizer;
+
+public class Knapsack {
+ public static int SIZE_LIMIT = 87;
+
+ Random rand = new Random();
+
+ int numberOfItems = 500;
+ Item[] items = Knapsack.readItems("items.txt", numberOfItems);
+
+ int runs = 5;
+ int popSize = 100;
+ int cromSize = numberOfItems;
+
+ double prob_mut = 0.2;
+ double prob_rec = 0.2;
+
+ int numGen = 100000;
+
+
+
+ public void run() {
+ System.out.println("Running");
+ for (int i=0; i<runs; i++) {
+ ga();
+ }
+ System.out.println("Done");
+ }
+
+ /* Genetic Algorithm */
+ public void ga() {
+
+ // Initialize Population
+ Indiv[] pop = new Indiv[popSize];
+ for (int i=0; i< popSize; i++) {
+ pop[i] = genIndiv();
+ pop[i].fitness = evaluate(pop[i]);
+ }
+
+ for (int i=0; i< numGen; i++) {
+ // Sort by lowest Fitness
+ for (int j=0; j< popSize; j++) {
+ pop[j].fitness = evaluate(pop[j]);
+ }
+ Arrays.sort(pop);
+
+ // System.out.println("Best fit:" + pop[2].fitness);
+
+ Indiv[] parents = tournament(pop);
+
+ // Recombination
+ int half = parents.length/2;
+ for (int j = 0; j < half; j++) {
+ if (rand.nextDouble() < prob_rec) {
+ recombine(parents, j, half+j);
+ }
+ }
+
+ // Mutation and Evaluation
+ for (int j = 0; j < parents.length; j++) {
+ if (rand.nextDouble() < prob_mut) {
+ mutate(parents[j]);
+ }
+ }
+
+ pop = parents;
+ }
+
+
+ // Best:
+
+ for (int j=0; j< popSize; j++) {
+ pop[j].fitness = evaluate(pop[j]);
+ }
+ Arrays.sort(pop);
+ Indiv indiv = pop[0];
+ int value = 0;
+ int weight = 0;
+ for (int i=0; i< indiv.size; i++) {
+ if (indiv.has[i]) {
+ value += items[i].value;
+ weight += items[i].weight;
+ }
+ }
+ System.out.println("Best value/weight: " + value + ", " + weight);
+
+ }
+
+ private void mutate(Indiv indiv) {
+ int sw = rand.nextInt(cromSize);
+ indiv.has[sw] = !indiv.has[sw];
+ }
+
+ private void recombine(Indiv[] parents, int a, int b) {
+ Indiv n1 = new Indiv(cromSize);
+ Indiv n2 = new Indiv(cromSize);
+ int cutPos = rand.nextInt(cromSize);
+ for (int k = 0; k < cromSize; k++) {
+ if (k < cutPos) {
+ n1.add(k,parents[a].has[k]);
+ n2.add(k,parents[b].has[k]);
+ } else {
+ n1.add(k,parents[b].has[k]);
+ n2.add(k,parents[a].has[k]);
+ }
+
+ }
+ parents[a] = n1;
+ parents[b] = n2;
+ }
+
+ private Indiv[] tournament(Indiv[] pop) {
+ int tSize = 50;
+ Indiv[] f = new Indiv[popSize];
+ for (int i = 0; i < popSize; i++) {
+ Collections.shuffle(Arrays.asList(pop));
+ f[i] = pop[0];
+ for (int j = 1; j < tSize; j++) {
+ if (pop[j].fitness < f[i].fitness) f[i] = pop[j];
+ }
+ }
+ return f;
+ }
+
+ private int[] phenotype(Indiv indiv) {
+ int value = 0;
+ int weight = 0;
+ for (int i=0; i< indiv.size; i++) {
+ if (indiv.has[i]) {
+ value += items[i].value;
+ weight += items[i].weight;
+ }
+ }
+ return new int[] {value, weight};
+ }
+
+ private double evaluate(Indiv indiv) {
+ int[] ph = phenotype(indiv);
+ int value = ph[0];
+ int weight = ph[1];
+
+ // Evaluation
+ if (weight >= SIZE_LIMIT) {
+ return 2.0;
+ } else {
+ return 1.0/(value); // Minimization problem.
+ }
+
+ }
+
+ private Indiv genIndiv() {
+ while (true) {
+ int c = 0;
+ Indiv e = new Indiv(cromSize);
+ for(int i=0;i<cromSize;i++) {
+ boolean b = ( rand.nextDouble() < 0.01 );
+ e.add(i, b);
+ if (b) c++;
+ }
+ // Avoids empty backpacks
+ if ( c > 0) {
+ return e;
+ }
+ }
+ }
+
+ public static void main(String[] args) {
+ new Knapsack().run();
+ }
+
+ public static Item[] readItems(String fname, int n) {
+ if (!new File(fname).exists())
+ return null;
+
+ Item[] tmp = new Item[n];
+ try {
+ FileInputStream fstream = new FileInputStream(fname);
+ DataInputStream in = new DataInputStream(fstream);
+ BufferedReader br = new BufferedReader(new InputStreamReader(in));
+ String strLine;
+ int i = 0;
+ while ((strLine = br.readLine()) != null) {
+ StringTokenizer tok = new StringTokenizer(strLine, ",");
+ tmp[i++] = new Item(tok.nextToken(), Float.parseFloat(tok.nextToken()), Float.parseFloat(tok.nextToken()));
+ }
+ // Close the input stream
+ in.close();
+ } catch (Exception e) {
+ System.err.println("Error: " + e.getMessage());
+ }
+ return tmp;
+ }
+
+}
View
11 tests/src/aeminium/compiler/tests/SimpleFor.java
@@ -0,0 +1,11 @@
+package aeminium.compiler.tests;
+
+public class SimpleFor {
+ public static void main(String[] args) {
+ int[] arr = new int[20];
+ for (int i=0; i<20; i++) {
+ arr[i] = i * 2;
+ }
+ System.out.println(arr[0] + ", " + arr[1] + ", " + arr[2]);
+ }
+}

No commit comments for this range

Something went wrong with that request. Please try again.