Optimization Quiz
2023. 11. 8. 19:56ㆍ학습/ML4ME[23-2]
Quiz 1


Quiz 2

from elice_utils import EliceUtils
from scipy.optimize import linprog
import numpy as np
elice_utils = EliceUtils()
def main():
# Consider what each variable represents in the linprog function
c = [-0.5, -1] #TODO
A = [[1, 1], [1, 8/3]] #TODO
b = [2, 4] #TODO
x0_bounds = [0, 1.5] #TODO
x1_bounds = [0, None] #TODO
# DO NOT TOUCH THIS LINE BELOW
res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds], method='interior-point')
print(f'Optimal values are x1 = {round(res.x[0], 1)} and x2 = {round(res.x[1], 1)}')
''' round는 반올림 함수이다
round(수, 반올림해 표시할 자리수); round(3.7, 1)이면 3.7을 소수점 첫번째 자리까지 나타낸다는 뜻'''
return res.x[0], res.x[1]
if __name__ == "__main__":
main()
Quiz 3

from elice_utils import EliceUtils
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import minimize
elice_utils = EliceUtils()
#Do not touch this function
def plt_show():
plt.savefig("fig")
elice_utils.send_image("fig.png")
def main():
# Consider what each variable represents in the linprog function
# Constants
v0 = 10 #TODO
g = 9.8 # g=9.8m/s^2
# Time array and height calculation
t = np.arange(0, 2.1, 0.1)
#Cost Function
h = v0 * t - 0.5 * g * t ** 2 #TODO
# Plot
plt.plot(t, h)
plt.grid(True)
plt.xlabel('t')
plt.ylabel('h')
plt_show()
# Objective function
def objective(t):
return -v0 * t + 0.5 * g * t ** 2 #TODO
# Bounds
bounds = [(0, 5)]
# Solve
res = minimize(objective, x0=2, bounds=bounds) # starting from x0=2 for better convergence
'''minimize 함수에 대한 설명은 다음 링크에
#How can you obtain x from res?
x = res.x[0] #TODO
hmax = v0 * x - 0.5 * g * x ** 2 #TODO
print(f'Optimal value t = {x:.2f} and the max height h = {hmax:.2f}')
return x, hmax
if __name__ == "__main__":
main()
'학습 > ML4ME[23-2]' 카테고리의 다른 글
Parametric Density Estimation: Binary variable distribution (0) | 2023.11.12 |
---|---|
Probability (0) | 2023.11.09 |
Signal Processing (0) | 2023.11.08 |
Linear algebra for ML (0) | 2023.11.08 |
Optimization (0) | 2023.11.07 |