from __future__ import divisionfrom pyomo.environ import *from random import randint, seedseed(321)N = 50weight = [randint(1,100) for i in range(0,N)]profit = [randint(1,100) for i in range(0,N)]totalWeight = sum(weight)KnapsackCapacity = totalWeight * 0.55profit_to_weight = [profit[i] / weight[i] for i in range(0,N)]import numpy as nplowerPercentile = np.percentile(profit_to_weight,37.5)upperPercentile = np.percentile(profit_to_weight,62.5)BinaryIdx = {i+1 for i in range(0,N)if lowerPercentile <= profit_to_weight[i] and profit_to_weight[i] <= upperPercentile}ContinuousIdx = {i for i in range(1,N+1)} - BinaryIdxm = ConcreteModel()m.xb = Var(BinaryIdx, domain=Binary )m.xc = Var(ContinuousIdx, bounds=(0.0,1.0))m.I = Set(initialize = RangeSet(N))X = {i : m.xb[i] if i in BinaryIdx else m.xc[i] for i in m.I}m.KnapsackConstraint = Constraint(expr = sum(weight[i-1]*X[i] for i in m.I) <= KnapsackCapacity)m.TotalProfit = Objective(expr = sum(profit[i-1]*X[i] for i in m.I), sense=maximize)opt = SolverFactory("glpk")results = opt.solve(m, tee=True)print("Profit weight p2w type value")for im1 in range(0,N):i = im1 + 1if i in BinaryIdx:type = "B"else:type = "C"print('{0:7.3f} {1:7.3f}{2:8.2f} {3} {4:4.2f}'.format(profit[im1], weight[im1], profit_to_weight[im1], type, X[i].value))
--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
|
Thank you Gabe for pointing this out. I notice your use of UnitInterval. The PYOMO documentation (both the book and online documentation such as https://software.sandia.gov/downloads/pub/pyomo/PyomoOnlineDocs.html#_predefined_virtual_sets that I have seen has not listed this as a predefined set, although PercentFraction (which seems to be the same [0,1] interval) is there. Is there more up-to-date documentation available? |
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.
--
You received this message because you are subscribed to a topic in the Google Groups "Pyomo Forum" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyomo-forum/1hgQEhrjEeE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyomo-forum+unsubscribe@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.