Codes

14
TSP Sub tsp() Dim mincol() As Integer, minval() As Single, n As Integer Dim costmat() As String Set tspmat = Range("tsprange") n = tspmat.Rows.Count ReDim mincol(n), minval(n) ReDim costmat(n, n) Dim tcost As Single Dim i As Integer, j As Integer, row As Integer For i = 1 To n mincol(i) = 0 minval(i) = 9999 costmat(i, 1) = 9999 For j = 2 To n costmat(i, j) = tspmat(i, j) Next j Next i tcost = 0 tspmat.Cells(n + 1, 0).Value = 1 'display the city in position-1 tspmat.Cells(n + 1, n).Value = 1 row = 1 For i = 1 To n - 1 For j = 2 To n If costmat(row, j) < minval(row) Then minval(row) = costmat(row, j) mincol(row) = j End If Next j tcost = tcost + minval(row) For k = 1 To n costmat(k, mincol(row)) = 9999

description

mn k

Transcript of Codes

Page 1: Codes

TSP

Sub tsp()Dim mincol() As Integer, minval() As Single, n As IntegerDim costmat() As String

Set tspmat = Range("tsprange")n = tspmat.Rows.CountReDim mincol(n), minval(n)

ReDim costmat(n, n)Dim tcost As Single

Dim i As Integer, j As Integer, row As Integer

For i = 1 To n mincol(i) = 0 minval(i) = 9999 costmat(i, 1) = 9999 For j = 2 To n costmat(i, j) = tspmat(i, j) Next jNext i

tcost = 0tspmat.Cells(n + 1, 0).Value = 1 'display the city in position-1tspmat.Cells(n + 1, n).Value = 1

row = 1For i = 1 To n - 1 For j = 2 To n If costmat(row, j) < minval(row) Then minval(row) = costmat(row, j) mincol(row) = j End If Next j tcost = tcost + minval(row) For k = 1 To n costmat(k, mincol(row)) = 9999 Next k tspmat.Cells(n + 1, i).Value = mincol(row) row = mincol(row)Next i

tcost = tcost + tspmat(row, 1)tspmat.Cells(n + 2, 1).Value = tcost

Page 2: Codes

End SubAssign 14: Vendor SelectionSub run()Set movies = Range("movie")

For i = 1 To 31 For j = 1 To 4 If movies(i, j) < Range("L14").Value Or movies(i, j) > Range("L15").Value Then movies(i, j) = "" End If Next jNext i

End Sub

Sub find()Set Data = Range("data")'Dim output As StringDim output(31, 7)Dim test_output As RangeRange("main_output") = ""Set test_output = Range("main_output")Dim x, y As IntegerDim Is_empty(31)

For i = 1 To 31 output(i, 1) = Data(i, 1) output(i, 2) = Data(i, 2) output(i, 3) = Data(i, 3) For j = 4 To 7 If Data(i, j) < Range("L14").Value Or Data(i, j) > Range("L15").Value Then output(i, j) = "" Else output(i, j) = Data(i, j) Is_empty(i) = 1 End If Next jNext i

x = 0For i = 1 To 31 If (Is_empty(i) = 1) Then x = x + 1 For j = 1 To 7 test_output(x, j).Value = output(i, j) Next j End If

Page 3: Codes

Next iEnd Sub

Individual Assign : Vendor SelectionOption Base 1Option ExplicitFunction Fiber_cost(cord As Range, cost As Integer)' @author : Shyam Shroff' Calculates the minimum spanning tree connecting all the selected basses using Prim's Algorithm' The weight of each edge in the graph is the length of the edge

' Input : Range (vertical) containing coordinates of bases' Input : Cost of fibre('000 Rs/KM)' Output: Edges that comprise the <ST' Total cost of fibre along minimum spanning tree ('000 Rs/KM)'Uses other user defined function remove_element

