Question: Hello, this is my assignment and I can write my code on the range of # your implementation starts below, # your implementation ends above.
Hello, this is my assignment and I can write my code on the range of # your implementation starts below, # your implementation ends above. I want to generate a sparse matrix that is here:
However, I cannot generate this matrix. You can see my codes in the below. My result is: Am I use vstack? Could you help me Really, I am confused and I cannot create this matrix. I think the problem is about the column code but I could not fix the problem. Thank you.
# load libraries
import numpy as np
import scipy.sparse as sp
import cplex as cp def mixedintegerlinearprogrammingdirection A senses b c l u types, names:
# create an empty optimization problem
prob cpCplex
# add decision variables to the problem including their coefficients in objective and ranges
prob.variables.addobj ctolist lb ltolist ub utolist types types.tolist names names.tolist
# define problem type
if direction "maximize":
prob.objective.setsenseprobobjective.sense.maximize
else:
prob.objective.setsenseprobobjective.sense.minimize
# add constraints to the problem including their directions and righthand side values
prob.linearconstraints.addsenses sensestolist rhs btolist
# add coefficients for each constraint
rowindices, colindices Anonzero
prob.linearconstraints.setcoefficientsziprowindices.tolist colindices.tolist Adata.tolist
printprobwriteasstring
# solve the problem
prob.solve
# check the solution status
printprobsolution.getstatus
printprobsolution.statusprobsolution.getstatus
# get the solution
xstar prob.solution.getvalues
objstar prob.solution.getobjectivevalue
returnxstar, objstar def coindistributionproblemcoinsfile, M:
# your implementation starts below
coins nploadtxtcoinsfile
m coins.shape
c nprepeat m
senses npconcatenatenprepeatE intmnprepeatE m
printsenses
f nprepeatcoins intm
nprepeat m
printf
print
b npconcatenatef
printbB
l nprepeat m
printl
u nprepeat m
printu
types nprepeatB m
printtypes
names nparrayxformati j for i in rangem for j in rangem
printnames
aij nprepeatcoins # row ve col dizilerini m ile yazyoruz
row nptilenparangem
col nprepeatnparange m
A spcsrmatrixaijrow
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
