GAP_Knapsack.h

Go to the documentation of this file.
00001 //===========================================================================//
00002 // This file is part of the Decomp Solver Framework.                         //
00003 //                                                                           //
00004 // Decomp is distributed under the Common Public License as part of the      //
00005 // COIN-OR repository (http://www.coin-or.org).                              //
00006 //                                                                           //
00007 // Author: Matthew Galati, Lehigh University                                 //
00008 //                                                                           //
00009 // Copyright (C) 2002-2007, Lehigh University, Matthew Galati, and Ted Ralphs//
00010 // All Rights Reserved.                                                      //
00011 //===========================================================================//
00012 
00013 #ifndef GAP_KNAPSACK_INCLUDED
00014 #define GAP_KNAPSACK_INCLUDED
00015 
00016 //TODO: consider one container object rather than K and then 
00017 //  load constraint and profit each time?
00018 
00019 // --------------------------------------------------------------------- //
00030 // --------------------------------------------------------------------- //
00031 #include "UtilKnapsack.h"
00032 
00033 // --------------------------------------------------------------------- //
00034 class GAP_Knapsack {
00035 
00036  private:
00038    int             m_length;     //n (use j index)   
00039    double          m_capacity;   //b
00040    double        * m_profit;     //p[j]
00041    double        * m_weight;     //w[j]
00042    double        * m_profitSort; //temp storage for KP HS algo
00043    double        * m_weightSort; //temp storage for KP HS algo
00044    int           * m_solution;   //storage for solution
00045    double          m_optObj;     //storage for optimal objective
00046    SOR_IntDblArr * m_ratio;      //temp storage for ratios
00047 
00048  public:
00050    inline const int       getLength     ()  const {return m_length;     }
00051    inline const double    getCapacity   ()  const {return m_capacity;   }
00052    inline const double *  getProfit     ()  const {return m_profit;     }
00053    inline const double *  getWeight     ()  const {return m_weight;     }
00054    inline const double *  getProfitSort ()  const {return m_profitSort; }
00055    inline const double *  getWeightSort ()  const {return m_weightSort; }
00056    inline const int    *  getSolution   ()  const {return m_solution;   }
00057    inline const double    getOptObj     ()  const {return m_optObj;     }
00058    inline const SOR_IntDblArr * getRatio() const  {return m_ratio;      }
00059    inline double       *  getMutableProfit ()     {return m_profit;     }
00060    inline double       *  getMutableWeight ()     {return m_weight;     }
00061    
00063    void setLength(const int length) {m_length = length;}
00064 
00065  public:
00067    void populateAndSortRatio(const double * profit = 0,
00068                              const double * weight = 0){
00069       const double * p = profit;
00070       const double * w = weight;
00071       if(!p) 
00072          p = m_profit;
00073       if(!w) 
00074          w = m_weight;
00075       KnapsackSortRatioOut(m_length, p, w, 
00076                            m_profitSort, 
00077                            m_weightSort, 
00078                            m_ratio->arr);
00079    }
00080 
00081    int solveKnapsack(){
00082       int    statusKP = 0;
00083       int    status   = 0;
00084       statusKP = 
00085          KnapsackOptimizeHS(m_length, m_capacity,
00086                             m_profitSort, m_weightSort, 
00087                             m_solution, &m_optObj, &status);
00088       CoinAssert(!statusKP && !status);
00089       //printf("Knapsack OptObj = %g\n", m_optObj);
00090       return status;
00091    }
00092 
00093  private:
00099    GAP_Knapsack(const GAP_Knapsack &);
00100    GAP_Knapsack & operator=(const GAP_Knapsack &);
00101 
00102  public:
00106    GAP_Knapsack(const int      length,
00107                 const double   capacity,
00108                 const double * profit,
00109                 const double * weight) :
00110       m_length    (length),
00111       m_capacity  (capacity),
00112       m_profit    (0),
00113       m_weight    (0),
00114       m_profitSort(0),
00115       m_weightSort(0),
00116       m_solution  (0),
00117       m_ratio     (0)
00118       {
00119          int status;
00120          CoinAssert(length >= 0);
00121          m_profit     = new double[length];
00122          m_weight     = new double[length];
00123          m_profitSort = new double[length+1];
00124          m_weightSort = new double[length+1];
00125          m_solution   = new int[length];
00126          CoinAssertHint(m_profit     && m_weight && 
00127                         m_profitSort && m_weightSort && m_solution, 
00128                         "Error: Out of Memory");
00129          memcpy(m_profit, profit, length * sizeof(double));
00130          memcpy(m_weight, weight, length * sizeof(double));
00131 
00132          m_ratio = SOR_IntDblArrNew(length, &status);
00133          CoinAssert(!status);
00134       }
00135    
00136    ~GAP_Knapsack() {      
00137       UTIL_DELARR(m_profit);
00138       UTIL_DELARR(m_weight);
00139       UTIL_DELARR(m_profitSort);
00140       UTIL_DELARR(m_weightSort);
00141       UTIL_DELARR(m_solution);
00142       SOR_IntDblArrFree(&m_ratio);
00143    };
00144 };
00145 
00146 #endif

Generated on 3 Jun 2015 for Dip-All by  doxygen 1.6.1