Skip to content
Snippets Groups Projects
Commit 7797f122 authored by Lemming's avatar Lemming
Browse files

1. Port Eclipse-Code zu Android

parent e0066d1b
No related branches found
No related tags found
1 merge request!1Eclipse-code to android studio
......@@ -81,8 +81,6 @@
<orderEntry type="jdk" jdkName="Android API 20 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="commons-math3-3.3" level="project" />
<orderEntry type="library" exported="" name="flanagan" level="project" />
<orderEntry type="library" exported="" name="commons-math3-3.3-javadoc" level="project" />
</component>
</module>
......@@ -22,5 +22,4 @@ android {
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile project(':commons-math3-3.3')
compile project(':commons-math3-3.3-javadoc')
}
......@@ -8,6 +8,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.format.Formatter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
......@@ -19,9 +20,9 @@ import org.apache.commons.math3.analysis.MultivariateFunction;
import org.apache.commons.math3.optimization.PointValuePair;
import org.apache.commons.math3.optimization.direct.NelderMeadSimplex;
import org.apache.commons.math3.optimization.direct.SimplexOptimizer;
import org.apache.commons.math3.util.FastMath;
import org.apache.commons.math3.optimization.*;
import java.text.Format;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
......@@ -36,9 +37,9 @@ public class Empty extends Activity {
private Hashtable<String, Advertiser> ADVERTISERS = new Hashtable<String, Advertiser>();
private ArrayList<Double> s;
private ArrayList<Float> beacon_variance;
private ArrayList<float[]> beacon_positions;
private ArrayList<Float> s = new ArrayList<Float>(); //Distance
private ArrayList<Float> beacon_variance = new ArrayList<Float>(); //Distance-Error
private ArrayList<float[]> beacon_positions = new ArrayList<float[]>(); //Position
private float[] sx;
@Override
......@@ -97,36 +98,96 @@ public class Empty extends Activity {
}
});
// setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
}
/**
* Class get called from nelder-mead
*/
private static class myfunc extends Empty implements MultivariateFunction {
/**
* This should be error-function as of python-code
* @param x
* @return
*/
public double value(double[] x) {
// x - beacon_positions
ArrayList<float[]> advers = super.getBeacon_positions();
List<float[]> keys = new ArrayList(advers);
ArrayList<float[]> tmp = new ArrayList<float[]>();
ArrayList<float[]> advers = getBeacon_positions();
ArrayList<Float> dist = getS();
ArrayList<Float> dist_er = getBeacon_variance();
ArrayList<Double> scaling = new ArrayList<Double>();
List<float[]> keys = new ArrayList<float[]>(advers);
ArrayList<Float> tmp = new ArrayList<Float>();
String dbg = "";
// Print out given x,y,z
/* for (int i = 0; i < x.length; i++) {
dbg += "x["+Integer.toString(i)+"]:" + Double.toString(x[i]) +" ";
}
System.out.println(dbg);
dbg = null;*/
// Python: (x-beacon_positions)**2
for (float[] element: keys) {
// Python: (x-beacon_positions)**2
double new_x = Math.pow(x[0] - element[0], 2);
double new_y = Math.pow(x[1] - element[1], 2);
double new_z = Math.pow(x[2] - element[2], 2);
tmp.add(new float[]{(float) new_x, (float) new_y, (float) new_z});
// Python: (((x-beacon_positions)**2).sum(axis=1))
double sum = new_x + new_y + new_z;
// Python: ((x-beacon_positions)**2).sum(axis=1))**0.5
tmp.add( (float) Math.pow(sum, 0.5) );
}
//tmp is now r
//dbg r
/* for (Float e: tmp) {
System.out.println("r: " + Float.toString(e));
}*/
for(int i = 0; i < tmp.size(); i++) {
// Python: (s-r)**2
double er = Math.pow( dist.get(i) - tmp.get(i), 2 );
//dbg
//System.out.println("error: " + Double.toString(er));
// Python: np.fmin(errors, 0*r+1**2)
er = Math.min(er, 1.);
//dbg
//System.out.println("errors:" + Double.toString(er));
tmp.set(i, (float) er);
}
// tmp is now error
// Scalling
/* for (Double e: dist_er) {
// 1/0.0 will produce "Infinity"
double scaling_tmp = 1/e.doubleValue();
if (scaling_tmp == Double.NEGATIVE_INFINITY || scaling_tmp == Double.POSITIVE_INFINITY) {
scaling_tmp = -0.000000001;
}
scaling.add( scaling_tmp );
}*/
// Sum up with scaling
/* double ret = 0.;
for(int i = 0; i < tmp.size(); i++) {
ret = ret + (tmp.get(i) * scaling.get(i));
}*/
double ret = 0;
for(Float e: tmp) {
ret = ret + e;
}
//System.out.println("ret: "+ Double.toString(ret));
return ret;
}
}
......@@ -157,9 +218,10 @@ public class Empty extends Activity {
}
public float[] vec_sub(float[] a, float[] b) {
float[] c = new float[0];
float[] c = new float[3];
//TODO Exception wenn nicht beide Arrays 3 Elemente
for (int i = 0; i <= a.length; i++) {
for (int i = 0; i <= a.length-1; i++) {
c[i] = a[i] - b[i];
}
......@@ -194,27 +256,33 @@ public class Empty extends Activity {
// gerneriere daten zur simulation
simulationsdaten();
s = null; //Distance
beacon_variance = null; //Distance-Error;
beacon_positions = null;
List<String> keys = new ArrayList(ADVERTISERS.keySet());
for (String e : keys) {
Advertiser cur = ADVERTISERS.get(e);
s.add( (double) cur.get_distance() );
s.add(cur.get_distance());
beacon_variance.add( cur.get_distance_error() );
beacon_positions.add( cur.getPos() );
}
sx = vec_sub(advertiser_min_distance().getPos(), new float[]{0.0f, 0.0f, 0.0f});
sx = vec_sub(advertiser_min_distance().getPos(), new float[]{0.2f, 0.0f, 0.0f});
SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
optimizer.setSimplex(new NelderMeadSimplex(new double[] { 0.2, 0.2 }));
optimizer.setSimplex(new NelderMeadSimplex(new double[] { 0.15, 0.15, 0.15}));
PointValuePair optimum
= optimizer.optimize(100, new myfunc(), GoalType.MINIMIZE, new double[] { -3, 0 });
double p = optimum.getPoint()[0];
= optimizer.optimize(5000, new myfunc(), GoalType.MINIMIZE, new double[] { 3., 3., 0.3});
double x = optimum.getPoint()[0];
double y = optimum.getPoint()[1];
double z = optimum.getPoint()[2];
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, String.format("Magic says x:%f y:%f z:%f", x, y, z), duration).show();
Log.d(LOGTAG, String.format("Magic says x:%f y:%f z:%f", x, y, z));
}
public void simulationsdaten() {
......@@ -231,7 +299,7 @@ public class Empty extends Activity {
}
public ArrayList<Double> getS() {
public ArrayList<Float> getS() {
return s;
}
......
......@@ -6,8 +6,7 @@
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".Empty"
android:id="@+id/">
tools:context=".Empty">
<TextView
android:text="@string/hello_world"
......
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