Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

object of type 'generator' has no len() #2

Open
sangnguyens opened this issue Jul 23, 2022 · 5 comments
Open

object of type 'generator' has no len() #2

sangnguyens opened this issue Jul 23, 2022 · 5 comments

Comments

@sangnguyens
Copy link

Hi,
when I execute the bl_bin_pool = main.main(bl_order, procedure="bl", tlim=20), error appears:

TypeError Traceback (most recent call last)
Input In [24], in <cell 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 ~/Documents/3d-bpp/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 ~/Documents/3d-bpp/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")

File ~/Documents/3d-bpp/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 ~/Documents/nguyens-RL/nguyens-RL/lib/python3.9/site-packages/ortools/sat/python/cp_model.py:182, in LinearExpr.Sum(cls, expressions)
179 @classmethod
180 def Sum(cls, expressions):
181 """Creates the expression sum(expressions)."""
--> 182 if len(expressions) == 1:
183 return expressions[0]
184 return _SumArray(expressions)

@Wadaboa
Copy link
Owner

Wadaboa commented Jul 26, 2022

Hi,

Thanks for opening the issue.

Could you please dump some information about your Python environment? Also, what is the actual exception raised by the code? I cannot see it in the stacktrace you pasted above.

Thanks,
Alessio.

@rlacjfjin
Copy link

You can rewrite the wrong part, which may be caused by the inconsistent version of ortools. (my ortools version is 9.3.10497)
I have found problems involving the cp_model.LinearExpr.Sum() function, which you can simply write as sum().

I hope to owner (@Wadaboa) provide an environment in which the project can run normally, such as requirements.txt or environment.yml of conda.

And I have a question:
You implemented it in the form of constraint programming, but is also can using MIP form, I think two ways are same.
Is there any reason for you to choose constraint programming? (For example, constraint programming solver is faster?)

Looking forward to your reply,
Thanks.

Zhe

@Wadaboa
Copy link
Owner

Wadaboa commented Aug 9, 2022

Hi @rlacjfjin,

In the init/ folder you can find 2 files: requirements.txt and environment.yml. The former is for pip environments, while the latter for Conda environments. The README also has a brief Installation section about this. As you can see in the mentioned files, the version of OR-Tools we relied on is 8.2.8710, so I expect the exception above to disappear when the environment is correctly set up. Please let me know if this isn't the case.

As for your question, we implemented the sub-problem as both CP and MIP for education purposes and also because of the additional utilities available in the CP solver, such as decision strategies and search branching methods. In our final tests, we went for MIP in the "no-placement" sub-problem and CP in the "placement" sub-problem: we experimented with different combinations and this one seemed to be the fastest, but this might vary depending on the data you use.

Let me know if you have any other question. Thanks,
Alessio.

@rlacjfjin
Copy link

@Wadaboa ,
Thank you, I have no more questions.

@kiesoman
Copy link

kiesoman commented Nov 8, 2022

Hi,
It is workable to install the newest version of OR-Tools in environment.
However, you should change the the content around 63 line in baseline.py as below:
cp_model.LinearExpr.Sum(
[fsi[s, i] * zsl[s, l] for s in range(n_superitems) for l in range(max_layers)]
)
the argument should be a list, and then it will fit len() requirement.

you also need to change 77 line and 114 line.
enjoy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants