Title: | Generate Feasible Samples of a Knapsack Problem |
---|---|
Description: | The sampl.mcmc function creates samples of the feasible region of a knapsack problem with both equalities and inequalities constraints. |
Authors: | Chin Soon Lim [aut] |
Maintainer: | Chin Soon Lim <[email protected]> |
License: | GPL (>= 2) | file LICENSE |
Version: | 0.1.1 |
Built: | 2025-02-24 02:45:54 UTC |
Source: | https://github.com/chinsoon12/knapsacksampling |
Flip a 1 and a 0 simultaneously
flip01(x)
flip01(x)
x |
an integer or logical vector |
x an integer vector
Generate an initial feasible solution by solving a linear programming with binary variables
initState(numVar, objVec = runif(numVar), constraints = NULL)
initState(numVar, objVec = runif(numVar), constraints = NULL)
numVar |
- number of variables |
objVec |
- objective function as a numeric vector |
constraints |
- a list of list of constraints with constr.mat, constr.dir, constr.rhs in each sublist |
a binary vector containing a feasible solution
#see documentation for sampl.mcmc
#see documentation for sampl.mcmc
Generate feasible solutions to a knapsack problem using Markov Chain Monte Carlo
sampl.mcmc(init, numSampl, maxIter = 2 * numSampl, constraints = NULL)
sampl.mcmc(init, numSampl, maxIter = 2 * numSampl, constraints = NULL)
init |
- an initial feasible solution |
numSampl |
- number of samples to be generated |
maxIter |
- maximum number of iterations to be run to prevent infinite loop |
constraints |
- a list of list of constraints with constr.mat, constr.dir, constr.rhs in each sublist Please see example for an example of constraints. |
a matrix of 0, 1 with each row representing a sample
#number of variables N <- 100 #number of variables in each group grpLen <- 10 #equality matrix A <- matrix(c(rep(1, N)), ncol=N, byrow=TRUE) #inequality matrix G <- matrix(c(rep(1, grpLen), rep(0, N - grpLen), rep(c(0,1), each=grpLen), rep(0, N - 2*grpLen)), ncol=N, byrow=TRUE) #construct a list of list of constraints constraints <- list( list(constr.mat=A, constr.dir=rep("==", nrow(A)), constr.rhs=c(20)), list(constr.mat=G, constr.dir=rep("<=", nrow(G)), constr.rhs=c(5, 5)), list(constr.mat=G, constr.dir=rep(">=", nrow(G)), constr.rhs=c(1, 2)) ) #generate an initial feasible solution init <- initState(N, constraints=constraints) #create feasible solutions to knapsack problems subject to constraints samples <- sampl.mcmc(init, 50, constraints=constraints)
#number of variables N <- 100 #number of variables in each group grpLen <- 10 #equality matrix A <- matrix(c(rep(1, N)), ncol=N, byrow=TRUE) #inequality matrix G <- matrix(c(rep(1, grpLen), rep(0, N - grpLen), rep(c(0,1), each=grpLen), rep(0, N - 2*grpLen)), ncol=N, byrow=TRUE) #construct a list of list of constraints constraints <- list( list(constr.mat=A, constr.dir=rep("==", nrow(A)), constr.rhs=c(20)), list(constr.mat=G, constr.dir=rep("<=", nrow(G)), constr.rhs=c(5, 5)), list(constr.mat=G, constr.dir=rep(">=", nrow(G)), constr.rhs=c(1, 2)) ) #generate an initial feasible solution init <- initState(N, constraints=constraints) #create feasible solutions to knapsack problems subject to constraints samples <- sampl.mcmc(init, 50, constraints=constraints)