source: src/main/java/genius/gui/dialogs/NewIssueDialog.java@ 126

Last change on this file since 126 was 126, checked in by Aron Hammond, 6 years ago

Added function to calculate opposition to MultiLateralAnalysis.java

Moved code to add RLBOA listeners to RLBOAUtils is misc package

Added input for strategyParameters to SessionPanel (gui)

!! close SessionInfo after tournament; this caused /tmp/ to fill up with GeniusData files

Our own package:

  • Added opponents and strategies that are mentioned in the report
  • Change class hierarchy, agents can now extend from RLBOAagentBilateral to inherit RL functionality.
  • States extend from AbstractState
File size: 17.8 KB
Line 
1package genius.gui.dialogs;
2
3import java.awt.BorderLayout;
4import java.awt.CardLayout;
5import java.awt.Component;
6import java.awt.GridLayout;
7import java.awt.event.ActionEvent;
8import java.awt.event.ItemEvent;
9import java.awt.event.ItemListener;
10import java.util.ArrayList;
11import java.util.List;
12
13import javax.swing.BorderFactory;
14import javax.swing.BoxLayout;
15import javax.swing.JComboBox;
16import javax.swing.JLabel;
17import javax.swing.JOptionPane;
18import javax.swing.JPanel;
19import javax.swing.JScrollPane;
20import javax.swing.JTextArea;
21import javax.swing.JTextField;
22
23import genius.core.issue.Issue;
24import genius.core.issue.IssueDiscrete;
25import genius.core.issue.IssueInteger;
26import genius.core.issue.IssueReal;
27import genius.core.issue.Objective;
28import genius.core.issue.Value;
29import genius.core.issue.ValueDiscrete;
30import genius.core.utility.AdditiveUtilitySpace;
31import genius.core.utility.Evaluator;
32import genius.core.utility.EvaluatorDiscrete;
33import genius.core.utility.EvaluatorInteger;
34import genius.core.utility.EvaluatorReal;
35import genius.gui.tree.NegotiatorTreeTableModel;
36import genius.gui.tree.DomainAndProfileEditorPanel;
37
38/**
39 * A dialog window of Genius GUI used to createFrom a new issue and/or evaluator
40 * for a issue.
41 *
42 *
43 * @author Dmytro Tykhonov
44 *
45 */
46public class NewIssueDialog extends NewObjectiveDialog implements ItemListener {
47
48 private static final long serialVersionUID = 329109532781050011L;
49 // Variables
50 protected static final String DISCRETE = "Discrete";
51 protected static final String INTEGER = "Integer";
52 protected static final String REAL = "Real";
53
54 protected JComboBox issueType;
55 protected String[] issueTypes;
56 protected JPanel issuePropertyCards;
57 protected JPanel issuePropertyPanel;
58 protected JPanel discretePanel;
59 protected JPanel integerPanel;
60 protected JPanel realPanel;
61
62 protected JTextArea discreteTextArea;
63 protected JTextArea discreteTextEvaluationArea;
64
65 protected JTextField integerMinField;
66 protected JTextField integerOtherField;
67 protected JTextField integerUtilityLowestValue;
68 protected JTextField integerUtilityHighestValue;
69 protected JTextField integerMaxField;
70
71 protected JTextField realMinField;
72 protected JTextField realOtherField;
73 protected JTextField realLinearField;
74 protected JTextField realParameterField;
75 protected JTextField realMaxField;
76
77 // Constructors
78 public NewIssueDialog(DomainAndProfileEditorPanel owner) {
79 this(owner, false);
80 }
81
82 public NewIssueDialog(DomainAndProfileEditorPanel owner, boolean modal) {
83 this(owner, modal, "Create new Issue");
84 }
85
86 public NewIssueDialog(DomainAndProfileEditorPanel owner, boolean modal, String name) {
87 super(owner, modal, name); // This returns only after user filled in the
88 // form and pressed OK
89 }
90
91 // Methods
92 protected void initPanels() {
93 super.initPanels();
94 JPanel tmpIssPropP = constructIssuePropertyPanel();
95
96 this.add(tmpIssPropP, BorderLayout.CENTER);
97
98 }
99
100 private JPanel constructIssuePropertyPanel() {
101 String[] issueTypesTmp = { DISCRETE, INTEGER };
102 issueTypes = issueTypesTmp;
103
104 // Initialize the comboBox.
105 issueType = new JComboBox(issueTypes);
106 issueType.setSelectedIndex(0);
107 issueType.addItemListener(this);
108
109 // Initialize the input components
110 discreteTextArea = new JTextArea(20, 10);
111 discreteTextEvaluationArea = new JTextArea(20, 4);
112
113 integerMinField = new JTextField(15);
114 integerOtherField = new JTextField(15);
115 integerUtilityLowestValue = new JTextField(15);
116 integerUtilityHighestValue = new JTextField(15);
117
118 integerMaxField = new JTextField(15);
119 realMinField = new JTextField(15);
120 realOtherField = new JTextField(15);
121 realLinearField = new JTextField(15);
122 realParameterField = new JTextField(15);
123 realMaxField = new JTextField(15);
124
125 // Initialize the panels.
126 discretePanel = constructDiscretePanel();
127 integerPanel = constructIntegerPanel();
128 realPanel = constructRealPanel();
129
130 issuePropertyCards = new JPanel();
131 issuePropertyCards.setLayout(new CardLayout());
132 issuePropertyCards.add(discretePanel, DISCRETE);
133 issuePropertyCards.add(integerPanel, INTEGER);
134 issuePropertyCards.add(realPanel, REAL);
135
136 issuePropertyPanel = new JPanel();
137 issuePropertyPanel.setBorder(BorderFactory.createTitledBorder("Issue Properties"));
138 issuePropertyPanel.setLayout(new BorderLayout());
139 issuePropertyPanel.add(issueType, BorderLayout.PAGE_START);
140 issuePropertyPanel.add(issuePropertyCards, BorderLayout.CENTER);
141
142 return issuePropertyPanel;
143 }
144
145 private JPanel constructDiscretePanel() {
146 JPanel panel = new JPanel();
147 panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
148
149 JPanel textPanel = new JPanel();
150
151 textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.PAGE_AXIS));
152 JLabel textLabel = new JLabel("Edit the discrete values below.");
153 textPanel.add(textLabel);
154 textPanel.add(new JScrollPane(discreteTextArea));
155 panel.add(textPanel);
156
157 if (!treeFrame.isDomain()) {
158 JPanel evalPanel = new JPanel();
159 evalPanel.setLayout(new BoxLayout(evalPanel, BoxLayout.PAGE_AXIS));
160 JLabel evalLabel = new JLabel("Evaluation values.");
161 evalPanel.add(evalLabel);
162 evalPanel.add(new JScrollPane(discreteTextEvaluationArea));
163 panel.add(evalPanel);
164 }
165
166 // for a domain, do not show the evaluations
167 if (treeFrame.isDomain()) {
168 discreteTextEvaluationArea.setVisible(false);
169 }
170 discreteTextArea.setEditable(false);
171 if (treeFrame.isDomain() && treeFrame.hasNoProfiles()) { // so it's a
172 // domain
173 // with no
174 // profiles
175 discreteTextArea.setEditable(true);
176 }
177 return panel;
178 }
179
180 private JPanel constructIntegerPanel() {
181 JPanel panel = new JPanel();
182
183 GridLayout layout = new GridLayout(6, 4);
184
185 panel.setLayout(layout);
186 // SPACING
187 for (int i = 0; i < 4; i++) {
188 panel.add(new JLabel());
189 }
190
191 panel.add(new JLabel("Minimum value: "));
192 panel.add(integerMinField);
193
194 panel.add(new JLabel("Evaluation of minimum value: "));
195 panel.add(integerUtilityLowestValue);
196
197 for (int i = 0; i < 4; i++) {
198 panel.add(new JLabel());
199 }
200
201 panel.add(new JLabel("Maximum value: "));
202 panel.add(integerMaxField);
203
204 panel.add(new JLabel("Evaluation of maximum value: "));
205 panel.add(integerUtilityHighestValue);
206
207 if (((NegotiatorTreeTableModel) treeFrame.getTreeTable().getTree().getModel()).getUtilitySpace() == null) {
208 integerUtilityLowestValue.setEnabled(false);
209 integerUtilityHighestValue.setEnabled(false);
210 if (!treeFrame.hasNoProfiles()) {
211 integerMinField.setEnabled(false);
212 integerMaxField.setEnabled(false);
213 }
214 integerUtilityLowestValue.setToolTipText("Disabled until there is a Utility Space.");
215 integerUtilityHighestValue.setToolTipText("Disabled until there is a Utility Space.");
216 } else {
217 integerMinField.setEnabled(false);
218 integerMaxField.setEnabled(false);
219 }
220
221 for (int i = 0; i < 4; i++) {
222 panel.add(new JLabel());
223 }
224
225 return panel;
226 }
227
228 private JPanel constructRealPanel() {
229 JPanel panel = new JPanel();
230 panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
231 JLabel label = new JLabel("Give the bounds of the Real values:");
232 panel.add(label);
233
234 JPanel min = new JPanel();
235 min.add(new JLabel("Min: "));
236 min.add(realMinField);
237 panel.add(min);
238
239 JPanel lin = new JPanel();
240 lin.setAlignmentX(Component.LEFT_ALIGNMENT);
241 lin.add(new JLabel("Linear: "));
242 lin.add(realLinearField);
243 panel.add(lin);
244
245 JPanel par = new JPanel();
246 par.setAlignmentX(Component.LEFT_ALIGNMENT);
247 par.add(new JLabel("Constant: "));
248 par.add(realParameterField);
249 panel.add(par);
250
251 JPanel max = new JPanel();
252 max.add(new JLabel("Max: "));
253 max.add(realMaxField);
254 panel.add(max);
255
256 if (((NegotiatorTreeTableModel) treeFrame.getTreeTable().getTree().getModel()).getUtilitySpace() == null) {
257 realLinearField.setEnabled(false);
258 realLinearField.setToolTipText("Disabled until there is a Utility Space.");
259 realParameterField.setEnabled(false);
260 realParameterField.setToolTipText("Disabled until there is a Utility Space.");
261 }
262
263 return panel;
264 }
265
266 protected boolean getWeightCheck() {
267 return true;
268 }
269
270 /*
271 *
272 * get values from the input thingy empty lines are not aloowed and just
273 * ignored..
274 */
275 protected String[] getDiscreteValues() throws InvalidInputException {
276 String[] values = discreteTextArea.getText().split("\n");
277 return values;
278 }
279
280 /**
281 * Gets the evaluations for the discrete issue from the input field in this
282 * dialog.
283 *
284 * @return An arrayList with the evaluations. Now returns elements with
285 * value 0 to indicate non-entered (empty field) values.
286 *
287 * @throws InvalidInputException
288 * if illegal input is given
289 */
290 protected ArrayList<Integer> getDiscreteEvalutions() throws InvalidInputException, ClassCastException {
291 String[] evalueStrings = discreteTextEvaluationArea.getText().split("\n", -1);
292
293 ArrayList<Integer> evalues = new ArrayList<Integer>();
294 for (int i = 0; i < evalueStrings.length; i++) {
295 Integer value = 0;
296 if (!evalueStrings[i].equals("")) {
297 value = Integer.valueOf(evalueStrings[i]);
298 if (value < 0)
299 throw new InvalidInputException("Encountered " + value + ". Negative numbers are not allowed here");
300 }
301 evalues.add(value);
302 }
303 System.out.println(evalues);
304 return evalues;
305 }
306
307 protected int getIntegerMin() throws InvalidInputException {
308 if (!integerMinField.getText().equals(""))
309 return Integer.parseInt(integerMinField.getText());
310 else
311 return 0;
312 }
313
314 protected int getIntegerOther() throws InvalidInputException {
315 if (!integerOtherField.getText().equals(""))
316 return Integer.parseInt(integerOtherField.getText());
317 else
318 return 0;
319 }
320
321 protected double getUtilityLowestInteger() throws InvalidInputException {
322 if (!integerUtilityLowestValue.getText().equals(""))
323 return Double.parseDouble(integerUtilityLowestValue.getText());
324 else
325 return 0;
326 }
327
328 protected double getUtilityHeighestInteger() throws InvalidInputException {
329 if (!integerUtilityHighestValue.getText().equals(""))
330 return Double.parseDouble(integerUtilityHighestValue.getText());
331 else
332 return 0;
333 }
334
335 protected int getIntegerMax() throws InvalidInputException {
336 if (!integerMaxField.getText().equals(""))
337 return Integer.parseInt(integerMaxField.getText());
338 else
339 return 0;
340 }
341
342 protected double getRealMin() throws InvalidInputException {
343 if (!realMinField.getText().equals(""))
344 return Double.parseDouble(realMinField.getText());
345 else
346 return 0.0;
347 }
348
349 protected double getRealOther() throws InvalidInputException {
350 if (!realOtherField.getText().equals(""))
351 return Double.parseDouble(realOtherField.getText());
352 else
353 return 0.0;
354 }
355
356 protected double getRealLinear() throws InvalidInputException {
357 if (!realLinearField.getText().equals(""))
358 return Double.parseDouble(realLinearField.getText());
359 else
360 return 0.0;
361 }
362
363 protected double getRealParameter() throws InvalidInputException {
364 if (!realParameterField.getText().equals(""))
365 return Double.parseDouble(realParameterField.getText());
366 else
367 return 0.0;
368 }
369
370 protected double getRealMax() throws InvalidInputException {
371 if (!realMaxField.getText().equals(""))
372 return Double.parseDouble(realMaxField.getText());
373 else
374 return 0.0;
375 }
376
377 protected Issue constructIssue() {
378 return updateIssue(null);
379 }
380
381 /**
382 * This updates the data structures after the issue dialog was completed and
383 * user pressed OK. Not clear to me how it can return only an issue, so
384 * where are the values that were set as well? (the values should be put
385 * into a utility space)? The utility space is updated under water, and the
386 * dialog can access it via the parent node (treeFrame) that has access to
387 * the utility space....
388 *
389 * @param issue
390 * @return the same issue as provided (but then updated).
391 * @throws exception
392 * if issues can not be accepted. e.g. negative evaluation
393 * values or if no evaluator available for issue while there is
394 * a utiliyt space.
395 */
396 protected Issue updateIssue(Issue issue) {
397 // FIXME THIS CODE IS UGLY. The behaviour is not ok.
398 String name;
399 int number;
400 String description;
401 Objective selected = null; // The Objective that is selected in the
402 // tree, which will be the new Issue's
403 // parent.
404 boolean newIssue = (issue == null); // Defines if a new Issue is added,
405 // or if an existing Issue is being
406 // edited.
407
408 // Wouter: added: they threw away the old evaluator... bad because you
409 // loose the weight settings of the evaluator.
410 // Wouter; code is ugly. They createFrom a NEW evaluator anyway.
411 // And at the end they check whethere there is a util space
412 // anyway, and if not they throw away the new evaluator.....
413 // Also we are paying here for the mix between domain and utility space
414 // editor-in-one
415 AdditiveUtilitySpace uts = treeFrame.getNegotiatorTreeTableModel().getUtilitySpace();
416 Evaluator evaluator = null;
417 if (uts != null && issue != null)
418 evaluator = uts.getEvaluator(issue.getNumber());
419
420 try {
421 name = getObjectiveName();
422 number = getObjectiveNumber();
423 description = getObjectiveDescription();
424 } catch (InvalidInputException e) {
425 JOptionPane.showMessageDialog(this, e.getMessage());
426 return null;
427 }
428 // If no issue is given to be modified,
429 // construct a new one that is the child of the selected Objective.
430 if (newIssue) {
431 selected = treeFrame.getRoot();
432 }
433
434 String selectedType = (String) issueType.getSelectedItem();
435 // Issue issue = null;
436 if (selectedType == DISCRETE) {
437 // EvaluatorDiscrete evDis = null;
438 String[] values;
439 ArrayList<Integer> evalues = null;
440 try {
441 values = getDiscreteValues();
442 } catch (InvalidInputException e) {
443 JOptionPane.showMessageDialog(this, e.getMessage());
444 return null;
445 }
446 try {
447 evalues = getDiscreteEvalutions();
448 if (evalues == null)
449 System.out.println("No evalues");
450 } catch (Exception f) { // Can also be a casting exception.
451 JOptionPane.showMessageDialog(this, "Problem reading evaluation values:" + f.getMessage());
452 }
453
454 if (newIssue) {
455 issue = new IssueDiscrete(name, number, values);
456 } else if (issue instanceof IssueDiscrete) {
457 issue.setName(name);
458 issue.setNumber(number);
459 ((IssueDiscrete) issue).clear();
460 ((IssueDiscrete) issue).addValues(values);
461 }
462 List<ValueDiscrete> v_enum = ((IssueDiscrete) issue).getValues();
463
464 // load values into discrete evaluator
465 if (evaluator != null && evalues != null) {
466 try {
467 ((EvaluatorDiscrete) evaluator).clear();
468
469 for (int i = 0; i < v_enum.size(); i++) {
470 if (i < evalues.size()) // evalues field is 0 if error
471 // occured at that field.
472 {
473 ((EvaluatorDiscrete) evaluator).setEvaluation(((Value) v_enum.get(i)), evalues.get(i));
474 }
475 }
476 } catch (Exception e) {
477 JOptionPane.showMessageDialog(this, e.getMessage());
478 }
479
480 // Wouter: I don't like the way this works now but notime to
481 // correct it.
482
483 if (uts != null)
484 uts.addEvaluator(issue, evaluator);
485 }
486 } else if (selectedType == INTEGER) {
487 int min;
488 int max;
489
490 // Evaluator evInt = null;
491 try {
492 min = getIntegerMin();
493 max = getIntegerMax();
494
495 if (!integerUtilityLowestValue.getText().equals("")) {
496 // evInt = new EvaluatorInteger();
497 // evInt.setWeight(0.0);
498 ((EvaluatorInteger) evaluator).setLowerBound(min);
499 ((EvaluatorInteger) evaluator).setUpperBound(max);
500 ((EvaluatorInteger) evaluator).setLinearFunction(getUtilityLowestInteger(),
501 getUtilityHeighestInteger());
502 }
503 } catch (InvalidInputException e) {
504 JOptionPane.showMessageDialog(this, e.getMessage());
505 return null;
506 }
507 if (newIssue) {
508 issue = new IssueInteger(name, number, min, max);
509 } else if (issue instanceof IssueInteger) {
510 issue.setName(name);
511 issue.setNumber(number);
512 ((IssueInteger) issue).setLowerBound(min);
513 ((IssueInteger) issue).setUpperBound(max);
514 }
515 if (uts != null)
516 uts.addEvaluator(issue, evaluator);
517 } else if (selectedType == REAL) {
518 double min;
519 double max;
520 // Evaluator evReal = null;
521 try {
522 min = getRealMin();
523 // other = getRealOther();
524 max = getRealMax();
525 if (!realLinearField.getText().equals("")) {
526 // evReal = new EvaluatorReal();
527 // evReal.setWeight(0.0);
528 ((EvaluatorReal) evaluator).setLowerBound(min);
529 ((EvaluatorReal) evaluator).setUpperBound(max);
530 ((EvaluatorReal) evaluator).setLinearParam(getRealLinear());
531 } else if (!realParameterField.getText().equals("")) {
532 // evReal = new EvaluatorReal();
533 // evReal.setWeight(0.0);
534 ((EvaluatorReal) evaluator).setLowerBound(min);
535 ((EvaluatorReal) evaluator).setUpperBound(max);
536 ((EvaluatorReal) evaluator).setConstantParam(getRealParameter());
537 }
538 } catch (InvalidInputException e) {
539 JOptionPane.showMessageDialog(this, e.getMessage());
540 return null;
541 }
542 if (newIssue) {
543 issue = new IssueReal(name, number, min, max);
544 } else if (issue instanceof IssueReal) {
545 issue.setName(name);
546 issue.setNumber(number);
547 ((IssueReal) issue).setLowerBound(min);
548 ((IssueReal) issue).setUpperBound(max);
549 }
550 if (uts != null)
551 uts.addEvaluator(issue, evaluator);
552
553 } else {
554 JOptionPane.showMessageDialog(this, "Please select an issue type!");
555 return null;
556 }
557
558 issue.setDescription(description);
559 if (newIssue) {
560 selected.addChild(issue);
561 }
562
563 return issue;
564 }
565
566 /**
567 * Overrides actionPerformed from Objective.
568 */
569 public void actionPerformed(ActionEvent e) {
570 if (e.getSource() == okButton) {
571 Issue issue = constructIssue();
572 if (issue == null)
573 return;
574 else {
575 // Notify the model that the contents of the treetable have
576 // changed
577 NegotiatorTreeTableModel model = (NegotiatorTreeTableModel) treeFrame.getNegotiatorTreeTableModel();
578 Object[] path = { model.getRoot() };
579 model.treeStructureChanged(this, path);
580 this.dispose();
581 }
582 } else if (e.getSource() == cancelButton) {
583 this.dispose();
584 }
585 }
586
587 public void itemStateChanged(ItemEvent e) {
588 ((CardLayout) issuePropertyCards.getLayout()).show(issuePropertyCards, (String) e.getItem());
589 }
590}
Note: See TracBrowser for help on using the repository browser.