Multiselect PULP problem row iterrow

Dear ms All.

My name is Adnan , I’m from Sarajevo. I’m new on the Streamlit forum and trying hard to learn Streamlit.

I have one problem with my code that I try to finish. I’ve tried to make some variables which I’ll use to make an formula (objective function) in Pulp. Variables are made using an foor loop and inserted in the list (using selected_rows.iterrow) named decision_variables.

After that I’ve tried to make an objective function which would be product of elements from decision_variables and elements from column ‘Lat’ from table as in the code bellow.

Everything works fine if I choose rows in the right order (first 0. row, then 1., 2. and etc). But if I choose rows randomly (for example 2., then 5., 8. etc) then I got these errors:

“TypeError: Can only add LpConstraintVar, LpConstraint, LpAffineExpression or True objects”

and at the end, after choose randomly rows I’ve got just one product (last choosed, not all products of Lat and decision_variable elements).

What could be a problem?

Here is a code:

import streamlit as st
import pandas as pd
from pulp import *

Load some example data.

DATA_URL =
http://s3-us-west-2.amazonaws.com/streamlit-demo-data/uber-raw-data-sep14.csv.gz
data = st.cache(pd.read_csv)(DATA_URL, nrows=1000)

Select some rows using st.multiselect.

st.write(‘### Full Dataset’, data)
selected_indices = st.multiselect(‘Select rows:’, data.index)
selected_rows = data.loc[selected_indices]
st.write(‘### Selected Rows’, selected_rows)

decision_variables =
for rownum, row in selected_rows.iterrows():
variable = str(‘x’ + str(rownum))
variable = LpVariable(str(variable), lowBound = 0, upBound = 1, cat= ‘Integer’)
decision_variables.append(variable)

print ("Total_number_of_decision_variables: " + str(len(decision_variables)))
print(decision_variables)
st.write(decision_variables)

define the problem

prob = LpProblem(‘test’,LpMinimize)

total_cost = “”
for rownum, row in selected_rows.iterrows():
for i, schedule in enumerate(decision_variables):
if rownum == i:
formula = row[‘Lat’]*schedule
total_cost += formula

prob += total_cost

print ("Optimization function: " + str(total_cost))

st.write(“Objective function is :”)
st.title(total_cost)

Your code is unreadable, please format it properly.

Dear Franky.

Here it goes:

import streamlit as st
import pandas as pd
from pulp import *

Load some example data.

DATA_URL =
http://s3-us-west-2.amazonaws.com/streamlit-demo-data/uber-raw-data-sep14.csv.gz
data = st.cache(pd.read_csv)(DATA_URL, nrows=1000)

Select some rows using st.multiselect. This will break down when you have >1000 rows.

st.write(‘### Full Dataset’, data)
selected_indices = st.multiselect(‘Select rows:’, data.index)
selected_rows = data.loc[selected_indices]
st.write(‘### Selected Rows’, selected_rows)

decision_variables =
for rownum, row in selected_rows.iterrows():
variable = str(‘x’ + str(rownum))
variable = LpVariable(str(variable), lowBound = 0, upBound = 1, cat= ‘Integer’)
decision_variables.append(variable)

print ("Total_number_of_decision_variables: " + str(len(decision_variables)))
print(decision_variables)
st.write(decision_variables)

define the problem

prob = LpProblem(‘test’,LpMinimize)

total_cost = “”
for rownum, row in selected_rows.iterrows():
for i, schedule in enumerate(decision_variables):
if rownum == i:
formula = row[‘Lat’]*schedule
total_cost += formula

prob += total_cost

print ("Optimization function: " + str(total_cost))

st.write(“Kreirana je funkcija Cilja kako slijedi :”)
st.title(total_cost)

Don’t post images of code in the forum, post your code in markdown :roll_eyes:

```python
# paste your code here
```
import streamlit as st
import pandas as pd
from pulp import *

# Load some example data.
DATA_URL = \
    "http://s3-us-west-2.amazonaws.com/streamlit-demo-data/uber-raw-data-sep14.csv.gz"
data = st.cache(pd.read_csv)(DATA_URL, nrows=1000)

# Select some rows using st.multiselect. This will break down when you have >1000 rows.
st.write('### Full Dataset', data)
selected_indices = st.multiselect('Select rows:', data.index)
selected_rows = data.loc[selected_indices]
st.write('### Selected Rows', selected_rows)

decision_variables = []
for rownum, row in selected_rows.iterrows():
    variable = str('x' + str(rownum))
    variable = LpVariable(str(variable), lowBound = 0, upBound = 1, cat= 'Integer')
    decision_variables.append(variable)


print ("Total_number_of_decision_variables: " + str(len(decision_variables)))
print(decision_variables)
st.write(decision_variables)

# define the problem
prob = LpProblem('test',LpMinimize)

total_cost = ""
for rownum, row in selected_rows.iterrows():
    for i, schedule in enumerate(decision_variables):
        if rownum == i:
            formula = row['Lat']*schedule
            total_cost += formula

    prob += total_cost

print ("Optimization function: " + str(total_cost))

st.write("**Kreirana je funkcija Cilja kako slijedi :**")
st.title(total_cost)`Preformatted text`
1 Like

at least :slight_smile: