Skip to content

baseline.py TypeError: object of type 'generator' has no len() #11

@Hufnagels

Description

@Hufnagels

Hi,
this is a very thankful job, u done.
I tried to play with it, but some error occurs.

In baseline.py, when reached the notebook section [35] the following error occurs: TypeError: object of type 'generator' has no len()

Conda installed python is 3.11 instead given in Dockerfile
ortools==8.2.8710 isn't installable

In every case of calling ortools 'cp_model.LinearExpr.Sum' function it returns this error.

Can You help to solve this?

Thanks

Related info

[35]
bl_bin_pool = main.main(bl_order, procedure="bl", tlim=20)
bl_bin_pool.get_original_layer_pool().to_dataframe()

2023-09-11 09:29:04.210 | INFO     | baseline:baseline:165 - Solving baseline model
2023-09-11 09:29:04.201 | INFO     | main:main:169 - BL procedure starting
2023-09-11 09:29:04.202 | INFO     | main:main:179 - BL iteration 1/1
2023-09-11 09:29:04.205 | DEBUG    | superitems:_gen_single_items_superitems:639 - Generated 20 superitems with a single item
2023-09-11 09:29:04.205 | INFO     | superitems:gen_superitems:623 - Generating horizontal superitems of type 'two-width'
2023-09-11 09:29:04.206 | DEBUG    | superitems:_gen_superitems_horizontal:685 - Generated 0 horizontal superitems with 2 items
2023-09-11 09:29:04.207 | DEBUG    | superitems:_gen_superitems_horizontal:692 - Generated 0 horizontal superitems with 4 items
2023-09-11 09:29:04.207 | INFO     | superitems:gen_superitems:626 - Generating vertical superitems with maximum stacking of 4
2023-09-11 09:29:04.208 | DEBUG    | superitems:_gen_superitems_vertical:770 - Generated 15 wide vertical superitems
2023-09-11 09:29:04.209 | DEBUG    | superitems:_gen_superitems_vertical:772 - Generated 0 deep vertical superitems
2023-09-11 09:29:04.209 | INFO     | superitems:gen_superitems:628 - Generated 35 superitems
2023-09-11 09:29:04.209 | INFO     | superitems:gen_superitems:630 - Remaining superitems after filtering by pallet dimensions: 35
2023-09-11 09:29:04.210 | INFO     | baseline:baseline:165 - Solving baseline model
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[35], line 1
----> 1 bl_bin_pool = main.main(bl_order, procedure="bl", tlim=20)
      2 bl_bin_pool.get_original_layer_pool().to_dataframe()

File ~/work/src/main.py:194, in main(order, procedure, max_iters, superitems_horizontal, superitems_horizontal_type, superitems_max_vstacked, density_tol, filtering_two_dims, filtering_max_coverage_all, filtering_max_coverage_single, tlim, enable_solver_output, height_tol, cg_use_height_groups, cg_mr_warm_start, cg_max_iters, cg_max_stag_iters, cg_sp_mr, cg_sp_np_type, cg_sp_p_type, cg_return_only_last)
    192 # Call the right packing procedure
    193 if procedure == "bl":
--> 194     layer_pool = baseline.baseline(superitems_pool, config.PALLET_DIMS, tlim=tlim)
    195 elif procedure == "mr":
    196     layer_pool = maxrects_warm_start(
    197         superitems_pool, height_tol=height_tol, density_tol=density_tol, add_single=False
    198     )

File ~/work/src/baseline.py:166, in baseline(superitems_pool, pallet_dims, tlim, num_workers)
    164 # Call the baseline model
    165 logger.info("Solving baseline model")
--> 166 sol, solve_time = baseline_model(
    167     fsi, ws, ds, hs, pallet_dims, tlim=tlim, num_workers=num_workers
    168 )
    169 logger.info(f"Solved baseline model in {solve_time:.2f} seconds")
    171 # Build the layer pool from the model's solution

