{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# JuMP" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## New: Udated to the latest JuMP (0.20 at time of writing) ##" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The problem\n", "\n", "Given a single product, p plants and m markets, define the best route between p and m as to minimize transport costs:\n", "\n", "$ min \\sum_{p}{\\sum_{m} {c_{p,m} * x_{p,m}}} $\n", "\n", "s.t.\n", "\n", "$\\sum_{m} x_{p,m} <= a_p $\n", "\n", "$\\sum_{p} x_{p,m} >= b_m $\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Importing the libraries\n", "\n", "You will need to import as a minima the JuMP module. If you wish to specify a solver engine rather than letting JuMP select a suitable one, you will need to import also the module relative to the solver, e.g. Ipopt or GLPK" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Import of the JuMP, GLPK and CSV modules (the latter one just to import the data from a header based table, as in the original trasnport example in GAMS \n", "using JuMP, GLPK, CSV, DataFrames" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Defining the \"sets\"\n", "\n", "JuMP doesn't really have a concept of sets, but it uses the native containers available in the core Julia language\\\\Variables, parameters and constraints can be indexed using these containers.\n", "While many works with position-based lists, I find more readable using dictionaries instead. So the “sets” are represented as lists, but then everything else is a dictionary with the elements of the list as keys.\n", "One note: it seems that Julia/JuMP don't like much the “-” symbol, so I replaced it to “_”." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{String,1}:\n", " \"new_york\"\n", " \"chicago\" \n", " \"topeka\" " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Define sets #\n", "# Sets\n", "# i canning plants / seattle, san-diego /\n", "# j markets / new-york, chicago, topeka / ;\n", "plants = [\"seattle\",\"san_diego\"] # canning plants\n", "markets = [\"new_york\",\"chicago\",\"topeka\"] # markets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Definition of the \"parameters\"\n", "\n", "Capacity of plants and demand of markets are directly defined as dictionaries, while the distance is first read as a DataFrame from a white-space separated table and then it is converted in a “(plant, market) ⇒ value” dictionary. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Dict{String,Int64} with 3 entries:\n", " \"chicago\" => 300\n", " \"new_york\" => 325\n", " \"topeka\" => 275" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Define parameters #\n", "# Parameters\n", "# a(i) capacity of plant i in cases\n", "# / seattle 350\n", "# san-diego 600 /\n", "a = Dict( # capacity of plant i in cases\n", " \"seattle\" => 350,\n", " \"san_diego\" => 600,\n", ")\n", " \n", "# b(j) demand at market j in cases\n", "# / new-york 325\n", "# chicago 300\n", "# topeka 275 / ;\n", "b = Dict( # demand at market j in cases\n", " \"new_york\" => 325,\n", " \"chicago\" => 300,\n", " \"topeka\" => 275,\n", ")\n", " " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
plants | new_york | chicago | topeka | |
---|---|---|---|---|
String | Float64 | Float64 | Float64 | |
1 | seattle | 2.5 | 1.7 | 1.8 |
2 | san_diego | 2.5 | 1.8 | 1.4 |