Ticket #12418: 12418_refactored.diff

File 12418_refactored.diff, 3.0 KB (added by dimpase, 8 years ago)

code refactoring - for review only

  • sage/coding/delsarte_bounds.py

    diff --git a/sage/coding/delsarte_bounds.py b/sage/coding/delsarte_bounds.py
    a b  
    4343      kraw += jth_term
    4444   return kraw
    4545
     46def _delsarte_LP_building(n, d, d_star, q, isinteger,  solver, maxc = 0):
     47    from sage.numerical.mip import MixedIntegerLinearProgram
     48
     49    p = MixedIntegerLinearProgram(maximization=True, solver=solver)
     50    A = p.new_variable(integer=isinteger) # A>=0 is assumed
     51    p.set_objective(sum([A[r] for r in xrange(n+1)]))
     52    p.add_constraint(A[0]==1)
     53    for i in xrange(1,d):
     54        p.add_constraint(A[i]==0)
     55    for j in xrange(1,n+1):
     56        rhs = sum([Krawtchouk(n,q,j,r)*A[r] for r in xrange(n+1)])
     57        p.add_constraint(0*A[0] <= rhs)
     58        if j >= d_star:
     59          p.add_constraint(0*A[0] <= rhs)
     60        else: # rhs is proportional to j-th weight of the dual code
     61          p.add_constraint(0*A[0] == rhs)
     62
     63    if maxc > 0:
     64        p.add_constraint(sum([A[r] for r in xrange(n+1)]), max=maxc)
     65    return A, p
    4666
    4767def delsarte_bound_hamming_space(n, d, q,
    4868                    isinteger=False, return_data=False, solver="PPL"):
     
    104124
    105125
    106126   """
    107    from sage.numerical.mip import MixedIntegerLinearProgram
    108 
    109    p = MixedIntegerLinearProgram(maximization=True, solver=solver)
    110    A = p.new_variable(integer=isinteger) # A>=0 is assumed
    111    p.set_objective(sum([A[r] for r in range(n+1)]))
    112    p.add_constraint(A[0]==1)
    113    for i in range(1,d):
    114       p.add_constraint(A[i]==0)
    115    for j in range(1,n+1):
    116       rhs = sum([Krawtchouk(n,q,j,r)*A[r] for r in range(n+1)])
    117       p.add_constraint(0*A[0] <= rhs)
     127    A, p = _delsarte_LP_building(n, d, 0, q, isinteger,  solver)
    118128   try:
    119129      bd=p.solve()
    120130   except sage.numerical.mip.MIPSolverException, exc:
     
    187197       16
    188198
    189199   """
    190    from sage.numerical.mip import MixedIntegerLinearProgram
    191 
    192200   if q_base == 0:
    193201      q_base = q
    194202
     
    210218                         # we might become infeasible. More precisely, after rounding down
    211219                         # to the closest value of q_base^m, the LP, with the constraint that
    212220                         # the objective function is at most q_base^m,
    213       p = MixedIntegerLinearProgram(maximization=True, solver=solver)
    214       A = p.new_variable(integer=isinteger) # A>=0 is assumed
    215       p.set_objective(sum([A[r] for r in xrange(n+1)]))
    216       # the following constraint comes from rounding
    217       p.add_constraint(sum([A[r] for r in xrange(n+1)]), max=q_base**m)
    218       p.add_constraint(A[0]==1)
    219       for i in xrange(1,d):
    220         p.add_constraint(A[i]==0)
    221       for j in xrange(1,n+1):
    222         rhs = sum([Krawtchouk(n,q,j,r)*A[r] for r in xrange(n+1)])
    223         if j >= d_star:
    224           p.add_constraint(0*A[0] <= rhs)
    225         else: # rhs is proportional to j-th weight of the dual code
    226           p.add_constraint(0*A[0] == rhs)
     221      A, p = _delsarte_LP_building(n, d, d_star, q, isinteger,  solver, q_base**m)
    227222      try:
    228223        bd=p.solve()
    229224      except sage.numerical.mip.MIPSolverException, exc: