josemartin22_Algoritmos-Bioinspirados-para-Netlogo/modelos/ACO-25BarSpaceTruss.nlogo3d
extensions [ rnd matrix ]

globals [
  ; Constants
  lb-cross-section   ; lower bound cross section (in²)
  ub-cross-section   ; upper bound cross section(in²)
  increment-section  ; increment (in²)

  iteration ; Iterations counter

  ; Matrix of pheromone for each virtual path
  trail-matrix

  ; Vector with all cross-sections areas
  sections-vector

  ; Best cross sections
  best-route
  best-route-cost
]

breed [joints joint]
joints-own [
  n  ; Joint member
]

breed [ants ant]
ants-own [
  route
  cost
  penalty
]

links-own [
  id         ; Edge identifier
  group      ; Element Group number
  edge-cost  ; Cost of the edge
]


to setup
  clear-all
  random-seed 123456789

  set-observer
  set-globals

  setup-joints
  setup-edges
  setup-matrix-vector
  setup-ants

  set best-route generate-random-route
  set best-route-cost (calculate-cost-of-route best-route)
  set best-route-cost best-route-cost * (1 + penalty-function best-route) ^ 6

  update-display
  reset-ticks
end


to-report generate-random-route
  ; Generate random route
  let nm (precision (1 + ((ub-cross-section - lb-cross-section) / increment-section)) 0)
  let random-route n-values (max [group] of links) [random nm]

  ; Best solution [0.10 0.30 3.40 0.10 2.10 1.00 0.50 3.40]
  ;set random-route [0 2 33 0 20 9 4 33] ; <- Vector of cross-section areas index

  report random-route
end


to go

; ACO Algorithm
  if-else iteration < max-iterations [

    find-route
    ask ants [
      ; Cost of their own route adding penalty
      set cost calculate-cost-of-route route
      set penalty penalty-function route
      set cost cost * (1 + penalty) ^ 6

      if penalty = 0 [show (word "FACTIBLE " route " Coste " cost)]

      ; Update best known route
      if (cost < best-route-cost) and (penalty = 0) [
        set best-route-cost cost
        set best-route route
        update-display
      ]
    ]
    if-else ranked-ant-system
      [ ranked-global-pheromone-update ]
    [
      global-pheromone-update
    ]

    set iteration iteration + 1
    tick
  ]
  [ stop ]
end

; Procedure to find a route
to find-route
  ; Number of design variables (groups)
  let ng max [group] of links
  let i 0

  ; Calculate cross-section area for each design variable
  while [i < ng]
  [
    ask ants [
      let row-design-variable (matrix:get-row trail-matrix i)
      let cross-sections-prob (calculate-probabilities row-design-variable)
      let j (calculate-max cross-sections-prob)
      set route (replace-item i route j)
      decrease-pheromone-path i j
    ]
    set i (i + 1)
  ]
end

to-report calculate-probabilities [row]
  let probabilities []

  let i 0
  while [i < (length row)]
  [
    let j-th-prob ( ((item i row) ^ alpha) * (1 / (item i sections-vector)) ^ beta )
    set probabilities (lput j-th-prob probabilities)
    set i (i + 1)
  ]
  report probabilities
end

to-report calculate-max [v-probabilities]

  let index 0
  let max-value (item 0 v-probabilities)

  let i 1
  while [i < (length v-probabilities)] [
    let value (item i v-probabilities)
    ; Update the highest known probability
    if value > max-value [
      set max-value value
      set index i
    ]
    set i (i + 1)
  ]
  report index
end

; Local pheromone update schema
to decrease-pheromone-path [design-variable selected-path]

  let i design-variable
  let j selected-path
  let new-value (epsilon * (matrix:get trail-matrix i j))
  set trail-matrix (matrix:set-and-report trail-matrix i j new-value)

end


; Calculate the length of a route
to-report calculate-cost-of-route [given-route]

  let route-cost 0
  let edges [self] of links

  ; Objective function (minimize W = sum (di * Li * Ai))
  foreach edges [ edge ->
    let member-group [group] of edge

    ; Traslation function from i-th member-group to section-area
    let cross-section-area (item (item (member-group - 1) given-route) sections-vector)

    ; Calculate the weigth of edge
    set route-cost route-cost + (density * ([edge-cost] of edge) * cross-section-area)
  ]
  report route-cost
end


; Procedure that calculates the penalty for a given solution
to-report penalty-function [given-route]

  let n-nodes count joints
  let n-links count links

  ; For each truss member, get his cross-section area from the given solution

  let A []
  set A (lput (item (item 0 given-route) sections-vector) A)
  set A (lput (item (item 1 given-route) sections-vector) A)
  set A (lput (item (item 1 given-route) sections-vector) A)
  set A (lput (item (item 1 given-route) sections-vector) A)
  set A (lput (item (item 1 given-route) sections-vector) A)
  set A (lput (item (item 2 given-route) sections-vector) A)
  set A (lput (item (item 2 given-route) sections-vector) A)
  set A (lput (item (item 2 given-route) sections-vector) A)
  set A (lput (item (item 2 given-route) sections-vector) A)
  set A (lput (item (item 3 given-route) sections-vector) A)
  set A (lput (item (item 3 given-route) sections-vector) A)
  set A (lput (item (item 4 given-route) sections-vector) A)
  set A (lput (item (item 4 given-route) sections-vector) A)
  set A (lput (item (item 5 given-route) sections-vector) A)
  set A (lput (item (item 5 given-route) sections-vector) A)
  set A (lput (item (item 5 given-route) sections-vector) A)
  set A (lput (item (item 5 given-route) sections-vector) A)
  set A (lput (item (item 6 given-route) sections-vector) A)
  set A (lput (item (item 6 given-route) sections-vector) A)
  set A (lput (item (item 6 given-route) sections-vector) A)
  set A (lput (item (item 6 given-route) sections-vector) A)
  set A (lput (item (item 7 given-route) sections-vector) A)
  set A (lput (item (item 7 given-route) sections-vector) A)
  set A (lput (item (item 7 given-route) sections-vector) A)
  set A (lput (item (item 7 given-route) sections-vector) A)

  ;Matrix with Degree of Freedom
  let Re matrix:transpose matrix:from-row-list [[0 0 0][0 0 0][0 0 0][0 0 0][0 0 0][0 0 0][1 1 1][1 1 1][1 1 1][1 1 1]]
  let w matrix:dimensions Re ; Contiene dimensiones [3x10]
  let S matrix:make-constant (3 * item 1 w) (3 * item 1 w) 0 ; Stiffness matrix [30x30]
  let U matrix:minus 1 Re  ; Nodes-dimensions with degree of freedom
  let f (range 18)  ; Index of nodes-dimensions with defree of freedom != 0 (constant)

  let Tj matrix:make-constant 3 n-links 0

  ; Two lists with the enumeration of the first and second joints of edges
  let list-1-edges (list 1 1 2 1 2 2 2 1 1 3 4 3 5 3 6 4 5 4 3 5 6 6 3 4 5)
  let list-2-edges (list 2 4 3 5 6 4 5 3 6 6 5 4 6 10 7 9 8 7 8 10 9 10 7 8 9)

  let edges [self] of links
  foreach edges [ edge ->

    ; Temporaly variables of each loop
    let H [] ; Pair of nodes that composes the edge
    let len 0
    let member-group 0
    let n-id 0

    ask edge [
      set H sort ([self] of both-ends)
      set len edge-cost
      set member-group group
      set n-id (id - 1)
    ]

    let H1 map [x -> x * 10] (list ([xcor] of (item 0 H)) ([ycor] of item 0 H) ([zcor] of item 0 H))
    let H2 map [x -> x * 10] (list ([xcor] of (item 1 H)) ([ycor] of item 1 H) ([zcor] of item 1 H))

    ; Diference of coordinates
    let C (map - H2 H1)

    ; Divide by the module (length of each member truss)
    let T matrix:from-row-list (list (map [ x -> x / len ] C))
    let T' matrix:transpose T ; Transpose of T
    let sl T' matrix:* T; Lambda matrix

    ; Translation function from i-th member-group to section-area
    let cross-section-area (item (item (member-group - 1) given-route) sections-vector)

    ; Component G to calculate
    let G (elasticity * (cross-section-area / len))
    matrix:set-column Tj n-id (item 0 (matrix:to-row-list (G matrix:* T)))

    let tmp1 (range ((([n] of item 0 H) * 3) - 2) ((([n] of item 0 H) * 3) + 1))
    let tmp2 (range ((([n] of item 1 H) * 3) - 2) ((([n] of item 1 H) * 3) + 1))
    let tmp (sentence tmp1 tmp2)

    ; Create the transformation lambda matrix [[sl -sl][-sl sl]]
    let l-matrix matrix:make-constant 6 6 0
    let n-sl (matrix:map [x -> -1 * x] sl)

    let i 0
    while [i < 3] [
      let row-i (sentence (matrix:get-row sl i) (matrix:get-row n-sl i))
      matrix:set-row l-matrix i row-i
      set i (i + 1)
    ]
    set i 0
    while [i < 3] [
      let row-i (sentence (matrix:get-row n-sl i) (matrix:get-row sl i))
      matrix:set-row l-matrix (i + 3) row-i
      set i (i + 1)
    ]

    ; Multiply by component G
    set l-matrix (matrix:map [x -> G * x] l-matrix)

    ; Modify the general Stiffness matrix
    let tmp-tam (length tmp)

    set i 0
    ; Iterate over rows of the submatrix
    while [i < tmp-tam] [
      let row ((item i tmp) - 1)
      let j 0
      ; Iterate over columns of the submatrix
      while [j < tmp-tam] [
        let column ((item j tmp) - 1)
        ; Modify the value
        let new-value ((matrix:get S row column) + (matrix:get l-matrix i j))
        set S (matrix:set-and-report S row column new-value)
        set j (j + 1)
      ]
      set i (i + 1)
    ]
  ]

  ; Resolve the system of linear equations
  let load (list (map [x -> 1e3 * x ] (list 1 -10 -10 0 -10 -10 0.5 0 0 0 0 0 0 0 0 0.6 0 0))) ; Load Case 1
  set load (matrix:transpose (matrix:from-row-list load))
  let sub-S (matrix:submatrix S 0 0 18 18)

  let solution (item 0 (matrix:to-row-list (matrix:transpose (matrix:solve sub-S load))))

  ; Update the new values of U with the respect load
  let j 0
  while [j < 6] [
    let i 0
    while [i < 3] [
      let new-value (item ((3 * j) + i) solution)
      set U (matrix:set-and-report U i j new-value)
      set i (i + 1)
    ]
    set j (j + 1)
  ]

  ; Create two submatrix for respective U sub-sets of columns
  let subU-1 matrix:make-constant 3 n-links 0
  let subU-2 matrix:make-constant 3 n-links 0

  let i 0
  while [i < n-links] [
    let index1 ((item i list-1-edges) - 1)
    let index2 ((item i list-2-edges) - 1)
    matrix:set-column subU-1 i (matrix:get-column U index1)
    matrix:set-column subU-2 i (matrix:get-column U index2)
    set i (i + 1)
  ]
  ; Difference of two subU matrices
  let subU-dif (subU-2 matrix:- subU-1)

  let F-product matrix:make-constant 3 n-links 0
  set F-product (matrix:times-element-wise Tj subU-dif)

  let F-sum []
  set i 0
  while [i < n-links] [
    set F-sum (lput (sum (matrix:get-column F-product i)) F-sum)
    set i (i + 1)
  ]

  ; Calculate the Tension/Compression Stress
  set F-sum (map [x -> abs(x) ] F-sum)
  let TS (map [ [x y] -> x / y ] F-sum A)
  set TS (map [ x -> (x / allowable-tension) - 1 ] TS)

  ; Calculate the Joints Displacement
  let JS (matrix:map [x -> abs(x)] (matrix:transpose U))
  set JS (matrix:map [x -> (x / displacement-limit) - 1] JS)

  ; Check how many items are positive (> 0)
  let count-TS (sum (filter [x -> x > 0] TS))
  let count-JS (sum (filter [x -> x > 0] (reduce sentence (matrix:to-row-list JS))))

  let penalization (count-TS + count-JS)
  report penalization
end


; Global pheromone update schema for ACO
to global-pheromone-update
  ; Update trail-matrix with respective evaporation rate
  set trail-matrix (matrix:map [ x -> x * (1 - rho)] trail-matrix)

  ; Add pheromone by each ant
  ask ants [
    let route-cost cost
    ; Add for each virtual path selected an amount of pheromone
    let i 0
    while [i < (length route)] [
      let j (item i route)
      let actual-value (matrix:get trail-matrix i j)
      let new-value (actual-value + (1 / route-cost))
      set trail-matrix (matrix:set-and-report trail-matrix i j new-value)
      set i (i + 1)
    ]
  ]
end

; Global pheromone update schema for Ranked Ant System
to ranked-global-pheromone-update

  ; Add pheromone by elitist ant and a selected number of them (rank)
  ; Ranked the ants by cost and select the top-ranked-number best
  let ants-rank (sort-on [cost] ants)
  set ants-rank (sublist ants-rank 0 (top-ranked-number + 1))

  ; Update trail-matrix with respective evaporation rate
  set trail-matrix (matrix:map [ x -> x * (1 - rho)] trail-matrix)

  let i 0
  while [i < top-ranked-number] [

    let a (item i ants-rank)
    let elitist-route best-route
    let elitist-cost calculate-cost-of-route elitist-route

    ; Pheromone update of each ranked ant
    ask a [
      let route-cost cost
      ; Add for each virtual path selected an amount of pheromone
      let j 0
      while [j < (length route)] [
        let k (item j route)

        ; Elitist influence
        let k' (item j elitist-route)
        let elitist-influence 0
        if (k = k') [ set elitist-influence (1 / elitist-cost) ]

        ; Update following the rank system schema
        let actual-value (matrix:get trail-matrix j k)
        let new-value (actual-value + rho * ((top-ranked-number * elitist-influence) + ((top-ranked-number - i)*(1 / route-cost))))
        set trail-matrix (matrix:set-and-report trail-matrix j k new-value)
        set j (j + 1)
      ]
    ]
    ; Next ranked ant
    set i (i + 1)
  ]
end

; Setup the ants
to setup-ants
  create-ants ants-population-size [

    ; Each ant is randomly assigned to one joint i as the starting
    ; point of its tour (where i = 1, 2, ..., members of the truss)
    let begin one-of joints
    setxyz [xcor] of begin [ycor] of begin [zcor] of begin
    hide-turtle
    pen-down

    ; Set all design variables to zero
    set route n-values (max [group] of links) [0]
    set cost (calculate-cost-of-route route)
    set penalty 0
  ]
end

; Setup the observer position
to set-observer
  orbit-down 75
  orbit-right 48
end

; Setup the constants of 25-Bar Truss Problem
to set-globals
  set lb-cross-section 0.1 ; in²
  set ub-cross-section 3.4 ; in²
  set increment-section 0.1 ; in²
  set iteration 0
end

; Setup the joints (nodes)
to setup-joints

  set-default-shape joints "circle"

  ; Coordinates for 35-Bar Truss (inches)
  create-joints 1 [set n 1 set color white setxyz -3.75 0 10]
  create-joints 1 [set n 2 set color white setxyz 3.75 0 10]
  create-joints 1 [set n 3 set color white setxyz -3.75 3.75 0]
  create-joints 1 [set n 4 set color white setxyz 3.75 3.75 0]
  create-joints 1 [set n 5 set color white setxyz 3.75 -3.75 0]
  create-joints 1 [set n 6 set color white setxyz -3.75 -3.75 0]
  create-joints 1 [set n 7 set color white setxyz -10 10 -10]
  create-joints 1 [set n 8 set color white setxyz 10 10 -10]
  create-joints 1 [set n 9 set color white setxyz 10 -10 -10]
  create-joints 1 [set n 10 set color white setxyz -10 -10 -10]

  ask patches with [pzcor <= -11] [set pcolor grey]

end

; Initialize the matrix vector with minimun pheromone value
to setup-matrix-vector

  ; Initialize the trail-matrix of pheromones with W-min
  let min-weight 0

  let edges [self] of links
  foreach edges [ edge ->
    set min-weight min-weight + (density * 10 * ([link-length] of edge) * lb-cross-section)
  ]

  ; Trail matrix (ng x nm)
  let ng max [group] of links
  let nm (precision (1 + ((ub-cross-section - lb-cross-section) / increment-section)) 0)

  ;Initialize values of trail-matrix
  set trail-matrix (matrix:make-constant ng nm (1 / min-weight)) ; Aprox 0.03

  ; Generate vector of different cross-sections areas
  set sections-vector []

  let i 0
  while [i < nm] [
    set sections-vector (lput (precision ((i * increment-section) + lb-cross-section) 1) sections-vector)
    set i (i + 1)
  ]
end


; Visualization of the best cross-sections areas
to update-display

  ; For each design variable, change the sections
  let i 0
  while [i < (length best-route)]
  [
    let j (item i best-route)
    let cross-section (item j sections-vector)

    ask links with [group = i + 1]
    [
      set color [0 255 0]
      ; Pause to apreciate the change of cross-section area
      wait 0.05

      ; Set color according to the thickness
      set thickness cross-section / (ub-cross-section - lb-cross-section)
      let new-color 1 - ((cross-section - lb-cross-section) / (ub-cross-section - lb-cross-section))
      set new-color (new-color * (99.9 - 91) + 91)
      set color new-color
    ]
    set i (i + 1)
  ]
end

; Get the list with cross-sections areas for a given solution
to-report get-sections [given-route]
let i 0
let sections []
while [i < length given-route] [
   let s (item (item i given-route) sections-vector)
   set sections lput s sections
   set i i + 1
]
report sections
end


to setup-edges

  set-default-shape links "truss"

  ; Element Information for 25-Bar Truss Topology

  ask joints with [n = 1] [
    create-links-to (joints with [n = 2]) [
      set id 1
      set group 1
    ]
    create-links-to (joints with [n = 4]) [
      set id 2
      set group 2
    ]
    create-links-to (joints with [n = 5]) [
      set id 4
      set group 2
    ]
    create-links-to (joints with [n = 3]) [
      set id 8
      set group 3
    ]
    create-links-to (joints with [n = 6]) [
      set id 9
      set group 3
    ]
  ]

  ask joints with [n = 2] [
    create-links-to (joints with [n = 3]) [
      set id 3
      set group 2
    ]
    create-links-to (joints with [n = 6]) [
      set id 5
      set group 2
    ]
    create-links-to (joints with [n = 4]) [
      set id 6
      set group 3
    ]
    create-links-to (joints with [n = 5]) [
      set id 7
      set group 3
    ]
  ]

  ask joints with [n = 3] [
    create-links-to (joints with [n = 4]) [
      set id 12
      set group 5
    ]
    create-links-to (joints with [n = 10]) [
      set id 14
      set group 6
    ]
    create-links-to (joints with [n = 8]) [
      set id 19
      set group 7
    ]
    create-links-to (joints with [n = 7]) [
      set id 23
      set group 8
    ]
  ]

  ask joints with [n = 4] [
    create-links-to (joints with [n = 9]) [
      set id 16
      set group 6
    ]
    create-links-to (joints with [n = 7]) [
      set id 18
      set group 7
    ]
    create-links-to (joints with [n = 8]) [
      set id 24
      set group 8
    ]
  ]

  ask joints with [n = 5] [
    create-links-to (joints with [n = 4]) [
      set id 11
      set group 4
    ]
    create-links-to (joints with [n = 8]) [
      set id 17
      set group 6
    ]
    create-links-to (joints with [n = 10]) [
      set id 20
      set group 7
    ]
    create-links-to (joints with [n = 9]) [
      set id 25
      set group 8
    ]
  ]

  ask joints with [n = 6] [
    create-links-to (joints with [n = 3]) [
      set id 10
      set group 4
    ]
    create-links-to (joints with [n = 5]) [
      set id 13
      set group 5
    ]
    create-links-to (joints with [n = 7]) [
      set id 15
      set group 6
    ]
    create-links-to (joints with [n = 9]) [
      set id 21
      set group 7
    ]
  ]

  ask joints with [n = 10] [
    create-links-to (joints with [n = 6]) [
      set id 22
      set group 8
    ]
  ]

  ; Initialize lenght of each truss member
  ask links [ set edge-cost (link-length * 10) ]

end
@#$#@#$#@
GRAPHICS-WINDOW
0
0
411
412
-1
-1
13.0
1
10
1
1
1
0
1
1
1
-15
15
-15
15
-15
15
1
0
1
ticks
30.0

BUTTON
65
62
149
104
setup
setup
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1

SLIDER
219
58
422
91
ants-population-size
ants-population-size
0
100
20.0
1
1
NIL
HORIZONTAL

BUTTON
65
111
149
155
go
go
T
1
T
OBSERVER
NIL
NIL
NIL
NIL
1

SLIDER
448
57
620
90
alpha
alpha
0
20
1.0
1
1
NIL
HORIZONTAL

SLIDER
448
96
620
129
beta
beta
1
20
2.0
1
1
NIL
HORIZONTAL

SLIDER
449
138
621
171
rho
rho
0
0.99
0.55
0.01
1
NIL
HORIZONTAL

BUTTON
66
161
149
203
step
go
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1

SLIDER
449
192
621
225
epsilon
epsilon
0
1
0.15
0.01
1
NIL
HORIZONTAL

MONITOR
191
523
281
568
Best Weight
calculate-cost-of-route best-route
3
1
11

MONITOR
329
524
545
569
CrossSections
get-sections best-route
2
1
11

SWITCH
221
145
423
178
ranked-ant-system
ranked-ant-system
1
1
-1000

SLIDER
221
188
424
221
top-ranked-number
top-ranked-number
1
ants-population-size - 1
15.0
1
1
NIL
HORIZONTAL

TEXTBOX
647
106
797
124
Visibility intensity
12
0.0
1

TEXTBOX
643
67
793
85
Pheromone intensity
12
0.0
1

TEXTBOX
645
140
795
170
Global pheromone evaporation rate
12
0.0
1

TEXTBOX
647
192
797
222
Local pheromone evaporation rate
12
0.0
1

TEXTBOX
319
15
584
45
Ant Colony System Parameters
16
0.0
1

TEXTBOX
341
606
540
628
Truss Design Parameters
16
0.0
1

SLIDER
102
657
274
690
density
density
0.01
20
0.1
0.01
1
NIL
HORIZONTAL

TEXTBOX
294
664
444
682
Material density (lb/in³)
12
0.0
1

SLIDER
102
702
274
735
elasticity
elasticity
1000000
100000000
1.0E7
1000000
1
NIL
HORIZONTAL

TEXTBOX
296
709
460
727
Modulus of elasticity (ksi)
12
0.0
1

SLIDER
492
659
665
692
displacement-limit
displacement-limit
0.05
1
0.35
0.05
1
NIL
HORIZONTAL

TEXTBOX
680
667
830
697
Max. joint displacement limitation (in)
12
0.0
1

TEXTBOX
682
718
858
736
Max tension suported (ksi)
12
0.0
1

SLIDER
492
704
666
737
allowable-tension
allowable-tension
0
100000
40000.0
500
1
NIL
HORIZONTAL

PLOT
53
259
422
504
Solutions
Iteration
Weight
0.0
10.0
0.0
10.0
true
true
"" ""
PENS
"best" 1.0 0 -2674135 true "" "if ticks > 1 [\nplot calculate-cost-of-route best-route\n]"
"avg" 1.0 0 -10899396 true "" "if ticks > 1 [\nlet sum-cost 0\nask ants [set sum-cost (sum-cost + calculate-cost-of-route route)]\nplot (sum-cost / ants-population-size)\n]"
"worst" 1.0 0 -13345367 true "" "if ticks > 1 [\nlet max-route ([route] of (max-one-of ants [cost]))\nplot calculate-cost-of-route max-route\n]"

PLOT
439
260
789
505
Penalty
Iteration
Penalty
0.0
10.0
0.0
10.0
true
true
"" ""
PENS
"best" 1.0 0 -2674135 true "" "if ticks > 1 [\nplot penalty-function best-route\n]"
"avg" 1.0 0 -10899396 true "" "if ticks > 1 [\nlet sum-penalty 0\nask ants [set sum-penalty (sum-penalty + penalty-function route)]\nplot (sum-penalty / ants-population-size)\n]"
"worst" 1.0 0 -13345367 true "" "if ticks > 1 [\nlet max-penalty ([route] of (max-one-of ants [cost]))\nplot penalty-function max-penalty\n]"

SLIDER
220
101
423
134
max-iterations
max-iterations
10
1000
500.0
1
1
NIL
HORIZONTAL

MONITOR
585
523
667
568
N. Iteration
iteration
0
1
11

@#$#@#$#@
## INTRODUCTION TO SPACE TRUSS DESIGN

Structural optimization is one of the most active areas of structural engineering in the last 10 years. The problems that includes it are characterized by having a great number of design variables with great number of design constraints, also contains in a large search space that requires a reasonable time to perform an optimun design. 

In truss design, the goal is to minimize the cost (it's directly related with the weight) of a given structure while satisfying design constraints such as nodal deflection limits, allowable tension and compression stress.

This weight depends on the cross-sectional areas of each member in the structure. So the objective will be to select the cross-sectional area of each member so that the weight of the structure is minimized and all constraints are met.

The truss optimization problem can be expressed as: 


![function](images/TrussDesignWeight.png)

where W is the weight of the truss; nm: the number of members of the truss, gamma: the material density of member i; Li: length of member i; and Ai: the cross-sectional area of member i between Al and Au  (lower bound and upper bound).

The parameters sigma and delta represent the stress and deflection of the truss, respectively, subjected to upper and lower boundaries [2].

Many of classical optimization methods require gradient information and the final design depends on the initially selected points. The complexity increases as the design variables of a structure so many differents methods based on heuristics (including genetic algorithms, swarm intelligence, etc...) has been developed to resolve this problems. 

Ant Colony Optimization owns similar advantages to evolutionary algorithms because is a multi-agent and randomized search method that does not need the given function to be differentiable. 

## WHAT IS IT?


In this model, we propose an ACO implementation for the 25-Bar Truss Design Problem. The next image shows the topology of a 25-bar space truss structure (topology was proposed in 1984 by Adeli and Kamal [1]). 

![25-bar](images/25-BarSpatialTruss.png)
(Image from Kaveh,A. and Azar,B. [3])

There are a single load case and many information is given [2]:

- The modulus of elasticity of the material is 10⁷ psi 
- The mass density of the material is 0.1 lb/in.³.
- The allowable stress for each truss member is +/- 40 ksi
- The allowable displacement limit for each joint is 0.35 in. in the x,y and z axis.

The range of discrete cross-sectional areas for the members of the structure is from 0.1 to 3.4 in² with a 0.1 in.² increment. 

The 25 members are organized into 8 groups where all members of the same group share the same cross-sectional area. The next table show the element information [2]:

![25-bar-groups](images/25-BarGroups.png)

In this 25-bar truss design, a single load case is applied to the structure, as listed in the next table [2]:

![load-case](images/25-BarLoadCase.png)

(Note: 1 kip = 4.45kN)


The operators of the ACO algorithm for the design of the space trusses have been implemented following those described in the article by Camp C. and Bichon J. [2].

The penalty scheme is also described here but the calculation of stress and displacement is more clearly explained by Adeli, H. and Kamal, O. [1] and [4].

## HOW IT WORKS

(what rules the agents use to create the overall behavior of the model)

## HOW TO USE IT

Press the SETUP button to create the topology visualization, construct a random solution and initialize all agents (joints, ants) and links (edges). 

Press the GO button to start the simulation of the model.

Press the STEP button to execute one iteration (tick) of the model. 

=== Parameters ===

The ANTS-POPULATION-SIZE slider controls the number of ants of our colony. 

The MAX-ITERATIONS slider controls how many iterations you want the ACO be running. 

The RANKED-ANT-SYSTEM switch allows to enable the Rank Ant System variant of Ant Colony Optimization algorithm. In this variant, the solutions are ranked by their fitness and each ant deposists an amount of pheromone proportionaly to the goodness of their solution (truss design). 

The TOP-RANKED-NUMBER slider controls how many ants we want to be ranked.

The ALPHA slider controls the influcence for the ants of the pheromone on edges.

The BETA slider controls the influence of the visibility heuristic (how greedy the ants are).

The RHO slider controls the value of the global evaporation rate parameter.

The EPSILON slider controls the value of the local evaporation rate parameter.

## THINGS TO NOTICE

In the model, there are two graphics, one of them show how the Ant Colony Optimization algorithm is performing the weight of the truss design each iteration (for the best founded solution, the worst and an average of all ants solutions).
The other one show the penalty of these solutions.

Notice that a lower weighted solution (for example the worst solution) corresponds to a higher penalty. The best founded solution always have to finish having zero penalty because it is a requirement of the problem that all constraints are met.

If we try to run the Rank Ant System version (based on [5]), we will notice how the population of ants tends to converge quickly to the best founded solution (the elitist ant) as the variable 'top-ranked-number' decreases.
The exploration is reduced here but we need fewer iterations to converge. Nevertheless, the solutions reached by both versions are similar.

## THINGS TO TRY

We can try to change the parameters of the Truss Design Problem whicg are: maximum allowable tension suported, displacement limit, density and modulus of elasticity. We will see different truss construction results depending of how much tension should the truss suports or displacement is allowed to the joints. 

We can also try to change the classics ACO parameters (alpha, beta, rho and size of population) to notice how they influence the performing of the algorithm.

## EXTENDING THE MODEL

To get accurated results, we can do a **hybridization** algorithm that uses ACO and **Local Search** to explode the solutions and do them better. 

We can also include a mechanism that changes the value of local and global pheromone evaporation as time goes, and allow us to reduce the exploration when good solutions are reached. 

If we want to reduce the searh space we can implement a mechanishm called 'SubOptimization Mechanism' (SOM), and if we want to reduce the number of iterations without decrease the probability to find the optimum solution we can use the 'Scatter Memory Mechanism' (SMM). 


## NETLOGO FEATURES

We use the matrix extension to facilitate matrix operations (like data retrieval, data manipulation, addition, subtraction, etc...) and to solve linear equations easily:

```
extensions [matrix]

; For example we can calculate the transpose of a given matrix
let matrix-t (matrix:transpose matrix)

; Addition of both matrixs
let result (matrix:plus matrix-t matrix) 

; Get all elements of a matrix into a list
set list (matrix:from-row-list matrix)
```


## CREDITS

This model is an implementation of Ant Colony System for 25-Bar Truss Design Problem.

It's a part of end-of-degree project -TFG- by Jose A. Martín Melguizo supervised by Rocio Romero Zaliz at the University Of Granada (UGR)

Granada, 17 May, 2021

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 License.  To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/4.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

![CC BY-NC-SA 4.0](https://licensebuttons.net/l/by-nc-sa/3.0/88x31.png)



## REFERENCES

[1] Adeli, H., Kamal, O. “Efficient optimization of space trusses. Computers and Structures“ 1986;24(3):501–511.

[2] Camp C. and Bichon J. “Design of space trusses using ant colony optimization,” Journal of Structural Engineering, ASCE 2004; 130(5):741–751.

[3] Kaveh, A., Azar, B. F., & Talatahari, S. (2008). Ant Colony Optimization for Design of Space Trusses. International Journal of Space Structures, 23(3), 167–181.

[4] Chapter 3a – Development Of Truss Equations pg 47-53
http://www.ce.memphis.edu/7117/notes/presentations/chapter_03a.pdf

[5] Hasançebi O, Çarbaş S. ANT COLONY SEARCH METHOD IN PRACTICAL STRUCTURAL OPTIMIZATION. International Journal of Optimization in Civil Engineering. 2011; 1 (1) :91-105
@#$#@#$#@
default
true
0
Polygon -7500403 true true 150 5 40 250 150 205 260 250

airplane
true
0
Polygon -7500403 true true 150 0 135 15 120 60 120 105 15 165 15 195 120 180 135 240 105 270 120 285 150 270 180 285 210 270 165 240 180 180 285 195 285 165 180 105 180 60 165 15

ant
true
0
Polygon -7500403 true true 136 61 129 46 144 30 119 45 124 60 114 82 97 37 132 10 93 36 111 84 127 105 172 105 189 84 208 35 171 11 202 35 204 37 186 82 177 60 180 44 159 32 170 44 165 60
Polygon -7500403 true true 150 95 135 103 139 117 125 149 137 180 135 196 150 204 166 195 161 180 174 150 158 116 164 102
Polygon -7500403 true true 149 186 128 197 114 232 134 270 149 282 166 270 185 232 171 195 149 186
Polygon -7500403 true true 225 66 230 107 159 122 161 127 234 111 236 106
Polygon -7500403 true true 78 58 99 116 139 123 137 128 95 119
Polygon -7500403 true true 48 103 90 147 129 147 130 151 86 151
Polygon -7500403 true true 65 224 92 171 134 160 135 164 95 175
Polygon -7500403 true true 235 222 210 170 163 162 161 166 208 174
Polygon -7500403 true true 249 107 211 147 168 147 168 150 213 150

arrow
true
0
Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150

box
false
0
Polygon -7500403 true true 150 285 285 225 285 75 150 135
Polygon -7500403 true true 150 135 15 75 150 15 285 75
Polygon -7500403 true true 15 75 15 225 150 285 150 135
Line -16777216 false 150 285 150 135
Line -16777216 false 150 135 15 75
Line -16777216 false 150 135 285 75

bug
true
0
Circle -7500403 true true 96 182 108
Circle -7500403 true true 110 127 80
Circle -7500403 true true 110 75 80
Line -7500403 true 150 100 80 30
Line -7500403 true 150 100 220 30

butterfly
true
0
Polygon -7500403 true true 150 165 209 199 225 225 225 255 195 270 165 255 150 240
Polygon -7500403 true true 150 165 89 198 75 225 75 255 105 270 135 255 150 240
Polygon -7500403 true true 139 148 100 105 55 90 25 90 10 105 10 135 25 180 40 195 85 194 139 163
Polygon -7500403 true true 162 150 200 105 245 90 275 90 290 105 290 135 275 180 260 195 215 195 162 165
Polygon -16777216 true false 150 255 135 225 120 150 135 120 150 105 165 120 180 150 165 225
Circle -16777216 true false 135 90 30
Line -16777216 false 150 105 195 60
Line -16777216 false 150 105 105 60

car
false
0
Polygon -7500403 true true 300 180 279 164 261 144 240 135 226 132 213 106 203 84 185 63 159 50 135 50 75 60 0 150 0 165 0 225 300 225 300 180
Circle -16777216 true false 180 180 90
Circle -16777216 true false 30 180 90
Polygon -16777216 true false 162 80 132 78 134 135 209 135 194 105 189 96 180 89
Circle -7500403 true true 47 195 58
Circle -7500403 true true 195 195 58

circle
false
0
Circle -7500403 true true 0 0 300

circle 2
false
0
Circle -7500403 true true 0 0 300
Circle -16777216 true false 30 30 240

cow
false
0
Polygon -7500403 true true 200 193 197 249 179 249 177 196 166 187 140 189 93 191 78 179 72 211 49 209 48 181 37 149 25 120 25 89 45 72 103 84 179 75 198 76 252 64 272 81 293 103 285 121 255 121 242 118 224 167
Polygon -7500403 true true 73 210 86 251 62 249 48 208
Polygon -7500403 true true 25 114 16 195 9 204 23 213 25 200 39 123

cylinder
false
0
Circle -7500403 true true 0 0 300

dot
false
0
Circle -7500403 true true 90 90 120

face happy
false
0
Circle -7500403 true true 8 8 285
Circle -16777216 true false 60 75 60
Circle -16777216 true false 180 75 60
Polygon -16777216 true false 150 255 90 239 62 213 47 191 67 179 90 203 109 218 150 225 192 218 210 203 227 181 251 194 236 217 212 240

face neutral
false
0
Circle -7500403 true true 8 7 285
Circle -16777216 true false 60 75 60
Circle -16777216 true false 180 75 60
Rectangle -16777216 true false 60 195 240 225

face sad
false
0
Circle -7500403 true true 8 8 285
Circle -16777216 true false 60 75 60
Circle -16777216 true false 180 75 60
Polygon -16777216 true false 150 168 90 184 62 210 47 232 67 244 90 220 109 205 150 198 192 205 210 220 227 242 251 229 236 206 212 183

fish
false
0
Polygon -1 true false 44 131 21 87 15 86 0 120 15 150 0 180 13 214 20 212 45 166
Polygon -1 true false 135 195 119 235 95 218 76 210 46 204 60 165
Polygon -1 true false 75 45 83 77 71 103 86 114 166 78 135 60
Polygon -7500403 true true 30 136 151 77 226 81 280 119 292 146 292 160 287 170 270 195 195 210 151 212 30 166
Circle -16777216 true false 215 106 30

flag
false
0
Rectangle -7500403 true true 60 15 75 300
Polygon -7500403 true true 90 150 270 90 90 30
Line -7500403 true 75 135 90 135
Line -7500403 true 75 45 90 45

flower
false
0
Polygon -10899396 true false 135 120 165 165 180 210 180 240 150 300 165 300 195 240 195 195 165 135
Circle -7500403 true true 85 132 38
Circle -7500403 true true 130 147 38
Circle -7500403 true true 192 85 38
Circle -7500403 true true 85 40 38
Circle -7500403 true true 177 40 38
Circle -7500403 true true 177 132 38
Circle -7500403 true true 70 85 38
Circle -7500403 true true 130 25 38
Circle -7500403 true true 96 51 108
Circle -16777216 true false 113 68 74
Polygon -10899396 true false 189 233 219 188 249 173 279 188 234 218
Polygon -10899396 true false 180 255 150 210 105 210 75 240 135 240

house
false
0
Rectangle -7500403 true true 45 120 255 285
Rectangle -16777216 true false 120 210 180 285
Polygon -7500403 true true 15 120 150 15 285 120
Line -16777216 false 30 120 270 120

leaf
false
0
Polygon -7500403 true true 150 210 135 195 120 210 60 210 30 195 60 180 60 165 15 135 30 120 15 105 40 104 45 90 60 90 90 105 105 120 120 120 105 60 120 60 135 30 150 15 165 30 180 60 195 60 180 120 195 120 210 105 240 90 255 90 263 104 285 105 270 120 285 135 240 165 240 180 270 195 240 210 180 210 165 195
Polygon -7500403 true true 135 195 135 240 120 255 105 255 105 285 135 285 165 240 165 195

line
true
0
Line -7500403 true 150 0 150 300

line half
true
0
Line -7500403 true 150 0 150 150

pentagon
false
0
Polygon -7500403 true true 150 15 15 120 60 285 240 285 285 120

person
false
0
Circle -7500403 true true 110 5 80
Polygon -7500403 true true 105 90 120 195 90 285 105 300 135 300 150 225 165 300 195 300 210 285 180 195 195 90
Rectangle -7500403 true true 127 79 172 94
Polygon -7500403 true true 195 90 240 150 225 180 165 105
Polygon -7500403 true true 105 90 60 150 75 180 135 105

plant
false
0
Rectangle -7500403 true true 135 90 165 300
Polygon -7500403 true true 135 255 90 210 45 195 75 255 135 285
Polygon -7500403 true true 165 255 210 210 255 195 225 255 165 285
Polygon -7500403 true true 135 180 90 135 45 120 75 180 135 210
Polygon -7500403 true true 165 180 165 210 225 180 255 120 210 135
Polygon -7500403 true true 135 105 90 60 45 45 75 105 135 135
Polygon -7500403 true true 165 105 165 135 225 105 255 45 210 60
Polygon -7500403 true true 135 90 120 45 150 15 180 45 165 90

sheep
false
15
Circle -1 true true 203 65 88
Circle -1 true true 70 65 162
Circle -1 true true 150 105 120
Polygon -7500403 true false 218 120 240 165 255 165 278 120
Circle -7500403 true false 214 72 67
Rectangle -1 true true 164 223 179 298
Polygon -1 true true 45 285 30 285 30 240 15 195 45 210
Circle -1 true true 3 83 150
Rectangle -1 true true 65 221 80 296
Polygon -1 true true 195 285 210 285 210 240 240 210 195 210
Polygon -7500403 true false 276 85 285 105 302 99 294 83
Polygon -7500403 true false 219 85 210 105 193 99 201 83

square
false
0
Rectangle -7500403 true true 30 30 270 270

square 2
false
0
Rectangle -7500403 true true 30 30 270 270
Rectangle -16777216 true false 60 60 240 240

star
false
0
Polygon -7500403 true true 151 1 185 108 298 108 207 175 242 282 151 216 59 282 94 175 3 108 116 108

target
false
0
Circle -7500403 true true 0 0 300
Circle -16777216 true false 30 30 240
Circle -7500403 true true 60 60 180
Circle -16777216 true false 90 90 120
Circle -7500403 true true 120 120 60

tree
false
0
Circle -7500403 true true 118 3 94
Rectangle -6459832 true false 120 195 180 300
Circle -7500403 true true 65 21 108
Circle -7500403 true true 116 41 127
Circle -7500403 true true 45 90 120
Circle -7500403 true true 104 74 152

triangle
false
0
Polygon -7500403 true true 150 30 15 255 285 255

triangle 2
false
0
Polygon -7500403 true true 150 30 15 255 285 255
Polygon -16777216 true false 151 99 225 223 75 224

truck
false
0
Rectangle -7500403 true true 4 45 195 187
Polygon -7500403 true true 296 193 296 150 259 134 244 104 208 104 207 194
Rectangle -1 true false 195 60 195 105
Polygon -16777216 true false 238 112 252 141 219 141 218 112
Circle -16777216 true false 234 174 42
Rectangle -7500403 true true 181 185 214 194
Circle -16777216 true false 144 174 42
Circle -16777216 true false 24 174 42
Circle -7500403 false true 24 174 42
Circle -7500403 false true 144 174 42
Circle -7500403 false true 234 174 42

turtle
true
0
Polygon -10899396 true false 215 204 240 233 246 254 228 266 215 252 193 210
Polygon -10899396 true false 195 90 225 75 245 75 260 89 269 108 261 124 240 105 225 105 210 105
Polygon -10899396 true false 105 90 75 75 55 75 40 89 31 108 39 124 60 105 75 105 90 105
Polygon -10899396 true false 132 85 134 64 107 51 108 17 150 2 192 18 192 52 169 65 172 87
Polygon -10899396 true false 85 204 60 233 54 254 72 266 85 252 107 210
Polygon -7500403 true true 119 75 179 75 209 101 224 135 220 225 175 261 128 261 81 224 74 135 88 99

wheel
false
0
Circle -7500403 true true 3 3 294
Circle -16777216 true false 30 30 240
Line -7500403 true 150 285 150 15
Line -7500403 true 15 150 285 150
Circle -7500403 true true 120 120 60
Line -7500403 true 216 40 79 269
Line -7500403 true 40 84 269 221
Line -7500403 true 40 216 269 79
Line -7500403 true 84 40 221 269

wolf
false
0
Polygon -16777216 true false 253 133 245 131 245 133
Polygon -7500403 true true 2 194 13 197 30 191 38 193 38 205 20 226 20 257 27 265 38 266 40 260 31 253 31 230 60 206 68 198 75 209 66 228 65 243 82 261 84 268 100 267 103 261 77 239 79 231 100 207 98 196 119 201 143 202 160 195 166 210 172 213 173 238 167 251 160 248 154 265 169 264 178 247 186 240 198 260 200 271 217 271 219 262 207 258 195 230 192 198 210 184 227 164 242 144 259 145 284 151 277 141 293 140 299 134 297 127 273 119 270 105
Polygon -7500403 true true -1 195 14 180 36 166 40 153 53 140 82 131 134 133 159 126 188 115 227 108 236 102 238 98 268 86 269 92 281 87 269 103 269 113

x
false
0
Polygon -7500403 true true 270 75 225 30 30 225 75 270
Polygon -7500403 true true 30 75 75 30 270 225 225 270
@#$#@#$#@
NetLogo 3D 6.2.0
@#$#@#$#@
need-to-manually-make-preview-for-this-model
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
default
0.0
-0.2 0 0.0 1.0
0.0 1 1.0 0.0
0.2 0 0.0 1.0
link direction
true
0
Line -7500403 true 150 150 90 180
Line -7500403 true 150 150 210 180

truss
0.0
-0.2 0 0.0 1.0
0.0 1 1.0 0.0
0.2 0 0.0 1.0
link direction
true
0
@#$#@#$#@
0
@#$#@#$#@
