@jetta/blocks-allocator 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetta/blocks-allocator",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Blocks wall allocation library",
5
5
  "main": "src/index.js",
6
6
  "exports": {
@@ -1,42 +1,86 @@
1
1
  const { VariableType, create_variable, create_restriction } = require('@jetta/lp-framework')
2
- const {
3
- get_dimension_to_max_weight_map,
4
- calculate_block_potential_weight
5
- } = require('../../../Block_Potential_Weight')
2
+ const create_item_assignment_variables = require('../Item_Assignment_Variables')
6
3
 
7
4
  /**
8
- * Creates a single LP restriction to ensure the total weight of items in the chosen blocks
9
- * does not exceed the container's supported weight.
10
- * @param {object} container - The container object, with a supported_weight() method.
11
- * @param {object[]} block_list - An array of Block objects.
12
- * @param {object[]} item_list - An array of Item objects.
13
- * @returns {Restriction[]} An array containing the single weight restriction.
5
+ * Creates one LP restriction so the weight of items assigned to chosen blocks
6
+ * stays within the container supported weight.
7
+ * Each assignment variable is weighted by that item's own weight.
8
+ * @param {object} container - Container with supported_weight() or a supported_weight number.
9
+ * @param {object[]} block_list - Block objects.
10
+ * @param {object[]} item_list - Item objects with get_id() and get_weight().
11
+ * @returns {object[]} One restriction, or an empty list when no positive item weight applies.
14
12
  */
15
13
  module.exports = function create_container_supported_weight_restriction(container, block_list, item_list)
16
14
  {
17
- const dimension_to_max_weight_map = get_dimension_to_max_weight_map(item_list)
15
+ const assignment = create_item_assignment_variables(block_list, item_list)
16
+ const variable_list = build_item_weight_variables(assignment.slot_demand_list, item_list)
17
+ if (variable_list.length === 0)
18
+ {
19
+ return []
20
+ }
21
+ return [create_restriction(
22
+ 'container_supported_weight',
23
+ variable_list,
24
+ {lower_limit: 0, upper_limit: read_supported_weight(container)}
25
+ )]
26
+ }
27
+
28
+ /**
29
+ * @param {Array<{compatibility_list: Array<{item_id: string, variable_name: string}>}>} slot_demand_list
30
+ * @param {object[]} item_list
31
+ * @returns {object[]}
32
+ */
33
+ function build_item_weight_variables(slot_demand_list, item_list)
34
+ {
35
+ const weight_by_item_id = map_item_weight(item_list)
18
36
  const variable_list = []
37
+ for (const slot_demand of slot_demand_list)
38
+ {
39
+ append_slot_item_weights(variable_list, slot_demand.compatibility_list, weight_by_item_id)
40
+ }
41
+ return variable_list
42
+ }
19
43
 
20
- for (const block of block_list)
44
+ /**
45
+ * @param {object[]} item_list
46
+ * @returns {Map<string, number>}
47
+ */
48
+ function map_item_weight(item_list)
49
+ {
50
+ const weight_by_item_id = new Map()
51
+ for (const item of item_list)
21
52
  {
22
- const block_potential_weight = calculate_block_potential_weight(block, dimension_to_max_weight_map)
53
+ weight_by_item_id.set(item.get_id(), item.get_weight())
54
+ }
55
+ return weight_by_item_id
56
+ }
23
57
 
24
- if (block_potential_weight > 0)
58
+ /**
59
+ * @param {object[]} variable_list
60
+ * @param {Array<{item_id: string, variable_name: string}>} compatibility_list
61
+ * @param {Map<string, number>} weight_by_item_id
62
+ */
63
+ function append_slot_item_weights(variable_list, compatibility_list, weight_by_item_id)
64
+ {
65
+ for (const entry of compatibility_list)
66
+ {
67
+ const item_weight = weight_by_item_id.get(entry.item_id) || 0
68
+ if (item_weight > 0)
25
69
  {
26
- variable_list.push(create_variable(block.get_id(), block_potential_weight, VariableType.INTEGER))
70
+ variable_list.push(create_variable(entry.variable_name, item_weight, VariableType.INTEGER))
27
71
  }
28
72
  }
73
+ }
29
74
 
30
- if (variable_list.length === 0)
75
+ /**
76
+ * @param {object} container
77
+ * @returns {number}
78
+ */
79
+ function read_supported_weight(container)
80
+ {
81
+ if (typeof container.supported_weight === 'function')
31
82
  {
32
- return []
83
+ return container.supported_weight()
33
84
  }
34
-
35
- const supported_weight = typeof container.supported_weight === 'function'
36
- ? container.supported_weight()
37
- : container.supported_weight
38
-
39
- const name = 'container_supported_weight'
40
- const restriction = create_restriction(name, variable_list, {lower_limit: 0, upper_limit: supported_weight})
41
- return [restriction]
85
+ return container.supported_weight
42
86
  }