/usr/src/RPM/BUILD/CoinBlis-0.93.2/Blis/examples/VRP/VrpVariable.h
Go to the documentation of this file.
00001 /*===========================================================================*
00002  * This file is part of a solver for the Vehicle Routing Problem             *
00003  * developed using the BiCePS Linear Integer Solver (BLIS).                  *
00004  *                                                                           *
00005  * This solver is distributed under the Eclipse Public License as part of    * 
00006  * the COIN-OR repository (http://www.coin-or.org).                          *
00007  *                                                                           *
00008  * Authors: Yan Xu, Lehigh University                                        *
00009  *          Ted Ralphs, Lehigh University                                    *
00010  *                                                                           *
00011  * Copyright (C) 2007 Yan Xu and Ted Ralphs.                                 *
00012  * All Rights Reserved.                                                      *
00013  *===========================================================================*/
00014 
00015 #ifndef VrpVariable_h_
00016 #define VrpVariable_h_
00017 
00018 //#############################################################################
00019 
00020 #include "BlisVariable.h"
00021 
00022 //#############################################################################
00023 
00025 class VrpVariable : public BlisVariable 
00026 {    
00027 private:
00028 
00029     /* The endpoints of the edge */
00030     int ends_[2];
00031     int uind_;
00032    
00033 protected:
00034 
00036     AlpsReturnStatus encodeVrp(AlpsEncoded *encoded) {
00037         AlpsReturnStatus status = AlpsReturnStatusOk;
00038 
00039         //std::cout << "****** encodeVrp var: size_ = " << size_ << std::endl;
00040 
00041         encoded->writeRep(ends_[0]);
00042         encoded->writeRep(ends_[1]);
00043         encoded->writeRep(uind_);
00044         
00045         return status;
00046     }    
00047 
00049     AlpsReturnStatus decodeVrp(AlpsEncoded &encoded) {
00050         AlpsReturnStatus status = AlpsReturnStatusOk;
00051 
00052         encoded.readRep(ends_[0]);
00053         encoded.readRep(ends_[1]);
00054         encoded.readRep(uind_);
00055         
00056         //std::cout << "****** decodeVrp var: size_ = " << size_ << std::endl;
00057 
00058         return status;
00059     }
00060     
00061 public:
00062 
00064     VrpVariable() {
00065        ends_[0] = 0;
00066        ends_[1] = 0;
00067     }
00068 
00070     VrpVariable(int v1, int v2, int cost, int ub) {
00071        ends_[0] = v1 < v2 ? v1 : v2;
00072        ends_[1] = v1 < v2 ? v2 : v1;
00073        uind_ = ends_[1]*(ends_[1] - 1)/2 + ends_[0];
00074        int indices [2];
00075        double values [2];
00076        indices[0] = ends_[0];
00077        indices[1] = ends_[1];
00078        values[0] = values[1] = 1.0;
00079        setData(2, indices, values);
00080        setIntType('B');
00081        setLbHard(0.0);
00082        setUbHard((double) ub);
00083        setObjCoef((double) cost);
00084     }
00085 
00087     virtual ~VrpVariable() {
00088         //std::cout << "delete a vrp variable " << std::endl;
00089     }
00090   
00093     inline int getIndex() { return uind_; }
00094     inline int getv0() { return ends_[0]; }
00095     inline int getv1() { return ends_[1]; }
00098     virtual void printDesc() {
00099         std::cout << "(" << getv0() << ", " << getv1() << ")";
00100     }
00101     
00103     virtual AlpsReturnStatus encode(AlpsEncoded *encoded){
00104         AlpsReturnStatus status;
00105 
00106         status = encodeBcpsObject(encoded);
00107         status = encodeBlis(encoded);
00108         status = encodeVrp(encoded);
00109         
00110         return status;
00111     }
00112 
00114     virtual AlpsKnowledge* decode(AlpsEncoded &encoded) const {
00115         AlpsReturnStatus status = AlpsReturnStatusOk;
00116         VrpVariable * var = new VrpVariable();    
00117         
00118         // Unpack Bcps part.
00119         status = var->decodeBcpsObject(encoded);
00120         if (status) {
00121             throw CoinError("Failed to decode Bcps part of var",
00122                             "decode",
00123                             "BlisObject");
00124         }
00125         
00126         // Unpack Blis part.
00127         status = var->decodeBlis(encoded);
00128         if (status) {
00129             throw CoinError("Failed to decode Blis part of var", 
00130                             "decode", 
00131                             "BlisObject");
00132         }
00133 
00134         // Unpack Vrp part.
00135         status = var->decodeVrp(encoded);
00136         if (status) {
00137             throw CoinError("Failed to decode Vrp part of var", 
00138                             "decode", 
00139                             "BlisObject");
00140         }
00141         return var;
00142     }
00143 
00144 };
00145 
00146 //#############################################################################
00147 
00148 #endif