scx
SCx
MIT's Supply Chain Micromaster (SCx) Python Package
Documentation
Technical documentation can be found here.
Examples
Setup
Cloud Setup (Google Colab)
- You can access google colab here
- Create a new notebook
- Install the
scxpackage by adding the following to a new code cell at the top of your notebook and running it:pip install scx
Local Setup
Make sure you have Python 3.11.x (or higher) installed on your system. You can download it here.
Recommended (but Optional) -> Expand this section to setup and activate a virtual environment.
Install (or upgrade) virtualenv:
python3 -m pip install --upgrade virtualenvCreate your virtualenv named
venv:python3 -m virtualenv venvActivate your virtual environment
- On Unix (Mac or Linux):
source venv/bin/activate- On Windows:
venv\scripts\activate
pip install scx
Optimization Getting Started
See all of the optimization examples here.
Basic Usage
from scx.optimize import Model
Simple Optimization
from scx.optimize import Model
# Create variables
product_1_amt = Model.variable(name="product_1", lowBound=0)
product_2_amt = Model.variable(name="product_2", lowBound=0)
# Initialize the model
my_model = Model(name="Generic_Problem", sense='maximize')
# Add the Objective Fn
my_model.add_objective(
fn = (product_1_amt*1)+(product_2_amt*1)
)
# Add Constraints
my_model.add_constraint(
name = 'input_1_constraint',
fn = product_1_amt*1+product_2_amt*2 <= 100
)
my_model.add_constraint(
name = 'input_2_constraint',
fn = product_1_amt*3+product_2_amt*1 <= 200
)
# Solve the model
my_model.solve(get_duals=True, get_slacks=True)
# Show the outputs
# NOTE: outputs can be fetched directly as a dictionary with `my_model.get_outputs()`
my_model.show_outputs()
Outputs:
{'duals': {'input_1_constraint': 0.4, 'input_2_constraint': 0.2},
'objective': 80.0,
'slacks': {'input_1_constraint': -0.0, 'input_2_constraint': -0.0},
'status': 'Optimal',
'variables': {'product_1': 60.0, 'product_2': 20.0}}
Database Getting Started
See all of the database examples here
Basic Usage
from scx.database import Database
# Specify the S3 path to the data
data_folder = 's3://scx-dev/databases/supermarket/'
# Create the database
db = Database(f'''
CREATE TABLE Customers AS SELECT * FROM read_parquet('{data_folder}customers.parquet');
''')
# Show the database Schema
db.show_info()
# Query the database
db.query("SELECT * FROM Customers LIMIT 5")
1""" 2# SCx 3[](https://badge.fury.io/py/scx) 4[](https://opensource.org/licenses/MIT) 5 6MIT's [Supply Chain Micromaster](https://micromasters.mit.edu/scm/) (SCx) Python Package 7 8## Documentation 9[Technical documentation](https://connor-makowski.github.io/scx/scx.html) can be found [here](https://connor-makowski.github.io/scx/scx.html). 10 11## Examples 12- [All Optimization Examples](https://github.com/connor-makowski/scx/tree/main/notebooks/optimization/) 13 - [Basic Examples](https://github.com/connor-makowski/scx/tree/main/notebooks/optimization/basic_examples) 14 - [Blinky Examples (with video walkthroughs)](https://github.com/connor-makowski/scx/tree/main/notebooks/optimization/blinky_examples) 15 - [Food On Wheels Examples (with video walkthroughs)](https://github.com/connor-makowski/scx/tree/main/notebooks/optimization/fow_examples) 16 - [Eco Pants Examples (with video walkthroughs)](https://github.com/connor-makowski/scx/tree/main/notebooks/optimization/eco_pants_examples) 17 - [Miscellaneous Examples](https://github.com/connor-makowski/scx/tree/main/notebooks/optimization/misc_examples) 18- [All Database Examples](https://github.com/connor-makowski/scx/tree/main/notebooks/database/) 19 - [Transaction Database Example](https://github.com/connor-makowski/scx/tree/main/notebooks/database/Transaction.ipynb) 20 21## Setup 22 23### Cloud Setup (Google Colab) 24- You can access google colab [here](https://colab.research.google.com/) 25- Create a new notebook 26- Install the `scx` package by adding the following to a new code cell at the **top** of your notebook and running it: 27 - `pip install scx` 28 29 30### Local Setup 31Make sure you have Python 3.11.x (or higher) installed on your system. You can download it [here](https://www.python.org/downloads/). 32<details> 33<summary> 34Recommended (but Optional) -> Expand this section to setup and activate a virtual environment. 35</summary> 36 37 - Install (or upgrade) virtualenv: 38 ``` 39 python3 -m pip install --upgrade virtualenv 40 ``` 41 - Create your virtualenv named `venv`: 42 ``` 43 python3 -m virtualenv venv 44 ``` 45 - Activate your virtual environment 46 - On Unix (Mac or Linux): 47 ``` 48 source venv/bin/activate 49 ``` 50 - On Windows: 51 ``` 52 venv\\scripts\\activate 53 ``` 54</details> 55 56``` 57pip install scx 58``` 59 60## Optimization Getting Started 61See all of the optimization examples [here](https://github.com/connor-makowski/scx/tree/main/notebooks/optimization/). 62 63### Basic Usage 64```py 65from scx.optimize import Model 66``` 67 68#### Simple Optimization 69```py 70from scx.optimize import Model 71 72# Create variables 73product_1_amt = Model.variable(name="product_1", lowBound=0) 74product_2_amt = Model.variable(name="product_2", lowBound=0) 75 76# Initialize the model 77my_model = Model(name="Generic_Problem", sense='maximize') 78 79# Add the Objective Fn 80my_model.add_objective( 81 fn = (product_1_amt*1)+(product_2_amt*1) 82) 83 84# Add Constraints 85my_model.add_constraint( 86 name = 'input_1_constraint', 87 fn = product_1_amt*1+product_2_amt*2 <= 100 88) 89my_model.add_constraint( 90 name = 'input_2_constraint', 91 fn = product_1_amt*3+product_2_amt*1 <= 200 92) 93 94# Solve the model 95my_model.solve(get_duals=True, get_slacks=True) 96 97# Show the outputs 98# NOTE: outputs can be fetched directly as a dictionary with `my_model.get_outputs()` 99my_model.show_outputs() 100``` 101Outputs: 102```py 103{'duals': {'input_1_constraint': 0.4, 'input_2_constraint': 0.2}, 104 'objective': 80.0, 105 'slacks': {'input_1_constraint': -0.0, 'input_2_constraint': -0.0}, 106 'status': 'Optimal', 107 'variables': {'product_1': 60.0, 'product_2': 20.0}} 108 109``` 110 111## Database Getting Started 112See all of the database examples [here](https://github.com/connor-makowski/scx/tree/main/notebooks/database/) 113 114### Basic Usage 115```py 116from scx.database import Database 117# Specify the S3 path to the data 118data_folder = 's3://scx-dev/databases/supermarket/' 119# Create the database 120db = Database(f''' 121 CREATE TABLE Customers AS SELECT * FROM read_parquet('{data_folder}customers.parquet'); 122''') 123# Show the database Schema 124db.show_info() 125 126# Query the database 127db.query("SELECT * FROM Customers LIMIT 5") 128```"""