ETB Monte Carlo progress

This commit is contained in:
rusEfi 2019-05-04 16:44:34 -04:00
parent 559d7298b7
commit 5490198933
4 changed files with 64 additions and 37 deletions

View File

@ -19,11 +19,25 @@ public abstract class TestSequenceStep {
this.condition = condition; this.condition = condition;
} }
public static int count(TestSequenceStep step) {
int result = 0;
while ((step = step.getNext()) != null)
result++;
return result;
}
public void execute(ScheduledExecutorService executor) { public void execute(ScheduledExecutorService executor) {
doJob(); boolean shouldRun = condition.shouldRunTask();
if (next != null && condition.shouldContinue()) { if (shouldRun)
FileLog.MAIN.logLine("Scheduling " + next + " with " + nextStepDelay + "ms delay"); doJob();
executor.schedule(() -> next.execute(executor), nextStepDelay, TimeUnit.MILLISECONDS); if (next != null) {
if (shouldRun) {
FileLog.MAIN.logLine("Scheduling " + next + " with " + nextStepDelay + "ms delay");
executor.schedule(() -> next.execute(executor), nextStepDelay, TimeUnit.MILLISECONDS);
} else {
// we've skipped current job and we skip the delay as well
next.execute(executor);
}
} else { } else {
MessagesCentral.getInstance().postMessage(TestSequenceStep.class, "ETB test sequence done!"); MessagesCentral.getInstance().postMessage(TestSequenceStep.class, "ETB test sequence done!");
} }
@ -45,7 +59,7 @@ public abstract class TestSequenceStep {
} }
public interface Condition { public interface Condition {
boolean shouldContinue(); boolean shouldRunTask();
Condition YES = () -> true; Condition YES = () -> true;
} }

View File

@ -15,6 +15,7 @@ import java.awt.event.ActionEvent;
* @see ETBPane#SET_ETB * @see ETBPane#SET_ETB
*/ */
public class DirectDrivePanel { public class DirectDrivePanel {
public static final String CANCEL_DIRECT_DRIVE_COMMAND = ETBPane.SET_ETB + "NaN";
private final JPanel content = new JPanel(new BorderLayout()); private final JPanel content = new JPanel(new BorderLayout());
private final JLabel currentOverride = new JLabel("NaN"); private final JLabel currentOverride = new JLabel("NaN");
private final JTextArea increment = new JTextArea("0.5"); private final JTextArea increment = new JTextArea("0.5");
@ -58,7 +59,7 @@ public class DirectDrivePanel {
reset.addActionListener(new AbstractAction() { reset.addActionListener(new AbstractAction() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
CommandQueue.getInstance().write(ETBPane.SET_ETB + "NaN"); CommandQueue.getInstance().write(CANCEL_DIRECT_DRIVE_COMMAND);
} }
}); });

View File

@ -1,5 +1,6 @@
package com.rusefi.ui.etb; package com.rusefi.ui.etb;
import com.rusefi.ETBPane;
import com.rusefi.core.MessagesCentral; import com.rusefi.core.MessagesCentral;
import com.rusefi.core.Sensor; import com.rusefi.core.Sensor;
import com.rusefi.core.SensorCentral; import com.rusefi.core.SensorCentral;
@ -11,9 +12,13 @@ import com.rusefi.io.CommandQueue;
import javax.swing.*; import javax.swing.*;
import java.util.Random; import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import static com.romraider.util.ThreadUtil.sleep;
import static com.rusefi.SensorLogger.getSecondsSinceFileStart; import static com.rusefi.SensorLogger.getSecondsSinceFileStart;
import static com.rusefi.Timeouts.SECOND; import static com.rusefi.Timeouts.SECOND;
import static com.rusefi.etb.TestSequenceStep.count;
import static com.rusefi.ui.etb.DirectDrivePanel.CANCEL_DIRECT_DRIVE_COMMAND;
import static com.rusefi.ui.etb.EtbTestSequence.*; import static com.rusefi.ui.etb.EtbTestSequence.*;
/** /**
@ -33,11 +38,18 @@ public class EtbMonteCarloSequence {
button.addActionListener(e -> { button.addActionListener(e -> {
counter = 0; counter = 0;
// 3000 data points at 10Hz should be 300 seconds worth of data executor.execute(new Runnable() {
StandardTestSequence.metric.start(/* buffer size: */3000, /*period, ms: */ 100); @Override
public void run() {
CommandQueue.getInstance().write(CANCEL_DIRECT_DRIVE_COMMAND);
sleep(3 * SECOND);
// 3000 data points at 10Hz should be 300 seconds worth of data
StandardTestSequence.metric.start(/* buffer size: */3000, /*period, ms: */ 100);
// start first cycle. At the end of the run it would decide if it wants to start from beginning again // start first cycle. At the end of the run it would decide if it wants to start from beginning again
executor.execute(this::runRandomCycle); executor.execute(() -> runRandomCycle());
}
});
}); });
} }
@ -57,21 +69,34 @@ public class EtbMonteCarloSequence {
CommandQueue.getInstance().write("set etb_i " + iFactor); CommandQueue.getInstance().write("set etb_i " + iFactor);
CommandQueue.getInstance().write("set etb_d " + dFactor); CommandQueue.getInstance().write("set etb_d " + dFactor);
MessagesCentral.getInstance().postMessage(EtbMonteCarloSequence.class,
getSecondsSinceFileStart() + " running " + stats);
TestSequenceStep firstStep = new EtbTarget(10 * SECOND, DEFAULT_POSITION, null, TestSequenceStep.Condition.YES); TestSequenceStep firstStep = new EtbTarget(10 * SECOND, DEFAULT_POSITION, null, TestSequenceStep.Condition.YES);
TestSequenceStep.Condition condition = new TestSequenceStep.Condition() { TestSequenceStep.Condition condition = new TestSequenceStep.Condition() {
@Override @Override
public boolean shouldContinue() { public boolean shouldRunTask() {
double currentValue = StandardTestSequence.metric.getStandardDeviation(); double currentValue = StandardTestSequence.metric.getStandardDeviation();
boolean shouldContinue = currentValue < bestResultSoFar; boolean shouldRun = currentValue < bestResultSoFar;
if (!shouldContinue) { if (!shouldRun) {
MessagesCentral.getInstance().postMessage(EtbMonteCarloSequence.class, MessagesCentral.getInstance().postMessage(EtbMonteCarloSequence.class,
"Two much error accumulated, aborting! " + currentValue + " > " + bestResultSoFar); "Two much error accumulated, aborting! " + currentValue + " > " + bestResultSoFar);
} }
return shouldContinue; return shouldRun;
} }
}; };
TestSequenceStep last = StandardTestSequence.addSequence(firstStep, null, condition);
AtomicInteger stepCounter = new AtomicInteger();
AtomicInteger totalSteps = new AtomicInteger();
Runnable onEachStep = () -> SwingUtilities.invokeLater(() -> {
String state = stepCounter.incrementAndGet() + "/" + totalSteps.get();
double value = StandardTestSequence.metric.getStandardDeviation();
MessagesCentral.getInstance().postMessage(EtbMonteCarloSequence.class,"Running " + state + ", current=" + value);
});
TestSequenceStep last = StandardTestSequence.addSequence(firstStep, onEachStep, condition);
last.addNext(new TestSequenceStep(5 * SECOND, EtbTarget.Condition.YES) { last.addNext(new TestSequenceStep(5 * SECOND, EtbTarget.Condition.YES) {
@Override @Override
protected void doJob() { protected void doJob() {
@ -84,6 +109,7 @@ public class EtbMonteCarloSequence {
MessagesCentral.getInstance().postMessage(EtbMonteCarloSequence.class, MessagesCentral.getInstance().postMessage(EtbMonteCarloSequence.class,
getSecondsSinceFileStart() + ":" + stats + ":result:" + cycleResult); getSecondsSinceFileStart() + ":" + stats + ":result:" + cycleResult);
if (counter == TOTAL_CYCLES_COUNT) { if (counter == TOTAL_CYCLES_COUNT) {
CommandQueue.getInstance().write(ETBPane.SET_ETB + 0);
MessagesCentral.getInstance().postMessage(EtbTestSequence.class, "ETB MC sequence done!"); MessagesCentral.getInstance().postMessage(EtbTestSequence.class, "ETB MC sequence done!");
return; return;
} }
@ -92,6 +118,7 @@ public class EtbMonteCarloSequence {
runRandomCycle(); runRandomCycle();
} }
}); });
totalSteps.set(count(firstStep));
firstStep.execute(executor); firstStep.execute(executor);
} }

View File

@ -53,38 +53,23 @@ public class EtbTestSequence {
} }
}; };
Runnable onEachStep = new Runnable() { Runnable onEachStep = () -> SwingUtilities.invokeLater(() -> {
@Override String state = stepCounter.incrementAndGet() + "/" + totalSteps.get();
public void run() { button.setText("Running " + state);
SwingUtilities.invokeLater(new Runnable() { double value = StandardTestSequence.metric.getStandardDeviation();
@Override result.setText(String.format(state + " Result: %.3f", value));
public void run() { });
String state = stepCounter.incrementAndGet() + "/" + totalSteps.get();
button.setText("Running " + state);
double value = StandardTestSequence.metric.getStandardDeviation();
result.setText(String.format(state + " Result: %.3f", value));
}
});
}
};
TestSequenceStep firstStep = new EtbTarget(10 * SECOND, 4, /*position*/onEachStep, TestSequenceStep.Condition.YES); TestSequenceStep firstStep = new EtbTarget(10 * SECOND, 4, /*position*/onEachStep, TestSequenceStep.Condition.YES);
TestSequenceStep result = StandardTestSequence.addSequence(firstStep, onEachStep, TestSequenceStep.Condition.YES); TestSequenceStep result = StandardTestSequence.addSequence(firstStep, onEachStep, TestSequenceStep.Condition.YES);
result.addNext(lastStep); result.addNext(lastStep);
totalSteps.set(count(firstStep)); totalSteps.set(TestSequenceStep.count(firstStep));
firstStep.execute(executor); firstStep.execute(executor);
}); });
} }
private static int count(TestSequenceStep step) {
int result = 0;
while ((step = step.getNext()) != null)
result++;
return result;
}
public JButton getButton() { public JButton getButton() {
return button; return button;
} }