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: [[10.0.5.0.25.0.10.0.]
[0.10.0.5.0.25.0.10.]
[1.1.0.0.0.0.0.0.]
[0.0.1.1.0.0.0.0.]
[0.0.0.0.1.1.0.0.]
[0.0.0.0.0.0.1.1.]] However, I cannot generate this matrix. You can see my codes in the below. My result is: [[11.1.5.0.25.0.10.0.][0.10.1.6.0.25.0.10.][0.0.0.0.1.1.0.0.][0.0.0.0.0.0.1.1.][0.0.0.0.0.0.0.0.][0.0.0.0.0.0.0.0.]]. Am I use v-stack? 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 mixed_integer_linear_programming(direction, A, senses, b, c, l, u, types, names):
# create an empty optimization problem
prob = cp.Cplex()
# add decision variables to the problem including their coefficients in objective and ranges
prob.variables.add(obj = c.tolist(), lb = l.tolist(), ub = u.tolist(), types = types.tolist(), names = names.tolist())
# define problem type
if direction == "maximize":
prob.objective.set_sense(prob.objective.sense.maximize)
else:
prob.objective.set_sense(prob.objective.sense.minimize)
# add constraints to the problem including their directions and right-hand side values
prob.linear_constraints.add(senses = senses.tolist(), rhs = b.tolist())
# add coefficients for each constraint
row_indices, col_indices = A.nonzero()
prob.linear_constraints.set_coefficients(zip(row_indices.tolist(), col_indices.tolist(), A.data.tolist()))
print(prob.write_as_string())
# solve the problem
prob.solve()
# check the solution status
print(prob.solution.get_status())
print(prob.solution.status[prob.solution.get_status()])
# get the solution
x_star = prob.solution.get_values()
obj_star = prob.solution.get_objective_value()
return(x_star, obj_star) def coin_distribution_problem(coins_file, M):
# your implementation starts below
coins = np.loadtxt(coins_file)
m = coins.shape[0]
c = np.repeat(1,2* m )
senses = np.concatenate((np.repeat("E", int(m/2)),(np.repeat("E", m))))
print(senses)
f = np.repeat(coins[2], int(m/2))
= np.repeat(1, m)
print(f)
print()
b = np.concatenate([f,])
print(b,"B1")
l = np.repeat(0,2* m)
print(l)
u = np.repeat(1,2* m)
print(u)
types = np.repeat("B",2* m)
print(types)
names = np.array(["x_{}_{}".format(i +1, j +1) for i in range(m) for j in range(m)])
print(names)
aij_1= np.repeat(coins,2) # row ve col dizilerini m ile yazyoruz
row_1= np.tile(np.arange(2),m)
col_1= np.repeat(np.arange(2* m),1)
A_1= sp.csr_matrix((aij_1,(row_1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!