Example #1
0
def read_json(filename=None, json_string=None, json_dict=None):
    if filename:
        the_dict = json.load(open(filename, 'r'))
    elif json_string:
        the_dict = json.loads(json_string)
    elif json_dict:
        the_dict = json_dict
    else:
        raise ValueError("no json source provided")

    # First make list, then add connections
    products = {
        name: Product(name=name,
                      units=value["units"],
                      sub_products_quantities={})
        for name, value in the_dict['products'].items()
    }
    for product in products.values():
        product.sub_products_quantities = {
            products[name]: quantity
            for name, quantity in the_dict['products'][product.name]
            ['sub_products'].items()
        }

    ExternalSupplier.reset_instance_tracker()
    # First make list, then add connections
    suppliers = {
        name: Machine(name=name,
                      min_batch_time=val['min_batch_time'],
                      max_batch_time=val['max_batch_time'],
                      batch_time=val['batch_time'],
                      batch_size=val['batch_size'],
                      is_on=val['is_on'],
                      output_product=products[val['output_product']],
                      test_suppliers=False)
        for name, val in the_dict['machines'].items()
    }
    suppliers = {
        **suppliers,
        **{
            name: ExternalSupplier(
                output_product=products[val['output_product']],
                min_batch_time=val['min_batch_time'],
                max_batch_time=val['max_batch_time'],
                batch_time=val['batch_time'],
                batch_size=val['batch_size'],
            )
            for name, val in the_dict['external_suppliers'].items()
        }
    }
    for supplier_links in the_dict['graph']:
        base = supplier_links[0]
        supplier = supplier_links[1]
        delay = supplier_links[2]
        suppliers[base].add_supplier(suppliers[supplier], delay)

    # return the last machine in the chain (the graph was built backwards)
    return suppliers[the_dict['graph'][0][0]]
Example #2
0
 def setUp(self):
     ExternalSupplier.reset_instance_tracker()
     self.flour = Product(name="flour", units='kg')
     self.water = Product(name="water", units='liter')
     self.cream = Product(name="cream", units='kg')
     self.dough = Product(name="dough", units='kg', sub_products_quantities={self.flour: 0.3, self.water: 0.5})
     self.filling = Product(name="filling", units='liter', sub_products_quantities={self.cream: 0.5, self.flour: 0.1})
     self.pie = Product(name="pie", units='unit', sub_products_quantities={self.dough: 0.4, self.filling: 0.6})
     self.dough_maker = Machine(name="Dough maker", min_output_rate=0.1, max_output_rate=3.0,
                                output_product=self.dough)
     self.filling_maker = Machine(name="Filling maker", min_output_rate=0.1, max_output_rate=4.0,
                                  output_product=self.filling)
     self.output_machine = Machine(name="Pie maker",
                                   min_output_rate=1, max_output_rate=3, output_product=self.pie,
                                   suppliers=[self.dough_maker, self.filling_maker], delays=[3.2, 1.2])
     print("graph :", self.output_machine.get_graph())
Example #3
0
def make_json_dict(output_machine: Machine):
    return {
        "products": output_machine.output_product.to_dict(),
        "machines": output_machine.to_dict(),
        "external_suppliers": ExternalSupplier.to_dict(),
        "graph": output_machine.get_graph(),
        "schedule": create_time_schedule(output_machine)
    }
Example #4
0
 def setUp(self):
     ExternalSupplier.reset_instance_tracker()
     self.flour = Product(name="flour", units='kg')
     self.water = Product(name="water", units='liter')
     self.cream = Product(name="cream", units='kg')
     self.dough = Product(name="dough",
                          units='kg',
                          sub_products_quantities={
                              self.flour: 0.3,
                              self.water: 0.5
                          })
     self.filling = Product(name="filling",
                            units='liter',
                            sub_products_quantities={
                                self.cream: 0.5,
                                self.flour: 0.1
                            })
     self.pie = Product(name="pie",
                        units='unit',
                        sub_products_quantities={
                            self.dough: 0.4,
                            self.filling: 0.6
                        })
     self.dough_maker = Machine(name="Dough maker",
                                min_batch_time=200,
                                max_batch_time=1000,
                                batch_time=500,
                                batch_size=50,
                                output_product=self.dough)
     self.filling_maker = Machine(name="Filling maker",
                                  min_batch_time=100,
                                  max_batch_time=500.0,
                                  batch_time=150,
                                  batch_size=20,
                                  output_product=self.filling)
     self.output_machine = Machine(
         name="Pie maker",
         min_batch_time=30,
         max_batch_time=300,
         batch_time=50,
         batch_size=30,
         output_product=self.pie,
         suppliers=[self.dough_maker, self.filling_maker],
         delays=[1.3, 1.2])
Example #5
0
    def __init__(self, name,
                 batch_size, batch_time, min_batch_time, max_batch_time,
                 output_product: Product,
                 suppliers: List['Machine'] = None, delays: List[float] = None,
                 is_on=True,
                 test_suppliers=True):
        """
        
        :param name: the name for the machine 
        :param min_output_rate: minimum performance or throttle point [units/second]
        :param max_output_rate: maximum performance or throttle point [units/second]
        :param output_product: the output that is outputted
        :param suppliers: list of machines that supply to this one
        :param next_machines: list of machines that this one supplies to
        :param is_on: whether the machine is working or not
        """
        self.name = name
        self.min_batch_time = float(min_batch_time)
        self.max_batch_time = float(max_batch_time)
        self.batch_time = float(batch_time)
        self.batch_size = float(batch_size)
        self.output_product = output_product
        self._suppliers = []
        self.next_machines = []
        self.is_on = is_on
        self._delays = []

        if len(delays or []) == 0:
            delays = [0.0] * len(suppliers or [])
        for i in range(len(suppliers or [])):
            self.add_supplier(suppliers[i], delays[i])

        if test_suppliers:
            # Check if a subproduct is not being provided
            # If a provider is missing, add  a generic one
            _new_suppliers = []
            for sub_product in output_product.sub_products_list:
                sub_product_has_provider = False
                for prev_machine in self._suppliers:
                    if prev_machine.output_product == sub_product:
                        sub_product_has_provider = True
                if not sub_product_has_provider:
                    print(f"Machine <{self.name}> has no provider for required input <{sub_product.name}>")
                    new_supplier = ExternalSupplier(output_product=sub_product, next_machines=[self])
                    _new_suppliers.append(new_supplier)
            for new_supplier in _new_suppliers:
                self.add_supplier(new_supplier, 0.0)
