CNDSM
1.00
|
The class ContQKsk defines an interface for Continuous Quadratic Knapsack problems (CQKnP) solvers. More...
#include <CQKnPClass.h>
Classes | |
class | CQKException |
Small class for exceptions. More... | |
class | Inf |
Small class using std::numeric_limits to extract the "infinity" value of a basic type (just use Inf<type>()). More... | |
Public Types | |
Public types |
Public Member Functions | |
virtual void | LoadSet (const int pn=0, const double *pC=0, const double *pD=0, const double *pA=0, const double *pB=0, const double pV=0, const bool sns=true)=0 |
Inputs a new CQKnP instance. More... | |
virtual void | ReadInstance (std::istream &inFile, bool RBV=false) |
Read the instance from file. More... | |
virtual void | SetEps (const double eps=1e-6)=0 |
Defines the precision required to construct the solution of the knapsack problem. More... | |
virtual CQKStatus | SolveKNP (void)=0 |
SolveKNP() attempts to solve the current CQKnP instance, returning the status indicating the outcome of the optimization. More... | |
virtual const double * | KNPGetX (void)=0 |
Return the best feasible solution of the CQKnP found so far (assuming CheckFsb() == true, see above), which is optimal if SolveKNP() has returned kOK (see above). More... | |
virtual double | KNPGetPi (void)=0 |
Returns the optimal dual multiplier of the knapsack constraint; the returned value is dependable only if SolveKNP() has returned kOK (see above). More... | |
virtual double | KNPGetFO (void)=0 |
Returns the optimal value of the objective function of the problem. More... | |
virtual int | KNPn (void) |
Returns the current number of items. More... | |
virtual void | KNPLCosts (double *csts, const int *nms=0, int strt=0, int stp=Inf< int >())=0 |
The linear costs of the items are written into csts[]. More... | |
virtual void | KNPQCosts (double *csts, const int *nms=0, int strt=0, int stp=Inf< int >())=0 |
The quadratic costs of the items are written into csts[]. More... | |
virtual double | KNPLCost (const int i)=0 |
Return the linear cost of the i-th item (i = 0 . More... | |
virtual double | KNPQCost (const int i)=0 |
Return the quadratic cost of the i-th item (i = 0 . More... | |
virtual void | KNPLBnds (double *bnds, const int *nms=0, int strt=0, int stp=Inf< int >())=0 |
The lower bounds of the items are written into bnds[]. More... | |
virtual void | KNPUBnds (double *bnds, const int *nms=0, int strt=0, int stp=Inf< int >())=0 |
The upper bounds of the items are written into bnds[]. More... | |
virtual double | KNPLBnd (const int i)=0 |
Return the lower bound of the i-th item (i = 0 . More... | |
virtual double | KNPUBnd (const int i)=0 |
Return the upper bound of the i-th item (i = 0 . More... | |
virtual double | KNPVlm (void)=0 |
Returns the volume `V' of the knapsack. More... | |
virtual void | WriteInstance (std::ostream &oFile, const int precc=16, const int precv=16) |
Write the instance to the provided ostream in the "complete" format read by ReadInstance() [see above]. More... | |
virtual void | ChgLCosts (const double *csts, const int *nms=0, int strt=0, int stp=Inf< int >())=0 |
Change the linear cost coefficients that are: More... | |
virtual void | ChgQCosts (const double *csts, const int *nms=0, int strt=0, int stp=Inf< int >())=0 |
Change the quadratic cost coefficients that are: More... | |
virtual void | ChgLCost (int i, const double cst)=0 |
Change the linear cost coefficient of item i to cst. More... | |
virtual void | ChgQCost (int i, const double cst)=0 |
Change the quadratic cost coefficient of item i to cst. More... | |
virtual void | ChgLBnds (const double *bnds, const int *nms=0, int strt=0, int stp=Inf< int >())=0 |
Change the item lower bounds that are: More... | |
virtual void | ChgUBnds (const double *bnds, const int *nms=0, int strt=0, int stp=Inf< int >())=0 |
Change the item upper bounds that are: More... | |
virtual void | ChgLBnd (int i, const double bnd)=0 |
Change the lower bound of item i to bnd. More... | |
virtual void | ChgUBnd (int i, const double bnd)=0 |
Change the upper bound of item i to bnd. More... | |
virtual void | ChgVlm (const double NVlm)=0 |
Change the volume. More... | |
Protected Attributes | |
int | n |
total number of items | |
int | status |
status of the algorithm: it is an int so that derived classes can use it to encode other information apart from the return value of SolveKNP(). More... | |
The class ContQKsk defines an interface for Continuous Quadratic Knapsack problems (CQKnP) solvers.
The data of the problem consists of a set of items = ( 0 , ... , n - 1 ), where each item i has:
The problem requires to find the most valuable set of items which fit in a knapsack of given volume V, but partly accepting items is allowed. The formulation of the problem is therefore:
This is a convex quadratic problem, hence a "easy" one. However it is repeatedly solved as a subproblem in many applications, so a fast solver may ultimately be useful.
enum CQKStatus |
|
pure virtual |
Inputs a new CQKnP instance.
The meaning of the parameters is the following:
This method must be called prior to invoking any other method of the class, with the exception of SetKNPLog() and SetEps() (not to mention the constructor, of course).
Referenced by CQKnPClass::ReadInstance().
|
inlinevirtual |
Read the instance from file.
While virtual, the method is implemented in the base class: the instance data is read in temporary data structures and then LoadSet() [see above] is called using these. Thus, the method will work for any derived class with no modification, since it uses the class-provided implmentation of LoadSet(); however, being virtual it can be re-implemented for maximum efficiency if desired.
The method supports two formats of the file: a "simplified" one and a "complete" one. The simplified format, assumed when RBV is false, is:
<number of items (n)>
for i = 0 to n - 1 <linear cost="" of="" item="" i>="">
for i = 0 to n - 1 <quadratic cost="" of="" item="" i>="">
In this case all lower bounds are zero, all upper bounds are INF, and the volume is 1. Otherwise (RBV == true) the file must continue with
for i = 0 to n - 1 <lower bound="" of="" item="" i>="">
for i = 0 to n - 1 <upper bound="" of="" item="" i>="">
<volume of="" knapsack>="">
Note that this method is "alternative" to LoadSet() (in the sense that the latter is invoked inside), so the object is "ready to use" once this method returns.
References CQKnPClass::LoadSet().
|
pure virtual |
Defines the precision required to construct the solution of the knapsack problem.
|
pure virtual |
SolveKNP() attempts to solve the current CQKnP instance, returning the status indicating the outcome of the optimization.
Possible values are:
|
pure virtual |
Return the best feasible solution of the CQKnP found so far (assuming CheckFsb() == true, see above), which is optimal if SolveKNP() has returned kOK (see above).
|
pure virtual |
Returns the optimal dual multiplier of the knapsack constraint; the returned value is dependable only if SolveKNP() has returned kOK (see above).
|
pure virtual |
Returns the optimal value of the objective function of the problem.
The method typically returns INF if SolveKNP() == kUnfeasible. It returns a finite feasible value if SolveKNP() == kOK or SolveKNP() == kStopped, but in the latter case it depends on the solver whether this is a lower or an upper bound on the optimal value. The return value is undefined in all other cases.
|
inlinevirtual |
Returns the current number of items.
The class has its protected field for storing this information, so this method is not pure virtual, but it is indeed virtual to allow re-definition if needed.
References CQKnPClass::ChgLBnd(), CQKnPClass::ChgLBnds(), CQKnPClass::ChgLCost(), CQKnPClass::ChgLCosts(), CQKnPClass::ChgQCost(), CQKnPClass::ChgQCosts(), CQKnPClass::ChgUBnd(), CQKnPClass::ChgUBnds(), CQKnPClass::ChgVlm(), CQKnPClass::KNPLBnd(), CQKnPClass::KNPLBnds(), CQKnPClass::KNPLCost(), CQKnPClass::KNPLCosts(), CQKnPClass::KNPQCost(), CQKnPClass::KNPQCosts(), CQKnPClass::KNPUBnd(), CQKnPClass::KNPUBnds(), CQKnPClass::KNPVlm(), CQKnPClass::n, and CQKnPClass::WriteInstance().
|
pure virtual |
The linear costs of the items are written into csts[].
If nms == 0 then all the costs are written into csts[], otherwise cst[ i ] contains the informations relative to item nms[ i ] (nms must be Inf<int>()-terminated).
The parameters `strt' and `stp' allow to restrict the output of the method to all and only the items `i' with strt <= i < min( KNPn() , stp ). `strt' and `stp' work in "&&" with nms; that is, if nms != 0 then only the values corresponding to items which are both in nms and whose index is in the correct range are returned.
Referenced by CQKnPClass::KNPn().
|
pure virtual |
The quadratic costs of the items are written into csts[].
If nms == 0 then all the costs are written into csts[], otherwise csts[ i ] contains the informations relative to item nms[ i ] (nms must be Inf<int>()-terminated).
The parameters `strt' and `stp' allow to restrict the output of the method to all and only the items `i' with strt <= i < min( KNPn() , stp ). `strt' and `stp' work in "&&" with nms; that is, if nms != 0 then only the values corresponding to items which are both in nms and whose index is in the correct range are returned (see above).
Referenced by CQKnPClass::KNPn().
|
pure virtual |
Return the linear cost of the i-th item (i = 0 .
. n - 1).
Referenced by CQKnPClass::KNPn(), and CQKnPClass::WriteInstance().
|
pure virtual |
Return the quadratic cost of the i-th item (i = 0 .
. n - 1).
Referenced by CQKnPClass::KNPn(), and CQKnPClass::WriteInstance().
|
pure virtual |
The lower bounds of the items are written into bnds[].
If nms == 0 then all the bounds are written, otherwise bnds[ i ] contains the information relative to item nms[ i ] (nms must be Inf<int>()-terminated).
The parameters `strt' and `stp' allow to restrict the output of the method to all and only the items `i' with strt <= i < min( KNPn() , stp ). `strt' and `stp' work in "&&" with nms; that is, if nms != 0 then only the values corresponding to items which are both in nms and whose index is in the correct range are returned.
Referenced by CQKnPClass::KNPn().
|
pure virtual |
The upper bounds of the items are written into bnds[].
If nms == 0 then all the bounds are written, otherwise bnds[ i ] contains the information relative to item nms[ i ] (nms must be Inf<int>()-terminated).
The parameters `strt' and `stp' allow to restrict the output of the method to all and only the items `i' with strt <= i < min( KNPn() , stp ). `strt' and `stp' work in "&&" with nms; that is, if nms != 0 then only the values corresponding to items which are both in nms and whose index is in the correct range are returned (see above).
Referenced by CQKnPClass::KNPn().
|
pure virtual |
Return the lower bound of the i-th item (i = 0 .
. n - 1).
Referenced by CQKnPClass::KNPn(), and CQKnPClass::WriteInstance().
|
pure virtual |
Return the upper bound of the i-th item (i = 0 .
. n - 1).
Referenced by CQKnPClass::KNPn(), and CQKnPClass::WriteInstance().
|
pure virtual |
Returns the volume `V' of the knapsack.
Referenced by CQKnPClass::KNPn(), and CQKnPClass::WriteInstance().
|
inlinevirtual |
Write the instance to the provided ostream in the "complete" format read by ReadInstance() [see above].
The parameter "precc" and "precv" allow to set the precision (number of decimal digits) of the costs (linear and quadratic) and the bounds (upper and lower) and volume, respectively, when printed to the ostream. Rhe default value is "all digits of a double", which results in pretty large files, but smaller precisions are possible if one e.g. knows that the instance data is integer. Since this can typically be different for costs-related and volume-related information, two different parameters are provided.
While virtual, the method is implemented in the base class: the instance data is read from the object using the class-provided implmentation of KNPLCost(), KNPQCost() and so on. Thus, the method will work for any derived class with no mdification; however, being virtual it can be re-implemented for maximum efficiency if desired.
References CQKnPClass::KNPLBnd(), CQKnPClass::KNPLCost(), CQKnPClass::KNPQCost(), CQKnPClass::KNPUBnd(), CQKnPClass::KNPVlm(), and CQKnPClass::n.
Referenced by CQKnPClass::KNPn().
|
pure virtual |
Change the linear cost coefficients that are:
That is, if strt <= nms[ i ] < stp, then the coefficient of the nms[ i ]-th item will be changed reading from csts[ i ]. If nms == 0 (as the default), all the entries in the given range will be changed.
Referenced by CQKnPClass::KNPn().
|
pure virtual |
Change the quadratic cost coefficients that are:
That is, if strt <= nms[ i ] < stp, then the coefficient of the nms[ i ]-th item will be changed reading from csts[ i ]. If nms == 0 (as the default), all the entries in the given range will be changed (see above).
Referenced by CQKnPClass::KNPn().
|
pure virtual |
Change the linear cost coefficient of item i to cst.
Referenced by CQKnPClass::KNPn().
|
pure virtual |
Change the quadratic cost coefficient of item i to cst.
Referenced by CQKnPClass::KNPn().
|
pure virtual |
Change the item lower bounds that are:
That is, if strt <= nms[ i ] < stp, then the bounds of the nms[ i ]-th item will be changed reading from bnds[ i ]. If nms == 0 (as the default), all the entries in the given range will be changed.
Referenced by CQKnPClass::KNPn().
|
pure virtual |
Change the item upper bounds that are:
That is, if strt <= nms[ i ] < stp, then the bounds of the nms[ i ]-th item will be changed reading from bnds[ i ]. If nms == 0 (as the default), all the entries in the given range will be changed.
Referenced by CQKnPClass::KNPn().
|
pure virtual |
Change the lower bound of item i to bnd.
Referenced by CQKnPClass::KNPn().
|
pure virtual |
Change the upper bound of item i to bnd.
Referenced by CQKnPClass::KNPn().
|
pure virtual |
Change the volume.
Referenced by CQKnPClass::KNPn().
|
protected |
status of the algorithm: it is an int so that derived classes can use it to encode other information apart from the return value of SolveKNP().