aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--playground/csp/main.py39
-rw-r--r--src_py/opcut/csp.py12
2 files changed, 45 insertions, 6 deletions
diff --git a/playground/csp/main.py b/playground/csp/main.py
new file mode 100644
index 0000000..d08f748
--- /dev/null
+++ b/playground/csp/main.py
@@ -0,0 +1,39 @@
+import sys
+sys.path += ['../../src_py']
+
+from opcut import csp
+
+
+def main():
+ panels = [
+ csp.Panel(id='p1', width=100, height=100)
+ ]
+ items = [
+ csp.Item(id='i1', width=10, height=10, rotate=True),
+ csp.Item(id='i2', width=10, height=9, rotate=True),
+ csp.Item(id='i3', width=20, height=8, rotate=True),
+ csp.Item(id='i4', width=10, height=20, rotate=True),
+ csp.Item(id='i5', width=30, height=19, rotate=True),
+ csp.Item(id='i6', width=10, height=18, rotate=True),
+ csp.Item(id='i7', width=10, height=17, rotate=True),
+ csp.Item(id='i8', width=20, height=16, rotate=True),
+ csp.Item(id='i9', width=10, height=15, rotate=True),
+ csp.Item(id='i10', width=30, height=14, rotate=True),
+ csp.Item(id='i11', width=10, height=20, rotate=True),
+ csp.Item(id='i12', width=30, height=19, rotate=True),
+ csp.Item(id='i13', width=10, height=18, rotate=True),
+ csp.Item(id='i14', width=10, height=17, rotate=True),
+ csp.Item(id='i15', width=20, height=16, rotate=True),
+ csp.Item(id='i16', width=10, height=15, rotate=True),
+ csp.Item(id='i17', width=30, height=14, rotate=True),
+ csp.Item(id='i18', width=10, height=20, rotate=True),
+ csp.Item(id='i19', width=30, height=19, rotate=True),
+ ]
+ cut_width = 1
+ method = csp.Method.FORWARD_GREEDY
+ result = csp.calculate(panels, items, cut_width, method)
+ print(result)
+
+
+if __name__ == '__main__':
+ sys.exit(main())
diff --git a/src_py/opcut/csp.py b/src_py/opcut/csp.py
index 04a6d39..209acec 100644
--- a/src_py/opcut/csp.py
+++ b/src_py/opcut/csp.py
@@ -14,15 +14,15 @@ State = util.namedtuple(
Panel = util.namedtuple(
'Panel',
+ ['id', 'Any'],
['width', 'float'],
- ['height', 'float'],
- ['label', 'str'])
+ ['height', 'float'])
Item = util.namedtuple(
'Item',
+ ['id', 'Any'],
['width', 'float'],
['height', 'float'],
- ['label', 'str'],
['rotate', 'bool'])
Used = util.namedtuple(
@@ -33,7 +33,7 @@ Used = util.namedtuple(
['y', 'float'],
['rotate', 'bool'])
-Unused = util.namespace(
+Unused = util.namedtuple(
'Unused',
['panel', 'Panel'],
['width', 'float'],
@@ -189,9 +189,9 @@ def _fitness(state):
result = 0
for panel in state.panels:
used_areas = [used.item.width * used.item.height
- for used in panel.used]
+ for used in state.used]
unused_areas = [unused.width * unused.height
- for unused in panel.unused]
+ for unused in state.unused]
result += (panel.width * panel.height - sum(used_areas)) / total_area
result -= (_fitness_K *
min(used_areas, default=0) * max(unused_areas, default=0) /