Skip to content

Commit

Permalink
Revert "Merge branch 'main' of https://github.com/ppaauuoo/paper_cutt…
Browse files Browse the repository at this point in the history
…ing_project"

This reverts commit 9ca1bdf, reversing
changes made to 693b84c.
  • Loading branch information
ppaauuoo committed Aug 13, 2024
1 parent 9ca1bdf commit 762539c
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 65 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/django.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Django CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: [3.11.9]

steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run Tests
run: |
python manage.py test
18 changes: 11 additions & 7 deletions order_optimization/getter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ def set_progress(progress) -> None:
cache.set("optimization_progress", progress, CACHE_TIMEOUT)

def get_selected_order(request)->Dict:
selector_id = int(request.POST.get("selector_id"))
selector_out = int(request.POST.get("selector_out"))
return {
"order_id": selector_id,
"out": selector_out
}
selector_id = request.POST.get("selector_id")
if selector_id:
selector_id = int(selector_id)
selector_out = int(request.POST.get("selector_out"))
return {
"order_id": selector_id,
"out": selector_out
}
return None


def get_genetic_algorithm(
request,
Expand Down Expand Up @@ -94,7 +98,7 @@ def get_orders(
tuning_values=tuning_values,
common=common,
filler = filler,
selector = get_selected_order(request)['order_id']
selector = get_selected_order(request)
).get()

def get_outputs(ga_instance: GA) -> Tuple[float, List[Dict]]:
Expand Down
33 changes: 19 additions & 14 deletions order_optimization/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ def handle_common(request) -> Callable:


for i, item in enumerate(results["output"]):
size_value = item["cut_width"] + results["trim"]
size_value = (item["cut_width"]*item["out"]) + results["trim"]
orders = get_orders(request, file_id,size_value,deadline_scope=-1,tuning_values=3, filter=False,common=True)
ga_instance = get_genetic_algorithm(orders, size_value)
ga_instance = get_genetic_algorithm(request,orders,size_value)


if abs(ga_instance.fitness_values) < abs(best_fitness):
Expand All @@ -128,6 +128,23 @@ def handle_common(request) -> Callable:

return cache.set("optimization_results", results, CACHE_TIMEOUT)

def update_results(results: Dict, best_index: int, best_output: List[Dict], best_fitness: float, size_value: float) -> None:
"""Update results with the best common order."""
old_order = results["output"][best_index]
results["output"].pop(best_index)
results["output"] = [item for item in results["output"] if item.get("out", 0) >= 1]
results["output"].extend(best_output)
results["fitness"] = (
( results["roll"] - (old_order["cut_width"]*old_order["out"]))
+ (best_fitness + size_value)
)
print("results['roll']:", results["roll"])
print("results['output'][best_index]['cut_width']:", results["output"][best_index]["cut_width"])
print("results['output'][best_index]['out']:", results["output"][best_index]["out"])
print("best_fitness:", best_fitness)
print("size_value:", size_value)
results["trim"] = abs(best_fitness)

def handle_filler(request):
results = cache.get("optimization_results")
init_order = results['output'][0]['order_number']
Expand Down Expand Up @@ -172,15 +189,3 @@ def results_format(ga_instance: object, output_data: dict, size_value: int, fitn



def update_results(results: Dict, best_index: int, best_output: List[Dict], best_fitness: float, size_value: float) -> None:
"""Update results with the best common order."""
results["output"][best_index]["out"] -= 1
results["output"] = [item for item in results["output"] if item.get("out", 0) >= 1]
results["output"].extend(best_output)
results["fitness"] = (
results["fitness"]
- results["output"][best_index]["cut_width"]
+ best_fitness
+ size_value
)
results["trim"] = abs(best_fitness)
2 changes: 1 addition & 1 deletion order_optimization/modules/ga.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def fitness_function(self, ga_instance, solution, solution_idx):
self.penalty = 0
self.penalty_value = 1000

if self.selector['order_id']:
if self.selector:
solution[0]=self.selector['out']

self.paper_type_logic(solution)
Expand Down
83 changes: 44 additions & 39 deletions order_optimization/modules/ordplan.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pandas as pd
from typing import Dict
class ORD:
def __init__(self, path: str, deadline_scope: int = 0, size: float = 66, tuning_values: int = 3, filter_value: int = 16, filter: bool = True, common: bool = False, filler: int =0,selector: int = 0) -> None:
def __init__(self, path: str, deadline_scope: int = 0, size: float = 66, tuning_values: int = 3, filter_value: int = 16, filter: bool = True, common: bool = False, filler: int =0,selector: Dict = None) -> None:
self.ordplan = pd.read_excel(path, engine='openpyxl')
self.deadline_scope = deadline_scope
self.filter = filter
Expand All @@ -20,51 +20,56 @@ def get(self)-> Dict:
ordplan["กำหนดส่ง"] = pd.to_datetime(ordplan["กำหนดส่ง"]).dt.strftime('%m/%d/%y')
ordplan.fillna(0, inplace=True) # fix error values ex. , -> NA

self.ordplan = ordplan

#filter deadline_scope
if self.deadline_scope >= 0:
deadline = ordplan["กำหนดส่ง"].iloc[self.deadline_scope]
ordplan = ordplan[ordplan["กำหนดส่ง"] == deadline].reset_index(drop=True)
while self.deadline_scope >= 0:
deadline = self.ordplan["กำหนดส่ง"].iloc[self.deadline_scope]
ordplan = self.ordplan[self.ordplan["กำหนดส่ง"] <= deadline].sort_values("กำหนดส่ง").reset_index(drop=True)
self.deadline_scope+=10

#โดยออเดอร์ที่สามารถนำมาคู่กันได้ สำหรับกระดาษไซส์นี้ จะมีขนาดไม่เกิน 31(+-filter value) โดย filter value คือค่าที่กำหนดเอง
if self.filter:
#เอาไซส์กระดาษมาหารกับปริมาณการตัด เช่น กระดาษ 63 ถ้าตัดสองครั้งจได้ ~31 แล้วบันทึกเก็บไว้
selected_values = self.size / self.tuning_values
for i, row in ordplan.iterrows():
diff = abs(selected_values - row["กว้างผลิต"])
ordplan.loc[i, "diff"] = diff


ordplan = (
ordplan[ordplan["diff"] < self.filter_value].sort_values(by="กว้างผลิต").reset_index(drop=True)
)
#โดยออเดอร์ที่สามารถนำมาคู่กันได้ สำหรับกระดาษไซส์นี้ จะมีขนาดไม่เกิน 31(+-filter value) โดย filter value คือค่าที่กำหนดเอง
if self.filter:
#เอาไซส์กระดาษมาหารกับปริมาณการตัด เช่น กระดาษ 63 ถ้าตัดสองครั้งจได้ ~31 แล้วบันทึกเก็บไว้
selected_values = self.size / self.tuning_values
for i, row in ordplan.iterrows():
diff = abs(selected_values - row["กว้างผลิต"])
ordplan.loc[i, "diff"] = diff

ordplan = (
ordplan[ordplan["diff"] < self.filter_value]
)

if self.selector:
self.selectorFilter()
ordplan = ordplan[ordplan['เลขที่ใบสั่งขาย'] != self.selector]
ordplan = pd.concat([self.selected_order, ordplan], ignore_index=True)
if self.selector:
self.selectorFilter()
ordplan = ordplan[ordplan['เลขที่ใบสั่งขาย'] != self.selector['order_id']]
ordplan = pd.concat([self.selected_order, ordplan], ignore_index=True)

if self.common:
col = [
"แผ่นหน้า",
"ลอน C",
"แผ่นกลาง",
"ลอน B",
"แผ่นหลัง",
"จน.ชั้น",
"ประเภททับเส้น",
]
# Filter based on the first order
init_order = ordplan.iloc[0]
if self.filler:
init_order = ordplan[ordplan['เลขที่ใบสั่งขาย'] == self.filler]
ordplan = ordplan[ordplan['เลขที่ใบสั่งขาย'] != self.filler]
if self.common:
col = [
"แผ่นหน้า",
"ลอน C",
"แผ่นกลาง",
"ลอน B",
"แผ่นหลัง",
"จน.ชั้น",
"ประเภททับเส้น",
]
# Filter based on the first order
init_order = ordplan.iloc[0]
if self.filler:
init_order = ordplan[ordplan['เลขที่ใบสั่งขาย'] == self.filler]
ordplan = ordplan[ordplan['เลขที่ใบสั่งขาย'] != self.filler]

if isinstance(init_order, pd.Series):
ordplan = ordplan[ordplan.apply(lambda order: all(init_order[i] == order[i] for i in col), axis=1)].reset_index(drop=True)
if isinstance(init_order, pd.Series):
ordplan = ordplan[ordplan.apply(lambda order: all(init_order[i] == order[i] for i in col), axis=1)]

self.ordplan = ordplan
if len(ordplan) >= 10 or len(self.ordplan) <= self.deadline_scope: break

self.ordplan = ordplan.reset_index(drop=True)

return ordplan
return self.ordplan

def handle_orders_logic(output_data):
init_len = output_data[0]['cut_len']
Expand All @@ -80,5 +85,5 @@ def handle_orders_logic(output_data):
return (init_order_number,foll_order_number)

def selectorFilter(self):
self.selected_order = self.ordplan[self.ordplan['เลขที่ใบสั่งขาย'] == self.selector]
self.selected_order = self.ordplan[self.ordplan['เลขที่ใบสั่งขาย'] == self.selector['order_id']]

8 changes: 4 additions & 4 deletions order_optimization/templates/optimize.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ <h1 class="mb-4">Order Optimization</h1>
</select>
</div>
<div class="form-group mb-3">
<label for="satisfied" class="form-label">Try until satisfied :</label>
<label for="satisfied" class="form-label">Closest deadline first :</label>
<input type="checkbox" id="satisfied" name="satisfied" class="form-check-input" value="true" {% if request.POST.satisfied == "true" %}checked{% endif %}>
</div>

Expand Down Expand Up @@ -97,7 +97,7 @@ <h1 class="mb-4">Order Optimization</h1>
</button>
<button type="submit" name="optimize" class="btn btn-primary" id="optimizeBtn">
<span class="spinner-border spinner-border-sm d-none" role="status" aria-hidden="true"></span>
<span class="btn-text">Optimize</span>
<span class="btn-text">Manual</span>
</button>
<button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#uploadModal">
<span class="btn-text">Add File</span>
Expand Down Expand Up @@ -283,8 +283,8 @@ <h2 class="mt-4">Results</h2>
</svg> {{ results.trim }}
<form method="post" onsubmit="document.getElementById('commonTrimBtn').classList.add('disabled'); document.getElementById('commonTrimBtn').querySelector('.spinner-border').classList.remove('d-none'); document.getElementById('overlay').style.display = 'flex'; document.body.style.overflow = 'hidden';">
{% csrf_token %}
<input type="hidden" id="selected_file_id" name="selected_file_id">
<button type="submit" name="common_trim" class="btn btn-primary btn-sm" id="commonTrimBtn" onclick="document.getElementById('selected_file_id').value = document.getElementById('file_id').value;">
<input type="hidden" id="selected_file_id_trim" name="selected_file_id">
<button type="submit" name="common_trim" class="btn btn-primary btn-sm" id="commonTrimBtn" onclick="document.getElementById('selected_file_id_trim').value = document.getElementById('file_id').value;">
<span class="spinner-border spinner-border-sm d-none" role="status" aria-hidden="true"></span>
<span class="btn-text">Find Common</span>
</button>
Expand Down

0 comments on commit 762539c

Please sign in to comment.