Clear Filters
Clear Filters

3D plotting of the bounding box around few boxes

10 views (last 30 days)
hi,
im trying to plot a bounding box around the final arrangement of boxes but im not getting the ideal output.
this is how the final arrangement of the boxes looks:
expected output:
the output im getting:
this is the flow of my code:
  1. i get the vertices of the 5 boxes in the final arrangement and parse them to a spreadsheet.
2. i use another function to plot the bounding bos from the data from spreadsheet.
i have attached all my .m files for reference. i need help on how to plot bounding box according to the boxes arrangement.

Accepted Answer

Voss
Voss on 6 May 2024
Two problems that I can see:
1. The tranpose here (actually it's a complex-conjugate tranpose):
item_vertices = vertices{i}'; % Transpose to get rows as vertices
csv_data{i+1} = [i, item_vertices(:)']; % Add item ID and flatten vertex coordinates
makes the coordinates output in the order of x1,x2,x3,x4,...,x8,y1,y2,...,y8,z1,...,z8, which doesn't match the order given in the csv header (x1,x2,x3,y1,y2,y3,...). Without the tranpose, the order matches the csv header.
2. The order of the vertices is different than in your previous question, which the bounding box plot code was written for. Therefore the connections between the vertices need to be specified differently here than before. See below for one fix; although it's probably best if you use a plotting code that makes sense to you, like your plotBins.
I marked the lines of code I modified with "CHANGED".
clc;
clear;
close all;
%% Problem Definition
model = CreateModel(); % Create Bin Packing Model
CostFunction = @(x) BinPackingCost(x, model); % Objective Function
nVar = model.n * 4; % Each item now has a dimension setting (x, y, z, orientation)
VarSize = [1 nVar]; % Decision Variables Matrix Size
VarMin = 0; % Lower Bound of Decision Variables
VarMax = 1; % Upper Bound of Decision Variables
%% PSO Parameters
MaxIt = 1000; % Maximum Number of Iterations
nPop = 50; % Population Size (Swarm Size)
w = 1; % Inertia Weight
wdamp = 0.99; % Inertia Weight Damping Ratio
c1 = 1.5; % Personal Learning Coefficient
c2 = 2.0; % Global Learning Coefficient
VelMax = 0.1 * (VarMax - VarMin); % Velocity Limits
VelMin = -VelMax;
%% Initialization
particle = repmat(struct('Position', [], 'Cost', [], 'Sol', [], 'Velocity', [], 'Best', struct()), nPop, 1);
GlobalBest.Cost = inf;
for i = 1:nPop
% Initialize position with additional orientation indices
particle(i).Position = unifrnd(VarMin, VarMax, VarSize);
% Ensure orientation indices are integers between 1 and 6
particle(i).Position(4:4:end) = randi(6, 1, model.n);
% Velocity and other initialization as before
particle(i).Velocity = zeros(VarSize);
[particle(i).Cost, particle(i).Sol] = CostFunction(particle(i).Position);
particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;
particle(i).Best.Sol = particle(i).Sol;
if particle(i).Best.Cost < GlobalBest.Cost
GlobalBest = particle(i).Best;
end
end
BestCost = zeros(MaxIt, 1);
%% PSO Main Loop
for it = 1:MaxIt
for i = 1:nPop
particle(i).Velocity = w * particle(i).Velocity ...
+ c1 * rand(VarSize) .* (particle(i).Best.Position - particle(i).Position) ...
+ c2 * rand(VarSize) .* (GlobalBest.Position - particle(i).Position);
particle(i).Velocity = max(particle(i).Velocity, VelMin);
particle(i).Velocity = min(particle(i).Velocity, VelMax);
particle(i).Position = particle(i).Position + particle(i).Velocity;
particle(i).Position = max(particle(i).Position, VarMin);
particle(i).Position = min(particle(i).Position, VarMax);
[particle(i).Cost, particle(i).Sol] = CostFunction(particle(i).Position);
if particle(i).Cost < particle(i).Best.Cost
particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;
particle(i).Best.Sol = particle(i).Sol;
if particle(i).Best.Cost < GlobalBest.Cost
GlobalBest = particle(i).Best;
end
end
end
BestCost(it) = GlobalBest.Cost;
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
w = w * wdamp;
end
Iteration 1: Best Cost = 0 Iteration 2: Best Cost = 0 Iteration 3: Best Cost = 0 Iteration 4: Best Cost = 0 Iteration 5: Best Cost = 0 Iteration 6: Best Cost = 0 Iteration 7: Best Cost = 0 Iteration 8: Best Cost = 0 Iteration 9: Best Cost = 0 Iteration 10: Best Cost = 0 Iteration 11: Best Cost = 0 Iteration 12: Best Cost = 0 Iteration 13: Best Cost = 0 Iteration 14: Best Cost = 0 Iteration 15: Best Cost = 0 Iteration 16: Best Cost = 0 Iteration 17: Best Cost = 0 Iteration 18: Best Cost = 0 Iteration 19: Best Cost = 0 Iteration 20: Best Cost = 0 Iteration 21: Best Cost = 0 Iteration 22: Best Cost = 0 Iteration 23: Best Cost = 0 Iteration 24: Best Cost = 0 Iteration 25: Best Cost = 0 Iteration 26: Best Cost = 0 Iteration 27: Best Cost = 0 Iteration 28: Best Cost = 0 Iteration 29: Best Cost = 0 Iteration 30: Best Cost = 0 Iteration 31: Best Cost = 0 Iteration 32: Best Cost = 0 Iteration 33: Best Cost = 0 Iteration 34: Best Cost = 0 Iteration 35: Best Cost = 0 Iteration 36: Best Cost = 0 Iteration 37: Best Cost = 0 Iteration 38: Best Cost = 0 Iteration 39: Best Cost = 0 Iteration 40: Best Cost = 0 Iteration 41: Best Cost = 0 Iteration 42: Best Cost = 0 Iteration 43: Best Cost = 0 Iteration 44: Best Cost = 0 Iteration 45: Best Cost = 0 Iteration 46: Best Cost = 0 Iteration 47: Best Cost = 0 Iteration 48: Best Cost = 0 Iteration 49: Best Cost = 0 Iteration 50: Best Cost = 0 Iteration 51: Best Cost = 0 Iteration 52: Best Cost = 0 Iteration 53: Best Cost = 0 Iteration 54: Best Cost = 0 Iteration 55: Best Cost = 0 Iteration 56: Best Cost = 0 Iteration 57: Best Cost = 0 Iteration 58: Best Cost = 0 Iteration 59: Best Cost = 0 Iteration 60: Best Cost = 0 Iteration 61: Best Cost = 0 Iteration 62: Best Cost = 0 Iteration 63: Best Cost = 0 Iteration 64: Best Cost = 0 Iteration 65: Best Cost = 0 Iteration 66: Best Cost = 0 Iteration 67: Best Cost = 0 Iteration 68: Best Cost = 0 Iteration 69: Best Cost = 0 Iteration 70: Best Cost = 0 Iteration 71: Best Cost = 0 Iteration 72: Best Cost = 0 Iteration 73: Best Cost = 0 Iteration 74: Best Cost = 0 Iteration 75: Best Cost = 0 Iteration 76: Best Cost = 0 Iteration 77: Best Cost = 0 Iteration 78: Best Cost = 0 Iteration 79: Best Cost = 0 Iteration 80: Best Cost = 0 Iteration 81: Best Cost = 0 Iteration 82: Best Cost = 0 Iteration 83: Best Cost = 0 Iteration 84: Best Cost = 0 Iteration 85: Best Cost = 0 Iteration 86: Best Cost = 0 Iteration 87: Best Cost = 0 Iteration 88: Best Cost = 0 Iteration 89: Best Cost = 0 Iteration 90: Best Cost = 0 Iteration 91: Best Cost = 0 Iteration 92: Best Cost = 0 Iteration 93: Best Cost = 0 Iteration 94: Best Cost = 0 Iteration 95: Best Cost = 0 Iteration 96: Best Cost = 0 Iteration 97: Best Cost = 0 Iteration 98: Best Cost = 0 Iteration 99: Best Cost = 0 Iteration 100: Best Cost = 0 Iteration 101: Best Cost = 0 Iteration 102: Best Cost = 0 Iteration 103: Best Cost = 0 Iteration 104: Best Cost = 0 Iteration 105: Best Cost = 0 Iteration 106: Best Cost = 0 Iteration 107: Best Cost = 0 Iteration 108: Best Cost = 0 Iteration 109: Best Cost = 0 Iteration 110: Best Cost = 0 Iteration 111: Best Cost = 0 Iteration 112: Best Cost = 0 Iteration 113: Best Cost = 0 Iteration 114: Best Cost = 0 Iteration 115: Best Cost = 0 Iteration 116: Best Cost = 0 Iteration 117: Best Cost = 0 Iteration 118: Best Cost = 0 Iteration 119: Best Cost = 0 Iteration 120: Best Cost = 0 Iteration 121: Best Cost = 0 Iteration 122: Best Cost = 0 Iteration 123: Best Cost = 0 Iteration 124: Best Cost = 0 Iteration 125: Best Cost = 0 Iteration 126: Best Cost = 0 Iteration 127: Best Cost = 0 Iteration 128: Best Cost = 0 Iteration 129: Best Cost = 0 Iteration 130: Best Cost = 0 Iteration 131: Best Cost = 0 Iteration 132: Best Cost = 0 Iteration 133: Best Cost = 0 Iteration 134: Best Cost = 0 Iteration 135: Best Cost = 0 Iteration 136: Best Cost = 0 Iteration 137: Best Cost = 0 Iteration 138: Best Cost = 0 Iteration 139: Best Cost = 0 Iteration 140: Best Cost = 0 Iteration 141: Best Cost = 0 Iteration 142: Best Cost = 0 Iteration 143: Best Cost = 0 Iteration 144: Best Cost = 0 Iteration 145: Best Cost = 0 Iteration 146: Best Cost = 0 Iteration 147: Best Cost = 0 Iteration 148: Best Cost = 0 Iteration 149: Best Cost = 0 Iteration 150: Best Cost = 0 Iteration 151: Best Cost = 0 Iteration 152: Best Cost = 0 Iteration 153: Best Cost = 0 Iteration 154: Best Cost = 0 Iteration 155: Best Cost = 0 Iteration 156: Best Cost = 0 Iteration 157: Best Cost = 0 Iteration 158: Best Cost = 0 Iteration 159: Best Cost = 0 Iteration 160: Best Cost = 0 Iteration 161: Best Cost = 0 Iteration 162: Best Cost = 0 Iteration 163: Best Cost = 0 Iteration 164: Best Cost = 0 Iteration 165: Best Cost = 0 Iteration 166: Best Cost = 0 Iteration 167: Best Cost = 0 Iteration 168: Best Cost = 0 Iteration 169: Best Cost = 0 Iteration 170: Best Cost = 0 Iteration 171: Best Cost = 0 Iteration 172: Best Cost = 0 Iteration 173: Best Cost = 0 Iteration 174: Best Cost = 0 Iteration 175: Best Cost = 0 Iteration 176: Best Cost = 0 Iteration 177: Best Cost = 0 Iteration 178: Best Cost = 0 Iteration 179: Best Cost = 0 Iteration 180: Best Cost = 0 Iteration 181: Best Cost = 0 Iteration 182: Best Cost = 0 Iteration 183: Best Cost = 0 Iteration 184: Best Cost = 0 Iteration 185: Best Cost = 0 Iteration 186: Best Cost = 0 Iteration 187: Best Cost = 0 Iteration 188: Best Cost = 0 Iteration 189: Best Cost = 0 Iteration 190: Best Cost = 0 Iteration 191: Best Cost = 0 Iteration 192: Best Cost = 0 Iteration 193: Best Cost = 0 Iteration 194: Best Cost = 0 Iteration 195: Best Cost = 0 Iteration 196: Best Cost = 0 Iteration 197: Best Cost = 0 Iteration 198: Best Cost = 0 Iteration 199: Best Cost = 0 Iteration 200: Best Cost = 0 Iteration 201: Best Cost = 0 Iteration 202: Best Cost = 0 Iteration 203: Best Cost = 0 Iteration 204: Best Cost = 0 Iteration 205: Best Cost = 0 Iteration 206: Best Cost = 0 Iteration 207: Best Cost = 0 Iteration 208: Best Cost = 0 Iteration 209: Best Cost = 0 Iteration 210: Best Cost = 0 Iteration 211: Best Cost = 0 Iteration 212: Best Cost = 0 Iteration 213: Best Cost = 0 Iteration 214: Best Cost = 0 Iteration 215: Best Cost = 0 Iteration 216: Best Cost = 0 Iteration 217: Best Cost = 0 Iteration 218: Best Cost = 0 Iteration 219: Best Cost = 0 Iteration 220: Best Cost = 0 Iteration 221: Best Cost = 0 Iteration 222: Best Cost = 0 Iteration 223: Best Cost = 0 Iteration 224: Best Cost = 0 Iteration 225: Best Cost = 0 Iteration 226: Best Cost = 0 Iteration 227: Best Cost = 0 Iteration 228: Best Cost = 0 Iteration 229: Best Cost = 0 Iteration 230: Best Cost = 0 Iteration 231: Best Cost = 0 Iteration 232: Best Cost = 0 Iteration 233: Best Cost = 0 Iteration 234: Best Cost = 0 Iteration 235: Best Cost = 0 Iteration 236: Best Cost = 0 Iteration 237: Best Cost = 0 Iteration 238: Best Cost = 0 Iteration 239: Best Cost = 0 Iteration 240: Best Cost = 0 Iteration 241: Best Cost = 0 Iteration 242: Best Cost = 0 Iteration 243: Best Cost = 0 Iteration 244: Best Cost = 0 Iteration 245: Best Cost = 0 Iteration 246: Best Cost = 0 Iteration 247: Best Cost = 0 Iteration 248: Best Cost = 0 Iteration 249: Best Cost = 0 Iteration 250: Best Cost = 0 Iteration 251: Best Cost = 0 Iteration 252: Best Cost = 0 Iteration 253: Best Cost = 0 Iteration 254: Best Cost = 0 Iteration 255: Best Cost = 0 Iteration 256: Best Cost = 0 Iteration 257: Best Cost = 0 Iteration 258: Best Cost = 0 Iteration 259: Best Cost = 0 Iteration 260: Best Cost = 0 Iteration 261: Best Cost = 0 Iteration 262: Best Cost = 0 Iteration 263: Best Cost = 0 Iteration 264: Best Cost = 0 Iteration 265: Best Cost = 0 Iteration 266: Best Cost = 0 Iteration 267: Best Cost = 0 Iteration 268: Best Cost = 0 Iteration 269: Best Cost = 0 Iteration 270: Best Cost = 0 Iteration 271: Best Cost = 0 Iteration 272: Best Cost = 0 Iteration 273: Best Cost = 0 Iteration 274: Best Cost = 0 Iteration 275: Best Cost = 0 Iteration 276: Best Cost = 0 Iteration 277: Best Cost = 0 Iteration 278: Best Cost = 0 Iteration 279: Best Cost = 0 Iteration 280: Best Cost = 0 Iteration 281: Best Cost = 0 Iteration 282: Best Cost = 0 Iteration 283: Best Cost = 0 Iteration 284: Best Cost = 0 Iteration 285: Best Cost = 0 Iteration 286: Best Cost = 0 Iteration 287: Best Cost = 0 Iteration 288: Best Cost = 0 Iteration 289: Best Cost = 0 Iteration 290: Best Cost = 0 Iteration 291: Best Cost = 0 Iteration 292: Best Cost = 0 Iteration 293: Best Cost = 0 Iteration 294: Best Cost = 0 Iteration 295: Best Cost = 0 Iteration 296: Best Cost = 0 Iteration 297: Best Cost = 0 Iteration 298: Best Cost = 0 Iteration 299: Best Cost = 0 Iteration 300: Best Cost = 0 Iteration 301: Best Cost = 0 Iteration 302: Best Cost = 0 Iteration 303: Best Cost = 0 Iteration 304: Best Cost = 0 Iteration 305: Best Cost = 0 Iteration 306: Best Cost = 0 Iteration 307: Best Cost = 0 Iteration 308: Best Cost = 0 Iteration 309: Best Cost = 0 Iteration 310: Best Cost = 0 Iteration 311: Best Cost = 0 Iteration 312: Best Cost = 0 Iteration 313: Best Cost = 0 Iteration 314: Best Cost = 0 Iteration 315: Best Cost = 0 Iteration 316: Best Cost = 0 Iteration 317: Best Cost = 0 Iteration 318: Best Cost = 0 Iteration 319: Best Cost = 0 Iteration 320: Best Cost = 0 Iteration 321: Best Cost = 0 Iteration 322: Best Cost = 0 Iteration 323: Best Cost = 0 Iteration 324: Best Cost = 0 Iteration 325: Best Cost = 0 Iteration 326: Best Cost = 0 Iteration 327: Best Cost = 0 Iteration 328: Best Cost = 0 Iteration 329: Best Cost = 0 Iteration 330: Best Cost = 0 Iteration 331: Best Cost = 0 Iteration 332: Best Cost = 0 Iteration 333: Best Cost = 0 Iteration 334: Best Cost = 0 Iteration 335: Best Cost = 0 Iteration 336: Best Cost = 0 Iteration 337: Best Cost = 0 Iteration 338: Best Cost = 0 Iteration 339: Best Cost = 0 Iteration 340: Best Cost = 0 Iteration 341: Best Cost = 0 Iteration 342: Best Cost = 0 Iteration 343: Best Cost = 0 Iteration 344: Best Cost = 0 Iteration 345: Best Cost = 0 Iteration 346: Best Cost = 0 Iteration 347: Best Cost = 0 Iteration 348: Best Cost = 0 Iteration 349: Best Cost = 0 Iteration 350: Best Cost = 0 Iteration 351: Best Cost = 0 Iteration 352: Best Cost = 0 Iteration 353: Best Cost = 0 Iteration 354: Best Cost = 0 Iteration 355: Best Cost = 0 Iteration 356: Best Cost = 0 Iteration 357: Best Cost = 0 Iteration 358: Best Cost = 0 Iteration 359: Best Cost = 0 Iteration 360: Best Cost = 0 Iteration 361: Best Cost = 0 Iteration 362: Best Cost = 0 Iteration 363: Best Cost = 0 Iteration 364: Best Cost = 0 Iteration 365: Best Cost = 0 Iteration 366: Best Cost = 0 Iteration 367: Best Cost = 0 Iteration 368: Best Cost = 0 Iteration 369: Best Cost = 0 Iteration 370: Best Cost = 0 Iteration 371: Best Cost = 0 Iteration 372: Best Cost = 0 Iteration 373: Best Cost = 0 Iteration 374: Best Cost = 0 Iteration 375: Best Cost = 0 Iteration 376: Best Cost = 0 Iteration 377: Best Cost = 0 Iteration 378: Best Cost = 0 Iteration 379: Best Cost = 0 Iteration 380: Best Cost = 0 Iteration 381: Best Cost = 0 Iteration 382: Best Cost = 0 Iteration 383: Best Cost = 0 Iteration 384: Best Cost = 0 Iteration 385: Best Cost = 0 Iteration 386: Best Cost = 0 Iteration 387: Best Cost = 0 Iteration 388: Best Cost = 0 Iteration 389: Best Cost = 0 Iteration 390: Best Cost = 0 Iteration 391: Best Cost = 0 Iteration 392: Best Cost = 0 Iteration 393: Best Cost = 0 Iteration 394: Best Cost = 0 Iteration 395: Best Cost = 0 Iteration 396: Best Cost = 0 Iteration 397: Best Cost = 0 Iteration 398: Best Cost = 0 Iteration 399: Best Cost = 0 Iteration 400: Best Cost = 0 Iteration 401: Best Cost = 0 Iteration 402: Best Cost = 0 Iteration 403: Best Cost = 0 Iteration 404: Best Cost = 0 Iteration 405: Best Cost = 0 Iteration 406: Best Cost = 0 Iteration 407: Best Cost = 0 Iteration 408: Best Cost = 0 Iteration 409: Best Cost = 0 Iteration 410: Best Cost = 0 Iteration 411: Best Cost = 0 Iteration 412: Best Cost = 0 Iteration 413: Best Cost = 0 Iteration 414: Best Cost = 0 Iteration 415: Best Cost = 0 Iteration 416: Best Cost = 0 Iteration 417: Best Cost = 0 Iteration 418: Best Cost = 0 Iteration 419: Best Cost = 0 Iteration 420: Best Cost = 0 Iteration 421: Best Cost = 0 Iteration 422: Best Cost = 0 Iteration 423: Best Cost = 0 Iteration 424: Best Cost = 0 Iteration 425: Best Cost = 0 Iteration 426: Best Cost = 0 Iteration 427: Best Cost = 0 Iteration 428: Best Cost = 0 Iteration 429: Best Cost = 0 Iteration 430: Best Cost = 0 Iteration 431: Best Cost = 0 Iteration 432: Best Cost = 0 Iteration 433: Best Cost = 0 Iteration 434: Best Cost = 0 Iteration 435: Best Cost = 0 Iteration 436: Best Cost = 0 Iteration 437: Best Cost = 0 Iteration 438: Best Cost = 0 Iteration 439: Best Cost = 0 Iteration 440: Best Cost = 0 Iteration 441: Best Cost = 0 Iteration 442: Best Cost = 0 Iteration 443: Best Cost = 0 Iteration 444: Best Cost = 0 Iteration 445: Best Cost = 0 Iteration 446: Best Cost = 0 Iteration 447: Best Cost = 0 Iteration 448: Best Cost = 0 Iteration 449: Best Cost = 0 Iteration 450: Best Cost = 0 Iteration 451: Best Cost = 0 Iteration 452: Best Cost = 0 Iteration 453: Best Cost = 0 Iteration 454: Best Cost = 0 Iteration 455: Best Cost = 0 Iteration 456: Best Cost = 0 Iteration 457: Best Cost = 0 Iteration 458: Best Cost = 0 Iteration 459: Best Cost = 0 Iteration 460: Best Cost = 0 Iteration 461: Best Cost = 0 Iteration 462: Best Cost = 0 Iteration 463: Best Cost = 0 Iteration 464: Best Cost = 0 Iteration 465: Best Cost = 0 Iteration 466: Best Cost = 0 Iteration 467: Best Cost = 0 Iteration 468: Best Cost = 0 Iteration 469: Best Cost = 0 Iteration 470: Best Cost = 0 Iteration 471: Best Cost = 0 Iteration 472: Best Cost = 0 Iteration 473: Best Cost = 0 Iteration 474: Best Cost = 0 Iteration 475: Best Cost = 0 Iteration 476: Best Cost = 0 Iteration 477: Best Cost = 0 Iteration 478: Best Cost = 0 Iteration 479: Best Cost = 0 Iteration 480: Best Cost = 0 Iteration 481: Best Cost = 0 Iteration 482: Best Cost = 0 Iteration 483: Best Cost = 0 Iteration 484: Best Cost = 0 Iteration 485: Best Cost = 0 Iteration 486: Best Cost = 0 Iteration 487: Best Cost = 0 Iteration 488: Best Cost = 0 Iteration 489: Best Cost = 0 Iteration 490: Best Cost = 0 Iteration 491: Best Cost = 0 Iteration 492: Best Cost = 0 Iteration 493: Best Cost = 0 Iteration 494: Best Cost = 0 Iteration 495: Best Cost = 0 Iteration 496: Best Cost = 0 Iteration 497: Best Cost = 0 Iteration 498: Best Cost = 0 Iteration 499: Best Cost = 0 Iteration 500: Best Cost = 0 Iteration 501: Best Cost = 0 Iteration 502: Best Cost = 0 Iteration 503: Best Cost = 0 Iteration 504: Best Cost = 0 Iteration 505: Best Cost = 0 Iteration 506: Best Cost = 0 Iteration 507: Best Cost = 0 Iteration 508: Best Cost = 0 Iteration 509: Best Cost = 0 Iteration 510: Best Cost = 0 Iteration 511: Best Cost = 0 Iteration 512: Best Cost = 0 Iteration 513: Best Cost = 0 Iteration 514: Best Cost = 0 Iteration 515: Best Cost = 0 Iteration 516: Best Cost = 0 Iteration 517: Best Cost = 0 Iteration 518: Best Cost = 0 Iteration 519: Best Cost = 0 Iteration 520: Best Cost = 0 Iteration 521: Best Cost = 0 Iteration 522: Best Cost = 0 Iteration 523: Best Cost = 0 Iteration 524: Best Cost = 0 Iteration 525: Best Cost = 0 Iteration 526: Best Cost = 0 Iteration 527: Best Cost = 0 Iteration 528: Best Cost = 0 Iteration 529: Best Cost = 0 Iteration 530: Best Cost = 0 Iteration 531: Best Cost = 0 Iteration 532: Best Cost = 0 Iteration 533: Best Cost = 0 Iteration 534: Best Cost = 0 Iteration 535: Best Cost = 0 Iteration 536: Best Cost = 0 Iteration 537: Best Cost = 0 Iteration 538: Best Cost = 0 Iteration 539: Best Cost = 0 Iteration 540: Best Cost = 0 Iteration 541: Best Cost = 0 Iteration 542: Best Cost = 0 Iteration 543: Best Cost = 0 Iteration 544: Best Cost = 0 Iteration 545: Best Cost = 0 Iteration 546: Best Cost = 0 Iteration 547: Best Cost = 0 Iteration 548: Best Cost = 0 Iteration 549: Best Cost = 0 Iteration 550: Best Cost = 0 Iteration 551: Best Cost = 0 Iteration 552: Best Cost = 0 Iteration 553: Best Cost = 0 Iteration 554: Best Cost = 0 Iteration 555: Best Cost = 0 Iteration 556: Best Cost = 0 Iteration 557: Best Cost = 0 Iteration 558: Best Cost = 0 Iteration 559: Best Cost = 0 Iteration 560: Best Cost = 0 Iteration 561: Best Cost = 0 Iteration 562: Best Cost = 0 Iteration 563: Best Cost = 0 Iteration 564: Best Cost = 0 Iteration 565: Best Cost = 0 Iteration 566: Best Cost = 0 Iteration 567: Best Cost = 0 Iteration 568: Best Cost = 0 Iteration 569: Best Cost = 0 Iteration 570: Best Cost = 0 Iteration 571: Best Cost = 0 Iteration 572: Best Cost = 0 Iteration 573: Best Cost = 0 Iteration 574: Best Cost = 0 Iteration 575: Best Cost = 0 Iteration 576: Best Cost = 0 Iteration 577: Best Cost = 0 Iteration 578: Best Cost = 0 Iteration 579: Best Cost = 0 Iteration 580: Best Cost = 0 Iteration 581: Best Cost = 0 Iteration 582: Best Cost = 0 Iteration 583: Best Cost = 0 Iteration 584: Best Cost = 0 Iteration 585: Best Cost = 0 Iteration 586: Best Cost = 0 Iteration 587: Best Cost = 0 Iteration 588: Best Cost = 0 Iteration 589: Best Cost = 0 Iteration 590: Best Cost = 0 Iteration 591: Best Cost = 0 Iteration 592: Best Cost = 0 Iteration 593: Best Cost = 0 Iteration 594: Best Cost = 0 Iteration 595: Best Cost = 0 Iteration 596: Best Cost = 0 Iteration 597: Best Cost = 0 Iteration 598: Best Cost = 0 Iteration 599: Best Cost = 0 Iteration 600: Best Cost = 0 Iteration 601: Best Cost = 0 Iteration 602: Best Cost = 0 Iteration 603: Best Cost = 0 Iteration 604: Best Cost = 0 Iteration 605: Best Cost = 0 Iteration 606: Best Cost = 0 Iteration 607: Best Cost = 0 Iteration 608: Best Cost = 0 Iteration 609: Best Cost = 0 Iteration 610: Best Cost = 0 Iteration 611: Best Cost = 0 Iteration 612: Best Cost = 0 Iteration 613: Best Cost = 0 Iteration 614: Best Cost = 0 Iteration 615: Best Cost = 0 Iteration 616: Best Cost = 0 Iteration 617: Best Cost = 0 Iteration 618: Best Cost = 0 Iteration 619: Best Cost = 0 Iteration 620: Best Cost = 0 Iteration 621: Best Cost = 0 Iteration 622: Best Cost = 0 Iteration 623: Best Cost = 0 Iteration 624: Best Cost = 0 Iteration 625: Best Cost = 0 Iteration 626: Best Cost = 0 Iteration 627: Best Cost = 0 Iteration 628: Best Cost = 0 Iteration 629: Best Cost = 0 Iteration 630: Best Cost = 0 Iteration 631: Best Cost = 0 Iteration 632: Best Cost = 0 Iteration 633: Best Cost = 0 Iteration 634: Best Cost = 0 Iteration 635: Best Cost = 0 Iteration 636: Best Cost = 0 Iteration 637: Best Cost = 0 Iteration 638: Best Cost = 0 Iteration 639: Best Cost = 0 Iteration 640: Best Cost = 0 Iteration 641: Best Cost = 0 Iteration 642: Best Cost = 0 Iteration 643: Best Cost = 0 Iteration 644: Best Cost = 0 Iteration 645: Best Cost = 0 Iteration 646: Best Cost = 0 Iteration 647: Best Cost = 0 Iteration 648: Best Cost = 0 Iteration 649: Best Cost = 0 Iteration 650: Best Cost = 0 Iteration 651: Best Cost = 0 Iteration 652: Best Cost = 0 Iteration 653: Best Cost = 0 Iteration 654: Best Cost = 0 Iteration 655: Best Cost = 0 Iteration 656: Best Cost = 0 Iteration 657: Best Cost = 0 Iteration 658: Best Cost = 0 Iteration 659: Best Cost = 0 Iteration 660: Best Cost = 0 Iteration 661: Best Cost = 0 Iteration 662: Best Cost = 0 Iteration 663: Best Cost = 0 Iteration 664: Best Cost = 0 Iteration 665: Best Cost = 0 Iteration 666: Best Cost = 0 Iteration 667: Best Cost = 0 Iteration 668: Best Cost = 0 Iteration 669: Best Cost = 0 Iteration 670: Best Cost = 0 Iteration 671: Best Cost = 0 Iteration 672: Best Cost = 0 Iteration 673: Best Cost = 0 Iteration 674: Best Cost = 0 Iteration 675: Best Cost = 0 Iteration 676: Best Cost = 0 Iteration 677: Best Cost = 0 Iteration 678: Best Cost = 0 Iteration 679: Best Cost = 0 Iteration 680: Best Cost = 0 Iteration 681: Best Cost = 0 Iteration 682: Best Cost = 0 Iteration 683: Best Cost = 0 Iteration 684: Best Cost = 0 Iteration 685: Best Cost = 0 Iteration 686: Best Cost = 0 Iteration 687: Best Cost = 0 Iteration 688: Best Cost = 0 Iteration 689: Best Cost = 0 Iteration 690: Best Cost = 0 Iteration 691: Best Cost = 0 Iteration 692: Best Cost = 0 Iteration 693: Best Cost = 0 Iteration 694: Best Cost = 0 Iteration 695: Best Cost = 0 Iteration 696: Best Cost = 0 Iteration 697: Best Cost = 0 Iteration 698: Best Cost = 0 Iteration 699: Best Cost = 0 Iteration 700: Best Cost = 0 Iteration 701: Best Cost = 0 Iteration 702: Best Cost = 0 Iteration 703: Best Cost = 0 Iteration 704: Best Cost = 0 Iteration 705: Best Cost = 0 Iteration 706: Best Cost = 0 Iteration 707: Best Cost = 0 Iteration 708: Best Cost = 0 Iteration 709: Best Cost = 0 Iteration 710: Best Cost = 0 Iteration 711: Best Cost = 0 Iteration 712: Best Cost = 0 Iteration 713: Best Cost = 0 Iteration 714: Best Cost = 0 Iteration 715: Best Cost = 0 Iteration 716: Best Cost = 0 Iteration 717: Best Cost = 0 Iteration 718: Best Cost = 0 Iteration 719: Best Cost = 0 Iteration 720: Best Cost = 0 Iteration 721: Best Cost = 0 Iteration 722: Best Cost = 0 Iteration 723: Best Cost = 0 Iteration 724: Best Cost = 0 Iteration 725: Best Cost = 0 Iteration 726: Best Cost = 0 Iteration 727: Best Cost = 0 Iteration 728: Best Cost = 0 Iteration 729: Best Cost = 0 Iteration 730: Best Cost = 0 Iteration 731: Best Cost = 0 Iteration 732: Best Cost = 0 Iteration 733: Best Cost = 0 Iteration 734: Best Cost = 0 Iteration 735: Best Cost = 0 Iteration 736: Best Cost = 0 Iteration 737: Best Cost = 0 Iteration 738: Best Cost = 0 Iteration 739: Best Cost = 0 Iteration 740: Best Cost = 0 Iteration 741: Best Cost = 0 Iteration 742: Best Cost = 0 Iteration 743: Best Cost = 0 Iteration 744: Best Cost = 0 Iteration 745: Best Cost = 0 Iteration 746: Best Cost = 0 Iteration 747: Best Cost = 0 Iteration 748: Best Cost = 0 Iteration 749: Best Cost = 0 Iteration 750: Best Cost = 0 Iteration 751: Best Cost = 0 Iteration 752: Best Cost = 0 Iteration 753: Best Cost = 0 Iteration 754: Best Cost = 0 Iteration 755: Best Cost = 0 Iteration 756: Best Cost = 0 Iteration 757: Best Cost = 0 Iteration 758: Best Cost = 0 Iteration 759: Best Cost = 0 Iteration 760: Best Cost = 0 Iteration 761: Best Cost = 0 Iteration 762: Best Cost = 0 Iteration 763: Best Cost = 0 Iteration 764: Best Cost = 0 Iteration 765: Best Cost = 0 Iteration 766: Best Cost = 0 Iteration 767: Best Cost = 0 Iteration 768: Best Cost = 0 Iteration 769: Best Cost = 0 Iteration 770: Best Cost = 0 Iteration 771: Best Cost = 0 Iteration 772: Best Cost = 0 Iteration 773: Best Cost = 0 Iteration 774: Best Cost = 0 Iteration 775: Best Cost = 0 Iteration 776: Best Cost = 0 Iteration 777: Best Cost = 0 Iteration 778: Best Cost = 0 Iteration 779: Best Cost = 0 Iteration 780: Best Cost = 0 Iteration 781: Best Cost = 0 Iteration 782: Best Cost = 0 Iteration 783: Best Cost = 0 Iteration 784: Best Cost = 0 Iteration 785: Best Cost = 0 Iteration 786: Best Cost = 0 Iteration 787: Best Cost = 0 Iteration 788: Best Cost = 0 Iteration 789: Best Cost = 0 Iteration 790: Best Cost = 0 Iteration 791: Best Cost = 0 Iteration 792: Best Cost = 0 Iteration 793: Best Cost = 0 Iteration 794: Best Cost = 0 Iteration 795: Best Cost = 0 Iteration 796: Best Cost = 0 Iteration 797: Best Cost = 0 Iteration 798: Best Cost = 0 Iteration 799: Best Cost = 0 Iteration 800: Best Cost = 0 Iteration 801: Best Cost = 0 Iteration 802: Best Cost = 0 Iteration 803: Best Cost = 0 Iteration 804: Best Cost = 0 Iteration 805: Best Cost = 0 Iteration 806: Best Cost = 0 Iteration 807: Best Cost = 0 Iteration 808: Best Cost = 0 Iteration 809: Best Cost = 0 Iteration 810: Best Cost = 0 Iteration 811: Best Cost = 0 Iteration 812: Best Cost = 0 Iteration 813: Best Cost = 0 Iteration 814: Best Cost = 0 Iteration 815: Best Cost = 0 Iteration 816: Best Cost = 0 Iteration 817: Best Cost = 0 Iteration 818: Best Cost = 0 Iteration 819: Best Cost = 0 Iteration 820: Best Cost = 0 Iteration 821: Best Cost = 0 Iteration 822: Best Cost = 0 Iteration 823: Best Cost = 0 Iteration 824: Best Cost = 0 Iteration 825: Best Cost = 0 Iteration 826: Best Cost = 0 Iteration 827: Best Cost = 0 Iteration 828: Best Cost = 0 Iteration 829: Best Cost = 0 Iteration 830: Best Cost = 0 Iteration 831: Best Cost = 0 Iteration 832: Best Cost = 0 Iteration 833: Best Cost = 0 Iteration 834: Best Cost = 0 Iteration 835: Best Cost = 0 Iteration 836: Best Cost = 0 Iteration 837: Best Cost = 0 Iteration 838: Best Cost = 0 Iteration 839: Best Cost = 0 Iteration 840: Best Cost = 0 Iteration 841: Best Cost = 0 Iteration 842: Best Cost = 0 Iteration 843: Best Cost = 0 Iteration 844: Best Cost = 0 Iteration 845: Best Cost = 0 Iteration 846: Best Cost = 0 Iteration 847: Best Cost = 0 Iteration 848: Best Cost = 0 Iteration 849: Best Cost = 0 Iteration 850: Best Cost = 0 Iteration 851: Best Cost = 0 Iteration 852: Best Cost = 0 Iteration 853: Best Cost = 0 Iteration 854: Best Cost = 0 Iteration 855: Best Cost = 0 Iteration 856: Best Cost = 0 Iteration 857: Best Cost = 0 Iteration 858: Best Cost = 0 Iteration 859: Best Cost = 0 Iteration 860: Best Cost = 0 Iteration 861: Best Cost = 0 Iteration 862: Best Cost = 0 Iteration 863: Best Cost = 0 Iteration 864: Best Cost = 0 Iteration 865: Best Cost = 0 Iteration 866: Best Cost = 0 Iteration 867: Best Cost = 0 Iteration 868: Best Cost = 0 Iteration 869: Best Cost = 0 Iteration 870: Best Cost = 0 Iteration 871: Best Cost = 0 Iteration 872: Best Cost = 0 Iteration 873: Best Cost = 0 Iteration 874: Best Cost = 0 Iteration 875: Best Cost = 0 Iteration 876: Best Cost = 0 Iteration 877: Best Cost = 0 Iteration 878: Best Cost = 0 Iteration 879: Best Cost = 0 Iteration 880: Best Cost = 0 Iteration 881: Best Cost = 0 Iteration 882: Best Cost = 0 Iteration 883: Best Cost = 0 Iteration 884: Best Cost = 0 Iteration 885: Best Cost = 0 Iteration 886: Best Cost = 0 Iteration 887: Best Cost = 0 Iteration 888: Best Cost = 0 Iteration 889: Best Cost = 0 Iteration 890: Best Cost = 0 Iteration 891: Best Cost = 0 Iteration 892: Best Cost = 0 Iteration 893: Best Cost = 0 Iteration 894: Best Cost = 0 Iteration 895: Best Cost = 0 Iteration 896: Best Cost = 0 Iteration 897: Best Cost = 0 Iteration 898: Best Cost = 0 Iteration 899: Best Cost = 0 Iteration 900: Best Cost = 0 Iteration 901: Best Cost = 0 Iteration 902: Best Cost = 0 Iteration 903: Best Cost = 0 Iteration 904: Best Cost = 0 Iteration 905: Best Cost = 0 Iteration 906: Best Cost = 0 Iteration 907: Best Cost = 0 Iteration 908: Best Cost = 0 Iteration 909: Best Cost = 0 Iteration 910: Best Cost = 0 Iteration 911: Best Cost = 0 Iteration 912: Best Cost = 0 Iteration 913: Best Cost = 0 Iteration 914: Best Cost = 0 Iteration 915: Best Cost = 0 Iteration 916: Best Cost = 0 Iteration 917: Best Cost = 0 Iteration 918: Best Cost = 0 Iteration 919: Best Cost = 0 Iteration 920: Best Cost = 0 Iteration 921: Best Cost = 0 Iteration 922: Best Cost = 0 Iteration 923: Best Cost = 0 Iteration 924: Best Cost = 0 Iteration 925: Best Cost = 0 Iteration 926: Best Cost = 0 Iteration 927: Best Cost = 0 Iteration 928: Best Cost = 0 Iteration 929: Best Cost = 0 Iteration 930: Best Cost = 0 Iteration 931: Best Cost = 0 Iteration 932: Best Cost = 0 Iteration 933: Best Cost = 0 Iteration 934: Best Cost = 0 Iteration 935: Best Cost = 0 Iteration 936: Best Cost = 0 Iteration 937: Best Cost = 0 Iteration 938: Best Cost = 0 Iteration 939: Best Cost = 0 Iteration 940: Best Cost = 0 Iteration 941: Best Cost = 0 Iteration 942: Best Cost = 0 Iteration 943: Best Cost = 0 Iteration 944: Best Cost = 0 Iteration 945: Best Cost = 0 Iteration 946: Best Cost = 0 Iteration 947: Best Cost = 0 Iteration 948: Best Cost = 0 Iteration 949: Best Cost = 0 Iteration 950: Best Cost = 0 Iteration 951: Best Cost = 0 Iteration 952: Best Cost = 0 Iteration 953: Best Cost = 0 Iteration 954: Best Cost = 0 Iteration 955: Best Cost = 0 Iteration 956: Best Cost = 0 Iteration 957: Best Cost = 0 Iteration 958: Best Cost = 0 Iteration 959: Best Cost = 0 Iteration 960: Best Cost = 0 Iteration 961: Best Cost = 0 Iteration 962: Best Cost = 0 Iteration 963: Best Cost = 0 Iteration 964: Best Cost = 0 Iteration 965: Best Cost = 0 Iteration 966: Best Cost = 0 Iteration 967: Best Cost = 0 Iteration 968: Best Cost = 0 Iteration 969: Best Cost = 0 Iteration 970: Best Cost = 0 Iteration 971: Best Cost = 0 Iteration 972: Best Cost = 0 Iteration 973: Best Cost = 0 Iteration 974: Best Cost = 0 Iteration 975: Best Cost = 0 Iteration 976: Best Cost = 0 Iteration 977: Best Cost = 0 Iteration 978: Best Cost = 0 Iteration 979: Best Cost = 0 Iteration 980: Best Cost = 0 Iteration 981: Best Cost = 0 Iteration 982: Best Cost = 0 Iteration 983: Best Cost = 0 Iteration 984: Best Cost = 0 Iteration 985: Best Cost = 0 Iteration 986: Best Cost = 0 Iteration 987: Best Cost = 0 Iteration 988: Best Cost = 0 Iteration 989: Best Cost = 0 Iteration 990: Best Cost = 0 Iteration 991: Best Cost = 0 Iteration 992: Best Cost = 0 Iteration 993: Best Cost = 0 Iteration 994: Best Cost = 0 Iteration 995: Best Cost = 0 Iteration 996: Best Cost = 0 Iteration 997: Best Cost = 0 Iteration 998: Best Cost = 0 Iteration 999: Best Cost = 0 Iteration 1000: Best Cost = 0
%% Results
figure;
plot(BestCost, 'LineWidth', 2);
xlabel('Iteration');
ylabel('Best Cost');
title('Convergence of PSO');
grid on;
%% Bin Configuration Visualization and Vertex Calculation
plotBins(GlobalBest.Sol, model);
vertices = cell(model.n, 1);
for i = GlobalBest.Sol.B{1}
item_dims = model.items(i, :);
orientation = GlobalBest.Position(4*i);
item_pos = GlobalBest.Position(4*i-3:4*i-1);
% Rotate dimensions based on orientation
switch orientation
case 1 % (L, W, H)
rotated_dims = item_dims;
case 2 % (L, H, W)
rotated_dims = [item_dims(1), item_dims(3), item_dims(2)];
case 3 % (W, L, H)
rotated_dims = [item_dims(2), item_dims(1), item_dims(3)];
case 4 % (W, H, L)
rotated_dims = [item_dims(2), item_dims(3), item_dims(1)];
case 5 % (H, L, W)
rotated_dims = [item_dims(3), item_dims(1), item_dims(2)];
case 6 % (H, W, L)
rotated_dims = [item_dims(3), item_dims(2), item_dims(1)];
end
[X, Y, Z] = getBoxVertices(rotated_dims, item_pos);
vertices{i} = [X; Y; Z];
end
%% Exporting Vertex Data to CSV
% Create a cell array to hold the data for the CSV file (This part remains the same)
csv_data = cell(model.n + 1, 1);
csv_data{1} = {'PresentId', 'x1', 'y1', 'z1', 'x2', 'y2', 'z2', 'x3', 'y3', 'z3', 'x4', 'y4', 'z4', 'x5', 'y5', 'z5', 'x6', 'y6', 'z6', 'x7', 'y7', 'z7', 'x8', 'y8', 'z8'};
% Fill the cell array with vertex data for each item (This part remains the same)
for i = 1:model.n
item_vertices = vertices{i}; % CHANGED
csv_data{i+1} = [i, item_vertices(:)']; % Add item ID and flatten vertex coordinates
end
% Open the file for writing
fileID = fopen('vertex_data.csv','w');
% Write header row (remains the same)
fprintf(fileID,'%s,',csv_data{1}{:});
fprintf(fileID,'\n');
% Write data rows (corrected loop)
for i = 2:size(csv_data,1)
row_data = csv_data{i}; % Get the cell array for the current row
for j = 1:length(row_data)
fprintf(fileID,'%d,',round(row_data(j))); % Round values to nearest integer
end
fprintf(fileID,'\n');
end
% Close the file (remains the same)
fclose(fileID);
%get the coordinates
solutionFile = 'vertex_data.csv';
solutionData = readmatrix(solutionFile);
% get the two bounding box corners (min(x),min(y),min(z)) and (max(x),max(y),max(z))
p1 = min(reshape(min(solutionData(:,2:end),[],1),3,[]).',[],1);
p8 = max(reshape(max(solutionData(:,2:end),[],1),3,[]).',[],1);
coordinates = solutionData(:, 2:end);
% append the bounding box coordinates to the end of the coordinates matrix:
p_bb = [p1,p1,p1,p1,p8,p8,p8,p8]; % CHANGED
p_bb([4 7 8 11]) = p8([1 1 2 2]); % CHANGED
p_bb([13 14 17 22]) = p1([1 2 2 1]); % CHANGED
coordinates(end+1,:) = p_bb;
N = size(coordinates,1);
colors = [0 1 0; 0 0 1; 1 0 0; 1 1 0; 1 0 1];
colors = repmat(colors,ceil(N/size(colors,1)),1);
% make the bounding box a special color:
colors(N,:) = [0.8 0.8 0.8];
figure
F = [1 2 3 4; 5 6 7 8; 1 2 6 5; 4 3 7 8; 2 6 7 3; 1 5 8 4]; % CHANGED
for ii = 1:N
C = reshape(coordinates(ii, :), 3, []).';
patch( ...
'Vertices',C, ...
'Faces',F, ...
'FaceColor','flat', ...
'FaceVertexCData',colors(ii,:), ...
'FaceAlpha',0.5);
end
view(3)
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('Visualization of Boxes');
grid on;
The boxes overlap is why they look funny but they are all single-colored cuboids.
  28 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!