[338] | 1 | package bargainingchips.utilityfunctions;
|
---|
| 2 |
|
---|
| 3 | import bargainingchips.Chip;
|
---|
| 4 | import bargainingchips.ChipIssueValue;
|
---|
| 5 |
|
---|
| 6 | /**
|
---|
| 7 | *
|
---|
| 8 | * This class contains mathematical methods helping computational procedures mostly in utility functions,
|
---|
| 9 | * namely,
|
---|
[339] | 10 | * - Bezier // mathematical curve
|
---|
[338] | 11 | * - comb // mathematical combination
|
---|
| 12 | * - fc // mathematical factorial
|
---|
| 13 | * - uSlope // y value respected to x value at a sloped line
|
---|
| 14 | * - writeT // String list of values (e.g., prices, quantities, etc) assigned to a single variable (e.g. {@link Chip})
|
---|
| 15 | *
|
---|
| 16 | *
|
---|
| 17 | * @author Faria Nassiri-Mofakham
|
---|
| 18 | *
|
---|
| 19 | */
|
---|
| 20 |
|
---|
| 21 | public class UtilityHelperMethods<T>
|
---|
| 22 | {
|
---|
| 23 | /**
|
---|
| 24 | * @param input parameter (e.g., an offered price per a {@link Chip},
|
---|
| 25 | * and `n' data points of type T (e.g., as of Double[], Integer[], etc).
|
---|
| 26 | *
|
---|
| 27 | * @return Bezier value of rank `n' for the offered parameter.
|
---|
| 28 | **/
|
---|
| 29 | public double bezier(T[] desired, Double offered)
|
---|
| 30 | {
|
---|
| 31 | double bez = 0.0;
|
---|
| 32 | int n= desired.length;
|
---|
| 33 | for (int i=0; i<n; i++)
|
---|
| 34 | {
|
---|
| 35 | bez = ( (offered < (double) desired[0]) ? 1.0 : ( (offered > (double) desired[n-1]) ? 0.0 : comb(n,i)*Math.pow(1-offered, n)*Math.pow(i, n)*(double)desired[i]));
|
---|
| 36 | }
|
---|
| 37 | return ( (bez>1) ? 1 : (bez<0) ? 0 : bez);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
| 41 | * @return combination of `n' and `k'
|
---|
| 42 | **/
|
---|
| 43 | public int comb(int n, int k)
|
---|
| 44 | {
|
---|
| 45 | return fc(n)/(fc(n-k)*fc(k));
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * @return factorial of `n'
|
---|
| 50 | **/
|
---|
| 51 | public int fc(int n) {
|
---|
| 52 | int result = 1;
|
---|
| 53 | for (; n > 1; n--) {
|
---|
| 54 | result *= n;
|
---|
| 55 | }
|
---|
| 56 | return result;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * @param a param and 2 data points and respected utilities, showing a sloped line
|
---|
| 61 | * @return utility of param
|
---|
| 62 | **/
|
---|
| 63 | public double uSlope(double param, T tData1, T tData2, Double uData1, Double uData2)
|
---|
| 64 | {
|
---|
| 65 | Double data1 = (Double) tData1;
|
---|
| 66 | Double data2 = (Double) tData2;
|
---|
| 67 |
|
---|
| 68 | return (param-data1)*(uData1-uData2)/(data1-data2)+uData1;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
| 72 | * @return an String list of values assigned to a single variable, e.g. list of quantities or prices per a {@link Chip}
|
---|
| 73 | **/
|
---|
| 74 | public String writeT(ChipIssueValue<T[]> t)
|
---|
| 75 | {
|
---|
| 76 | String s = "{";
|
---|
| 77 | T[] j;
|
---|
| 78 | for (Chip c: t)
|
---|
| 79 | {
|
---|
| 80 | s += " " + c.toString() + "={";
|
---|
| 81 | j= t.getUnitValue(c);
|
---|
| 82 | //System.out.println("j: "+j);
|
---|
| 83 | for (int k=0; k<j.length; k++)
|
---|
| 84 | s += j[k].toString()+((k<j.length-1) ? ", " : "");
|
---|
| 85 | s += "}";
|
---|
| 86 | }
|
---|
| 87 | s += " }";
|
---|
| 88 | return s;
|
---|
| 89 | }
|
---|
| 90 | }
|
---|