Skip to content
Snippets Groups Projects
Commit 0d9f3fc2 authored by René Schöne's avatar René Schöne
Browse files

Squashed commit of branch 'learner'

- Fixed pattern to parse datetime coming from openHAB.
- add benchmark
- Create RecognitionEvents in component Plan.
- Add expression printing and fix expression tests.
- Fixing tests.
- Remove build artefacts of datasets module.
- Fixing item behavior.
- Fix bug when adding item using REST API.
  - More debug output for REST application and MQTT receiver
  - Flush cache if new MQTT Host is set
- first integration for test
- update integration activity ML
- add item room_brightness
- add ML Items in starter file
- fix import error
- update newData function
- Fix Learner bugs.
  - Let ExternalMachineLearningModel.check() return true instead of throwing an exception
  - In Learner, use temporary files to store encog model
  - In MachineLearningImpl, do not use result for now (as newData won't work at the moment)
  - Minor fixes for base (add logger, add correct input to jastadd gradle task)
- Progressing to integration of machine learning.
  - Added setKnowledgeBase to MachineLearningEncoder and -Decoder, and removed Root argument in other methods
  - Changed Learner-Impl to match new adapters
  - Updated starter to new adapters, and to use CSV files for initialization of learner
- update readercsv
- Fix imports and dependencies.
- Fix errors with changed MachineLearningEncoder interface.
- upate learner
- add ML interfaces
- implement decode encode
- learner with activity and preference
parent 4221d160
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
#!/usr/bin/env python2.7
import argparse
import os
import shutil
......
/build/
/bin/
logs/
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
apply plugin: 'java'
apply plugin: 'application'
dependencies {
compile project(':eraser-base')
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.8'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.11.1'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.1'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.hamcrest', name: 'hamcrest-junit', version: '2.0.0.0'
}
run {
mainClassName = 'de.tudresden.inf.st.eraser.ml_test_boqi.Main'
standardInput = System.in
if (project.hasProperty("appArgs")) {
args Eval.me(appArgs)
}
}
sourceSets {
main {
java {
srcDir 'src/main/java'
}
}
}
package de.tudresden.inf.st.eraser.ml_test_boqi;
import de.tudresden.inf.st.eraser.jastadd.model.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.commons.math3.stat.StatUtils;
import de.tudresden.inf.st.eraser.jastadd.model.Item;
public class Main {
private static final Logger logger = LogManager.getLogger(Main.class);
public static void main(String[] args) {
logger.info("Hello World!");
createAndTestBrightnessNetwork();
}
private static Root createModel() {
Root model = Root.createEmptyRoot();
Group group = new Group();
group.setID("Group1");
model.addGroup(group);
// inputs items muss normalize 1.0, 0.06666666666666665, 0.4545454545454546, -0.5593220338983051, 1(bias)
NumberItem monthItem = new NumberItem();
monthItem.setState(-1.0);
monthItem.setID("month");
monthItem.setLabel("datetime-month");
NumberItem dayItem = new NumberItem();
dayItem.setState(0.2666666666666666);
dayItem.setID("day");
dayItem.setLabel("datetime-day");
NumberItem hourItem = new NumberItem();
hourItem.setState(-0.6363636363636364);
hourItem.setID("hour");
hourItem.setLabel("datetime-hour");
NumberItem minuteItem = new NumberItem();
minuteItem.setState(-0.5593220338983051);
minuteItem.setID("minute");
minuteItem.setLabel("datetime-minute");
NumberItem biasItem = new NumberItem();
biasItem.setState(1);
biasItem.setID("bias");
biasItem.setLabel("bias");
group.addItem(monthItem);
group.addItem(dayItem);
group.addItem(hourItem);
group.addItem(minuteItem);
group.addItem(biasItem);
return model;
}
private static final int REPETITIONS = 1;
private static void classifyTimed(
NeuralNetworkRoot nn,
Function<NeuralNetworkRoot, DoubleNumber> classify,
Function<DoubleNumber, String> leafToString) {
List<String> results = new ArrayList<>();
List<Long> times = new ArrayList<>();
long before = System.nanoTime();
DoubleNumber classification = classify.apply(nn);
long diff = System.nanoTime() - before;
results.add(leafToString.apply(classification));
times.add(TimeUnit.NANOSECONDS.toMillis(diff));
logger.info("Classification results: {}", results);
logger.info("Took {}ms", String.join("ms, ", times.stream().map(l -> Long.toString(l)).collect(Collectors.toList())));
logger.info("Took on average: {}ms",
Arrays.stream(times.toArray(new Long[0])).mapToLong(l -> l).average().orElse(-1));
logger.info("Took on median: {}ms",
Arrays.stream(times.toArray(new Long[0])).mapToLong(l -> l).sorted()
.skip((REPETITIONS - 1) / 2).limit(2 - REPETITIONS % 2).average().orElse(Double.NaN));
}
/**
* Purpose: Create a neural network with 3 layers (5 + 8 + 4 neurons)
*/
private static void createAndTestBrightnessNetwork() {
Root model = createModel();
Item monthItem = model.resolveItem("month").orElseThrow(
() -> new RuntimeException("Month not found"));
Item dayItem = model.resolveItem("day").orElseThrow(
() -> new RuntimeException("Day not found"));
Item hourItem = model.resolveItem("hour").orElseThrow(
() -> new RuntimeException("Hour not found"));
Item minuteItem = model.resolveItem("minute").orElseThrow(
() -> new RuntimeException("Minute not found"));
Item biasItem = model.resolveItem("bias").orElseThrow(
() -> new RuntimeException("Bias not found"));
NeuralNetworkRoot nn = new NeuralNetworkRoot();
DoubleArrayDoubleFunction sigmoid = inputs -> Math.signum(Arrays.stream(inputs).sum());
DoubleArrayDoubleFunction tanh= inputs ->Math.tanh(Arrays.stream(inputs).sum());
DoubleArrayDoubleFunction function_one= inputs->function_one();
//Weights outputs from learner Module
ArrayList<Double> weights= new ArrayList<Double>(Arrays.asList(
-4.8288886204,0.6723236931,2.1451097188,-0.8551053267,-0.7858304445,4.1369566727,-3.3096691918,
-0.2190980261,2.6871317298,1.2272772167,-2.5292510941,-1.2860407542,-4.2280191541,1.004752063,
0.8345207039,0.0123185817,-0.5921808915,0.0967336988,-0.305892589,0.5572392781,-0.7190098073,
-1.6247354373,0.4589248822,-0.0269816271,2.2208040852,-3.6281085698,0.2204999381,4.7263701556,
-4.8348948698,0.231141867,8.7120706018,-1.4912707741,0.9482851705,0.1377551973,-6.6525856465,
-1.321197315,-2.7369948929,17.664289214,-3.1279212743,-0.8245974167,-1.4251924355,0.8370511414,
2.0841638143,-0.210152817,-1.9414132298,-1.7973688846,-2.1977997794,-3.6046836685,-3.3403186721,
-6.1556924635,-2.8952903587,-1.0773989561,0.2300429028,-0.2184650371,0.0297181797,0.5709092417,
1.3960358442,-3.1577981239,0.0423944625,-17.8143314027,-1.4439317172,-0.5137688896,1.0166045804,
0.3059149818,1.0938282764,0.6203368549,0.702449827));
// input layer
InputNeuron month = new InputNeuron();
month.setItem(monthItem);
InputNeuron day = new InputNeuron();
day.setItem(dayItem);
InputNeuron hour = new InputNeuron();
hour.setItem(hourItem);
InputNeuron minute = new InputNeuron();
minute.setItem(minuteItem);
InputNeuron bias = new InputNeuron();
bias.setItem(biasItem);
nn.addInputNeuron(month);
nn.addInputNeuron(day);
nn.addInputNeuron(hour);
nn.addInputNeuron(minute);
nn.addInputNeuron(bias);
// output layer
OutputLayer outputLayer = new OutputLayer();
OutputNeuron output0 = new OutputNeuron();
output0.setActivationFormula(tanh);
OutputNeuron output1 = new OutputNeuron();
output1.setActivationFormula(tanh);
OutputNeuron output2 = new OutputNeuron();
output2.setActivationFormula(tanh);
OutputNeuron output3 = new OutputNeuron();
output3.setActivationFormula(tanh);
outputLayer.addOutputNeuron(output0);
outputLayer.addOutputNeuron(output1);
outputLayer.addOutputNeuron(output2);
outputLayer.addOutputNeuron(output3);
outputLayer.setCombinator(inputs->predictor(inputs));
nn.setOutputLayer(outputLayer);
// hidden layer
HiddenNeuron[] hiddenNeurons = new HiddenNeuron[8];
for (int i = 0; i < (hiddenNeurons.length); i++) {
if (i==7){
HiddenNeuron hiddenNeuron = new HiddenNeuron();
hiddenNeuron.setActivationFormula(function_one);
hiddenNeurons[i] = hiddenNeuron;
nn.addHiddenNeuron(hiddenNeuron);
bias.connectTo(hiddenNeuron,1.0);
hiddenNeuron.connectTo(output0, weights.get(i));
hiddenNeuron.connectTo(output1, weights.get(i+8));
hiddenNeuron.connectTo(output2, weights.get(i+8*2));
hiddenNeuron.connectTo(output3, weights.get(i+8*3));
}
else{
HiddenNeuron hiddenNeuron = new HiddenNeuron();
hiddenNeuron.setActivationFormula(tanh);
hiddenNeurons[i] = hiddenNeuron;
nn.addHiddenNeuron(hiddenNeuron);
month.connectTo(hiddenNeuron, weights.get((hiddenNeurons.length*4)+i*5));
day.connectTo(hiddenNeuron, weights.get((hiddenNeurons.length*4+1)+i*5));
hour.connectTo(hiddenNeuron, weights.get((hiddenNeurons.length*4+2)+i*5));
minute.connectTo(hiddenNeuron, weights.get((hiddenNeurons.length*4+3)+i*5));
bias.connectTo(hiddenNeuron,weights.get((hiddenNeurons.length*4+4)+i*5));
hiddenNeuron.connectTo(output0, weights.get(i));
hiddenNeuron.connectTo(output1, weights.get(i+8));
hiddenNeuron.connectTo(output2, weights.get(i+8*2));
hiddenNeuron.connectTo(output3, weights.get(i+8*3));}
}
model.getMachineLearningRoot().setPreferenceLearning(nn);
System.out.println(model.prettyPrint());
classifyTimed(nn, NeuralNetworkRoot::classify,
classification -> Double.toString(classification.number));
}
private static double function_one() {
return 1.0;
}
private static double predictor(double[] inputs) {
int index=0;
double maxinput=StatUtils.max(inputs);
System.out.println(inputs);
for (int i = 0; i < inputs.length; i++)
{
if (inputs[i] == maxinput){
index=i;
}
}
//outputs from learner
ArrayList<Double> outputs= new ArrayList<Double>(Arrays.asList(2.0,1.0,3.0,0.0));
double output=outputs.get(index);
return output;
}
}
//inputs:
//[BasicMLData:-1.0,0.2666666666666666,-0.6363636363636364,-0.5593220338983051]
//outputs:
//[BasicMLData:-0.9151867668336432,-0.1568555041251098,-0.9786996639280675,-0.9436628188408074]
//[7, 20, 12, 13] -> predicted: 1(correct: 2)
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console">
<PatternLayout pattern="%highlight{%d{HH:mm:ss.SSS} %-5level} %c{1.} - %msg%n"/>
</Console>
<RollingFile name="RollingFile" fileName="logs/jastadd-mquat.log"
filePattern="logs/jastadd-mquat-%i.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n"/>
<Policies>
<OnStartupTriggeringPolicy/>
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console"/>
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
......@@ -3,6 +3,7 @@ rootProject.name = 'eraser'
include ':eraser-base'
include 'openhab-mock'
include 'integration'
include ':benchmark'
include ':commons.color'
include ':skywriter-hue-integration'
include ':org.openhab.action.machinelearn'
......@@ -19,4 +20,5 @@ include ':feedbackloop.learner'
include ':influx_test'
include ':eraser.spark'
include ':eraser.starter'
include ':integration_test'
include ':feedbackloop.learner_backup'
include ':datasets'
ThingType: id="hue:bridge" label="Hue Bridge" description="The hue bridge represents the Philips hue bridge." parameters=[] channelTypes=[] ;
ThingType: id="hue:0200" label="Color Light" description="A dimmable light with changeable colors." ;
ThingType: id="hue:0210" label="Extended Color Light" description="A dimmable light with changeable colors and tunable color temperature." parameters=[] channelTypes=[];
ThingType: id="skywriter-hat" label="SkyWriterHAT" description="SkyWriterHAT Gesture Recognition" parameters=["brokername"] channelTypes=["flick-type", "skywriter-x", "skywriter-y"] ;
Thing: id="hue:bridge:0017880adcf4" label="Philips hue (10.8.0.160)" type="hue:bridge" channels=[] ;
Thing: id="hue:0210:0017880adcf4:1" label="Wohnzimmer" type="hue:0210" channels=["hue:0210:0017880adcf4:1:color_temperature", "hue:0210:0017880adcf4:1:effect"] ;
Thing: id="hue:0200:0017880adcf4:5" label="Hue iris 1" type="hue:0200" channels=["hue:0210:0017880adcf4:4:color", "hue:0210:0017880adcf4:4:color_temperature", "hue:0210:0017880adcf4:4:alert", "hue:0210:0017880adcf4:4:effect"] ;
Thing: id="hue:0210:0017880adcf4:4" label="Hue go 1" type="hue:0210" channels=["hue:0200:0017880adcf4:5:color", "hue:0200:0017880adcf4:5:alert", "hue:0200:0017880adcf4:5:effect"] ;
Thing: id="skywriter1" label="Our skywriter" type="skywriter-hat" channels=["skywriter1-flick", "skywriter1-x", "skywriter1-y"] ;
ChannelType: id="hue:color" label="Color" description="The color channel allows to control the color of a light. It is also possible to dim values and switch the light on and off." itemType="Color" category="ColorLight" ;
ChannelType: id="hue:alert" label="Alert" description="The alert channel allows a temporary change to the bulb’s state." itemType="String" category="Unknown" ;
ChannelType: id="hue:effect" label="Color Loop" description="The effect channel allows putting the bulb in a color looping mode." itemType="Switch" category="ColorLight" ;
ChannelType: id="hue:color_temperature" label="Color Temperature" description="The color temperature channel allows to set the color temperature of a light from 0 (cold) to 100 (warm)." itemType="Dimmer" category="ColorLight" ;
ChannelType: id="flick-type" itemType="String" label="Last Flick" description="Last Flick detected (and its direction)" category="Motion" readOnly ;
ChannelType: id="skywriter-x" itemType="String" label="Current X coordinate" description="Current X coordinate" category="Motion" readOnly ;
ChannelType: id="skywriter-y" itemType="String" label="Current Y coordinate" description="Current Y coordinate" category="Motion" readOnly ;
Channel: id="hue:0210:0017880adcf4:1:color_temperature" type="hue:color_temperature" links=[] ;
Channel: id="hue:0210:0017880adcf4:1:effect" type="hue:effect" links=[] ;
Channel: id="hue:0210:0017880adcf4:4:color" type="hue:color" links=[] ;
Channel: id="hue:0210:0017880adcf4:4:color_temperature" type="hue:color_temperature" links=[] ;
Channel: id="hue:0210:0017880adcf4:4:alert" type="hue:alert" links=[] ;
Channel: id="hue:0210:0017880adcf4:4:effect" type="hue:effect" links=[] ;
Channel: id="hue:0200:0017880adcf4:5:color" type="hue:color" links=["iris1_item"] ;
Channel: id="hue:0200:0017880adcf4:5:alert" type="hue:alert" links=[] ;
Channel: id="hue:0200:0017880adcf4:5:effect" type="hue:color" links=[] ;
Channel: id="skywriter1-flick" type="flick-type" links=[];
Channel: id="skywriter1-x" type="skywriter-x" links=["skywriter1_x"];
Channel: id="skywriter1-y" type="skywriter-y" links=["skywriter1_y"];
Item: id="iris1_item" label="Iris 1" state="121,88,68" topic="iris1_item/state";
Item: id="skywriter1_x" label="X" state="0" topic="skywriter/x";
Item: id="skywriter1_y" label="Y" state="0" topic="skywriter/y";
//Item: id="skywriter1_xyz" label="Y" state="0" topic="skywriter/xyz";
Group: id="Lights" items=["iris1_item"];
Group: id="Skywriter" items=["skywriter1_x", "skywriter1_y"];
Parameter: id="brokername" label="Broker Name" description="Name of the broker as defined in the &lt;broker&gt;.url in services/mqtt.cfg. See the MQTT Binding for more information on how to configure MQTT broker connections." type="Text" context="service" default="mosquitto" required ;
// MqttHost is set programmatically
Mqtt: incoming="oh2/out/" outgoing="oh2/in/" ;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment