What package could I use to get all the iterations of the simplex algorithm when solving a linear program in R?
I want to solve a linear program in R with the simplex algorithm. However, rather than just getting the final optimal solution, I also want to get all the iterations that the simplex algorithm goes through. What package could I use to get all the iterations of the simplex algorithm when solving a linear program in R?
See also questions close to this topic

Plot data for each row within a single command
I'm an R newbie and need help with the following.
I have the following data
# Simulate matrix of integers set.seed(1) df < matrix(sample.int(5, size = 3*5, replace = TRUE), nrow = 3, ncol = 5) print(df) df < tbl_df(df) # tabulate as dataframe df < rbind(df, c(3,5,4,1,4)) print(df)
Within a single command, I need to plot the data for each row, so that yaxis: data in each row (in my case these are values from 1 to 5); xaxis: values 1,2,3,4,5 that refer to each column. So effectively, for each row, I am trying to plot how row values change for every single column.
I have tried the following, which works but has two problems which I need to resolve. First, this only plots 1 row at a time. Not an efficient way of doing things especially if there are many rows. Second, I could not find a way to refer to the xaxis as the number of columns, so I resorted to simply counting the number of columns (i.e. 5) and put a c(1:5) vector to represent a number of columns. I also tried to put ncol(df) to represent xaxis but that returns an error saying that variables have different length. Indeed when requesting ncol(df) it return number 5, which is the number of columns but it does not do what I wanted it to, i.e. to represent number of columns sequentially 1,2,3,4,5.
plot(c(1:5),df[1,], type = "b", pch=19, col = "blue", xlab = "number of columns", ylab = "response format")
Thank you, your help is much appreciated

Efficiency R codes in windows server
I test r codes in local computer and it works well. local computer: RAM:4 CPU:5i system: windows. I use same codes in windows server: RAM:64 CPU:Intel Xeon 2 x L5640 (24Core x 2.2GhzHT) and hard disk:1TB NVMe and R codes does not work more quickly and it does not work at all with more data. Why R does not have enough efficiency on the server? Unfortunately, I'm not familiar with Linux. I have limited time to use the server and I need your guidance.

How to find next latest date within 30 days of existing dates in R?
I have a dataset_A like below and i would like to know what is the recent date within on (or) next 30 days for the same id from dataset_B for each date in dataset_A.
dataset_A
id disch_date 1234 20180314 1234 20180317 1234 20180419 2345 20180415 2345 20180515 3456 20180516
dataset_B
id visit_date 1234 20180317 1234 20180320 1234 20180228 1234 20180430 2345 20180512 2345 20180513 2345 20180717 3456 20180516 3456 20180519
expected_output
id disch_date visit_date 1234 20180314 20180317 1234 20180317 20180317 1234 20180419 20180430 2345 20180415 20180512 2345 20180515 NA 3456 20180516 20180516
Tried:
dataset_A$visit_date < sapply(dataset_A$disch_date, function(x) any( abs(xdataset_B$visit_date) <= 30))

Restricted basis entry for stepwise linearization
I am working on a nonlinear function (such as x^2) using bounded stepwise linearization method. Actually, it requires some restricted conditions, namely restricted basis entry rule. It means: Only two adjacent w_ij are allowed to enter the basis.
i is the index of variable and j is the index of interval.
More explanation on this method can found in:
http://civil.colorado.edu/~balajir/CVEN5393/lectures/chapter08.pdf
I could not find any clue on how to implement it in pyomo. I will be grateful if you could help me.

Value of objective and constraint are endogenous in lpSolveAPI
Hello I'm running a linear programming in R with lpSolveAPI and it's almost my first try to do it. I made a simple version of model about maximizing consumer surplus and variables are set of 9 prices. However, I really don't know how to solve endogenity problem between price and demand.
To do this, I supposed simple demand function
q_(h,i)=A_(h,i)*P_h^e, price P_h of each hour is different
. Following this demand function, consumer's surplus change would be∆CS_(h,i)=A_(h,i)/(e+1)(P^(e+1)P'^(e+1))
and objective fc ismax ∆CS_(h,i)
Then I add constraints. A company impose 9 prices set to consumer, but their margin rate are controlled as constraints.
marginrate=(sum(demand)*pricetotalcost)/totalcost
. And this margin rate is endogenously affected by consumers' demand.Demand data of each time period are listed in AA~II data.frame
lprec<make.lp(0,9) set.objfn(lprec, obj=1300c(sum(AA$demand),sum(BB$demand),...sum(II$demand)), indices=c(1,2,3,4,5,6,7,8,9)) add.constraint(lprec,xt=c(sum(AA$demand),...,sum(II$demand)),"=", marginrate*totalcost+totalcost,indices=c(1,2,3,4,5,6,7,8,9))
And plus other constraints. Here, at first, I'd want to add constraint that this model can reflect demand change by price change. But I cannot imagine how to put AA~II$demand is determined by prices which are chosen variables
q_(h,i)=A_(h,i)*P_h^e
.Next another problem is total cost. Total cost is not only affected by endogenous value of demand, but by cost function which depends on total demand of consumer.
I'm not sure my question can be delivered to you. My key question is how to solve endogeneity problem in lp model. I'm first in lp model and it is easy to understand lp model constituted with scholar value (e.g. profit maximization with constant cost), but it's not easy to handle vector form of data into lp model.
Thank you for your help !

