RandomOfferdFlexibility.java 12 KB

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