File ~/work/src/baseline.py:62, in baseline_model(fsi, ws, ds, hs, pallet_dims, tlim, num_workers)
     58 # Constraints
     59 # Ensure that every item is included in exactly one layer
     60 for i in range(n_items):
     61     model.Add(
---> 62         cp_model.LinearExpr.Sum(
     63             fsi[s, i] * zsl[s, l] for s in range(n_superitems) for l in range(max_layers)
     64         )
     65         == 1
     66     )
     68 # Define the height of layer l
     69 for l in range(max_layers):

File /opt/conda/lib/python3.11/site-packages/ortools/sat/python/cp_model.py:183, in LinearExpr.Sum(cls, expressions)
    180 @classmethod
    181 def Sum(cls, expressions):
    182     """Creates the expression sum(expressions)."""
--> 183     if len(expressions) == 1:
    184         return expressions[0]
    185     return _SumArray(expressions)

TypeError: object of type 'generator' has no len()

I tried to convert it to a docker container with the following params:

  • ortools==9.5.2237 (original isn't reachable)
  • python 3.9.6
  • nb_black (installed from git --> 1.0.7 has restriction for python < 3.6)

The Dockerfile:

FROM jupyter/base-notebook

# Name your environment and choose the python version
ARG env_name=python3.9.6
ARG py_ver=3.9.6

COPY --chown=${NB_UID}:${NB_GID} /init/requirements.txt /tmp/
COPY --chown=${NB_UID}:${NB_GID} /init/environment.yml /tmp/
RUN mamba env create -p "${CONDA_DIR}/envs/${env_name}" -f /tmp/environment.yml && \
  mamba clean --all -f -y

# Create Python kernel and link it to jupyter
RUN "${CONDA_DIR}/envs/${env_name}/bin/python" -m ipykernel install --user --name="${env_name}" && \
  fix-permissions "${CONDA_DIR}" && \
  fix-permissions "/home/${NB_USER}"

RUN "${CONDA_DIR}/envs/${env_name}/bin/pip" install --no-cache-dir \
  'flake8'

USER root
RUN apt update -y
RUN apt install git -y

RUN pip install --no-cache-dir -r /tmp/requirements.txt
#USER ${NB_UID}
RUN pip install git+https://github.com/IsaGrue/nb_black.git

USER root
RUN activate_custom_env_script=/usr/local/bin/before-notebook.d/activate_custom_env.sh && \
  echo "#!/bin/bash" > ${activate_custom_env_script} && \
  echo "eval \"$(conda shell.bash activate "${env_name}")\"" >> ${activate_custom_env_script} && \
  chmod +x ${activate_custom_env_script}

USER ${NB_UID}

RUN echo "conda activate ${env_name}" >> "${HOME}/.bashrc"

The docker-compose.yaml

version: "3"

services:
  app:
    container_name: 3dp-packing
    build:
      context: .
      dockerfile: ./Dockerfile
    # command: flask --app ./src/hello --debug run --host=0.0.0.0 --port=8080
    image: 3dp-packing:latest
    volumes:
      - ${PWD}/work:/home/jovyan/work

    ports:
      - 8888:8888
      - 8787:8787

requirements.txt

numpy
pandas
ortools==9.5.2237
matplotlib
ipympl==0.7.0
rectpack
tqdm==4.60.0
scipy
seaborn
streamlit==1.8.1
watchdogs==1.8.2
loguru==0.5.3

environment.yaml

name: 3d-bpp

channels:
  - conda-forge
  - defaults

dependencies:
  - black==21.7b0
  - loguru==0.5.3
  - matplotlib==3.4.2
  - nb_black==1.0.7
  - numpy
  - pandas
  - pip==21.2.1
  - python==3.9.6
  - seaborn==0.11.2
  - tqdm==4.61.2
  - streamlit==0.85.1
  - pip:
      - ortools==9.5.2237
      - rectpack==0.2.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions