I have a store that contains items. Each item is either a component (which is atomal) or a product which consists of various components (but never of 2 or more of the same components).

Now, when I want to get a product out of the store, there are various scenarios:

  • The store contains the necessary number of the product.
  • The store contains components of which I can assemble the product.
  • The store contains products that share components with the required product. I can disassemble those and assemble the required item.
  • Any combination of the above.

Below you can see my code so far (getAssemblyPath). It does find a way to assemble the required item if it is possible, but it does not optimize the assembly path.

I want to optimize the path in two ways:

  • First, choose the path which takes the least number of assembly/disassembly actions.
  • Second, if there are various such paths, choose the path which leave the least amount of disassembled components in the store.

Now, here I am at a complete loss of how to get this optimization done (I am not even sure if this is a question for SO or for Maths).

How can I alter getAssemblyPath so that it meets my optimization requirements?

My code so far:

#! /usr/bin/python

class Component:
    def __init__ (self, name): self.__name = name

    def __repr__ (self): return 'Component {}'.format (self.__name)

class Product:
    def __init__ (self, name, components):
        self.__name = name
        self.__components = components

    @property
    def components (self): return self.__components

    def __repr__ (self): return 'Product {}'.format (self.__name)

class Store:
    def __init__ (self): self.__items = {}

    def __iadd__ (self, item):
        item, count = item
        if not item in self.__items: self.__items [item] = 0
        self.__items 
Edit
Report