Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v5.0.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
Expand Down
51 changes: 34 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,36 +1,53 @@
# This Makefile provides commands for setting up the development environment,
# running formatting tools, and cleaning the repository.

# Set the default target to 'help' when running make without arguments
.DEFAULT_GOAL := help

VENV :=.venv
# Create a Python virtual environment using uv (faster alternative to venv)
.PHONY: venv
venv:
curl -LsSf https://astral.sh/uv/install.sh | sh;
@uv venv # Create a virtual environment in the current directory

# Mark 'install' as a phony target (not associated with a file)
.PHONY: install
install: ## Install a virtual environment
python -m venv ${VENV}
${VENV}/bin/pip install --upgrade pip
${VENV}/bin/pip install -r requirements.txt
install: venv ## Install a virtual environment
@uv pip install --upgrade pip # Ensure pip is up to date
@uv pip install --no-cache-dir -r requirements.txt # Install project dependencies from requirements.txt

# Code Quality
.PHONY: fmt
fmt: install ## Run autoformatting and linting
${VENV}/bin/pip install pre-commit
${VENV}/bin/pre-commit install
${VENV}/bin/pre-commit run --all-files
@uv pip install pre-commit # Install pre-commit hooks
@uv run pre-commit install # Set up pre-commit hooks
@uv run pre-commit run --all-files # Run pre-commit hooks on all files

# Book Building
.PHONY: build
build: install ## Build the book
${VENV}/bin/pip install jupyter-book
${VENV}/bin/jupyter-book clean book
${VENV}/bin/jupyter-book build book
touch book/_build/html/.nojekyll
@uv pip install jupyter-book # Install jupyter-book
@uv run jupyter-book clean book # Clean previous builds
@uv run jupyter-book build book # Build the book
touch book/_build/html/.nojekyll # Add .nojekyll file for GitHub Pages

# Cleanup
.PHONY: clean
clean: ## Clean up caches and build artifacts
@git clean -X -d -f
@git clean -X -d -f # Remove files ignored by git

# Help
.PHONY: help
help: ## Display this help screen
@echo -e "\033[1mAvailable commands:\033[0m"
@grep -E '^[a-z.A-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-18s\033[0m %s\n", $$1, $$2}' | sort
@echo -e "\033[1mAvailable commands:\033[0m" # Print header in bold
@grep -E '^[a-z.A-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-18s\033[0m %s\n", $$1, $$2}' | sort # Extract and format targets with comments

# Jupyter Setup
.PHONY: jupyter
jupyter: install ## Start jupyterlab
${VENV}/bin/pip install jupyterlab
${VENV}/bin/jupyter lab
@uv pip install jupyterlab # Install JupyterLab
@uv run jupyter lab # Start JupyterLab server

.PHONY: test
test: install ## Run the tests
@uv run python -m test
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# [CVXPY tutorial](https://cvxgrp.org/cvx_short_course)
# 📊 [CVXPY tutorial](https://cvxgrp.org/cvx_short_course)

## 📋 Overview

CVXPY is an open source Python-embedded modeling language for convex
optimization problems. It lets you express your problem in a natural way that
follows the math, rather than in the restrictive standard form required by
solvers. This tutorial will cover the basics of convex optimization, and how to
optimization problems. It lets you express your problem naturally, following
the math, rather than in the restrictive standard form required by
solvers. This tutorial will cover the basics of convex optimization and how to
use CVXPY to specify and solve convex optimization problems, with an emphasis on
real-world applications. No prior knowledge of convex optimization is assumed.

## Getting started
## 🚀 Getting started

1. Clone this repository.
2. Install CVXPY following [these instructions](https://www.cvxpy.org/install/index.html).
We recommend Python 3.10 and CVXPY 1.3.
3. Test your CVXPY installation by running ``test.py`` in the repository.
2. Install CVXPY by running ``make install``
3. Test your CVXPY installation by running ``make test`` in the repository.
15 changes: 14 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
import cvxpy as cp
import numpy as np

print("Starting optimization problem...")

# Problem data.
m = 30
n = 20
print(f"Problem dimensions: m={m}, n={n}")
np.random.seed(1)
A = np.random.randn(m, n)
b = np.random.randn(m)
Expand All @@ -16,9 +19,19 @@
prob = cp.Problem(objective, constraints)

# The optimal objective value is returned by `prob.solve()`.
print("Solving the optimization problem...")
result = prob.solve()
print(f"Optimal objective value: {result}")
print(f"Problem status: {prob.status}")

# The optimal value for x is stored in `x.value`.
print("\nOptimal value for x:")
print(x.value)

# The optimal Lagrange multiplier for a constraint is stored in
# `constraint.dual_value`.
print(constraints[0].dual_value)
print("\nDual values for constraints:")
print(f"First constraint (0 <= x): {constraints[0].dual_value}")
print(f"Second constraint (x <= 1): {constraints[1].dual_value}")

print("\nOptimization completed successfully!")