Page 1 of 1

ใช้ VBA ในการไล่คำตอบรวมค่าน้ำหนัก

Posted: Sun Jan 10, 2016 9:21 pm
by lovejuthamatploy

Code: Select all

Use_next_truck:
truck_id = truck_id + 1

next_customer:
Max_weight = -99999
Cus_with_max_WeightPO = -1

For xy = 1 To n_node
    If WeightPO(xy) > Max_weight And in_truck_no(xy) = 0 Then
    Max_weight = WeightPO(xy)
    Cus_with_max_WeightPO = xy
    End If
Next xy


If Cus_with_max_WeightPO = -1 Then End

If truck_weight(truck_id) + Weight(Cus_with_max_WeightPO) > truck_capa Then
    GoTo Use_next_truck
End If

truck_weight(truck_id) = truck_weight(truck_id) + Weight(Cus_with_max_WeightPO)
in_truck_no(Cus_with_max_WeightPO) = truck_id
Debug.Print "Truck_id = " & Str(truck_id) & " Customer ID = " & Str(Cus_with_max_WeightPO) & " Truck Weight = "; Str(truck_weight(truck_id))

GoTo next_customer

End Sub
จากตัวอย่าง
ID weight
1 10
2 35
3 20
4 48
5 25

ในการรวมน้ำหนักของลูกค้า ถ้าน้ำหนักรวมมากกว่า 50 จะปรับให้มีการขึ้นรถคันใหม่
แต่ใน Code ที่เขียนมา จะเช็คเพียงแค่ถ้าไปบวกเพิ่มอีกลูกค้าแล้วน้ำหนักเกิน จะปรับเป็นรถคันใหม่ทันที
ต้องการให้น้ำหนักรวมของลูกค้า ถ้ารวมแล้วเกิน 50 ตัน ให้ทำการเช็คไปยังลูกค้าอื่นก่อน ว่ามีลูกค้ารายไหนที่ยังไม่โดนจับขึ้นรถ
และสามารถเอาน้ำหนักรวมกันได้อีก เหมือนกับการเช็คไล่ทุก ID

คำตอบที่ได้จากการ run
Truck_id = 1 Customer ID = 1 Truck Weight = 10
Truck_id = 1 Customer ID = 2 Truck Weight = 45
Truck_id = 2 Customer ID = 3 Truck Weight = 20
Truck_id = 3 Customer ID = 4 Truck Weight = 48
Truck_id = 4 Customer ID = 5 Truck Weight = 25

คำตอบที่ต้องการ
Truck_id = 1 Customer ID = 1 Truck Weight = 10
Truck_id = 1 Customer ID = 2 Truck Weight = 45
Truck_id = 2 Customer ID = 3 Truck Weight = 20 ( เมื่อ ID 3 น้ำหนัก = 20 ตัน ยังสามารถไปรวมกับ ID 5 = 25 ได้อีก )
Truck_id = 2 Customer ID = 5 Truck Weight = 45
Truck_id = 3 Customer ID = 4 Truck Weight = 48

Re: ใช้ VBA ในการไล่คำตอบรวมค่าน้ำหนัก

Posted: Mon Jan 11, 2016 1:08 pm
by menem
ผมลองเขียนได้แค่นี้นะครับ :)

Re: ใช้ VBA ในการไล่คำตอบรวมค่าน้ำหนัก

Posted: Mon Jan 11, 2016 9:29 pm
by lovejuthamatploy
ขอบคุณมากค่ะที่ให้ความรู้เพิ่มเติม ในการเขียนCode vba