Dip  0.92.4
GAP_Knapsack.h
Go to the documentation of this file.
1 //===========================================================================//
2 // This file is part of the Decomp Solver Framework. //
3 // //
4 // Decomp is distributed under the Common Public License as part of the //
5 // COIN-OR repository (http://www.coin-or.org). //
6 // //
7 // Author: Matthew Galati, Lehigh University //
8 // //
9 // Copyright (C) 2002-2007, Lehigh University, Matthew Galati, and Ted Ralphs//
10 // All Rights Reserved. //
11 //===========================================================================//
12 
13 #ifndef GAP_KNAPSACK_INCLUDED
14 #define GAP_KNAPSACK_INCLUDED
15 
16 //TODO: consider one container object rather than K and then
17 // load constraint and profit each time?
18 
19 // --------------------------------------------------------------------- //
30 // --------------------------------------------------------------------- //
31 #include "UtilKnapsack.h"
32 
33 // --------------------------------------------------------------------- //
34 class GAP_Knapsack {
35 
36  private:
38  int m_length; //n (use j index)
39  double m_capacity; //b
40  double * m_profit; //p[j]
41  double * m_weight; //w[j]
42  double * m_profitSort; //temp storage for KP HS algo
43  double * m_weightSort; //temp storage for KP HS algo
44  int * m_solution; //storage for solution
45  double m_optObj; //storage for optimal objective
46  SOR_IntDblArr * m_ratio; //temp storage for ratios
47 
48  public:
50  inline const int getLength () const {return m_length; }
51  inline const double getCapacity () const {return m_capacity; }
52  inline const double * getProfit () const {return m_profit; }
53  inline const double * getWeight () const {return m_weight; }
54  inline const double * getProfitSort () const {return m_profitSort; }
55  inline const double * getWeightSort () const {return m_weightSort; }
56  inline const int * getSolution () const {return m_solution; }
57  inline const double getOptObj () const {return m_optObj; }
58  inline const SOR_IntDblArr * getRatio() const {return m_ratio; }
59  inline double * getMutableProfit () {return m_profit; }
60  inline double * getMutableWeight () {return m_weight; }
61 
63  void setLength(const int length) {m_length = length;}
64 
65  public:
67  void populateAndSortRatio(const double * profit = 0,
68  const double * weight = 0){
69  const double * p = profit;
70  const double * w = weight;
71  if(!p)
72  p = m_profit;
73  if(!w)
74  w = m_weight;
76  m_profitSort,
77  m_weightSort,
78  m_ratio->arr);
79  }
80 
82  int statusKP = 0;
83  int status = 0;
84  statusKP =
87  m_solution, &m_optObj, &status);
88  CoinAssert(!statusKP && !status);
89  //printf("Knapsack OptObj = %g\n", m_optObj);
90  return status;
91  }
92 
93  private:
99  GAP_Knapsack(const GAP_Knapsack &);
101 
102  public:
106  GAP_Knapsack(const int length,
107  const double capacity,
108  const double * profit,
109  const double * weight) :
110  m_length (length),
111  m_capacity (capacity),
112  m_profit (0),
113  m_weight (0),
114  m_profitSort(0),
115  m_weightSort(0),
116  m_solution (0),
117  m_ratio (0)
118  {
119  int status;
120  CoinAssert(length >= 0);
121  m_profit = new double[length];
122  m_weight = new double[length];
123  m_profitSort = new double[length+1];
124  m_weightSort = new double[length+1];
125  m_solution = new int[length];
128  "Error: Out of Memory");
129  memcpy(m_profit, profit, length * sizeof(double));
130  memcpy(m_weight, weight, length * sizeof(double));
131 
132  m_ratio = SOR_IntDblArrNew(length, &status);
133  CoinAssert(!status);
134  }
135 
143  };
144 };
145 
146 #endif
double * getMutableProfit()
Definition: GAP_Knapsack.h:59
void SOR_IntDblArrFree(SOR_IntDblArrPtr *A)
SOR_IntDblArr * m_ratio
Definition: GAP_Knapsack.h:46
void KnapsackSortRatioOut(const int n, const double *p, const double *w, double *psort, double *wsort, SOR_IntDbl *ratio)
int m_length
GAP_Knapsack problem instance data.
Definition: GAP_Knapsack.h:38
const SOR_IntDblArr * getRatio() const
Definition: GAP_Knapsack.h:58
double m_optObj
Definition: GAP_Knapsack.h:45
int KnapsackOptimizeHS(const int n, const double c, double *p, double *w, int *x, double *z, int *pstatus)
int solveKnapsack()
Definition: GAP_Knapsack.h:81
const double * getProfitSort() const
Definition: GAP_Knapsack.h:54
const int * getSolution() const
Definition: GAP_Knapsack.h:56
const int getLength() const
Definition: GAP_Knapsack.h:50
#define UTIL_DELARR(x)
Definition: UtilMacros.h:29
#define CoinAssertHint(expression, hint)
Definition: CoinError.hpp:184
GAP_Knapsack(const GAP_Knapsack &)
const double * getProfit() const
Definition: GAP_Knapsack.h:52
double * m_profitSort
Definition: GAP_Knapsack.h:42
void populateAndSortRatio(const double *profit=0, const double *weight=0)
Definition: GAP_Knapsack.h:67
const double getOptObj() const
Definition: GAP_Knapsack.h:57
double * getMutableWeight()
Definition: GAP_Knapsack.h:60
double m_capacity
Definition: GAP_Knapsack.h:39
double * m_weight
Definition: GAP_Knapsack.h:41
GAP_Knapsack(const int length, const double capacity, const double *profit, const double *weight)
Default constructor.
Definition: GAP_Knapsack.h:106
SOR_IntDblArrPtr SOR_IntDblArrNew(int size, int *pstatus)
double * m_profit
Definition: GAP_Knapsack.h:40
~GAP_Knapsack()
Default constructor.
Definition: GAP_Knapsack.h:136
double * m_weightSort
Definition: GAP_Knapsack.h:43
const double getCapacity() const
Definition: GAP_Knapsack.h:51
int * m_solution
Definition: GAP_Knapsack.h:44
const double * getWeight() const
Definition: GAP_Knapsack.h:53
#define CoinAssert(expression)
Definition: CoinError.hpp:183
GAP_Knapsack & operator=(const GAP_Knapsack &)
const double * getWeightSort() const
Definition: GAP_Knapsack.h:55
void setLength(const int length)
Definition: GAP_Knapsack.h:63
SOR_IntDbl * arr
Definition: UtilKnapsack.h:29