{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "e899db8b-20a2-49b7-a8e8-74361b9d5ce8", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import cvxpy as cp" ] }, { "cell_type": "code", "execution_count": 105, "id": "cee5b993-b83f-46dc-a498-0f7af5668ba6", "metadata": {}, "outputs": [], "source": [ "x1 = cp.Variable() # the letter coming after cp. should be capitilized\n", "x2 = cp.Variable()\n", "objective = cp.Maximize(140*x1 + 235*x2) \n", "constraints = [x1 >= 0, x2 >= 0, x1 + x2<=180, x1+2*x2 <= 240, .3*x1 + .1*x2 <=30] # constraints should be defined in an array\n", "#constraints+=[x1**2 + 2*x2**2 <=1] # Can add a convex quadratic constraint (but not a concave one!) += adds a constraint to the array\n", "problem = cp.Problem(objective, constraints) # A problem includes an objective (with min/max specified) and a list of constraints\n", "problem.solve() #This solves the optimization problem\n", "x1_val = x1.value #x1 is still a cvxpy object after solving, but we can save its value like this\n", "x2_val = x2.value\n", "optimal_val = problem.value #saves optimal value\n", "print(x1_val) #returns the value of x1\n", "print(x2_val)\n", "print(optimal_val)" ] }, { "cell_type": "code", "execution_count": 99, "id": "aba36a5b-506c-47bf-96a1-222199a51689", "metadata": {}, "outputs": [], "source": [ "#same problem as above\n", "x1 = cp.Variable()\n", "x2 = cp.Variable()\n", "objective_fn = 140*x1 \n", "objective_fn += 235*x2 #one can also define an objective function in multiple lines\n", "objective = cp.Maximize(objective_fn) \n", "constraints = [x1 >= 0, x2 >= 0, x1 + x2<=180, x1+2*x2 <= 240, .3*x1 + .1*x2 <=30]\n", "problem = cp.Problem(objective, constraints)\n", "problem.solve(verbose = True) #If you want more information about the solving, you can add \"verbose = True\" to the argument of problem.solve.\n", "x1_val = x1.value\n", "x2_val = x2.value\n", "optimal_val = problem.value" ] }, { "cell_type": "code", "execution_count": 100, "id": "ceced565-a16e-4f17-9b4d-3fec17738016", "metadata": {}, "outputs": [], "source": [ "# Least Squares Problem\n", "m=16\n", "n=8\n", "A = np.random.normal(0,1,[m,n])\n", "b = np.random.normal(0,1,[m,1])\n", "x = cp.Variable(n)\n", "objective = cp.Minimize(cp.sum_squares(A@x - b)) #cvxpy has many built-in functions that you can use to define constraints/objective\n", "# objective = cp.Minimize( cp.norm(A@x-b) ) #norm of an affine function - can also try other norms\n", "# objective = cp.Maximize( cp.norm(A@x-b) ) # will produce error- input to maximize is not concave\n", "problem = cp.Problem(objective)\n", "problem.solve()\n", "x_val = x.value\n", "optimal_val = problem.value" ] }, { "cell_type": "code", "execution_count": 101, "id": "740f39dd-dc1d-4398-9121-20c89d704478", "metadata": {}, "outputs": [], "source": [ "m=16\n", "n=8\n", "p=4\n", "A = np.random.normal(0,1,[m,n])\n", "b = np.random.normal(0,1,[m,1])\n", "C = np.random.normal(0,1,[p,n])\n", "d = np.random.normal(0,1,[p,1])\n", "x = cp.Variable(n)\n", "objective = cp.Minimize(cp.norm( A@x -b, 2))\n", "constraints = [C@x == d, x >= .1, cp.norm(x,\"inf\") <= 100] # equality constraints use ==\n", "problem = cp.Problem(objective,constraints)\n", "problem.solve()\n", "x_val = x.value\n", "optimal_val = problem.value" ] }, { "cell_type": "code", "execution_count": 102, "id": "fb2fa4e2-c7ce-4ef1-8cda-8e40fa827575", "metadata": {}, "outputs": [], "source": [ "# Infeasible / unbounded problem\n", "x1 = cp.Variable()\n", "x2 = cp.Variable()\n", "constraints = [x1+x2 <= -5] \n", "#constraints = [x1+x2 < -5] # Strict inequalities cause an error\n", "constraints+=[x1**2 + x2**2 <= 1] # Removing this constraint will make the problem unbounded instead of infeasible.\n", "problem = cp.Problem(cp.Minimize(x1+x2),constraints)\n", "problem.solve()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.11" } }, "nbformat": 4, "nbformat_minor": 5 }