0% found this document useful (0 votes)
13 views3 pages

Syntax Modul 3

The document outlines a Python script that installs and utilizes the gspread library to access Google Sheets data. It retrieves specific values from a spreadsheet, constructs linear programming variables and constraints using the Gurobi optimizer, and solves the optimization problem. The optimal objective value of the model is printed at the end of the script.

Uploaded by

Ayaaa. rh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Syntax Modul 3

The document outlines a Python script that installs and utilizes the gspread library to access Google Sheets data. It retrieves specific values from a spreadsheet, constructs linear programming variables and constraints using the Gurobi optimizer, and solves the optimization problem. The optimal objective value of the model is printed at the end of the script.

Uploaded by

Ayaaa. rh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

%pip install --upgrade gspread

import gspread

from google.colab import auth


auth.authenticate_user()

from google.auth import default


creds, _=default()

gc = gspread.authorize(creds)

Requirement already satisfied: gspread in /usr/local/lib/python3.11/dist-packages (6.2.1)


Requirement already satisfied: google-auth>=1.12.0 in /usr/local/lib/python3.11/dist-packages (from gspread
Requirement already satisfied: google-auth-oauthlib>=0.4.1 in /usr/local/lib/python3.11/dist-packages (from
Requirement already satisfied: cachetools<6.0,>=2.0.0 in /usr/local/lib/python3.11/dist-packages (from goog
Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.11/dist-packages (from googl
Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.11/dist-packages (from google-auth>=
Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.11/dist-packages (from go
Requirement already satisfied: pyasn1<0.7.0,>=0.6.1 in /usr/local/lib/python3.11/dist-packages (from pyasn1
Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.11/dist-packages (from requests-oa
Requirement already satisfied: requests>=2.0.0 in /usr/local/lib/python3.11/dist-packages (from requests-oa
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from re
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.11/dist-packages (from requests>=2.0.
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/dist-packages (from requests
 

googlespreadsheet = "https://docs.google.com/spreadsheets/d/13lBNtuLEku8pGbcx89LR5LPN1ORVD7cTWaz8o07yJTE/edit?gi
sh = gc.open_by_url(googlespreadsheet)

worksheet = sh.worksheet("Sheet1")
print(worksheet.cell(5,2).value)

N = 3
M = 4

C = {}
for i in range(N):
C[i] = int(worksheet.cell(5,i+2).value)

b = {}
for j in range(M):
b[j] = int(worksheet.cell(6,j+2).value)

A = {}
for j in range(M):
A[j] = {}
for i in range(N):
A[j][i] = int(worksheet.cell(7+j,i+2).value)

print (C)
print (b)
print (A)

3000
{0: 3000, 1: 4000, 2: 3500}
{0: 300, 1: 240, 2: 200, 3: 180}
{0: {0: 2, 1: 1, 2: 4}, 1: {0: 1, 1: 2, 2: 1}, 2: {0: 2, 1: 1, 2: 3}, 3: {0: 1, 1: 5, 2: 1}}

#N = 3
#M = 4

#C = [3000,4000,3500]
#b = [300,240,200,180]
#A = [[2,1,4],[1,2,1],[2,1,3],[1,5,1]]

from gurobipy import Model, quicksum


m = Model("LP")

x = {}
for i in range(N):
x[i] = m.addVar(vtype = "C")

m.setObjective(
quicksum(C[i] * x[i] for i in range(N))
)

m.ModelSense = 0

for j in range(2):
m.addConstr(
quicksum(A[j][i] * x[i] for i in range(N)) <= b[j]
)

for j in range(2,M):
m.addConstr(
quicksum(A[j][i] * x[i] for i in range(N)) >= b[j]
)

m.optimize()

print (m.ObjVal)

Gurobi Optimizer version 12.0.2 build v12.0.2rc0 (linux64 - "Ubuntu 22.04.4 LTS")

CPU model: Intel(R) Xeon(R) CPU @ 2.20GHz, instruction set [SSE2|AVX|AVX2]


Thread count: 1 physical cores, 2 logical processors, using up to 2 threads

Optimize a model with 4 rows, 3 columns and 12 nonzeros


Model fingerprint: 0xfb4dfc73
Coefficient statistics:
Matrix range [1e+00, 5e+00]
Objective range [3e+03, 4e+03]
Bounds range [0e+00, 0e+00]
RHS range [2e+02, 3e+02]
Presolve time: 0.01s
Presolved: 4 rows, 3 columns, 12 nonzeros

Iteration Objective Primal Inf. Dual Inf. Time


0 1.3500000e+34 4.750000e+30 1.350000e+04 0s
3 6.0000000e+05 0.000000e+00 0.000000e+00 0s

Solved in 3 iterations and 0.01 seconds (0.00 work units)


Optimal objective 6.000000000e+05
600000.0

You might also like