Dim dist() As Double ' 2 D array of distances between 2 basesDim b_spaned() As Integer ' array of base already spannedDim b_remaining() As Integer ' array of bases yet to be spannedDim edges() As String ' array of edges selected for MSTDim num_b_spanned As Integer ' counter of number of bases spannedDim min_edge, tmp_edge As StringDim w_min_edge As Double ' weight of min edgeDim new_b As IntegerDim Total_cost As DoubleDim ans As BooleanDim b As Integer ' number of basesDim i, j As Integer ' iterators

b = cord.Rows.Count ' number of bases

ReDim dist(1 To b, 1 To b)ReDim b_spanned(1 To b)ReDim b_remaining(1 To b)ReDim edges(1 To b) As String

'InitializationTotal_cost = 0num_b_spanned = 0For i = 1 To b b_spanned(i) = 0 b_remaining(i) = iNext i

'Populate 2-dim array of distances between bases

Page 4: Codes

For i = 1 To b For j = 1 To b dist(i, j) = Sqr((cord(i, 1) - cord(j, 1)) ^ 2 + (cord(i, 2) - cord(j, 2)) ^ 2) Next jNext i ' Start Minimum Spanning Tree Calculation

' Assign any one base to b_spannedb_spanned(1) = 1

'Remove the base from b_remainingans = remove_element(b_remaining, b, 1)

' Increment counter of number of bases spannednum_b_spanned = 1

Do While (num_b_spanned < b) ' Loop till all bases are spanned w_min_edge = 99999 ' 99999 to denote infinity ' Find min edge from a base in b_spanned to base in b_remaining For i = 1 To num_b_spanned ' for each base in b_spanned For j = 1 To b - num_b_spanned ' for each base in b_remaining tmp_edge = (b_spanned(i) & "-") & b_remaining(j) ' create a string denoting the edge If ((dist(b_spanned(i), b_remaining(j)) < w_min_edge)) Then min_edge = tmp_edge new_b = b_remaining(j) w_min_edge = dist(b_spanned(i), new_b) End If Next j Next i 'Add new base to b_spanned num_b_spanned = num_b_spanned + 1 b_spanned(num_b_spanned) = new_b 'Add new edge to edges edges(num_b_spanned - 1) = min_edge 'remove base from b_remaining ans = remove_element(b_remaining, b, new_b) 'Add to cost Total_cost = Total_cost + w_min_edge * costLoop

'Add total cost to the end of the array of edges

Page 5: Codes

edges(b) = Format(Total_cost, "#######.##")

Fiber_cost = edges

End Function

Function remove_element(arr As Variant, size As Integer, x As Integer) As Boolean' Removes element x from array arr, and moves other elements forward' size denotes the size of the array arr' Returns true if element was found and removed and false otherwise

Dim i, found

found = 0

For i = 1 To size If arr(i) = x Then found = 1 End If If found = 1 Then If (i = size) Then arr(i) = 0 Exit For End If arr(i) = arr(i + 1) End IfNext iIf found = 1 Then remove_element = TrueEnd IfEnd Function

Magic SquareOption ExplicitOption Base 1Function MgSq()'------------------------------------------------------------------------' @@author: shyam shroff' Array function that outputs an odd order Magic Sqare' in the highlighted range from which the funciton is called' The highlighted area should have equal number of rows and columns and' the number of rows and columns should be odd' No error checking is done' M .............. Matrix to be filled in; set to range "Magic Sqare"' r .............. number of rows = number of columns in the magic sqare' n .............. total number of elements in the magic square = r*r' i & j .......... the row and columns number for the element to be filled in' ni & nj ........ possible row and column for next element to be filled in

Page 6: Codes

' k .............. the next vallue to be filled in'------------------------------------------------------------------------Dim r As Integerr = Selection.Rows.CountDim M() As IntegerReDim M(1 To r, 1 To r) As IntegerDim n As Integer, k As IntegerDim i As Integer, j As Integer, ni As Integer, nj As Integer

For i = 1 To r For j = 1 To r M(i, j) = 0 ' Initialize M Next jNext i

n = r * ri = 1j = (r + 1) / 2' Put 1 in the topmost row, middle columnM(i, j) = 1For k = 2 To n

