Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please complete the python code and show that code provides correct requested outputs. from random import uniform def computeApproximateSolution ( n , m , c

Please complete the python code and show that code provides correct requested outputs.
from random import uniform
def computeApproximateSolution(n, m, c_matrix, d_values):
assert n >=1
assert len(c_matrix)== m
assert all(len(c_list)== n for c_list in c_matrix)
assert len(d_values)== m
r_values =[uniform(-1,1) for i in range(n)]
# your code here
raise NotImplementedError
def testSolution(n, m, c_matrix, d_values, x_values):
# always check pre-conditions: saves so much time later
assert len(c_matrix)== m
assert all(len(c_list)== n for c_list in c_matrix)
assert len(d_values)== m
assert len(x_values)== n
# Check how many inequalities satisfied
num_ineqs =0
for (c_list, d) in zip(c_matrix, d_values):
if sum([cj * xj for (cj, xj) in zip(c_list,x_values )])<= d+1E-3:
num_ineqs = num_ineqs +1
assert num_ineqs >= m/2, f' Half number of inequalities to be sat: {m/2} your solution satisfies: {num_ineqs} inequalities '
print('Test Passed')
return
n =5
m =24
c_matrix =[
[1,-1,0,1,-1],
[1,2,0,0,2],
[-1,0,1,1,1],
[1,0,0,0,-1],
[-1,0,0,-1,-1],
[1,0,0,1,1],
[1,0,-1,1,0],
[0,2,1,0,2],
[-1,1,1,-1,0],
[1,1,1,0,1],
[-1,1,1,0,0],
[1,1,1,1,0],
[-1,1,0,1,-1],
[1,-2,0,0,-2],
[1,0,1,-1,-1],
[1,0,1,0,1],
[-1,0,0,1,1],
[-1,0,0,1,1],
[1,-1,1,1,1],
[0,-2,-1,0,2],
[-1,-1,-1,-1,0],
[-1,1,-1,0,1],
[1,0,0,1,0],
[-1,0,-1,0,-1],
]
d_list =[
-5,3,-4,-2,-3,-1,
-5,3,-4,-2,-3,-1,
5,-3,4,2,3,1,
5,-3,4,2,3,1,
]
(k, x_values)= computeApproximateSolution(n, m, c_matrix, d_list)
print(k)
print(x_values)
testSolution(n, m, c_matrix, d_list, x_values)
print('5 points')
from random import uniform, randint, seed
## Warning: these are large instances. If your solution takes more than 120 seconds, then
## chances are that you will not receive any credit for this problem.
def gen_random_instance(n, m):
c_matrix =[[randint(-5,5) for i in range(n)] for j in range(m)]
d_values =[randint(-10,10) for i in range(m)]
return (c_matrix, d_values)
seed(100001)
print('Test # 1')
n =10
m =55
(c_matrix, d_values)= gen_random_instance(n, m)
(k, x_values)= computeApproximateSolution(n, m, c_matrix, d_values)
print(k)
print(x_values)
testSolution(n, m, c_matrix, d_values, x_values)
print('Test # 2')
n =35
m =230
(c_matrix, d_values)= gen_random_instance(n, m)
(k, x_values)= computeApproximateSolution(n, m, c_matrix, d_values)
print(k)
print(x_values)
testSolution(n, m, c_matrix, d_values, x_values)
print('Test # 3')
n =100
m =550
(c_matrix, d_values)= gen_random_instance(n, m)
(k, x_values)= computeApproximateSolution(n, m, c_matrix, d_values)
print(k)
print(x_values)
testSolution(n, m, c_matrix, d_values, x_values)
print('Test # 4')
n =80
m =900
(c_matrix, d_values)= gen_random_instance(n, m)
(k, x_values)= computeApproximateSolution(n, m, c_matrix, d_values)
print(k)
print(x_values)
testSolution(n, m, c_matrix, d_values, x_values)
print('Test # 5')
n =70
m =445
(c_matrix, d_values)= gen_random_instance(n, m)
(k, x_values)= computeApproximateSolution(n, m, c_matrix, d_values)
print(k)
print(x_values)
testSolution(n, m, c_matrix, d_values, x_values)
print('15 points!')Implement a function comput epproximat esolution with inputs
, : number of variables.
m : number of inequalities
c_matrix : a list of list of coefficients of the LHS of inequalities
[c11,dots,c1n?c21,dots,c2n?dots?c11,dots,can?]
Please note python indexes starting from 0.
d_ualues : a list of RHS coefficients: d1,dots,da
Your function should retum a pair: (k,[x1,dots,xn])

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Manga Guide To Databases

Authors: Mana Takahashi, Shoko Azuma, Co Ltd Trend

1st Edition

1593271905, 978-1593271909

More Books

Students also viewed these Databases questions

Question

Describe an embedded system in less than 100 words.

Answered: 1 week ago

Question

=+1. How can the process of movie utilization be described?

Answered: 1 week ago