TypeError: can't pickle dict_keys objects
# ___________________________________________________________________________
#
# Pyomo: Python Optimization Modeling Objects
# Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC
# Under the terms of Contract DE-NA0003525 with National Technology and
# Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
# rights in this software.
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________
#
# Knapsack Problem
#
from copy import deepcopy
from pyomo.environ import *
v = {'hammer':8, 'wrench':3, 'screwdriver':6, 'towel':11}
w = {'hammer':5, 'wrench':7, 'screwdriver':4, 'towel':3}
limit = 14
M = ConcreteModel()
M.ITEMS = Set(initialize=v.keys())
M.x = Var(M.ITEMS, within=Binary)
M.value = Objective(expr=sum(v[i]*M.x[i] for i in M.ITEMS), sense=maximize)
M.weight = Constraint(expr=sum(w[i]*M.x[i] for i in M.ITEMS) <= limit)
# check
M2 = deepcopy(M)
--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
ERROR: Unable to clone Pyomo component attribute.
Component 'ITEMS' contains an uncopyable field 'initialize' (<class 'dict_keys'>)
pyomo 5.2 py35_0 conda-forge
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.
This is a Python 3.x problem. “dict.keys()” returns an unclonable dict_keys object instead of a list. We try and convert simple things like tuples and generators into lists, but it turns out that dict_keys is not a GeneratorType (<sigh>).
My simple fix to more aggressively convert any iterable thing into a list caused several test failures, so we will have to think more about how to properly fix it (filing an issue in GitHub would be helpful).
In the meantime, as a workaround for Python 3.x, do not initialize a Set with a dict.keys(), and instead explicitly convert it to a list: “list(dict.keys())”.
john
From: pyomo...@googlegroups.com [mailto:pyomo...@googlegroups.com]
On Behalf Of Cord Kaldemeyer
Sent: Monday, January 08, 2018 6:09 AM
To: Pyomo Forum <pyomo...@googlegroups.com>
Subject: [EXTERNAL] Re: Error with deepcopy (pickle) applied to ConcreteModel
Hi Bill,
of course! I haven't thought about having the latest version and now updated from Pyomo 4.4 to Pyomo 5.2 which gives me another error message:
ERROR:
Unable to clone
Pyomo component attribute.
Component
'ITEMS' contains an uncopyable
field 'initialize'
(<class
'dict_keys'>)
I am on Ubuntu 16.04 LTS x64 with Anaconda 3.5 and Pyomo 5.2 installed via conda-forge:
pyomo 5.2 py35_0 conda-forge
Thanks.