RMOL Logo  1.00.0
C++ library of Revenue Management and Optimisation classes and functions
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
Emsr.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // C
5 #include <assert.h>
6 // STL
7 #include <iostream>
8 #include <cmath>
9 #include <list>
10 #include <algorithm>
11 // StdAir
12 #include <stdair/stdair_rm_types.hpp>
13 #include <stdair/bom/LegCabin.hpp>
14 #include <stdair/bom/VirtualClassStruct.hpp>
15 // RMOL
16 #include <rmol/bom/Emsr.hpp>
17 #include <rmol/bom/EmsrUtils.hpp>
18 
19 namespace RMOL {
20  // //////////////////////////////////////////////////////////////////
21  void Emsr::heuristicOptimisationByEmsrA (stdair::LegCabin& ioLegCabin) {
22  stdair::VirtualClassList_T& lVirtualClassList =
23  ioLegCabin.getVirtualClassList ();
24  const stdair::CabinCapacity_T& lCabinCapacity =
25  ioLegCabin.getOfferedCapacity();
26 
32  stdair::VirtualClassList_T::iterator itVC =lVirtualClassList.begin();
33  assert (itVC != lVirtualClassList.end());
34 
35  stdair::VirtualClassStruct& lFirstVC = *itVC;
36  lFirstVC.setCumulatedBookingLimit (lCabinCapacity);
37  ++itVC;
38  for (; itVC != lVirtualClassList.end(); ++itVC) {
39  stdair::VirtualClassStruct& lNextVC = *itVC;
40 
41  // Initialise the protection for class/bucket j.
42  stdair::ProtectionLevel_T lProtectionLevel = 0.0;
43 
44  for(stdair::VirtualClassList_T::iterator itHigherVC =
45  lVirtualClassList.begin(); itHigherVC != itVC; ++itHigherVC) {
46  stdair::VirtualClassStruct& lHigherVC = *itHigherVC;
47  const double lPartialProtectionLevel =
48  EmsrUtils::computeProtectionLevel (lHigherVC, lNextVC);
49  lProtectionLevel += lPartialProtectionLevel;
50  }
51  stdair::VirtualClassList_T::iterator itCurrentVC = itVC; --itCurrentVC;
52  stdair::VirtualClassStruct& lCurrentVC = *itCurrentVC;
53  lCurrentVC.setCumulatedProtection (lProtectionLevel);
54 
55  // Compute the booking limit for the class/bucket j+1 (can be negative).
56  const double lBookingLimit = lCabinCapacity - lProtectionLevel;
57 
58  // Set the booking limit for class/bucket j+1.
59  lNextVC.setCumulatedBookingLimit (lBookingLimit);
60  }
61  }
62 
63  // //////////////////////////////////////////////////////////////////
64  void Emsr::heuristicOptimisationByEmsrB (stdair::LegCabin& ioLegCabin) {
65  stdair::VirtualClassList_T& lVirtualClassList =
66  ioLegCabin.getVirtualClassList ();
67  const stdair::CabinCapacity_T& lCabinCapacity =
68  ioLegCabin.getOfferedCapacity();
69 
75  stdair::VirtualClassList_T::iterator itVC =lVirtualClassList.begin();
76  assert (itVC != lVirtualClassList.end());
77 
78  stdair::VirtualClassStruct& lFirstVC = *itVC;
79  lFirstVC.setCumulatedBookingLimit (lCabinCapacity);
80  ++itVC;
81  stdair::VirtualClassStruct lAggregatedVC = lFirstVC;
82  for (; itVC != lVirtualClassList.end(); ++itVC) {
83  stdair::VirtualClassStruct& lNextVC = *itVC;
84 
85  // Compute the protection level for the aggregated class/bucket
86  // using the Little-Wood formular.
87  const stdair::ProtectionLevel_T lProtectionLevel =
88  EmsrUtils::computeProtectionLevel (lAggregatedVC, lNextVC);
89 
90  // Set the protection level for class/bucket j.
91  stdair::VirtualClassList_T::iterator itCurrentVC = itVC; --itCurrentVC;
92  stdair::VirtualClassStruct& lCurrentVC = *itCurrentVC;
93  lCurrentVC.setCumulatedProtection (lProtectionLevel);
94 
95  // Compute the booking limit for class/bucket j+1 (can be negative).
96  const double lBookingLimit = lCabinCapacity - lProtectionLevel;
97 
98  // Set the booking limit for class/bucket j+1.
99  lNextVC.setCumulatedBookingLimit (lBookingLimit);
100 
101  // Compute the aggregated class/bucket of classes/buckets 1,..,j.
102  EmsrUtils::computeAggregatedVirtualClass (lAggregatedVC, lNextVC);
103 
104  }
105  }
106 
107  // //////////////////////////////////////////////////////////////////
108  void Emsr::heuristicOptimisationByEmsr (stdair::LegCabin& ioLegCabin) {
109  stdair::VirtualClassList_T& lVirtualClassList =
110  ioLegCabin.getVirtualClassList ();
111  const stdair::CabinCapacity_T& lCapacity = ioLegCabin.getOfferedCapacity();
112  ioLegCabin.emptyBidPriceVector();
113  stdair::BidPriceVector_T& lBidPriceVector =
114  ioLegCabin.getBidPriceVector();
115 
116  // Cabin capacity in integer.
117  const int lCabinCapacity = static_cast<const int> (lCapacity);
118 
119  // List of all EMSR values.
120  stdair::EmsrValueList_T lEmsrValueList;
121 
127  for (stdair::VirtualClassList_T::iterator itVC = lVirtualClassList.begin();
128  itVC != lVirtualClassList.end(); ++itVC) {
129  stdair::VirtualClassStruct& lCurrentVC = *itVC;
130  for (int k = 1; k <= lCabinCapacity; ++k) {
131  const double emsrValue = EmsrUtils::computeEmsrValue (k, lCurrentVC);
132  lEmsrValueList.push_back(emsrValue);
133  }
134  }
135 
136  // Sort the EMSR values from high to low.
137  std::sort(lEmsrValueList.rbegin(), lEmsrValueList.rend());
138 
139  // Sanity check
140  const int lEmsrValueListSize = lEmsrValueList.size();
141  assert (lEmsrValueListSize >= lCabinCapacity);
142 
143  // Copy the EMSR sorted values to the BPV.
144  stdair::EmsrValueList_T::const_iterator itCurrentValue =
145  lEmsrValueList.begin();
146  for (int j = 0; j < lCabinCapacity; ++j, ++itCurrentValue) {
147  const double lBidPrice = *itCurrentValue;
148  lBidPriceVector.push_back (lBidPrice);
149  }
150  lEmsrValueList.clear();
151 
152  // Build the protection levels and booking limits.
153  if (lVirtualClassList.size() > 1) {
154  int lCapacityIndex = 0;
155  for (stdair::VirtualClassList_T::iterator itVC =lVirtualClassList.begin();
156  itVC != lVirtualClassList.end();) {
157  stdair::VirtualClassStruct& lCurrentVC = *itVC;
158  if (itVC != lVirtualClassList.end()) {
159  ++itVC;
160  }
161  stdair::VirtualClassStruct& lNextVC = *itVC;
162  const stdair::Yield_T lNextYield = lNextVC.getYield();
163  while ((lCapacityIndex < lCabinCapacity)
164  && (lBidPriceVector.at(lCapacityIndex) > lNextYield)) {
165  ++lCapacityIndex;
166  }
167  lCurrentVC.setCumulatedProtection (lCapacityIndex);
168  lNextVC.setCumulatedBookingLimit (lCapacity - lCapacityIndex);
169  }
170  }
171  }
172 
173 }