- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

# /home/coin/svn-release/Bcps-0.93.10/Alps/examples/Knap/KnapTreeNode.h

Go to the documentation of this file.

    00001 /*===========================================================================*
    00002  * This file is part of the Abstract Library for Parallel Search (ALPS).     *
    00003  *                                                                           *
    00004  * ALPS is distributed under the Eclipse Public License as part of the       *
    00005  * COIN-OR repository (http://www.coin-or.org).                              *
    00006  *                                                                           *
    00007  * Authors: Yan Xu, Lehigh University                                       *
    00008  *          Ted Ralphs, Lehigh University                                    *
    00009  *          Laszlo Ladanyi, IBM T.J. Watson Research Center                  *
    00010  *          Matthew Saltzman, Clemson University                             *
    00011  *                                                                           *
    00012  *                                                                           *
    00013  * Copyright (C) 2001-2013, Lehigh University, Yan Xu, and Ted Ralphs.       *
    00014  *===========================================================================*/
    00015
    00016 #ifndef KnapTreeNode_h_
    00017 #define KnapTreeNode_h_
    00018
    00019 #include <utility>
    00020
    00021 #include "AlpsTreeNode.h"
    00022
    00023 #include "KnapModel.h"
    00024
    00025
    00026 //#############################################################################
    00027
    00028 enum KnapVarStatus {
    00029     KnapVarFree,
    00030     KnapVarFixedToOne,
    00031     KnapVarFixedToZero
    00032 };
    00033
    00034 //#############################################################################
    00035
    00036 class KnapNodeDesc : public AlpsNodeDesc {
    00037
    00038  private:
    00039
    00040     /* Here, we need to fill in what the node description will look
    00041        like. For now, we will not use differencing -- just explicitly
    00042        represent it. Probably this means that we will just store the
    00043        original problem data and a list of the variables that have been
    00044        fixed. */
    00045
    00048     // In the constructor, we should allocate this array to be the right
    00049     // length.
    00050     KnapVarStatus* varStatus_;
    00051
    00053     int usedCapacity_;
    00054     int usedValue_;
    00055
    00056  public:
    00057 #if 0
    00058     KnapNodeDesc()
    00059         :
    00060         usedCapacity_(0),
    00061         usedValue_(0)
    00062         {
    00063             const int n =
    00064                 dynamic_cast<const KnapModel*>(model_)->getNumItems();
    00065             varStatus_ = new KnapVarStatus[n];
    00066             std::fill(varStatus_, varStatus_ + n, KnapVarFree);
    00067         }
    00068 #endif
    00069
    00070     KnapNodeDesc(KnapModel* m)
    00071         :
    00072         AlpsNodeDesc(m),
    00073         usedCapacity_(0),
    00074         usedValue_(0)
    00075         {
    00076             //model_ = m;      // data member declared in Alps
    00077             const int n = dynamic_cast<KnapModel* >(model_)->getNumItems();
    00078             varStatus_ = new KnapVarStatus[n];
    00079             std::fill(varStatus_, varStatus_ + n, KnapVarFree);
    00080         }
    00081
    00082     KnapNodeDesc(KnapModel* m, KnapVarStatus *& st, int cap, int val)
    00083         :
    00084         //      model_(m), varStatus_(0), usedCapacity_(cap), usedValue_(val) {
    00085         AlpsNodeDesc(m),
    00086         varStatus_(0),
    00087         usedCapacity_(cap),
    00088         usedValue_(val)
    00089         {
    00090             // model_ = m;
    00091             std::swap(varStatus_, st);
    00092         }
    00093
    00094     virtual ~KnapNodeDesc() { delete[] varStatus_; }
    00095
    00096     // inline KnapModel* getModel() { return model_; }
    00097     // inline const KnapModel* getModel() const { return model_; }// move Alps
    00098
    00099     inline void setVarStatus(const int i, const KnapVarStatus status)
    00100         { varStatus_[i] = status; }
    00101
    00102     inline KnapVarStatus getVarStatus(const int i)
    00103         { return varStatus_[i]; }
    00104
    00105     inline const KnapVarStatus* getVarStati() const
    00106         { return varStatus_; }
    00107
    00108     inline int getUsedCapacity() const { return usedCapacity_; }
    00109     inline int getUsedValue() const { return usedValue_; }
    00110 };
    00111
    00112 //#############################################################################
    00113
    00114 class KnapTreeNode : public AlpsTreeNode {
    00115  private:
    00116     // NO: default constructor, copy constructor, assignment operator
    00117     KnapTreeNode(const KnapTreeNode&);
    00118     KnapTreeNode& operator=(const KnapTreeNode&);
    00119
    00120  private:
    00122     int branchedOn_;
    00123
    00124  public:
    00125     KnapTreeNode() : branchedOn_(-1) {
    00126         desc_ = new KnapNodeDesc(dynamic_cast<KnapModel* >
    00127                                  (getKnowledgeBroker()->getModel()));
    00128     }
    00129
    00130     KnapTreeNode(KnapModel* model) : branchedOn_(-1) {
    00131         desc_ = new KnapNodeDesc(model);
    00132     }
    00133
    00134     KnapTreeNode(KnapNodeDesc*& desc) : branchedOn_(-1) {
    00135         desc_ = desc;
    00136         desc = 0;
    00137         // At the time of registering node, that node hasn't set broker
    00138         // desc_->setModel(getKnowledgeBroker()->getDataPool()->getModel());
    00139     }
    00140
    00141     ~KnapTreeNode() { }
    00142
    00143     void setBranchOn(int b) { branchedOn_ = b; }
    00144
    00145     virtual AlpsTreeNode* createNewTreeNode(AlpsNodeDesc*& desc) const;
    00146
    00147     virtual int process(bool isRoot = false, bool rampUp = false);
    00148
    00149     virtual std::vector< CoinTriple<AlpsNodeDesc*, AlpsNodeStatus, double> >
    00150         branch();
    00151
    00152     // We only need these for parallel execution I think...
    00153     virtual AlpsEncoded* encode() const;
    00154
    00155     virtual AlpsKnowledge* decode(AlpsEncoded&) const;
    00156 };
    00157
    00158 #endif