<jupyter_start><jupyter_text>OptimIQ - Semillero de Optimizacion en Ingenieria Quimica Pyomo Fundamentals - Exercise 1: Knapsack March 3rd, 2022 This notebook contains material from the OptimIQ course by <NAME> (dbernaln at purdue.edu); the content is available on **[Github](https://github.com/bernalde/OptimIQ)**.The text is released under the **[CC-BY-NC-ND-4.0](https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode) license, and code is released under the **[MIT license](https://opensource.org/licenses/MIT).*Run in **[Google Colab](https://colab.research.google.com/github/bernalde/OptimIQ/blob/master/notebooks/pyomo_fundamentals_exercise_1.ipynb)**This notebook is mostly taken from the Pyomo 2018 Summer Workshop available **[here](http://www.pyomo.org/workshop-examples)**.  Introduction to Pyomo[Pyomo](https://www.pyomo.org) is an object-oriented, python-based package for equation-oriented (or *algebraic*) modeling and optimization.Here we will briefly cover the commands required to specify an objective function or add a constraint to an existing model.In the next cell, we will create a Pyomo model, and add a couple of variables to that model.Inline Exercise:Execute the following cell to create a Pyomo model with some variables that will be used later. Before we get started on some actual examples, let's make sure that everything is working correctly. The cell below contains commands to install Pyomo as it is not included by default in Google Colab.<jupyter_code># If using this on Google collab, we need to install the packages
import sys
IN_COLAB = "google.colab" in sys.modules

# Import other libraries such as pandas and numpy
import pandas as pd
import numpy as np

# Let's install all these packages
if IN_COLAB:    
    !pip install -q pyomo
# Let's install all these packages
if IN_COLAB:    
    _=!wget -N -q "https://ampl.com/dl/open/ipopt/ipopt-linux64.zip"
    _=!unzip -o -q ipopt-linux64
    _=!apt-get install -y -q coinor-cbc
    _=!pip install -q cplex
    _=!pip install -q gurobipy
    _=!pip install -q xpress<jupyter_output><empty_output><jupyter_text>Everything should install correctly and not we can use the module.<jupyter_code>!pyomo help --solvers<jupyter_output><empty_output><jupyter_text>Problem definition<jupyter_code>from pyomo.environ import *

A = ['hammer', 'wrench', 'screwdriver', 'towel']
b = {'hammer':8, 'wrench':3, 'screwdriver':6, 'towel':11}
w = {'hammer':5, 'wrench':7, 'screwdriver':4, 'towel':3}
W_max = 14
model = ConcreteModel()
model.x = Var( A, within=Binary )

model.obj = Objective(
    expr = sum( b[i]*model.x[i] for i in A ), 
    sense = maximize )

model.weight_con = Constraint(
    expr = sum( w[i]*model.x[i] for i in A ) <= W_max )

opt = SolverFactory('glpk')
opt_success = opt.solve(model)<jupyter_output><empty_output><jupyter_text>Quick Python exercise, print solutions!<jupyter_code>total_weight = sum( w[i]*value(model.x[i]) for i in A )
# TODO: INSERT CODE HERE TO PRINT TOTAL WEIGHT AND BENEFIT

print('%12s %12s' % ('Item', 'Selected'))
print('=========================')
for i in A:
    # TODO: INSERT CODE HERE TO PRINT EACH ITEM AND WHETHER OR NOT IT WAS SELECTED
print('-------------------------')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12642967542503863, 'fraction_numerical': 0.010510046367851623, 'mean_word_length': 4.697183098591549, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 18, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '587361', 'license': 'MIT', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1025, 'n_words': 353, 'path': 'notebooks/pyomo_fundamentals_exercise_1.ipynb', 'repo_name': 'bernalde/OptimIQ', 'size': '3241'}