IBMDecisionOptimization / docplex-examples

These samples demonstrate how to use the DOcplex library to model and solve optimization problems.
https://ibmdecisionoptimization.github.io/
Apache License 2.0
396 stars 228 forks source link

TypeError: can't pickle dict_keys objects #14

Closed arani-mohammad closed 5 years ago

arani-mohammad commented 5 years ago

Hi everyone, I faced the following error while I tried to solve my operations research problem: TypeError: can't pickle dict_keys objects The above error showed up when I wrote the following line using the academic version of cplex toolbox 12.9 on python version 3.6 and my operating system is Windows 7 x64: solution=mdl.solve(log_output=True)

It is important to note that when I removed the input argument (log_output=True), everything works fine. But I need a detailed output.

If anyone needs background information please read the following: As a conventional OR programming, first I introduced the sets like this: # LD: Set of locations of donors, d ∈ LD LD = {"d"+"%d"%i:i for i in range(1,5)} print("LD: ", LD) # LD: {'d1': 1, 'd2': 2, 'd3': 3, 'd4': 4} Then I randomly generated the parameters of my problem with an specific seed: FTC_d_q = {(d,q):rnd.randint(low=5 , high=10) for d in range(1,len(LD)+1) for q in range(1,len(LPF)+1)} After I introduced variables as following: dict_x_p_d_q_t = {(p,d,q,t):0 for p in range(1,len(P)+1) for d in range(1,len(LD)+1) for q in range(1,len(LPF)+1) for t in range(1,len(T)+1)} As recap, parameters and variable are defined as dictionaries. Finally, I started adding objective function and constraints:

mdl.minimize(

    # term 1
    mdl.sum(W_q[q]*JO_q[q] for q in range(1,len(LPF)+1)) 

    # term 2
    + mdl.sum(W_P_q_P_t[q_P,t]*JO_q_P[q_P] 
                    for q_P in range(1,len(LTF)+1)
                    for t in range(1,len(T)+1)
                    if t == 1) 

    # term 3
    + mdl.sum(W_PP[c,r]*JO_P_c_r[c,r] 
                     for c in range(1,len(LC)+1)
                     for r in range(1,len(C)+1)) 

    # term 4
    + mdl.sum(W_PPP[z]*JO_PP_z[z] for z in range(1,len(LVD)+1)) 

)
Equ_Names = ["Equ4."+str(i_temp) for i_temp in range(1,len(LPF)*len(T)+10)]

mdl.add_constraints(((mdl.sum(x_q[p,d,q,t]
                                for p in range(1,len(P)+1) 
                                for d in range(1,len(LD)+1)) )
                    <= (W_q[q]*SC_q[q])
                       for q in range(1,len(LPF)+1)
                       for t in range(1,len(T)+1)) 
                     ,names=Equ_Names)

As mentioned, since I have followed the described procedure to code my OR problem I employed cplex library to solve the problem by calling the following command: solution=mdl.solve(log_output=True) And received the error I mentioned earlier. The problem is a part of my dissertation and it has the following statistics:

 - number of variables: 43109
   - binary=11959, integer=31024, continuous=126
 - number of constraints: 60593
   - linear=60593

Please let me know if any additional information is required.

vlkong commented 5 years ago

Hi,

Can you please upgrade to docplex 2.10.150 and let me know if you still have this problem ? https://pypi.org/project/docplex/2.10.150/

arani-mohammad commented 5 years ago

Thank you very much. Everything is working perfectly. Do I need to close this issue?

vlkong commented 5 years ago

I'll close the issue if everything is now working. Thanks again.