[1] | 1 | package agents.ai2014.group12;
|
---|
| 2 |
|
---|
| 3 | import java.util.ArrayList;
|
---|
| 4 | import java.util.Set;
|
---|
| 5 |
|
---|
| 6 | import genius.core.issue.ValueDiscrete;
|
---|
| 7 | import genius.core.utility.EvaluatorDiscrete;
|
---|
| 8 |
|
---|
| 9 | /**
|
---|
| 10 | * PreferenceBlock is our representation of the Issue
|
---|
| 11 | *
|
---|
| 12 | */
|
---|
| 13 | public class PreferenceBlock {
|
---|
| 14 | ArrayList<Node> nodeList = new ArrayList<Node>();
|
---|
| 15 | String issue;
|
---|
| 16 | Double weight;
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
| 19 | * Constructor for the PreferenceBlock
|
---|
| 20 | * @param block, string to parse to get the values of the issue out.
|
---|
| 21 | * @param issueName
|
---|
| 22 | * @param weight
|
---|
| 23 | */
|
---|
| 24 | public PreferenceBlock(EvaluatorDiscrete evaluator, String issueName, Double weight){
|
---|
| 25 | this.issue = issueName;
|
---|
| 26 | this.weight = weight;
|
---|
| 27 | Set<ValueDiscrete> evalSet = evaluator.getValues();
|
---|
| 28 | Object[] evalArray = evalSet.toArray();
|
---|
| 29 | for(Object i : evalArray) {
|
---|
| 30 | ValueDiscrete description = (ValueDiscrete) i;
|
---|
| 31 | Double val = evaluator.getDoubleValue(description);
|
---|
| 32 | Node node = new Node(description.getValue(), val, false);
|
---|
| 33 | nodeList.add(node);
|
---|
| 34 | }
|
---|
| 35 | orderNodesLowToHigh();
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | public void normalizeNodeValues() {
|
---|
| 39 | Double sum = sumNodeValues();
|
---|
| 40 | if(sum > 0) {
|
---|
| 41 | for(int i = 0;i<nodeList.size();i++) {
|
---|
| 42 | nodeList.get(i).setValue(nodeList.get(i).getValue()/sum);
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public Double sumNodeValues() {
|
---|
| 48 | Double sum = 0.0;
|
---|
| 49 | for(int i = 0; i<nodeList.size();i++) {
|
---|
| 50 | sum += nodeList.get(i).getValue();
|
---|
| 51 | }
|
---|
| 52 | return sum;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | /**
|
---|
| 56 | * Returns the node that is currently the highest for this issue
|
---|
| 57 | * @return
|
---|
| 58 | */
|
---|
| 59 | public Node getHighestPreference() {
|
---|
| 60 | double max = 0;
|
---|
| 61 | Node rtn = new Node("", 0, false);
|
---|
| 62 | for(Node n : nodeList) {
|
---|
| 63 | if(n.getValue() >= max) {
|
---|
| 64 | rtn = n;
|
---|
| 65 | max = n.getValue();
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | return rtn;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
| 72 | * Searches the nodeList of this issue for the value name of a node
|
---|
| 73 | * @param s
|
---|
| 74 | * @return index of the found node
|
---|
| 75 | */
|
---|
| 76 | public int indexOf(String s) {
|
---|
| 77 | int index = 0;
|
---|
| 78 | for(int i = 0; i < nodeList.size(); i++) {
|
---|
| 79 | if(nodeList.get(i).getName().equals(s)) {
|
---|
| 80 | index = i;
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | return index;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /**
|
---|
| 87 | * Method to order the issue's nodes from low to high
|
---|
| 88 | */
|
---|
| 89 | public void orderNodesLowToHigh() {
|
---|
| 90 | nodeList = quicksort(nodeList);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | /**
|
---|
| 94 | * Basic quicksort algorithm
|
---|
| 95 | * @param input
|
---|
| 96 | * @return sorted arraylist of nodes
|
---|
| 97 | */
|
---|
| 98 | public ArrayList<Node> quicksort(ArrayList<Node> input) {
|
---|
| 99 | if(input.size() <= 1){
|
---|
| 100 | return input;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | int middle = (int) Math.ceil((double)input.size() / 2);
|
---|
| 104 | Node pivot = input.get(middle);
|
---|
| 105 |
|
---|
| 106 | ArrayList<Node> less = new ArrayList<Node>();
|
---|
| 107 | ArrayList<Node> greater = new ArrayList<Node>();
|
---|
| 108 |
|
---|
| 109 | for (int i = 0; i < input.size(); i++) {
|
---|
| 110 | if(input.get(i).getValue() <= pivot.getValue()){
|
---|
| 111 | if(i == middle){
|
---|
| 112 | continue;
|
---|
| 113 | }
|
---|
| 114 | less.add(input.get(i));
|
---|
| 115 | }
|
---|
| 116 | else{
|
---|
| 117 | greater.add(input.get(i));
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | return concatenate(quicksort(less), pivot, quicksort(greater));
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | /**
|
---|
| 125 | * Helper method for the quicksort algorithm, is used to concatenate the two arraylists to eachother
|
---|
| 126 | * @param less
|
---|
| 127 | * @param pivot
|
---|
| 128 | * @param greater
|
---|
| 129 | * @return concatenated arraylist
|
---|
| 130 | */
|
---|
| 131 | private ArrayList<Node> concatenate(ArrayList<Node> less, Node pivot, ArrayList<Node> greater){
|
---|
| 132 |
|
---|
| 133 | ArrayList<Node> list = new ArrayList<Node>();
|
---|
| 134 |
|
---|
| 135 | for (int i = 0; i < less.size(); i++) {
|
---|
| 136 | list.add(less.get(i));
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | list.add(pivot);
|
---|
| 140 |
|
---|
| 141 | for (int i = 0; i < greater.size(); i++) {
|
---|
| 142 | list.add(greater.get(i));
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | return list;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * Nodelist getter
|
---|
| 150 | * @return
|
---|
| 151 | */
|
---|
| 152 | public ArrayList<Node> getList(){
|
---|
| 153 | return nodeList;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | /**
|
---|
| 157 | * Method to retrieve all the nodes that have not yet been flagged
|
---|
| 158 | * @return
|
---|
| 159 | */
|
---|
| 160 | public ArrayList<Node> getValuesWithoutFlag() {
|
---|
| 161 | ArrayList<Node> rtn = new ArrayList<Node>();
|
---|
| 162 | for(int i = 0; i<nodeList.size(); i++) {
|
---|
| 163 | if(!nodeList.get(i).getFlag()) {
|
---|
| 164 | rtn.add(nodeList.get(i));
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 | return rtn;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | /**
|
---|
| 171 | * Method to retrieve the index of the highest ordered value that is not yet flagged.
|
---|
| 172 | * @return
|
---|
| 173 | */
|
---|
| 174 | public int getHighestIndexWithoutFlag() {
|
---|
| 175 | int rtnIndex = -1;
|
---|
| 176 | for(int i = 0; i<nodeList.size(); i++) {
|
---|
| 177 | if(nodeList.get(i).getFlag()) {
|
---|
| 178 | return i-1;
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 | return rtnIndex;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | }
|
---|