[Optimization] Codes Answer to online quiz 1

Matlab with cvx: 

% Decision Variables
cvx_begin
    variables xsb xre xc xs

    % Objective Function
    maximize(0.04*xsb + 0.06*xre + 0.08*xc + 0.09*xs)

    % Constraints
    subject to
        xsb + xre + xc + xs == 2.5e6
        xc + xs <= 2*xsb
        xs <= xc
        xc <= 1.7*xre
        xsb >= 0
        xre >= 0
        xc >= 0
        xs >= 0
cvx_end

% Display the optimal solution
fprintf('Optimal Solution:\n');
fprintf('State Bonds: %.1f euros\n', xsb);
fprintf('Real Estate Loans: %.1f euros\n', xre);
fprintf('Car Loans: %.1f euros\n', xc);
fprintf('Scholarship Loans: %.1f euros\n', xs);

Python:

import cvxpy as cp

# Decision Variables
xsb = cp.Variable()
xre = cp.Variable()
xcℓ = cp.Variable()
xsℓ = cp.Variable()

# Objective Function
objective = cp.Maximize(0.04*xsb + 0.06*xre + 0.08*xcℓ + 0.09*xsℓ)

# Constraints
constraints = [
    xsb + xre + xcℓ + xsℓ == 2.5e6,
    xcℓ + xsℓ <= 2*xsb,
    xsℓ <= xcℓ,
    xcℓ <= 1.7*xre,
    xsb >= 0,
    xre >= 0,
    xcℓ >= 0,
    xsℓ >= 0
]

# Solve the problem
problem = cp.Problem(objective, constraints)
problem.solve()

# Display the optimal solution
print("Optimal Solution:")
print(f"State Bonds: {xsb.value:.1f} euros")
print(f"Real Estate Loans: {xre.value:.1f} euros")
print(f"Car Loans: {xcℓ.value:.1f} euros")
print(f"Scholarship Loans: {xsℓ.value:.1f} euros")

相关推荐

  1. nvm1.1.11

    2024-02-19 09:24:05       52 阅读
  2. 1.下午试题1

    2024-02-19 09:24:05       29 阅读
  3. HTML-1

    2024-02-19 09:24:05       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-19 09:24:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-19 09:24:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-19 09:24:05       87 阅读
  4. Python语言-面向对象

    2024-02-19 09:24:05       96 阅读

热门阅读

  1. harbor v1.7.1镜像仓库无法访问,并提示502 Bad Gateway

    2024-02-19 09:24:05       48 阅读
  2. Nginx错误502 Bad Gateway

    2024-02-19 09:24:05       53 阅读
  3. AutoSAR(基础入门篇)7.3-使用DEV配置SWC&RTE

    2024-02-19 09:24:05       52 阅读
  4. Rust-所有权(ownership)

    2024-02-19 09:24:05       53 阅读
  5. Rust语言之sha-256爆破

    2024-02-19 09:24:05       49 阅读
  6. go redis

    go redis

    2024-02-19 09:24:05      42 阅读
  7. android pdf框架-3,对开源库的探究1

    2024-02-19 09:24:05       45 阅读