Solving Assignment Problem with conditional minimum group sizes using CVXPY
I'm using
cvxpy
withinpython
to solve a particular type of assignment problem. I'd like to assign M people to N groups in a way that minimizes cost, with the following constraints on groups: Groups cannot have more than J members
 If a group is populated, it has to have at least K members, otherwise a group can have zero members.
Of cousre, J <= K. I can solve the problem ignoring #2 above. In the below example, M = 6, N = 3 and J = 3. Ideally, I'd like to set K = 2. I generate preferences such that everyone prefers group 1 (column 1 in the cost function), most people then prefer group 2, but one person prefers group 3 to group 2:
import numpy as np import cvxpy as cp preference = np.array([[1,2,3], [1,2,3], [1,2,3], [1,2,3], [1,2,3], [1,3,2]]) groupmax = np.array([3,3,3]) selection = cp.Variable(shape=preference.shape,boolean=True) group_constraint_1 = cp.sum(selection,axis=0) <= groupmax assignment_constraint = cp.sum(selection,axis=1) == 1 cost = cp.sum(cp.multiply(preference,selection)) constraints = [group_constraint_1,assignment_constraint] assign_prob = cp.Problem(cp.Minimize(cost),constraints) assign_prob.solve(solver=cp.GLPK_MI) print(selection.value)
The solution/assignment is :
[[1. 0. 0.] [1. 0. 0.] [1. 0. 0.] [0. 1. 0.] [0. 1. 0.] [0. 0. 1.]]
That is, I have one group with max size 3, another group with size 2, and a group with size 1. In my ideal setup, a group of 1 (group 3) is too small, and that person would have to be assigned to group 2. Note, if I simply put a minimum group size of 2, I instead get three groups of 2:
import numpy as np import cvxpy as cp preference = np.array([[1,2,3], [1,2,3], [1,2,3], [1,2,3], [1,2,3], [1,3,2]]) groupmax = np.array([3,3,3]) groupmin = np.array([2,2,2]) selection = cp.Variable(shape=preference.shape,boolean=True) group_constraint_1 = cp.sum(selection,axis=0) <= groupmax group_constraint_2 = cp.sum(selection,axis=0) => groupmin assignment_constraint = cp.sum(selection,axis=1) == 1 cost = cp.sum(cp.multiply(preference,selection)) constraints = [group_constraint_1,group_constraint_2,assignment_constraint] assign_prob = cp.Problem(cp.Minimize(cost),constraints) assign_prob.solve(solver=cp.GLPK_MI) print(selection.value)
The solution is now:
[[1. 0. 0.] [1. 0. 0.] [0. 1. 0.] [0. 1. 0.] [0. 0. 1.] [0. 0. 1.]]
I tried the following workaround, but the third constraint below is rejected by
cvxpy
because the problem is no longer DCP. I think the issue is that I am multiplying a variable by another variable in the constraint. I can't figure out another way to have the total number of people in a group either be greater than 2 or exactly zero:import numpy as np import cvxpy as cp preference = np.array([[1,2,3], [1,2,3], [1,2,3], [1,2,3], [1,2,3], [1,3,2]]) groupmax = np.array([3,3,3]) selection = cp.Variable(shape=preference.shape,boolean=True) switch_1 = cp.Variable(shape=preference.shape[1],boolean=True) switch_2 = cp.Variable(shape=preference.shape[1],boolean=True) group_constraint_1 = cp.sum(selection,axis=0) <= groupmax group_constraint_2 = cp.sum(selection,axis=0)  2 * switch_1 >= 0 group_constraint_3 = cp.sum(selection,axis=0) * switch_2 == 0 switch_constraint = switch_1 + switch_2 == 1 assignment_constraint = cp.sum(selection,axis=1) == 1 cost = cp.sum(cp.multiply(preference,selection)) constraints = [group_constraint_1,group_constraint_2,group_constraint_3, switch_constraint,assignment_constraint] assign_prob = cp.Problem(cp.Minimize(cost), constraints) assign_prob.solve(solver=cp.GLPK_MI) print(selection.value)
I now get the following error:
DCPError: Problem does not follow DCP rules.
Is there a way to incorporate this nonstandard constraint? Also, if I can use the above constraints, I can solve my problem, but I can solve my problem even more easily if you can tell me how to incorporate a constraint like the following:
 Group sizes must either be multiples of zero, J, or K.