matplotlib ValueError:x和y必须具有相同的第一个维度,但具有形状(5,)和(1,)

t9aqgxwy  于 2023-06-06  发布在  其他
关注(0)|答案(1)|浏览(152)

我正在为背包问题创建一个蛮力方法,不幸的是,当试图绘制结果时,我得到了这个错误:ValueError:x和y必须具有相同的第一维度,但具有形状(5,)和(1,)

import time
import random
import matplotlib.pyplot as plt

def brute_force(values, weights, capacity):
    num_items = len(values)
    max_value = 0
    subset = []
    
    for i in range(2**num_items):
        current_value = 0
        current_weight = 0
        current_subset = []
        
        for j in range(num_items):
            if(i & (1 << j)) != 0:
                current_value += values[j]
                current_weight += weights[j]
                current_subset.append(j)
                
        if current_weight <= capacity and current_value > max_value:
            max_value = current_value
            subset = current_subset
            
    return max_value, subset

input_sizes = [10, 20, 30, 40, 50]
running_times = []
for size in input_sizes:
   values = [random.randint(1,100) for _ in range(size)]
   weights = [random.randint(1, 100) for _ in range(size)]
   capacity = sum(weights) // 2

  start_time = time.time()
  max_value, subset = brute_force(values, weights, capacity)
  end_time = time.time()
  running_time = end_time - start_time
  running_times.append(running_time)

  print(f"Input size: {size}")
  print(f"Chosen subset: {subset}")
  print(f"Chosen weight: {sum(weights[i] for i in subset)}")
  print(f"Running time: {running_time: .6f} seconds")

  plt.plot(input_sizes, running_times, marker = 'o')
  plt.xlabel('Input size')
  plt.ylabel('Running time (s)')
  plt.title('Brute force Knapsack')
  plt.show()
fcipmucu

fcipmucu1#

plt在循环内,这意味着running_times只有一个值,而input_sizes有5个值。
这应该可以工作:

import time
import random
import matplotlib.pyplot as plt

def brute_force(values, weights, capacity):
    num_items = len(values)
    max_value = 0
    subset = []
    
    for i in range(2**num_items):
        current_value = 0
        current_weight = 0
        current_subset = []
        
        for j in range(num_items):
            if(i & (1 << j)) != 0:
                current_value += values[j]
                current_weight += weights[j]
                current_subset.append(j)
                
        if current_weight <= capacity and current_value > max_value:
            max_value = current_value
            subset = current_subset
            
    return max_value, subset

input_sizes = [10, 20, 30, 40, 50]
running_times = []
for size in input_sizes:
    values = [random.randint(1,100) for _ in range(size)]
    weights = [random.randint(1, 100) for _ in range(size)]
    capacity = sum(weights) // 2

    start_time = time.time()
    max_value, subset = brute_force(values, weights, capacity)
    end_time = time.time()
    running_time = end_time - start_time
    running_times.append(running_time)

    print(f"Input size: {size}")
    print(f"Chosen subset: {subset}")
    print(f"Chosen weight: {sum(weights[i] for i in subset)}")
    print(f"Running time: {running_time: .6f} seconds")

plt.plot(input_sizes, running_times, marker = 'o')
plt.xlabel('Input size')
plt.ylabel('Running time (s)')
plt.title('Brute force Knapsack')
plt.show()

我将input_list减少到[1, 2, 3, 4, 5](为了速度,所以你可以把它放回去),输出看起来像这样:

相关问题