RandomOfferdFlexibility.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. package addOns;
  2. import java.awt.BorderLayout;
  3. import java.awt.ComponentOrientation;
  4. import java.awt.Dimension;
  5. import java.awt.FlowLayout;
  6. import java.awt.GridBagConstraints;
  7. import java.awt.GridBagLayout;
  8. import java.math.RoundingMode;
  9. import java.text.NumberFormat;
  10. import java.util.ArrayList;
  11. import java.util.Collections;
  12. import java.util.Hashtable;
  13. import java.util.List;
  14. import java.util.Locale;
  15. import java.util.Random;
  16. import java.util.stream.Collectors;
  17. import javax.swing.BorderFactory;
  18. import javax.swing.JButton;
  19. import javax.swing.JCheckBox;
  20. import javax.swing.JFormattedTextField;
  21. import javax.swing.JFrame;
  22. import javax.swing.JLabel;
  23. import javax.swing.JPanel;
  24. import javax.swing.JSlider;
  25. import javax.swing.text.NumberFormatter;
  26. import api.AddOn;
  27. import classes.Flexibility;
  28. import classes.HolonElement.Priority;
  29. import ui.controller.Control;
  30. /**
  31. * A short algorithm to distribute the Priorities for the whole Canvas.
  32. * @author tom
  33. *
  34. */
  35. public class RandomOfferdFlexibility implements AddOn {
  36. private Control control;
  37. private JPanel content = new JPanel();
  38. private class PriorityDependeces{
  39. public JCheckBox checkbox;
  40. public JSlider slider = new JSlider(JSlider.HORIZONTAL,0, 100, 50);
  41. public JLabel positive = new JLabel("0 \u2192 0(0)");
  42. public JLabel negative = new JLabel("0 \u2192 0(0)");
  43. public FlexOffered offer = new FlexOffered();
  44. public List<Flexibility> flexList = new ArrayList<Flexibility>();
  45. public PriorityDependeces(String name){
  46. checkbox = new JCheckBox(name, true);
  47. }
  48. public void update() {
  49. List<Flexibility> positiveList = flexList.stream().filter(flex -> flex.isPositive()).collect(Collectors.toList());
  50. offer.positive.maximumOffered = positiveList.size();
  51. offer.positive.actualOffered = (int)positiveList.stream().filter(flex -> (flex.offered)).count();
  52. List<Flexibility> negativeList = flexList.stream().filter(flex -> flex.isNegative()).collect(Collectors.toList());
  53. offer.negative.maximumOffered = negativeList.size();
  54. offer.negative.actualOffered = (int)negativeList.stream().filter(flex -> (flex.offered)).count();
  55. offer.updateActualProportion();
  56. setTarget(offer.proportion);
  57. }
  58. public void setTarget(double proprotion) {
  59. offer.updateTarget(proprotion);
  60. //Update slider
  61. slider.setValue((int)(offer.proportion * 100.0));
  62. //Update Label
  63. positive.setText(offer.positive.actualOffered + " \u2192 " + offer.positive.targetOffered + "(" + offer.positive.maximumOffered + ")");
  64. negative.setText(offer.negative.actualOffered + " \u2192 " + offer.negative.targetOffered + "(" + offer.negative.maximumOffered + ")");
  65. }
  66. public void updateCanvasToTargetAmounts() {
  67. List<Flexibility> positiveList = flexList.stream().filter(flex -> flex.isPositive()).collect(Collectors.toList());
  68. Collections.shuffle(positiveList, new Random());
  69. for(int i = 0; i < positiveList.size(); i++){
  70. positiveList.get(i).offered = (i < offer.positive.targetOffered);
  71. }
  72. List<Flexibility> negativeList = flexList.stream().filter(flex -> flex.isNegative()).collect(Collectors.toList());
  73. Collections.shuffle(negativeList, new Random());
  74. for(int i = 0; i < negativeList.size(); i++){
  75. negativeList.get(i).offered = (i < offer.negative.targetOffered);
  76. }
  77. if(control != null) {
  78. control.calculateStateAndVisualForCurrentTimeStep();
  79. }
  80. }
  81. }
  82. private class FlexOffered{
  83. public double proportion = 0.5;
  84. public FlexTypeOffered positive = new FlexTypeOffered();
  85. public FlexTypeOffered negative = new FlexTypeOffered();
  86. public void updateTarget(double proportion) {
  87. //Clamp between 0 and 1
  88. proportion = Math.min(1, Math.max(0, proportion));
  89. if(1 == proportion) {
  90. negative.targetOffered = 0;
  91. positive.targetOffered = positive.maximumOffered;
  92. }else if(0 == proportion) {
  93. positive.targetOffered = 0;
  94. negative.targetOffered = negative.maximumOffered;
  95. }else {
  96. //x * proportion = positive.maximumOffered
  97. int maximumAmountBothA = (int)((double)positive.maximumOffered /proportion);
  98. int amountOtherSide = maximumAmountBothA - positive.maximumOffered;
  99. if(amountOtherSide <= negative.maximumOffered) {
  100. negative.targetOffered = amountOtherSide;
  101. positive.targetOffered = positive.maximumOffered;
  102. }else {
  103. int maximumAmountBothB = (int)((double)negative.maximumOffered / (1.0 -proportion));
  104. int amountOtherSideB = maximumAmountBothB - negative.maximumOffered;
  105. positive.targetOffered = amountOtherSideB;
  106. negative.targetOffered = negative.maximumOffered;
  107. }
  108. }
  109. }
  110. public void updateActualProportion() {
  111. if(positive.actualOffered + negative.actualOffered == 0) {
  112. proportion = 0.5;
  113. }else {
  114. proportion = (double)positive.actualOffered / (double)(positive.actualOffered + negative.actualOffered);
  115. }
  116. }
  117. public double getActualProportion() {
  118. if(positive.actualOffered + negative.actualOffered == 0) {
  119. return 0.5;
  120. }
  121. return (double)positive.actualOffered / (double)(positive.actualOffered + negative.actualOffered);
  122. }
  123. }
  124. private class FlexTypeOffered{
  125. int actualOffered = 0;
  126. int maximumOffered = 0;
  127. int targetOffered = 0;
  128. }
  129. PriorityDependeces low = new PriorityDependeces("low");
  130. PriorityDependeces medium = new PriorityDependeces("medium");
  131. PriorityDependeces high = new PriorityDependeces("high");
  132. PriorityDependeces essential = new PriorityDependeces("essential");
  133. public static void main(String[] args)
  134. {
  135. JFrame newFrame = new JFrame("exampleWindow");
  136. RandomOfferdFlexibility instance = new RandomOfferdFlexibility();
  137. newFrame.setContentPane(instance.getPanel());
  138. newFrame.pack();
  139. newFrame.setVisible(true);
  140. newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  141. }
  142. public RandomOfferdFlexibility(){
  143. low.offer.positive.maximumOffered = low.offer.positive.actualOffered = 1000;
  144. low.offer.negative.maximumOffered = low.offer.negative.actualOffered = 2000;
  145. double distribution = 0.8;
  146. low.offer.updateTarget(distribution);
  147. System.out.println("distribution:" + distribution + " Positive:" + low.offer.positive.targetOffered
  148. + " Negative:" + low.offer.negative.targetOffered);
  149. System.out.println("actualDistribution:" + low.offer.getActualProportion());
  150. content.setLayout(new BorderLayout());
  151. content.add(createFlexPanel(), BorderLayout.CENTER);
  152. JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
  153. JButton buttonReload = new JButton("Reload");
  154. buttonReload.setToolTipText("Press to relaod all canvas changes.");
  155. buttonReload.addActionListener(actionEvent -> update());
  156. buttonPanel.add(buttonReload);
  157. JButton buttonRun = new JButton("Run");
  158. buttonRun.setToolTipText("Changes the actual offered flex to the random target amount of selected prioritys.");
  159. buttonRun.addActionListener(actionEvent -> run());
  160. buttonPanel.add(buttonRun);
  161. content.add(buttonPanel, BorderLayout.PAGE_END);
  162. //content.setPreferredSize(new Dimension(300,500));
  163. }
  164. private JPanel createFlexPanel() {
  165. JPanel flexPanel = new JPanel();
  166. flexPanel.setBorder(BorderFactory.createTitledBorder("Flexibility"));
  167. flexPanel.setLayout(new GridBagLayout());
  168. GridBagConstraints c = new GridBagConstraints();
  169. c.fill = GridBagConstraints.HORIZONTAL;
  170. c.gridx = 0;
  171. c.gridy = 0;
  172. c.ipadx = 10;
  173. //c.ipady = 100;
  174. //Label
  175. flexPanel.add(new JLabel("Priority:"), c);
  176. c.gridx++;
  177. flexPanel.add(new JLabel("Target:"), c);
  178. c.gridx++;
  179. flexPanel.add(new JLabel("Positive#(Available):"), c);
  180. c.gridx++;
  181. flexPanel.add(new JLabel("Negative#(Available):"), c);
  182. c.gridx = 0;
  183. c.gridy = 1;
  184. flexPanel.add(low.checkbox, c);c.gridx++;
  185. c.weightx = 1;
  186. flexPanel.add(createTargetSetterPanel(this.low), c);c.gridx++;
  187. c.weightx = 0;
  188. flexPanel.add(this.low.positive, c);c.gridx++;
  189. flexPanel.add(this.low.negative, c);
  190. c.gridx = 0;
  191. c.gridy = 2;
  192. flexPanel.add(medium.checkbox, c);c.gridx++;
  193. flexPanel.add(createTargetSetterPanel(this.medium), c);c.gridx++;
  194. flexPanel.add(this.medium.positive, c);c.gridx++;
  195. flexPanel.add(this.medium.negative, c);
  196. c.gridx = 0;
  197. c.gridy = 3;
  198. flexPanel.add(high.checkbox, c);c.gridx++;
  199. flexPanel.add(createTargetSetterPanel(this.high), c);c.gridx++;
  200. flexPanel.add(this.high.positive, c);c.gridx++;
  201. flexPanel.add(this.high.negative, c);
  202. c.gridx = 0;
  203. c.gridy = 4;
  204. flexPanel.add(essential.checkbox, c);c.gridx++;
  205. flexPanel.add(createTargetSetterPanel(this.essential), c);c.gridx++;
  206. flexPanel.add(this.essential.positive, c);c.gridx++;
  207. flexPanel.add(this.essential.negative, c);
  208. return flexPanel;
  209. }
  210. private JPanel createTargetSetterPanel(PriorityDependeces priorityD) {
  211. JPanel panel = new JPanel();
  212. panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10));
  213. panel.setLayout(new GridBagLayout());
  214. GridBagConstraints c = new GridBagConstraints();
  215. c.fill = GridBagConstraints.HORIZONTAL;
  216. c.gridx = 0;
  217. c.gridy = 0;
  218. c.weightx = 0;
  219. c.anchor = GridBagConstraints.NORTH;
  220. panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
  221. NumberFormat doubleFormat = NumberFormat.getNumberInstance(Locale.US);
  222. doubleFormat.setMinimumFractionDigits(0);
  223. doubleFormat.setMaximumFractionDigits(2);
  224. doubleFormat.setRoundingMode(RoundingMode.HALF_UP);
  225. NumberFormatter doubleFormatter = new NumberFormatter(doubleFormat);
  226. doubleFormatter.setMinimum(0.0);
  227. doubleFormatter.setMaximum(1.0);
  228. //doubleFormatter.setCommitsOnValidEdit(true);
  229. JFormattedTextField change = new JFormattedTextField(doubleFormatter);
  230. change.addActionListener(ChangeEvent -> priorityD.slider.setValue((int)(Double.parseDouble(change.getValue().toString()) * 100.0)));
  231. change.setText("0.1");
  232. change.setPreferredSize(new Dimension(40,20));
  233. panel.add(change, c);
  234. c.fill = GridBagConstraints.HORIZONTAL;
  235. c.gridx = 1;
  236. c.weightx = 1;
  237. priorityD.slider.setMajorTickSpacing(50);
  238. priorityD.slider.setMinorTickSpacing(5);
  239. priorityD.slider.setPaintTicks(true);
  240. Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
  241. labelTable.put( Integer.valueOf( 0 ), new JLabel("Positiv") );
  242. labelTable.put( Integer.valueOf( 100 ), new JLabel("Negativ") );
  243. priorityD.slider.addChangeListener(changeEvent -> {
  244. priorityD.offer.proportion = (double)priorityD.slider.getValue()/100.0;
  245. priorityD.slider.setToolTipText("" + priorityD.offer.proportion);
  246. change.setText("" +priorityD.offer.proportion);
  247. priorityD.setTarget(priorityD.offer.proportion);
  248. });
  249. priorityD.slider.setLabelTable( labelTable );
  250. priorityD.slider.setPaintLabels(true);
  251. panel.add(priorityD.slider, c);
  252. return panel;
  253. }
  254. private void run() {
  255. //control.getModel().getObjectsOnCanvas().stream().filter(aCps -> aCps instanceof HolonObject)
  256. if(control == null) {
  257. System.out.println("Nothing to do");
  258. return;
  259. }
  260. if(low.checkbox.isSelected()) low.updateCanvasToTargetAmounts();
  261. if(medium.checkbox.isSelected()) medium.updateCanvasToTargetAmounts();
  262. if(high.checkbox.isSelected()) high.updateCanvasToTargetAmounts();
  263. if(essential.checkbox.isSelected()) essential.updateCanvasToTargetAmounts();
  264. }
  265. public void update() {
  266. if(control == null) {
  267. return;
  268. }
  269. control.calculateStateAndVisualForCurrentTimeStep();
  270. control.updateCanvas();
  271. List<Flexibility> flexList = control.getSimManager().getActualFlexManager().getAllFlexWrapper().stream().filter(flexwrapper -> flexwrapper.getFlex().offered).map(flex -> flex.getFlex()).collect(Collectors.toList());
  272. low.flexList = flexList.stream().filter(flex -> flex.getElement().getPriority() == Priority.Low && flex.fulfillsConstrains()).collect(Collectors.toList());
  273. medium.flexList = flexList.stream().filter(flex -> flex.getElement().getPriority() == Priority.Medium && flex.fulfillsConstrains()).collect(Collectors.toList());
  274. high.flexList = flexList.stream().filter(flex -> flex.getElement().getPriority() == Priority.High && flex.fulfillsConstrains()).collect(Collectors.toList());
  275. essential.flexList = flexList.stream().filter(flex -> flex.getElement().getPriority() == Priority.Essential && flex.fulfillsConstrains()).collect(Collectors.toList());
  276. low.update();
  277. medium.update();
  278. high.update();
  279. essential.update();
  280. }
  281. @Override
  282. public JPanel getPanel() {
  283. return content;
  284. }
  285. @Override
  286. public void setController(Control control) {
  287. this.control = control;
  288. update();
  289. }
  290. }