ni = (i - 1) If ni < 1 Then ' if ni is less than first row, move to the last row ni = r End If nj = (j - 1) If nj < 1 Then ' if nj is less than first column, move to the last column nj = r End If If M(ni, nj) = 0 Then ' if the cell selected is empty, this is our next target cell i = ni j = nj Else ' else move to the cell immediate below of the current cell i = i + 1 End If M(i, j) = k ' put the value in the selected cell and move on to find next cellNext k

MgSq = M ' assigning the value to the function

End Function

Sub MagicSum()

Page 7: Codes

'------------------------------------------------------------------------' Given a highlighted range.' If the range is not sqaure,' to pring the message "Non Square" just above the range (one row above, in the first column),' and quit' Assuming the entries in the range represent a magic square,' to compute CSum, what the sum of each row, column or diagonal should be;' to compute the actual sum of each row, column and diagonal' and to print in the designated cells alongside the range either of the following:' 1. the sum if it is equal to CSum or' 2. the sum preceeded by ** if the sum is not equal to CSum' The above are printed in the following locations:' 1. For each row, in the column following the last column of the range' 2. For each column, in the row following the last row of the range' 3. For the fwd diagonal, in the cell in the row preceding the firs row, and in the column' following the last column' 4. For the bckwrd diagonal, in the cell in the row following last row, and in the column' following the last column'' r ..... number of rows and no of columns in the highlighted range' s ..... number of columns in the highlighted range' Csum .. Correct sum of each row, column and diagonal' sum ... variable for a sum' i & j . loop counters'------------------------------------------------------------------------

Dim sum As Long, i As Integer, j As Integer, r As Integer, csum As Long, s As Integer

r = Selection.Rows.Counts = Selection.Columns.Count

If s <> r Then ' If range is non-square print message and exit Selection(0, 1) = "Non-Square" ' Selection(0,1) is shortcut for Selection.Cells(0,1).Value Exit SubEnd Ifcsum = r * (r * r + 1) / 2 ' expected sum of each row, column and diagonal' ................. find fwd diagonal sumsum = 0For i = 1 To r sum = sum + Selection(i, r - i + 1) ' summing the diagonalNext iSelection(0, r + 1) = sum

If sum <> csum Then ' if the sum is not as expected, append ** to the sum Selection(0, r + 1) = "**" & sumEnd If

Page 8: Codes

' ................. find backward diagonal sumsum = 0For i = 1 To r sum = sum + Selection(i, i)Next i

Selection(r + 1, r + 1) = sumIf sum <> csum Then ' if the sum is not as expected, append ** to the sum Selection(r + 1, r + 1) = "**" & sumEnd If

' ................. find each row sumFor i = 1 To r sum = 0 For j = 1 To r sum = sum + Selection(i, j) Next j Selection(i, r + 1) = sum If sum <> csum Then ' if the sum is not as expected, append ** to the sum Selection(i, r + 1) = "**" & sum End IfNext i

' .................. find each column sumFor j = 1 To r sum = 0 For i = 1 To r sum = sum + Selection(i, j) Next i Selection(r + 1, j) = sum If sum <> csum Then ' if the sum is not as expected, append ** to the sum Selection(r + 1, j) = "**" & sum End IfNext jEnd Sub

' Vector rotation through 90, 180 or 270 degreesFunction vrotate(M As Range, T As Integer)' T ....... 1 means 90 degree rotation' T ....... 2 means 180 degree rotation' T ....... 3 means 270 degree rotation

Dim result() As Integer, i As Integer, j As IntegerDim r As Integer, c As Integerr = M.Rows.Countc = M.Columns.Count' each case corresponds to one type of rotationSelect Case T

Page 9: Codes

Case 1 ' 90 degree rotation ReDim result(1 To c, 1 To r) As Integer For i = 1 To c For j = 1 To r result(i, j) = M(r - j + 1, i) Next j Next i

