Error with deepcopy (pickle) applied to ConcreteModel

2,130 views
Skip to first unread message

Cord Kaldemeyer

unread,
Jan 8, 2018, 4:57:17 AM1/8/18
to Pyomo Forum
Hi everybody,

when using deepcopy on a Concretemodel (which I need for a specific problem), I get an error:

TypeError: can't pickle dict_keys objects

Here's a modified example from the repository that illustrates the problem:

#  ___________________________________________________________________________
#
#  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)


Dict-keys in this case are of type string and I am wondering why it is not working.

Any hints on how I can create a deep copy of a ConcreteModel?

Thanks in advance!

Cheers
Cord

William Hart

unread,
Jan 8, 2018, 6:32:16 AM1/8/18
to pyomo...@googlegroups.com
Can you give more details?  The compute platform?  Version of python and pyomo? Etc

--
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.

Cord Kaldemeyer

unread,
Jan 8, 2018, 8:08:57 AM1/8/18
to pyomo...@googlegroups.com
Hi Will,

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

What's wrong about the dict that's used to initialize the set?

Cheers
Cord
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.

Siirola, John D

unread,
Jan 8, 2018, 1:48:51 PM1/8/18
to pyomo...@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.

Siirola, John D

unread,
Jan 8, 2018, 3:10:46 PM1/8/18
to pyomo...@googlegroups.com

FYI: PR 305 (https://github.com/Pyomo/pyomo/pull/305) has a proposed fix for this bug.

 

john

Cord Kaldemeyer

unread,
Jan 9, 2018, 7:43:25 AM1/9/18
to Pyomo Forum
Hi John,

thanks for your answer! How are the chances to merge the PR into the next release?

Otherwise, I'll have to rewrite my code with you hotfix which would also be o.k..

Best
Cord

Cord Kaldemeyer

unread,
Jan 11, 2018, 3:35:51 AM1/11/18
to Pyomo Forum
I'll just keep track on github.

Thanks!

Siirola, John D

unread,
Jan 11, 2018, 8:37:00 AM1/11/18
to pyomo...@googlegroups.com
I can guarantee the fix will be in the next release. I am actually hoping to have the PR into master by the end of this week. 

John

Cord Kaldemeyer

unread,
Jan 11, 2018, 2:46:58 PM1/11/18
to Pyomo Forum
Thanks a lot!

Best
Cord
Reply all
Reply to author
Forward
0 new messages