View Javadoc

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 LinearInitiatorEvaluator extends StrategyEvaluator {
33     boolean first_response = true;
34  
35     double min;
36     double max;
37     double noquibble;
38  
39     // The Strategy Parameters
40     double price = 0;
41     double min_price = 0;
42     double max_price = 0;
43     double offer = 0;
44     double start_time = 0;
45     double end_time = 0;
46     double expected_cost = 0;
47     double reserve_price = 0;
48     double default_step = 0;
49     double step = 0;
50     double dt = 0;
51  
52     public int evaluateFirst(Vector goals, ProtocolDbResult info) {
53        this.goals = goals;
54        this.protocolInfo = info;
55  
56        min       = getDoubleParam("min.percent",80)/100.0;
57        max       = getDoubleParam("max.percent",120)/100.0;
58        noquibble = getDoubleParam("noquibble.range",2);
59  
60        default_step  = getDoubleParam("step.default",0.2);
61        reserve_price = getDoubleParam("reservation.price",Double.MAX_VALUE);
62  
63        Goal g = (Goal)goals.elementAt(0);
64        expected_cost = g.getCost();
65  
66        start_time = context.now();
67        end_time = g.getReplyTime().getTime();
68  
69        g.setCost(0);
70  
71        return MESSAGE;
72     }
73  
74     public int evaluateNext(DelegationStruct ds) {
75        this.goals = ds.goals;
76        if ( !ds.msg_type.equals("propose") )
77           return FAIL;
78  
79        Goal g = (Goal)ds.goals.elementAt(0);
80        offer = g.getCost();
81        double now = context.now();
82  
83        if ( first_response ) {
84           first_response = false;
85  
86           dt = now - start_time;
87           // modify end_time to conclude  before real end_time
88           end_time = end_time - dt;
89  
90           expected_cost = Math.max(offer,expected_cost);
91  
92           min_price = min*Math.min(reserve_price,expected_cost);
93           max_price = Math.min(max*expected_cost,reserve_price);
94  
95           price = min_price;
96           step = (max_price-min_price)*dt/(end_time-start_time);
97           step = Math.max(step,default_step);
98        }
99  
100       if ( offer < price + noquibble )
101          return OK;
102 
103       if ( !first_response ) price += step;
104 
105       if ( price < min_price )
106          return FAIL;
107       else if ( offer < price + noquibble )
108          return OK;
109 
110       if ( now + dt >= end_time )
111 			{
112          // running out of time: terminate
113 	       if ( offer < max_price )
114             return OK;
115          else
116             return FAIL;
117       }
118 
119       g.setCost(price);
120       return MESSAGE;
121    }
122 }