Case 2 ' 180 degree rotation, the result should be same as 90 degree rotation twice ReDim result(1 To c, 1 To r) As Integer For i = 1 To c For j = 1 To r result(i, j) = M(r - i + 1, r - j + 1) Next j Next i

Case 3 ' 270 degree rotation, the result should be same as 90 degree rotation thrice ReDim result(1 To c, 1 To r) As Integer For i = 1 To c For j = 1 To r result(i, j) = M(j, r - i + 1) Next j Next i

End Select

vrotate = resultEnd Function

Fiber CostOption Base 1Option ExplicitFunction Fiber_cost(cord As Range, cost As Integer)' @@author : Shyam Shroff' Calculates the minimum spanning tree connecting all the selected basses using Prim's Algorithm' The weight of each edge in the graph is the length of the edge

' Input : Range (vertical) containing coordinates of bases' Input : Cost of fibre('000 Rs/KM)' Output: Edges that comprise the <ST' Total cost of fibre along minimum spanning tree ('000 Rs/KM)'Uses other user defined function remove_element

Dim dist() As Double ' 2 D array of distances between 2 basesDim b_spaned() As Integer ' array of base already spannedDim b_remaining() As Integer ' array of bases yet to be spanned

Page 10: Codes

Dim edges() As String ' array of edges selected for MSTDim num_b_spanned As Integer ' counter of number of bases spannedDim min_edge, tmp_edge As StringDim w_min_edge As Double ' weight of min edgeDim new_b As IntegerDim Total_cost As DoubleDim ans As BooleanDim b As Integer ' number of basesDim i, j As Integer ' iterators

b = cord.Rows.Count ' number of bases

ReDim dist(1 To b, 1 To b)ReDim b_spanned(1 To b)ReDim b_remaining(1 To b)ReDim edges(1 To b) As String

'InitializationTotal_cost = 0num_b_spanned = 0For i = 1 To b b_spanned(i) = 0 b_remaining(i) = iNext i

'Populate 2-dim array of distances between basesFor i = 1 To b For j = 1 To b dist(i, j) = Sqr((cord(i, 1) - cord(j, 1)) ^ 2 + (cord(i, 2) - cord(j, 2)) ^ 2) Next jNext i ' Start Minimum Spanning Tree Calculation

' Assign any one base to b_spannedb_spanned(1) = 1

'Remove the base from b_remainingans = remove_element(b_remaining, b, 1)

' Increment counter of number of bases spannednum_b_spanned = 1

Do While (num_b_spanned < b) ' Loop till all bases are spanned w_min_edge = 99999 ' 99999 to denote infinity ' Find min edge from a base in b_spanned to base in b_remaining For i = 1 To num_b_spanned ' for each base in b_spanned

Page 11: Codes

For j = 1 To b - num_b_spanned ' for each base in b_remaining tmp_edge = (b_spanned(i) & "-") & b_remaining(j) ' create a string denoting the edge If ((dist(b_spanned(i), b_remaining(j)) < w_min_edge)) Then min_edge = tmp_edge new_b = b_remaining(j) w_min_edge = dist(b_spanned(i), new_b) End If Next j Next i 'Add new base to b_spanned num_b_spanned = num_b_spanned + 1 b_spanned(num_b_spanned) = new_b 'Add new edge to edges edges(num_b_spanned - 1) = min_edge 'remove base from b_remaining ans = remove_element(b_remaining, b, new_b) 'Add to cost Total_cost = Total_cost + w_min_edge * costLoop

'Add total cost to the end of the array of edgesedges(b) = Format(Total_cost, "#######.##")

Fiber_cost = edges

End Function

Function remove_element(arr As Variant, size As Integer, x As Integer) As Boolean' Removes element x from array arr, and moves other elements forward' size denotes the size of the array arr' Returns true if element was found and removed and false otherwise

Dim i, found

found = 0

For i = 1 To size If arr(i) = x Then found = 1 End If If found = 1 Then If (i = size) Then arr(i) = 0

Page 12: Codes

Exit For End If arr(i) = arr(i + 1) End IfNext iIf found = 1 Then remove_element = TrueEnd IfEnd Function