|
@@ -0,0 +1,452 @@
|
|
|
+package exampleAlgorithms;
|
|
|
+
|
|
|
+import java.awt.BorderLayout;
|
|
|
+import java.awt.Component;
|
|
|
+import java.awt.Cursor;
|
|
|
+import java.awt.Dimension;
|
|
|
+import java.awt.FlowLayout;
|
|
|
+import java.awt.Font;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.BufferedWriter;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStreamWriter;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.text.NumberFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Locale;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+import java.util.stream.Stream;
|
|
|
+
|
|
|
+import javax.swing.BorderFactory;
|
|
|
+import javax.swing.ImageIcon;
|
|
|
+import javax.swing.JButton;
|
|
|
+import javax.swing.JCheckBox;
|
|
|
+import javax.swing.JFileChooser;
|
|
|
+import javax.swing.JFormattedTextField;
|
|
|
+import javax.swing.JFrame;
|
|
|
+import javax.swing.JLabel;
|
|
|
+import javax.swing.JOptionPane;
|
|
|
+import javax.swing.JPanel;
|
|
|
+import javax.swing.JProgressBar;
|
|
|
+import javax.swing.JScrollPane;
|
|
|
+import javax.swing.JSplitPane;
|
|
|
+import javax.swing.JTextArea;
|
|
|
+import javax.swing.filechooser.FileNameExtensionFilter;
|
|
|
+import javax.swing.text.NumberFormatter;
|
|
|
+
|
|
|
+import api.Algorithm;
|
|
|
+import classes.AbstractCpsObject;
|
|
|
+import classes.CpsUpperNode;
|
|
|
+import classes.HolonElement;
|
|
|
+import classes.HolonObject;
|
|
|
+import classes.HolonSwitch;
|
|
|
+import ui.controller.Control;
|
|
|
+import ui.model.Model;
|
|
|
+import ui.model.DecoratedHolonObject.HolonObjectState;
|
|
|
+import ui.model.DecoratedGroupNode;
|
|
|
+import ui.model.DecoratedNetwork;
|
|
|
+import ui.model.DecoratedState;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+public class BaseLine implements Algorithm {
|
|
|
+ //Parameter for Algo with default Values:
|
|
|
+ private boolean closeSwitches = true;
|
|
|
+
|
|
|
+ //Settings For GroupNode using and cancel
|
|
|
+ private boolean useGroupNode = false;
|
|
|
+ private DecoratedGroupNode dGroupNode = null;
|
|
|
+ private boolean cancel = false;
|
|
|
+
|
|
|
+
|
|
|
+ //Parameter defined by Algo
|
|
|
+ private HashMap<Integer, AccessWrapper> access;
|
|
|
+ private List<Boolean> initialState;
|
|
|
+ private List<HolonSwitch> switchList;
|
|
|
+
|
|
|
+ //Gui Part:
|
|
|
+ private Control control;
|
|
|
+ private JTextArea textArea;
|
|
|
+ private JPanel content = new JPanel();
|
|
|
+ //ProgressBar
|
|
|
+ private JProgressBar progressBar = new JProgressBar();
|
|
|
+ private int progressBarCount = 0;
|
|
|
+ private long startTime;
|
|
|
+ private Thread runThread;
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args)
|
|
|
+ {
|
|
|
+ JFrame newFrame = new JFrame("exampleWindow");
|
|
|
+ BaseLine instance = new BaseLine();
|
|
|
+ newFrame.setContentPane(instance.getAlgorithmPanel());
|
|
|
+ newFrame.pack();
|
|
|
+ newFrame.setVisible(true);
|
|
|
+ newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
+ }
|
|
|
+ public BaseLine() {
|
|
|
+ content.setLayout(new BorderLayout());
|
|
|
+
|
|
|
+ textArea = new JTextArea();
|
|
|
+ textArea.setEditable(false);
|
|
|
+ JScrollPane scrollPane = new JScrollPane(textArea);
|
|
|
+ JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
|
|
|
+ createOptionPanel() , scrollPane);
|
|
|
+ splitPane.setResizeWeight(0.0);
|
|
|
+ content.add(splitPane, BorderLayout.CENTER);
|
|
|
+ content.setPreferredSize(new Dimension(800,800));
|
|
|
+ }
|
|
|
+ public JPanel createOptionPanel() {
|
|
|
+ JPanel optionPanel = new JPanel(new BorderLayout());
|
|
|
+ JScrollPane scrollPane = new JScrollPane(createParameterPanel());
|
|
|
+ scrollPane.setBorder(BorderFactory.createTitledBorder("Parameter"));
|
|
|
+ optionPanel.add(scrollPane, BorderLayout.CENTER);
|
|
|
+ optionPanel.add(createButtonPanel(), BorderLayout.PAGE_END);
|
|
|
+ return optionPanel;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Component createParameterPanel() {
|
|
|
+ JPanel parameterPanel = new JPanel(null);
|
|
|
+ parameterPanel.setPreferredSize(new Dimension(510,300));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ JLabel showDiagnosticsLabel = new JLabel("Set all switches closed:");
|
|
|
+ showDiagnosticsLabel.setBounds(200, 60, 170, 20);
|
|
|
+ parameterPanel.add(showDiagnosticsLabel);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ JCheckBox switchesCheckBox = new JCheckBox();
|
|
|
+ switchesCheckBox.setSelected(closeSwitches);
|
|
|
+ switchesCheckBox.setBounds(370, 60, 25, 20);
|
|
|
+ switchesCheckBox.addActionListener(actionEvent -> closeSwitches = switchesCheckBox.isSelected());
|
|
|
+ parameterPanel.add(switchesCheckBox);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return parameterPanel;
|
|
|
+ }
|
|
|
+ public JPanel createButtonPanel() {
|
|
|
+ JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
|
|
|
+ JButton cancelButton = new JButton("Cancel Run");
|
|
|
+ cancelButton.addActionListener(actionEvent -> cancel());
|
|
|
+ buttonPanel.add(cancelButton);
|
|
|
+ JButton clearButton = new JButton("Clear Console");
|
|
|
+ clearButton.addActionListener(actionEvent -> clear());
|
|
|
+ buttonPanel.add(clearButton);
|
|
|
+ JButton resetButton = new JButton("Reset");
|
|
|
+ resetButton.setToolTipText("Resets the State to before the Algorithm has runed.");
|
|
|
+ resetButton.addActionListener(actionEvent -> reset());
|
|
|
+ buttonPanel.add(resetButton);
|
|
|
+ JButton runButton = new JButton("Run");
|
|
|
+ runButton.addActionListener(actionEvent -> {
|
|
|
+ Runnable task = () -> run();
|
|
|
+ runThread = new Thread(task);
|
|
|
+ runThread.start();
|
|
|
+ });
|
|
|
+ buttonPanel.add(runButton);
|
|
|
+ return buttonPanel;
|
|
|
+ }
|
|
|
+ private void cancel() {
|
|
|
+ if(runThread.isAlive()) {
|
|
|
+ println("");
|
|
|
+ println("Cancel run.");
|
|
|
+ cancel = true;
|
|
|
+ progressBar.setValue(0);
|
|
|
+ } else {
|
|
|
+ println("Nothing to cancel.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void run() {
|
|
|
+ cancel = false;
|
|
|
+ disableGuiInput(true);
|
|
|
+ startTimer();
|
|
|
+ executeBaseLine();
|
|
|
+ if(cancel) {
|
|
|
+ reset();
|
|
|
+ disableGuiInput(false);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ printElapsedTime();
|
|
|
+ disableGuiInput(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void reset() {
|
|
|
+ if(initialState != null) {
|
|
|
+ println("Resetting..");
|
|
|
+ resetState();
|
|
|
+ updateVisual();
|
|
|
+ }else {
|
|
|
+ println("No run inistialized.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void disableGuiInput(boolean bool) {
|
|
|
+ control.guiDiable(bool);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public JPanel getAlgorithmPanel() {
|
|
|
+ return content;
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public void setController(Control control) {
|
|
|
+ this.control = control;
|
|
|
+
|
|
|
+ }
|
|
|
+ private void clear() {
|
|
|
+ textArea.setText("");
|
|
|
+ }
|
|
|
+ private void print(String message) {
|
|
|
+ textArea.append(message);
|
|
|
+ }
|
|
|
+ private void println(String message) {
|
|
|
+ textArea.append(message + "\n");
|
|
|
+ }
|
|
|
+ private void selectGroupNode() {
|
|
|
+ Object[] possibilities = control.getSimManager().getActualVisualRepresentationalState().getCreatedGroupNodes().values().stream().map(aCps -> new Handle<DecoratedGroupNode>(aCps)).toArray();
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ Handle<DecoratedGroupNode> selected = (Handle<DecoratedGroupNode>) JOptionPane.showInputDialog(content, "Select GroupNode:", "GroupNode?", JOptionPane.OK_OPTION,new ImageIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)) , possibilities, "");
|
|
|
+ if(selected != null) {
|
|
|
+ println("Selected: " + selected);
|
|
|
+ dGroupNode = selected.object;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private void progressBarStep(){
|
|
|
+ progressBar.setValue(++progressBarCount);
|
|
|
+ }
|
|
|
+ private void calculateProgressBarParameter() {
|
|
|
+ int max = 100;
|
|
|
+ progressBarCount = 0;
|
|
|
+ progressBar.setValue(0);
|
|
|
+ progressBar.setMaximum(max);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void startTimer(){
|
|
|
+ startTime = System.currentTimeMillis();
|
|
|
+ }
|
|
|
+ private void printElapsedTime(){
|
|
|
+ long elapsedMilliSeconds = System.currentTimeMillis() - startTime;
|
|
|
+ println("Execution Time of Algo in Milliseconds:" + elapsedMilliSeconds);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //Algo Part:
|
|
|
+ /**
|
|
|
+ * The Execution of the BaseLine Algo.
|
|
|
+ *
|
|
|
+ *
|
|
|
+ * Begin
|
|
|
+ * set HolonElements aktiv;
|
|
|
+ * for(All Networks) do
|
|
|
+ * if(not (production < consumption)) continue;
|
|
|
+ * inAktiveCount = 0;
|
|
|
+ * while(inAktiveCount <= consumerWihtMaxNumberElements) do
|
|
|
+ * conList = createListWithConsumerThatHaveInActiveElementsAmountOf(inAktiveCount);
|
|
|
+ * sortByBiggestElement(conList);
|
|
|
+ * for(con : conList) do
|
|
|
+ * for(element : con.getAllActiveElementsSortByConsumption) do
|
|
|
+ * if(element <= production) do
|
|
|
+ * set element inAktiv;
|
|
|
+ * continue;
|
|
|
+ * end do
|
|
|
+ * end do
|
|
|
+ * end do
|
|
|
+ * inAktiveCount += 1;
|
|
|
+ * end while
|
|
|
+ * end for
|
|
|
+ * End
|
|
|
+ *
|
|
|
+ */
|
|
|
+ private void executeBaseLine() {
|
|
|
+ extractPositionAndAccess();
|
|
|
+ int actualIteration = control.getModel().getCurIteration();
|
|
|
+ if(closeSwitches)setAllSwitchesClosed();
|
|
|
+ setHolonElemntsAktiv();
|
|
|
+ control.calculateStateAndVisualForCurrentTimeStep();
|
|
|
+ DecoratedState actualstate = control.getSimManager().getActualDecorState();
|
|
|
+ for(DecoratedNetwork net : actualstate.getNetworkList()) {
|
|
|
+ float production = net.getSupplierList().stream().map(supplier -> supplier.getEnergyToSupplyNetwork()).reduce(0.0f, (a, b) -> a + b);
|
|
|
+ float consumption = net.getConsumerList().stream().map(con -> con.getEnergyNeededFromNetwork()).reduce(0.0f, (a, b) -> a + b);
|
|
|
+ float difference = Math.abs(production - consumption);
|
|
|
+ println("production:" + production + " consumption:" + consumption);
|
|
|
+ if(!(production < consumption))continue;
|
|
|
+ if(net.getConsumerList().isEmpty() && net.getConsumerSelfSuppliedList().isEmpty())continue;
|
|
|
+ //Stream.concat(net.getConsumerList().stream(), net.getConsumerSelfSuppliedList().stream());
|
|
|
+ int consumerWihtMaxNumberElements = Stream.concat(net.getConsumerList().stream(), net.getConsumerSelfSuppliedList().stream()).map(con -> con.getModel().getNumberOfElements()).max((lhs,rhs) ->Integer.compare(lhs, rhs)).orElse(0);
|
|
|
+ println("consumerWihtMaxNumberElements:" + consumerWihtMaxNumberElements);
|
|
|
+ for(int inAktiveCount = 0;inAktiveCount <= consumerWihtMaxNumberElements; inAktiveCount++) {
|
|
|
+ println("inAktiveCount:" + inAktiveCount);
|
|
|
+ final int inAktiveCountFinal = inAktiveCount;
|
|
|
+ List<HolonObject> conList = Stream.concat(net.getConsumerList().stream(), net.getConsumerSelfSuppliedList().stream()).map(con -> con.getModel()).filter(object -> (object.getNumberOfInActiveElements() == inAktiveCountFinal)).collect(Collectors.toList());
|
|
|
+ conList.sort((a,b) -> Float.compare(a.getMaximumConsumingElementEnergy(actualIteration), b.getMaximumConsumingElementEnergy(actualIteration)));
|
|
|
+ consumer:
|
|
|
+ for(HolonObject con: conList) {
|
|
|
+ println("Consumer" + con);
|
|
|
+ List<HolonElement> sortedElementList = con.getElements().stream().filter(ele -> ele.isActive() && ele.isConsumer()).sorted((a,b) -> -Float.compare(-a.getEnergyAtTimeStep(actualIteration), -b.getEnergyAtTimeStep(actualIteration))).collect(Collectors.toList());
|
|
|
+ for(HolonElement element: sortedElementList) {
|
|
|
+ float elementConsumption = -element.getEnergyAtTimeStep(actualIteration);
|
|
|
+ if(elementConsumption <= difference) {
|
|
|
+ println("elementConsumption:" + elementConsumption);
|
|
|
+ difference -= elementConsumption;
|
|
|
+ element.setActive(false);
|
|
|
+ continue consumer;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ updateVisual();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private void setHolonElemntsAktiv() {
|
|
|
+ for(int i = 0;i<access.size();i++) {
|
|
|
+ AccessWrapper aw = access.get(i);
|
|
|
+ if(aw.getType() == AccessWrapper.HOLONELEMENT) aw.setState(true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private void setAllSwitchesClosed() {
|
|
|
+ for(HolonSwitch hSwitch : switchList) {
|
|
|
+ hSwitch.setManualMode(true);
|
|
|
+ hSwitch.setManualState(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * Method to get the current Position alias a ListOf Booleans for aktive settings on the Objects on the Canvas.
|
|
|
+ * Also initialize the Access Hashmap to swap faster positions.
|
|
|
+ * @param model
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<Boolean> extractPositionAndAccess() {
|
|
|
+ Model model = control.getModel();
|
|
|
+ switchList = new ArrayList<HolonSwitch>();
|
|
|
+ initialState = new ArrayList<Boolean>();
|
|
|
+ access= new HashMap<Integer, AccessWrapper>();
|
|
|
+ rollOutNodes((useGroupNode && (dGroupNode != null))? dGroupNode.getModel().getNodes() :model.getObjectsOnCanvas(), initialState, model.getCurIteration());
|
|
|
+ return initialState;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * Method to extract the Informations recursively out of the Model.
|
|
|
+ * @param nodes
|
|
|
+ * @param positionToInit
|
|
|
+ * @param timeStep
|
|
|
+ */
|
|
|
+ private void rollOutNodes(List<AbstractCpsObject> nodes, List<Boolean> positionToInit, int timeStep) {
|
|
|
+ for(AbstractCpsObject aCps : nodes) {
|
|
|
+ if (aCps instanceof HolonObject) {
|
|
|
+ for (HolonElement hE : ((HolonObject) aCps).getElements()) {
|
|
|
+ positionToInit.add(hE.isActive());
|
|
|
+ access.put(positionToInit.size() - 1 , new AccessWrapper(hE));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (aCps instanceof HolonSwitch) {
|
|
|
+ HolonSwitch sw = (HolonSwitch) aCps;
|
|
|
+ positionToInit.add(sw.getState(timeStep));
|
|
|
+ switchList.add(sw);
|
|
|
+ access.put(positionToInit.size() - 1 , new AccessWrapper(sw));
|
|
|
+ }
|
|
|
+ else if(aCps instanceof CpsUpperNode) {
|
|
|
+ rollOutNodes(((CpsUpperNode)aCps).getNodes(), positionToInit ,timeStep );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * To let the User See the current state without touching the Canvas.
|
|
|
+ */
|
|
|
+ private void updateVisual() {
|
|
|
+ control.calculateStateAndVisualForCurrentTimeStep();
|
|
|
+ control.updateCanvas();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * Sets the Model back to its original State before the LAST run.
|
|
|
+ */
|
|
|
+ private void resetState() {
|
|
|
+ setState(initialState);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sets the State out of the given position for calculation or to show the user.
|
|
|
+ * @param position
|
|
|
+ */
|
|
|
+ private void setState(List<Boolean> position) {
|
|
|
+ for(int i = 0;i<position.size();i++) {
|
|
|
+ access.get(i).setState(position.get(i));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * A Wrapper Class for Access HolonElement and HolonSwitch in one Element and not have to split the List.
|
|
|
+ */
|
|
|
+ private class AccessWrapper {
|
|
|
+ public static final int HOLONELEMENT = 0;
|
|
|
+ public static final int SWITCH = 1;
|
|
|
+ private int type;
|
|
|
+ private HolonSwitch hSwitch;
|
|
|
+ private HolonElement hElement;
|
|
|
+ public AccessWrapper(HolonSwitch hSwitch){
|
|
|
+ type = SWITCH;
|
|
|
+ this.hSwitch = hSwitch;
|
|
|
+ }
|
|
|
+ public AccessWrapper(HolonElement hElement){
|
|
|
+ type = HOLONELEMENT;
|
|
|
+ this.hElement = hElement;
|
|
|
+ }
|
|
|
+ public void setState(boolean state) {
|
|
|
+ if(type == HOLONELEMENT) {
|
|
|
+ hElement.setActive(state);
|
|
|
+ }else{//is switch
|
|
|
+ hSwitch.setManualMode(true);
|
|
|
+ hSwitch.setManualState(state);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ public boolean getState(int timeStep) {
|
|
|
+ return (type == HOLONELEMENT)?hElement.isActive():hSwitch.getState(timeStep);
|
|
|
+ }
|
|
|
+ public int getType() {
|
|
|
+ return type;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private class Handle<T>{
|
|
|
+ public T object;
|
|
|
+ Handle(T object){
|
|
|
+ this.object = object;
|
|
|
+ }
|
|
|
+ public String toString() {
|
|
|
+ return object.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|