Dip  0.92.4
MMKP_MCKnap.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 // Authors: Matthew Galati, SAS Institute Inc. (matthew.galati@sas.com) //
8 // Ted Ralphs, Lehigh University (ted@lehigh.edu) //
9 // Jiadong Wang, Lehigh University (jiw408@lehigh.edu) //
10 // //
11 // Copyright (C) 2002-2019, Lehigh University, Matthew Galati, and Ted Ralphs//
12 // All Rights Reserved. //
13 //===========================================================================//
14 
15 #ifndef MMKP_MCKNAP_INCLUDED
16 #define MMKP_MCKNAP_INCLUDED
17 
18 //---
19 //--- The MMKP MCKnap -- interface class for
20 //--- Pisinger's Multi-Choice Knapsack Solver
21 //--- http://www.diku.dk/~pisinger/codes.html
22 //---
23 
24 //---
25 //--- Multi-Choice Knapsack Problem
26 //--- min sum{i in 1..n, j in 1..l[i]} c[i,j] x[i,j]
27 //--- s.t. sum{i in 1..n, j in 1..l[i]} w[i,j] x[i,j] <= b
28 //--- sum{j in 1..l[i]} x[i,j] = 1 , i in 1..n
29 //---
30 
31 
32 // --------------------------------------------------------------------- //
33 extern "C"{
34 #include "mcknap.h"
35 }
36 
37 //extern solstruct optsol;
38 
39 // --------------------------------------------------------------------- //
40 #include "UtilMacros.h"
41 using namespace std;
42 
43 // --------------------------------------------------------------------- //
45  private:
46 
47  //---
48  //--- typedef int itype; // item profits and weights
49  //--- typedef long stype; // sum of pofit or weight
50  //--- typedef unsigned long vtype; // solution vector
51  //---
52  //---
53  //--- typedef struct {
54  //--- itemset *fset;
55  //--- itemset *lset;
56  //--- ntype size;
57  //--- } isetset;
58  //---
59  isetset * m_setset; // set of itemsetsets
60  double * m_costDbl; // temp storage
61  int * m_cost; // cost vector (integer)
62  int * m_weight; // weight vector (integer)
63  int m_nCols; // size of mc-knap problem
66  int m_capacity; // capacity (integer)
67  int m_cscale; // cost vector scale factor
68  int m_wscale; // weight vector scale factor
69 
70  public:
71  //separate function just to reset cost...from one call to next
72  void solveTrivialMaxSum(const double * redCost,
73  const double * origCost,
74  vector<int> & solInd,
75  double & varRedCost,
76  double & varOrigCost);
77 
78  inline const int getIndexIJ(const int i,
79  const int j) const{
80  return (i * m_nGroupCols) + j;
81  }
82 
83  inline pair<int,int> getIndexInv(const int index) const {
84  return make_pair(index / m_nGroupCols, index % m_nGroupCols);
85  }
86 
87  void setMCKnapData(const double capacity,
88  const double * weight);
89 
90  void solveMCKnap(const double * redCost,
91  const double * origCost,
92  vector<int> & solInd,
93  vector<double> & solEls,
94  double & varRedCost,
95  double & varOrigCost);
96 
97  public:
98  MMKP_MCKnap(const int nGroupRows,
99  const int nGroupCols) :
100  m_setset (NULL),
101  m_costDbl (NULL),
102  m_cost (NULL),
103  m_weight (NULL),
104  m_nCols (nGroupRows * nGroupCols),
105  m_nGroupRows(nGroupRows),
106  m_nGroupCols(nGroupCols),
107  m_capacity (0),
108  m_cscale (1),
109  m_wscale (1)
110  {
111  m_costDbl = new double[m_nCols];
112  m_cost = new int[m_nCols];
113  m_weight = new int[m_nCols];
114  assert(m_costDbl && m_cost && m_weight);
115  }
117  if(m_setset){
118  itemset * setPtr = m_setset->fset;
119  if(setPtr){
120  int i;
121  for(i = 0; i < m_setset->size; i++){
122  if(setPtr->fset)
123  free(setPtr->fset);
124  setPtr++;
125  }
126  free(m_setset->fset);
127  }
128  free(m_setset);
129  }
130  UTIL_DELARR(m_costDbl);
131  UTIL_DELARR(m_cost);
132  UTIL_DELARR(m_weight);
133  }
134 };
135 
136 
137 #endif
int m_nGroupCols
Definition: MMKP_MCKnap.h:65
const int getIndexIJ(const int i, const int j) const
Definition: MMKP_MCKnap.h:78
pair< int, int > getIndexInv(const int index) const
Definition: MMKP_MCKnap.h:83
isetset * m_setset
Definition: MMKP_MCKnap.h:59
itemrec * fset
Definition: mcknap.h:97
int * m_cost
Definition: MMKP_MCKnap.h:61
#define UTIL_DELARR(x)
Definition: UtilMacros.h:29
Definition: mcknap.h:95
int m_capacity
Definition: MMKP_MCKnap.h:66
int * m_weight
Definition: MMKP_MCKnap.h:62
int m_nGroupRows
Definition: MMKP_MCKnap.h:64
double * m_costDbl
Definition: MMKP_MCKnap.h:60
MMKP_MCKnap(const int nGroupRows, const int nGroupCols)
Definition: MMKP_MCKnap.h:98