Example #6
0
def to_graph_obj(output_machine):
    machine_names = [key for key in output_machine.to_dict().keys()]
    ext_supplier_names = [key for key in ExternalSupplier.to_dict().keys()]
    all_names = machine_names + ext_supplier_names
    nodes = []
    edges = []
    for name in machine_names:
        a_machine = output_machine.search_machine_by_name(name)
        provided_product_quantities = {}
        for supplier in a_machine.suppliers:
            provided_product_quantities[
                supplier.
                name] = a_machine.output_product.sub_products_quantities[
                    supplier.
                    output_product] / a_machine.count_suppliers_of_product(
                        supplier.output_product)
        nodes.append(
            Node(name, a_machine.output_rate, provided_product_quantities))
    for name in ext_supplier_names:
        nodes.append(Source(name))
    for i, node in enumerate(nodes):
        suppliers = output_machine.search_machine_by_name(
            all_names[i]).suppliers
        if suppliers is None:
            continue
        for j, supplier in enumerate(suppliers):
            up_node = nodes[all_names.index(supplier.name)]
            edges.append(
                Edge(up_node,
                     node,
                     delay=output_machine.search_machine_by_name(
                         node.id).delays[j]))
    edges.append(
        Edge(nodes[[n.id for n in nodes].index(output_machine.name)],
             Sink('Sink', {output_machine.name: 1.0})))

    return Graph(edges, subgraph=False)
Example #7
0
def run_python():
    json = request.get_json()
    print(json)
    ExternalSupplier.reset_instance_tracker()
    flour = Product(name="flour", units='kg')
    water = Product(name="water", units='liter')
    cream = Product(name="cream", units='kg')
    dough = Product(name="dough",
                    units='kg',
                    sub_products_quantities={
                        flour: 0.4,
                        water: 0.6
                    })
    filling = Product(name="filling",
                      units='liter',
                      sub_products_quantities={
                          cream: 0.4,
                          flour: 0.3
                      })
    pie = Product(name="pie",
                  units='unit',
                  sub_products_quantities={
                      dough: 0.4,
                      filling: 0.4
                  })
    dough_maker1 = Machine(name="Dough maker 1",
                           min_batch_time=200,
                           max_batch_time=1000,
                           batch_time=500,
                           batch_size=50,
                           output_product=dough)
    dough_maker2 = Machine(name="Dough maker 2",
                           min_batch_time=200,
                           max_batch_time=1000,
                           batch_time=500,
                           batch_size=50,
                           output_product=dough)
    filling_maker1 = Machine(name="Filling maker 1",
                             min_batch_time=100,
                             max_batch_time=500.0,
                             batch_time=150,
                             batch_size=20,
                             output_product=filling)
    filling_maker2 = Machine(name="Filling maker 2",
                             min_batch_time=100,
                             max_batch_time=500.0,
                             batch_time=150,
                             batch_size=20,
                             output_product=filling)
    filling_maker3 = Machine(name="Filling maker 3",
                             min_batch_time=100,
                             max_batch_time=500.0,
                             batch_time=150,
                             batch_size=20,
                             output_product=filling)
    output_machine = Machine(name="Pie maker",
                             min_batch_time=10,
                             max_batch_time=300,
                             batch_time=50,
                             batch_size=30,
                             output_product=pie,
                             suppliers=[
                                 dough_maker1, dough_maker2, filling_maker1,
                                 filling_maker2, filling_maker3
                             ],
                             delays=[22.3, 20.1, 13.2, 11.1, 15.3])

    # maximum_output = ale_optimizer.maximize_output(output_machine)
    maximum_output = ale_optimizer.optimize_topology(output_machine, 1.55)
    print("\nMaximum production is 1 pie every {:.2f} seconds".format(
        1 / maximum_output))
    write_json(output_machine, filename="../Optimized_plant2.json")

    return write_json(output_machine)
Example #8
0
                         node.id).delays[j]))
    edges.append(
        Edge(nodes[[n.id for n in nodes].index(output_machine.name)],
             Sink('Sink', {output_machine.name: 1.0})))

    return Graph(edges, subgraph=False)


def update_output_machine(_output_machine, _graph_obj):
    for edge in _graph_obj.edges:
        _output_machine.search_machine_by_name(
            edge.node_1.id).set_output_rate = edge.flow


if __name__ == "__main__":
    ExternalSupplier.reset_instance_tracker()
    flour = Product(name="flour", units='kg')
    water = Product(name="water", units='liter')
    cream = Product(name="cream", units='kg')
    dough = Product(name="dough",
                    units='kg',
                    sub_products_quantities={
                        flour: 0.4,
                        water: 0.6
                    })
    filling = Product(name="filling",
                      units='liter',
                      sub_products_quantities={
                          cream: 0.7,
                          flour: 0.3
                      })