/home/coin/SVN-release/Bcp-1.2.1/Applications/Mkc/include/MKC_var.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2000, International Business Machines
00002 // Corporation and others.  All Rights Reserved.
00003 #ifndef _MKC_VAR_H
00004 #define _MKC_VAR_H
00005 
00006 #include <cfloat>
00007 #include "BCP_vector.hpp"
00008 #include "BCP_var.hpp"
00009 #include "BCP_buffer.hpp"
00010 
00011 enum MKC_var_t {
00012    MKC_RegularVar,
00013    MKC_BranchingVar
00014 };
00015 
00016 class MKC_var : public BCP_var_algo {
00017 public:
00018    double cost;   // the cost of this variable
00019    int entry_num; // 1  + the number of objects put into the KS.
00020    int * entries; // the first entry is the index of the KS this var belongs
00021                   // to, ks_index the orders put into the KS.
00022    int color[2];  // the two colors of the variable
00023 public:
00024    MKC_var(const double c, const int num, int *& entr, const int clr[2]) :
00025       BCP_var_algo(BCP_BinaryVar, c, 0.0, 1.0),
00026       cost(c), entry_num(num), entries(entr) {
00027          entr = 0;
00028          color[0] = clr[0];
00029          color[1] = clr[1];
00030    }
00031    MKC_var(const MKC_var& x) :
00032       BCP_var_algo(BCP_BinaryVar, x.cost, 0.0, 1.0),
00033       cost(x.cost), entry_num(x.entry_num), entries(new int[x.entry_num]) {
00034          memcpy(entries, x.entries, x.entry_num * sizeof(int));
00035          color[0] = x.color[0];
00036          color[1] = x.color[1];
00037    }
00038    MKC_var(BCP_buffer& buf) :
00039      BCP_var_algo(BCP_BinaryVar, 0.0, 0.0, 1.0),
00040      cost(0.0), entry_num(0), entries(0) {
00041      buf.unpack(cost).unpack(entries, entry_num)
00042         .unpack(color[0]).unpack(color[1]);
00043      set_obj(cost);
00044    }
00045 
00046    ~MKC_var() {
00047       delete[] entries;
00048    }
00049 
00050 public:
00051   double dj(const double* dual) const {
00052     double red_cost = cost;
00053     for (int i = entry_num - 1; i >= 0; --i)
00054       red_cost -= dual[entries[i]];
00055     return red_cost;
00056   }
00057 
00058   void pack(BCP_buffer& buf) const {
00059     buf.pack(cost).pack(entries, entry_num).pack(color[0]).pack(color[1]);
00060   }
00061 };
00062 
00063 //#############################################################################
00064 
00065 class MKC_branching_var : public BCP_var_algo {
00066 public:
00067    // the index of the order that is assigned to a list of slabs
00068    int order_index;
00069    int slab_num;
00070    int * slab_list;
00071 public:
00072    MKC_branching_var(const int order_ind, const int num, int *& slist) :
00073       BCP_var_algo(BCP_ContinuousVar, 0.0, 0.0, 1e40),
00074       order_index(order_ind), slab_num(num), slab_list(slist) {
00075          slist = 0;
00076    }
00077    MKC_branching_var(BCP_buffer& buf) :
00078      BCP_var_algo(BCP_ContinuousVar, 0.0, 0.0, 1e40),
00079      order_index(-1), slab_num(0), slab_list(0) {
00080      buf.unpack(order_index).unpack(slab_list, slab_num);
00081    }
00082 
00083    ~MKC_branching_var() {
00084       delete[] slab_list;
00085    }
00086 
00087   void pack(BCP_buffer& buf) const {
00088     buf.pack(order_index).pack(slab_list, slab_num);
00089   }
00090 };
00091 
00092 //#############################################################################
00093 
00094 inline BCP_var_algo*
00095 MKC_var_unpack(BCP_buffer& buf)
00096 {
00097   int type;
00098   buf.unpack(type);
00099   switch (type) {
00100   case MKC_RegularVar: return new MKC_var(buf);
00101   case MKC_BranchingVar: return new MKC_branching_var(buf);
00102   }
00103   throw BCP_fatal_error("MKC: Unknown variable type in MKC_var_unpack().\n");
00104   return 0; // fake return
00105 }
00106 
00107 //#############################################################################
00108 
00109 inline void
00110 MKC_var_pack(const BCP_var_algo* var, BCP_buffer& buf)
00111 {
00112   const MKC_var* regular_var = dynamic_cast<const MKC_var*>(var);
00113   if (regular_var) {
00114     const int type = MKC_RegularVar;
00115     buf.pack(type);
00116     regular_var->pack(buf);
00117     return;
00118   }
00119 
00120   const MKC_branching_var* b_var = dynamic_cast<const MKC_branching_var*>(var);
00121   if (b_var) {
00122     const int type = MKC_BranchingVar;
00123     buf.pack(type);
00124     b_var->pack(buf);
00125     return;
00126   }
00127 
00128   throw BCP_fatal_error("MKC: Unknown variable type in MKC_var_pack().\n");
00129 }
00130 
00131 #endif

Generated on Thu Jan 15 03:00:58 2009 for coin-Bcp by  doxygen 1.4.7