GAP_Knapsack.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef GAP_KNAPSACK_INCLUDED
00014 #define GAP_KNAPSACK_INCLUDED
00015
00016
00017
00018
00019
00030
00031 #include "UtilKnapsack.h"
00032
00033
00034 class GAP_Knapsack {
00035
00036 private:
00038 int m_length;
00039 double m_capacity;
00040 double * m_profit;
00041 double * m_weight;
00042 double * m_profitSort;
00043 double * m_weightSort;
00044 int * m_solution;
00045 double m_optObj;
00046 SOR_IntDblArr * m_ratio;
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
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