создать ограничение на минимальное количество объектов, выбранных в задаче о рюкзаке - PullRequest
0 голосов
/ 26 мая 2020

У меня проблема с оптимизацией ниже. Это вариант задачи о рюкзаке. Я адаптировал решение проблемы ограниченного рюкзака к версии, которая допускает диапазон значений для объектов. количества содержатся в массиве «количества». когда переменная выбора принимает внутренний продукт с массивом количеств, она получает количество каждого объекта. Я могу сконструировать «ограничение веса» для максимального общего количества, которое я могу иметь в рюкзаке. Я хотел бы также создать «min_qty», который устанавливает ограничение на минимальное количество каждого объекта, который должен быть выбран. Я пробовал построить это ограничение с помощью min_qty, как показано ниже, но получаю ошибку «-inf», когда пытаюсь запустить код. Кто-нибудь видит, в чем проблема, и вы можете предложить, как ее решить? Я включил в задачу массивы.

код:

import numpy as np
import pandas as pd
from time import time
import scipy.stats as stats

from IPython.display import display # Allows the use of display() for DataFrames

# Pretty display for notebooks
%matplotlib inline

###########################################
# Suppress matplotlib user warnings
# Necessary for newer version of matplotlib
import warnings
warnings.filterwarnings("ignore", category = UserWarning, module = "matplotlib")
#
# Display inline matplotlib plots with IPython
from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'inline')
###########################################

import matplotlib.pyplot as plt
import matplotlib.cm as cm

import warnings
warnings.filterwarnings('ignore')

import seaborn as sns

from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
from sklearn.preprocessing import MinMaxScaler
from sklearn.decomposition import PCA



from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import numpy as np
import cvxpy

from collections import namedtuple    

selection = cvxpy.Variable(len(weights), boolean=True)


    min_qty= quantities * selection >= 1


    weight_constraint = cvxpy.sum(selection * quantities.T, axis=0) <= P

    # Our total utility is the sum of the item utilities
    total_utility = utilities * selection

    # We tell cvxpy that we want to maximize total utility 

    knapsack_problem = cvxpy.Problem(cvxpy.Maximize(total_utility), [weight_constraint,min_qty])

    # Solving the problem
    # maximum netsales
    knapsack_problem.solve(solver=cvxpy.GLPK_MI)

ошибка:

-inf

входные данные:

print(weights)

[ 122.76        133.92        145.08        156.24        167.4
  178.56        189.72        200.88        212.04        223.2
  234.36        245.52        256.68        267.84        279.
  290.16        301.32        312.48        323.64        334.8
  345.96        357.12        368.28        379.44        390.6
  401.76        412.92        424.08        435.24        446.4
  457.56        468.72        479.88        491.04        502.2
  513.36        524.52        341.          354.64        368.28
  381.92        395.56        409.2         422.84        436.48
  450.12        463.76        477.4         491.04        504.68
  518.32        531.96        545.6         559.24        572.88
  586.52        600.16        613.8         627.44        641.08
   19.06918919   38.13837838   57.20756757   76.27675676   95.34594595
  114.41513514  133.48432432  152.55351351  171.6227027   190.69189189
  209.76108108  228.83027027  247.89945946  266.96864865  286.03783784
  305.10702703  324.17621622  343.24540541  362.31459459  381.38378378
  400.45297297  419.52216216  438.59135135  457.66054054  476.72972973
  495.79891892  514.86810811  533.9372973   553.00648649  572.07567568
  591.14486486  610.21405405  629.28324324  648.35243243  667.42162162
  686.49081081  705.56        724.62918919  743.69837838  762.76756757
  781.83675676  800.90594595  819.97513514  839.04432432  858.11351351
  877.1827027   896.25189189  617.22        640.08        662.94
  685.8         708.66        731.52        754.38        777.24
  800.1         822.96        845.82        868.68        891.54
  914.4         937.26        960.12        982.98       1005.84
 1028.7        1051.56       1074.42        845.82        868.68
  891.54        914.4         937.26        960.12        982.98
 1005.84       1028.7        1051.56       1074.42        136.4
  150.04        163.68        177.32        190.96        204.6
  218.24        231.88        245.52        259.16        272.8
  286.44        300.08        313.72        327.36        341.
  354.64        368.28        381.92        395.56        409.2
  422.84        436.48        450.12        463.76        477.4
  491.04        504.68        518.32        531.96        545.6
  559.24        572.88        586.52        600.16        613.8
  627.44        641.08      ]



