|
@@ -15,6 +15,7 @@ import java.text.NumberFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
import java.util.DoubleSummaryStatistics;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.LinkedList;
|
|
|
import java.util.List;
|
|
|
import java.util.Locale;
|
|
@@ -29,6 +30,7 @@ import javax.swing.BoxLayout;
|
|
|
import javax.swing.ImageIcon;
|
|
|
import javax.swing.JButton;
|
|
|
import javax.swing.JCheckBox;
|
|
|
+import javax.swing.JComboBox;
|
|
|
import javax.swing.JFileChooser;
|
|
|
import javax.swing.JFormattedTextField;
|
|
|
import javax.swing.JLabel;
|
|
@@ -39,6 +41,8 @@ import javax.swing.JScrollPane;
|
|
|
import javax.swing.JSplitPane;
|
|
|
import javax.swing.text.NumberFormatter;
|
|
|
|
|
|
+import algorithm.objectiveFunction.ObjectiveFunctionByCarlos;
|
|
|
+import algorithm.objectiveFunction.SwitchObjectiveFunction;
|
|
|
import classes.AbstractCanvasObject;
|
|
|
import classes.GroupNode;
|
|
|
import classes.Flexibility;
|
|
@@ -76,8 +80,9 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
|
|
|
|
|
|
//access
|
|
|
private ArrayList<AccessWrapper> access;
|
|
|
+ private HashMap<HolonObject, AccessWrapper> accessKillSwitch = new HashMap<HolonObject, AccessWrapper>();
|
|
|
LinkedList<List<Boolean>> resetChain = new LinkedList<List<Boolean>>();
|
|
|
- boolean algoUseElements = false, algoUseSwitches = true, algoUseFlexes = true;
|
|
|
+ boolean algoUseElements = false, algoUseSwitches = true, algoUseFlexes = true, algoUseKillSwitch = true;
|
|
|
|
|
|
//time
|
|
|
private long startTime;
|
|
@@ -108,6 +113,10 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
|
|
|
private boolean useEmailNotification = false;
|
|
|
|
|
|
|
|
|
+ //ObjectiveFunction
|
|
|
+ enum ObjectiveFunction {Normal, Switch};
|
|
|
+ ObjectiveFunction evaluationFunction = ObjectiveFunction.Normal;
|
|
|
+
|
|
|
public AlgorithmFrameworkFlex(){
|
|
|
content.setLayout(new BorderLayout());
|
|
|
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
|
|
@@ -115,9 +124,6 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
|
|
|
splitPane.setResizeWeight(0.0);
|
|
|
content.add(splitPane, BorderLayout.CENTER);
|
|
|
content.setPreferredSize(new Dimension(1200,800));
|
|
|
-
|
|
|
- //Add rounds
|
|
|
-
|
|
|
}
|
|
|
|
|
|
|
|
@@ -174,6 +180,33 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
|
|
|
useFlexes.addActionListener(actionEvent -> algoUseFlexes = useFlexes.isSelected());
|
|
|
parameterPanel.add(useFlexes);
|
|
|
|
|
|
+ JCheckBox useKillSwitches = new JCheckBox("KillSwitch");
|
|
|
+ useKillSwitches.setSelected(algoUseFlexes);
|
|
|
+ useKillSwitches.setBounds(900, 130, 185, 20);
|
|
|
+ useKillSwitches.addActionListener(actionEvent ->
|
|
|
+ {
|
|
|
+ cancel();
|
|
|
+ reset();
|
|
|
+ algoUseKillSwitch = useKillSwitches.isSelected();
|
|
|
+ });
|
|
|
+ parameterPanel.add(useKillSwitches);
|
|
|
+
|
|
|
+ String[] objectiveFunctionStrings = { "Normal", "Switch"};
|
|
|
+ JLabel fitnessLabel = new JLabel("FitnessFunction:");
|
|
|
+ fitnessLabel.setBounds(910, 160, 90, 20);
|
|
|
+ parameterPanel.add(fitnessLabel);
|
|
|
+ JComboBox<String> ofBox = new JComboBox<String>(objectiveFunctionStrings);
|
|
|
+ ofBox.addActionListener(actionEvent ->
|
|
|
+ {
|
|
|
+ boolean pickNormal = ((String)ofBox.getSelectedItem()).equals("Normal");
|
|
|
+ evaluationFunction = pickNormal?ObjectiveFunction.Normal:ObjectiveFunction.Switch;
|
|
|
+ });
|
|
|
+ ofBox.setBounds(1000, 160, 70, 20);
|
|
|
+ parameterPanel.add(ofBox);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
JCheckBox emailNotificationCheckbox = new JCheckBox("EmailNotification");
|
|
|
emailNotificationCheckbox.setSelected(this.useEmailNotification);
|
|
|
emailNotificationCheckbox.setBounds(900, 200, 130, 20);
|
|
@@ -475,7 +508,16 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- protected abstract double evaluateState(DecoratedState actualstate);
|
|
|
+ private double evaluateState(DecoratedState actualstate) {
|
|
|
+ switch(this.evaluationFunction) {
|
|
|
+ case Switch:
|
|
|
+ return SwitchObjectiveFunction.getFitnessValueForState(actualstate);
|
|
|
+ case Normal:
|
|
|
+ default:
|
|
|
+ return ObjectiveFunctionByCarlos.getFitnessValueForState(actualstate);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
|
|
|
private void run() {
|
|
@@ -714,17 +756,21 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
|
|
|
*/
|
|
|
protected List<Boolean> extractPositionAndAccess() {
|
|
|
Model model = control.getModel();
|
|
|
+ this.accessKillSwitch = new HashMap<HolonObject, AccessWrapper>();
|
|
|
access= new ArrayList<AccessWrapper>();
|
|
|
List<Boolean> initialState = new ArrayList<Boolean>();
|
|
|
rollOutNodes((dGroupNode != null)? dGroupNode.getModel().getNodes() :model.getObjectsOnCanvas(), initialState, model.getCurIteration());
|
|
|
resetChain.add(initialState);
|
|
|
if(algoUseFlexes) {
|
|
|
for(FlexWrapper flex :control.getSimManager().getActualFlexManager().getAllFlexWrapperWithState(FlexState.OFFERED)){
|
|
|
- access.add(new AccessWrapper(flex.getFlex()));
|
|
|
+ //flex.getFlex().getElement().parentObject;
|
|
|
+ AccessWrapper killSwitchAccess = this.algoUseKillSwitch ? this.accessKillSwitch.get(flex.getFlex().getElement().parentObject): null;;
|
|
|
+ access.add(new AccessWrapper(flex.getFlex(), killSwitchAccess));
|
|
|
initialState.add(false);
|
|
|
}
|
|
|
for(FlexWrapper flex :control.getSimManager().getActualFlexManager().getAllFlexWrapperWithState(FlexState.IN_USE)){
|
|
|
- access.add(new AccessWrapper(flex.getFlex()));
|
|
|
+ AccessWrapper killSwitchAccess = this.algoUseKillSwitch ? this.accessKillSwitch.get(flex.getFlex().getElement().parentObject): null;
|
|
|
+ access.add(new AccessWrapper(flex.getFlex(), killSwitchAccess));
|
|
|
initialState.add(true);
|
|
|
}
|
|
|
}
|
|
@@ -743,10 +789,19 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
|
|
|
*/
|
|
|
private void rollOutNodes(List<AbstractCanvasObject> nodes, List<Boolean> positionToInit, int timeStep) {
|
|
|
for(AbstractCanvasObject aCps : nodes) {
|
|
|
- if (aCps instanceof HolonObject && algoUseElements) {
|
|
|
- for (HolonElement hE : ((HolonObject) aCps).getElements()) {
|
|
|
- positionToInit.add(hE.isActive());
|
|
|
- access.add(new AccessWrapper(hE));
|
|
|
+ if (aCps instanceof HolonObject) {
|
|
|
+ HolonObject hObject = (HolonObject) aCps;
|
|
|
+ AccessWrapper killSwitchAccess = new AccessWrapper(hObject);
|
|
|
+ if(this.algoUseKillSwitch) {
|
|
|
+ positionToInit.add(false);
|
|
|
+ access.add(killSwitchAccess);
|
|
|
+ accessKillSwitch.put(hObject, killSwitchAccess);
|
|
|
+ }
|
|
|
+ if(this.algoUseElements) {
|
|
|
+ for (HolonElement hE : hObject.getElements()) {
|
|
|
+ positionToInit.add(hE.isActive());
|
|
|
+ access.add(new AccessWrapper(hE, killSwitchAccess));
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
else if (aCps instanceof HolonSwitch&& algoUseSwitches) {
|
|
@@ -951,59 +1006,96 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ private enum AccessType {None, HolonElement, Switch, Flexibility, KillSwitch};
|
|
|
/**
|
|
|
* 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;
|
|
|
- public static final int FLEXIBILITY = 2;
|
|
|
- private int type;
|
|
|
+ private AccessType type;
|
|
|
+ private List<Boolean> intialStatesOfElementForKilllSwitch;
|
|
|
+
|
|
|
+ private HolonObject hObject;
|
|
|
private HolonSwitch hSwitch;
|
|
|
+
|
|
|
private HolonElement hElement;
|
|
|
private Flexibility flex;
|
|
|
+ private AccessWrapper correspondingKillSwitch;
|
|
|
+
|
|
|
+ private boolean lastState;
|
|
|
+
|
|
|
+
|
|
|
+ public AccessWrapper(HolonObject hObject){
|
|
|
+ type = AccessType.KillSwitch;
|
|
|
+ this.hObject = hObject;
|
|
|
+ intialStatesOfElementForKilllSwitch = new ArrayList<Boolean>();
|
|
|
+ for(HolonElement hE: hObject.getElements()) {
|
|
|
+ intialStatesOfElementForKilllSwitch.add(hE.isActive());
|
|
|
+ }
|
|
|
+ }
|
|
|
public AccessWrapper(HolonSwitch hSwitch){
|
|
|
- type = SWITCH;
|
|
|
+ type = AccessType.Switch;
|
|
|
this.hSwitch = hSwitch;
|
|
|
}
|
|
|
- public AccessWrapper(HolonElement hElement){
|
|
|
- type = HOLONELEMENT;
|
|
|
+ public AccessWrapper(HolonElement hElement, AccessWrapper correspondingKillSwitch){
|
|
|
+ type = AccessType.HolonElement;
|
|
|
this.hElement = hElement;
|
|
|
+ this.correspondingKillSwitch = correspondingKillSwitch;
|
|
|
}
|
|
|
|
|
|
- public AccessWrapper(Flexibility flex){
|
|
|
- type = FLEXIBILITY;
|
|
|
+ public AccessWrapper(Flexibility flex, AccessWrapper correspondingKillSwitch){
|
|
|
+ type = AccessType.Flexibility;
|
|
|
this.flex = flex;
|
|
|
+ this.correspondingKillSwitch = correspondingKillSwitch;
|
|
|
}
|
|
|
|
|
|
public void setState(boolean state) {
|
|
|
+ lastState = state;
|
|
|
switch(type) {
|
|
|
- case HOLONELEMENT:
|
|
|
- hElement.setActive(state);
|
|
|
+ case HolonElement:
|
|
|
+ if(!algoUseKillSwitch || (algoUseKillSwitch && !correspondingKillSwitch.getLastState())) {
|
|
|
+ hElement.setActive(state);
|
|
|
+ }
|
|
|
break;
|
|
|
- case SWITCH:
|
|
|
+ case Switch:
|
|
|
hSwitch.setManualMode(true);
|
|
|
hSwitch.setManualState(state);
|
|
|
break;
|
|
|
- case FLEXIBILITY:
|
|
|
- if(state) {
|
|
|
+ case Flexibility:
|
|
|
+ if(state && (!algoUseKillSwitch || (algoUseKillSwitch && !correspondingKillSwitch.getLastState()))) {
|
|
|
control.getSimManager().getActualFlexManager().orderFlex(flex);
|
|
|
}
|
|
|
break;
|
|
|
+ case KillSwitch:
|
|
|
+ if(state) {
|
|
|
+ for (HolonElement hE : hObject.getElements()) {
|
|
|
+ hE.setActive(false);
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ for(int i = 0; i < hObject.getElements().size(); i++) {
|
|
|
+ hObject.getElements().get(i).setActive(intialStatesOfElementForKilllSwitch.get(i));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
default:
|
|
|
+ break;
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public boolean getLastState() {
|
|
|
+ return lastState;
|
|
|
+ }
|
|
|
+
|
|
|
public String typeString() {
|
|
|
switch(type) {
|
|
|
- case HOLONELEMENT:
|
|
|
- return "HOLONELEMENT";
|
|
|
- case SWITCH:
|
|
|
- return "SWITCH";
|
|
|
- case FLEXIBILITY:
|
|
|
- return "FLEXIBILITY";
|
|
|
+ case HolonElement:
|
|
|
+ return "HolonElement";
|
|
|
+ case Switch:
|
|
|
+ return "Switch";
|
|
|
+ case Flexibility:
|
|
|
+ return "Flexibility";
|
|
|
+ case KillSwitch:
|
|
|
+ return "KillSwitch";
|
|
|
default:
|
|
|
return "unknown";
|
|
|
}
|