1 /*
2 * The contents of this file are subject to the BT "ZEUS" Open Source
3 * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file
4 * except in compliance with the Licence. You may obtain a copy of the Licence
5 * from $ZEUS_INSTALL/licence.html or alternatively from
6 * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7 *
8 * Except as stated in Clause 7 of the Licence, software distributed under the
9 * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or
10 * implied. See the Licence for the specific language governing rights and
11 * limitations under the Licence.
12 *
13 * The Original Code is within the package zeus.*.
14 * The Initial Developer of the Original Code is British Telecommunications
15 * public limited company, whose registered office is at 81 Newgate Street,
16 * London, EC1A 7AJ, England. Portions created by British Telecommunications
17 * public limited company are Copyright 1996-9. All Rights Reserved.
18 *
19 * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20 */
21
22
23
24 package zeus.actors.graphs;
25
26 import java.util.*;
27 import zeus.util.*;
28 import zeus.actors.*;
29 import zeus.actors.rtn.util.*;
30 import zeus.concepts.*;
31
32 public class SimpleInitiatorEvaluator extends StrategyEvaluator {
33 protected static final double round_time = 0.1;
34
35 // The Strategy Parameters
36 double min;
37 double max;
38 double noquibble;
39
40 double cost = 0;
41 double price = 0;
42 double offer = 0;
43 double start_time = 0;
44 double end_time = 0;
45 double rate = 0;
46
47 public int evaluateFirst(Vector goals, ProtocolDbResult info) {
48 this.goals = goals;
49 this.protocolInfo = info;
50
51 min = getDoubleParam("min.percent",80)/100.0;
52 max = getDoubleParam("max.percent",120)/100.0;
53 noquibble = getDoubleParam("noquibble.range",2);
54
55 Goal g = (Goal)goals.elementAt(0);
56 ExternalDb db = context.ExternalDb();
57
58 end_time = g.getReplyTime().getTime() - round_time;
59 start_time = context.now();
60
61 // determine expected cost of goods
62 cost = g.getCost();
63 if ( Misc.isZero(cost) ) {
64 Fact f = g.getFact();
65 cost = f.getNetCost();
66 }
67
68 min *= cost;
69 max *= cost;
70 double r_price = getDoubleParam("reservation.price",Double.MAX_VALUE);
71 max = Math.min(max,r_price);
72
73 price = (int)min;
74 rate = Math.log(max/min)/(end_time-start_time);
75
76 g.setCost(price);
77 // System.out.println(context.whoami() + " EvaluateFirst: " + start_time + ", " + end_time + ", " + price);
78 return MESSAGE;
79 }
80
81 public int evaluateNext(DelegationStruct ds) {
82 this.goals = ds.goals;
83 if ( !ds.msg_type.equals("propose") ) {
84 // System.out.println("FAIL 0");
85 return FAIL;
86 }
87
88 Goal g = (Goal)ds.goals.elementAt(0);
89
90 offer = g.getCost();
91
92 if ( offer < price + noquibble ) {
93 // System.out.println("OK -1");
94 return OK;
95 }
96
97 double dt = context.now() - start_time;
98 // System.out.println("dt = " + dt + " rate = " + rate);
99
100 price = (int)(min*Math.exp(rate*dt));
101
102 // System.out.println(context.whoami() + " EvaluateNext " + g.getFact().getType() + ": " + offer + " " + price);
103
104 if ( offer < price + noquibble ) {
105 // System.out.println("OK 0");
106 return OK;
107 }
108
109 if ( context.now() + round_time >= end_time ) {
110 if ( offer < max ) {
111 // System.out.println("OK 1");
112 return OK;
113 }
114 else {
115 // System.out.println("FAIl 1");
116 return FAIL;
117 }
118 }
119
120 g.setCost(price);
121 return MESSAGE;
122 }
123 }