<jupyter_start><jupyter_text>$$Exercise: \;1$$<jupyter_code>!pip install -q pyomo
from pyomo.environ import *
import numpy as np<jupyter_output><empty_output><jupyter_text>$$Q-4$$<jupyter_code>coef = np.loadtxt('file1.txt', delimiter = ',')
print(coef.shape)
print('Number of rows=', coef.shape[0])
print('Number of columns=', coef.shape[1])
print(coef)<jupyter_output>[[  0.85   3.75  -8.75  -3.45 -22.38  20.     0.  ]
 [ -0.7   -3.9   -1.     0.     1.     0.    14.9 ]
 [  1.     0.     0.    -1.     0.    -1.     0.9 ]
 [  0.    -3.    -2.     0.     0.    -1.   -51.7 ]
 [  0.     0.     0.     4.8    9.12   7.2   75.  ]
 [ -0.8   -1.5    8.    -2.     1.     0.   -27.  ]]<jupyter_text>$$Q-5$$<jupyter_code>model = ConcreteModel()
# setting the number of decision variables and constraints
M = coef.shape[0] - 1 
N = coef.shape[1] - 1 
print(M)
print(N)
# coefficient of the objective function
obj_coef = coef[0,:-1]

# coefficients of the constraints
constr_coef = coef[1:,:-1]

# rhs of the constraints
constr_rhs = coef[1:,-1]

#Since the variables have the same upper bound and lower bound, we can simply use scalars to store them
lower_bound = 0
upper_bound = np.inf
# set row and column indices
row_indices = np.arange(M)
col_indices = np.arange(N)
print(col_indices)
print(row_indices)
# declare the decision variables in the model
model.x = Var(col_indices)
# create a constraint list to hold multiple constraints
model.constraints = ConstraintList()
for i in row_indices:
      model.constraints.add(sum(constr_coef[i][j]*model.x[j] for j in col_indices)<= constr_rhs[i])
# set upper and lower bounds of the variables
for j in col_indices:
  model.x[j].setlb(lower_bound)
  model.x[j].setub(upper_bound)
# add model objective function
model.objective = Objective(expr = summation(obj_coef, model.x), sense = maximize)
model.pprint()<jupyter_output>2 Set Declarations
    constraints_index : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    5 : {1, 2, 3, 4, 5}
    x_index : Size=1, Index=None, Ordered=False
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    6 : {0, 1, 2, 3, 4, 5}

1 Var Declarations
    x : Size=6, Index=x_index
        Key : Lower : Value : Upper : Fixed : Stale : Domain
          0 :     0 :  None :   inf : False :  True :  Reals
          1 :     0 :  None :   inf : False :  True :  Reals
          2 :     0 :  None :   inf : False :  True :  Reals
          3 :     0 :  None :   inf : False :  True :  Reals
          4 :     0 :  None :   inf : False :  True :  Reals
          5 :     0 :  None :   inf : False :  True :  Reals

1 Objective Declarations
    objective : Size=1, Index=None, Active=True
        Key  : Active : Sense    : Expression
        None :   True : maximize : 0.85*x[0] + 3.75*x[1] - 8.75*x[2[...]<jupyter_text>$$Q-6$$<jupyter_code>!apt-get install -y -qq coinor-cbc
opt_cbc = SolverFactory('cbc')
result = opt_cbc.solve(model)
print('Solver status:', result.solver.status)
print('Solver termination condition:',result.solver.termination_condition)<jupyter_output>WARNING: Loading a SolverResults object with a warning status into
    model.name="unknown";
      - termination condition: unbounded
      - message from solver: <undefined>
Solver status: warning
Solver termination condition: unbounded<jupyter_text>$$Q-7$$$The\; termination \;condition\; is \;unbounded,\; i.e. \;in \;this\; problem,\; we \;can \;maximize \;the\; objective \;function \;indefinitely\; without\; violating\; the\; constraints.\; The\; solver\; status\; shows \;that\; we \;cannot \;find \;an \;optimal \;solution,\; as \;the \;feasible \;region \;is\; unbounded.$ $$Q-8$$$We\; were \;solving\; the\; problem\; in\; the\; wrong\; optimization \;sense, \;so \;the\; feasible\; region \;is\; unbounded.$<jupyter_code># reset the sense to minimize
model.objective.set_sense(minimize)
model.pprint()

result = opt_cbc.solve(model)
print(result)
print('SolverStatus:', result.solver.status)
print('Termination Condition:', result.solver.termination_condition)
# display results
print('Objective Function:', model.objective())
print('\nDecision Variables: ')
for j in col_indices:
      print('x[',j,']= ', model.x[j].value)
print('\nConstraints:')
model.constraints.display()<jupyter_output>Objective Function: -97.02184239599997

Decision Variables: 
x[ 0 ]=  0.9
x[ 1 ]=  23.002456
x[ 2 ]=  0.0
x[ 3 ]=  0.0
x[ 4 ]=  8.2236842
x[ 5 ]=  0.0

Constraints:
constraints : Size=5
    Key : Lower : Body                : Upper
      1 :  None :         -82.1158942 :  14.9
      2 :  None :                 0.9 :   0.9
      3 :  None :          -69.007368 : -51.7
      4 :  None :   74.99999990399999 :  75.0
      5 :  None : -26.999999799999998 : -27.0
{'fraction_non_alphanumeric': 0.1469914636685405, 'fraction_numerical': 0.0526754112013325, 'mean_word_length': 2.7125193199381763, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '427026', 'license': 'MIT', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1737, 'n_words': 472, 'path': 'Loading values from a file, provided file1.ipynb', 'repo_name': 'RoyaakasH/Modelling-and-Computation-Lab', 'size': '4803'}