print(utilities)

[ 185.56723112  202.43697941  219.30672769  236.17647597  253.04622426
  269.91597254  286.78572082  303.65546911  320.52521739  337.39496568
  354.26471396  371.13446224  388.00421053  404.87395881  421.74370709
  438.61345538  455.48320366  472.35295195  489.22270023  506.09244851
  522.9621968   539.83194508  556.70169336  573.57144165  590.44118993
  607.31093822  624.1806865   641.05043478  657.92018307  674.78993135
  691.65967963  708.52942792  725.3991762   742.26892449  759.13867277
  776.00842105  792.87816934  584.24141876  607.61107551  630.98073227
  654.35038902  677.72004577  701.08970252  724.45935927  747.82901602
  771.19867277  794.56832952  817.93798627  841.30764302  864.67729977
  888.04695652  911.41661327  934.78627002  958.15592677  981.52558352
 1004.89524027 1028.26489703 1051.63455378 1075.00421053 1098.37386728
   43.32095244   86.64190488  129.96285732  173.28380976  216.6047622
  259.92571464  303.24666708  346.56761952  389.88857196  433.2095244
  476.53047684  519.85142928  563.17238172  606.49333416  649.8142866
  693.13523904  736.45619148  779.77714392  823.09809636  866.4190488
  909.74000124  953.06095368  996.38190612 1039.70285856 1083.023811
 1126.34476344 1169.66571588 1212.98666832 1256.30762076 1299.6285732
 1342.94952564 1386.27047808 1429.59143052 1472.91238296 1516.23333539
 1559.55428783 1602.87524027 1646.19619271 1689.51714515 1732.83809759
 1776.15905003 1819.48000247 1862.80095491 1906.12190735 1949.44285979
 1992.76381223 2036.08476467  898.51767735  931.79610984  965.07454233
  998.35297483 1031.63140732 1064.90983982 1098.18827231 1131.46670481
 1164.7451373  1198.02356979 1231.30200229 1264.58043478 1297.85886728
 1331.13729977 1364.41573227 1397.69416476 1430.97259725 1464.25102975
 1497.52946224 1530.80789474 1564.08632723 1171.06369416 1202.71406427
 1234.36443438 1266.0148045  1297.66517461 1329.31554472 1360.96591483
 1392.61628495 1424.26665506 1455.91702517 1487.56739528  224.85858124
  247.34443936  269.83029748  292.31615561  314.80201373  337.28787185
  359.77372998  382.2595881   404.74544622  427.23130435  449.71716247
  472.20302059  494.68887872  517.17473684  539.66059497  562.14645309
  584.63231121  607.11816934  629.60402746  652.08988558  674.57574371
  697.06160183  719.54745995  742.03331808  764.5191762   787.00503432
  809.49089245  831.97675057  854.4626087   876.94846682  899.43432494
  921.92018307  944.40604119  966.89189931  989.37775744 1011.86361556
 1034.34947368 1056.83533181]



print(quantities)



[[ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0]
 [11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
  35 36 37 38 39 40 41 42 43 44 45 46 47  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0 25 26 27 28 29 30 31 32 33 34 35
  36 37 38 39 40 41 42 43 44 45 46 47  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  1  2  3  4  5  6  7  8  9 10 11 12
  13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  37 38 39 40 41 42 43 44 45 46 47  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0 27 28 29 30 31 32 33 34 35 36 37 38 39
  40 41 42 43 44 45 46 47  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0 37 38 39 40 41 42 43 44 45 46 47  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 10 11 12 13 14
  15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
  39 40 41 42 43 44 45 46 47]]
...