Advertisement
illpastethat

module1 bit

Nov 7th, 2014
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 12.85 KB | None | 0 0
  1. Sub ShowDisplayForm()
  2.     frmDisplay.Show
  3.    
  4. End Sub
  5.  
  6. Sub AON_Nodes_Algorithm()
  7.     Dim numNodes As Integer, i As Integer
  8.    
  9.     'Takes input as the number of required nodes and displays the list of nodes
  10.     'Includes a start (0) and finish (numNodes + 1) node
  11.     numNodes = Worksheets("InputTable").Range("A2").End(xlDown)
  12.    
  13.     Worksheets("AON Nodes Table").Range("A1").Value = 0
  14.    
  15.     i = 0
  16.    
  17.     Do While i <= numNodes
  18.         i = i + 1
  19.         Worksheets("AON Nodes Table").Range("A1").Offset(i, 0).Value = i
  20.     Loop
  21.  
  22. End Sub
  23.  
  24. Sub AON_Arcs_Algorithm()
  25.     Dim numNodes As Integer, i As Integer, P() As Integer, sz As Integer
  26.     numNodes = Worksheets("InputTable").Range("A2").End(xlDown)
  27.    
  28.     i = 0
  29.    
  30.     Do While i <= numNodes
  31.         i = i + 1
  32.        
  33.         Call GetPred(Worksheets("InputTable").Range("A2"), P(), sz)
  34.     Loop
  35.    
  36. End Sub
  37.  
  38. Sub AOA_Nodes_Algorithm()
  39.     Dim numNodes As Integer, i As Integer
  40.     numNodes = Worksheets("InputTable").Range("A2").End(xlDown)
  41.    
  42.     i = 0
  43.    
  44.     Do While i <= numNodes + 1
  45.         Worksheets("AOA Nodes Table").Range("A2").Offset(i, 0) = i
  46.         Worksheets("AOA Nodes Table").Range("A2").Offset(i, 1) = 2 * i + 1
  47.         Worksheets("AOA Nodes Table").Range("A2").Offset(i, 2) = 2 * i + 2
  48.         i = i + 1
  49.     Loop
  50.    
  51. End Sub
  52.  
  53. Sub CreateProjectNetwork()
  54.     Dim AOAarcs() As Integer, finishtime As Double, r As Range, numNodes As Integer, i As Integer
  55.     Dim ES As Integer, LS As Integer, Critical As Integer, maxLS As Integer, r2 As Range
  56.    
  57.    
  58.     Application.ScreenUpdating = False
  59.    
  60.     AOAarcs = GetAOAarcs()
  61.     Call FormatSolverWorksheet(AOAarcs)
  62.     finishtime = SolveProjectNetworkMin()
  63.     Call GetMinValues
  64.     finishtime = SolveProjectNetworkMax(finishtime)
  65.     Call GetMaxValues
  66.    
  67.     'Create Gantt Chart
  68.     Worksheets("Gantt Chart").Cells.Clear
  69.     Set r = Worksheets("Solution Table").Range("A2")
  70.     Set r2 = Worksheets("Gantt Chart").Range("A1")
  71.     numNodes = Worksheets("Solution Table").Range("A2").End(xlDown)
  72.     'Begin Filling Table
  73.     r2.Offset(0, 0) = "Activity"
  74.     r2.Offset(0, 1).Value = 1
  75.     r2.Offset(0, 2).Value = 2
  76.    
  77.     'Set default values
  78.     i = 1
  79.     maxLS = 0
  80.    
  81.     'For each row in solution table (except end node), create gantt chart row
  82.     Do While i < numNodes - 1
  83.         r2.Offset(i, 0) = r.Offset(i, 0) 'Activity number
  84.         ES = r.Offset(i, 1).Value
  85.         LS = r.Offset(i, 2).Value
  86.         If LS > maxLS Then
  87.             maxLS = LS
  88.         End If
  89.         Critical = r.Offset(i, 5).Value
  90.    
  91.         'Select Area to be formatted
  92.         Worksheets("Gantt Chart").Select
  93.         Range(Range("A1").Offset(i, ES + 1), Range("A1").Offset(i, LS)).Select
  94.         If Critical = 1 Then
  95.             With Selection.Interior
  96.                 .Pattern = xlSolid
  97.                 .PatternColorIndex = xlAutomatic
  98.                 .ThemeColor = xlThemeColorAccent3
  99.                 .TintAndShade = 0
  100.                 .PatternTintAndShade = 0
  101.             End With
  102.         Else
  103.             With Selection.Interior
  104.                 .Pattern = xlSolid
  105.                 .PatternColorIndex = xlAutomatic
  106.                 .Color = 49407
  107.                 .TintAndShade = 0
  108.                 .PatternTintAndShade = 0
  109.             End With
  110.         End If
  111.     i = i + 1
  112.     Loop
  113.     Worksheets("Gantt Chart").Select
  114.     'Add top row number from 1 to the max finish time (end node)
  115.     Range("B1:C1").AutoFill Destination:=Range(Range("B1"), Range("B1").Offset(0, maxLS - 1)), Type:=xlFillDefault
  116.    
  117.     Application.ScreenUpdating = True
  118. End Sub
  119.  
  120. Sub FormatSolverWorksheet(AOAarcs() As Integer)
  121.     Dim r As Range, r2 As Range, r3 As Range
  122.     Dim i As Integer, conststr As String
  123.     Dim numconst As Integer, numvar As Integer
  124.     Dim begpos As Integer, endpos As Integer
  125.    
  126.     numconst = UBound(AOAarcs) - LBound(AOAarcs) + 1
  127.    
  128.     Worksheets("Project Network").Cells.Clear
  129.    
  130.     Set r = Worksheets("Project Network").Range("A2")
  131.    
  132.     'column titles
  133.     r.Value = "Activity"
  134.     r.Offset(0, 1) = "Begin"
  135.     r.Offset(0, 2) = "End"
  136.     r.Offset(0, 3) = "Time"
  137.    
  138.     'transfer AOAarcs array to worksheet
  139.     For i = 1 To numconst
  140.         r.Offset(i, 0) = AOAarcs(i - 1, 0)
  141.         r.Offset(i, 1) = AOAarcs(i - 1, 1)
  142.         r.Offset(i, 2) = AOAarcs(i - 1, 2)
  143.         r.Offset(i, 3) = AOAarcs(i - 1, 3)
  144.     Next i
  145.    
  146.     numvar = Application.WorksheetFunction.Max( _
  147.                Range(r.Offset(1, 2), r.Offset(1, 2).End(xlDown)))
  148.    
  149.     Set r2 = Worksheets("Project Network").Range("D1")
  150.     For i = 1 To numvar
  151.         r2.Offset(0, i) = i
  152.     Next i
  153.    
  154.     Call FormatTable(Range(r.Offset(-1, 0), r2.Offset(numconst + 1, numvar)), vbWhite)
  155.     Call FormatTable(Range(r2.Offset(1, 1), r2.Offset(1, numvar)), vbYellow)
  156.     Range(r2.Offset(1, 1), r2.Offset(1, numvar)).Name = "DecVar"
  157.    
  158.     'LHS coefficients
  159.     For i = 1 To numconst
  160.         r.Offset(i, 3 + r.Offset(i, 1)) = -1
  161.         r.Offset(i, 3 + r.Offset(i, 2)) = 1
  162.     Next i
  163.    
  164.     Set r3 = r2.Offset(1, numvar + 1)
  165.    
  166.     'objective formula
  167.     r3.FormulaR1C1 = "=SUM(RC[-" & numvar & "]:RC[-1])"
  168.     Call FormatTable(r3, vbGreen)
  169.     r3.Name = "ObjFunc"
  170.    
  171.     'constraint formulas
  172.     For i = 1 To numconst
  173.         conststr = "=SUMPRODUCT(RC[-" & numvar & "]:RC[-1],R2C[-" & numvar & "]:R2C[-1])"
  174.         r3.Offset(i, 0).FormulaR1C1 = conststr
  175.     Next i
  176.     Call FormatTable(Range(r3.Offset(1, 0), r3.Offset(numconst, 0)), vbCyan)
  177.     Range(r3.Offset(1, 0), r3.Offset(numconst, 0)).Name = "LHSRef"
  178.     Range(r.Offset(1, 3), r.Offset(numconst, 3)).Name = "RHSRef"
  179.    
  180. End Sub
  181.  
  182.  
  183. Function SolveProjectNetworkMin()
  184.     ' Check Tools>References ... Analytic Solver Platform 2014-R2 Type Library
  185.    
  186.     Dim PR As New RSP.Problem
  187.     PR.Variables.Clear
  188.     PR.Functions.Clear
  189.     PR.Solver.SolverType = Solver_Type_Minimize
  190.    
  191.     Dim DV As New RSP.Variable
  192.     DV.Init Range("DecVar")
  193.     DV.NonNegative
  194.     PR.Variables.Add DV
  195.    
  196.     Dim OBJ As New RSP.Function
  197.     OBJ.Init Range("ObjFunc")
  198.     OBJ.FunctionType = Function_Type_Objective
  199.     PR.Functions.Add OBJ
  200.    
  201.     Dim CNST As New RSP.Function
  202.     CNST.Init Range("LHSRef")
  203.     CNST.Relation Cons_Rel_GE, Range("RHSRef")
  204.     PR.Functions.Add CNST
  205.  
  206.     PR.Engine = PR.Engines("Standard LP/Quadratic")
  207.     PR.Solver.Optimize (Solve_Type_Solve)
  208.    
  209.     Dim result As Integer
  210.     result = PR.Solver.OptimizeStatus
  211.     If result = 5 Then
  212.         MsgBox "Problem infeasible."
  213.     End If
  214.    
  215.     SolveProjectNetworkMin = Range("ObjFunc").Offset(0, -1)
  216.    
  217.     Worksheets("Project Network").Activate
  218. End Function
  219.  
  220. Function SolveProjectNetworkMax(finishtime As Double)
  221.     ' Check Tools>References ... Analytic Solver Platform 2014-R2 Type Library
  222.    
  223.     Dim PR As New RSP.Problem
  224.     PR.Variables.Clear
  225.     PR.Functions.Clear
  226.     PR.Solver.SolverType = Solver_Type_Maximize
  227.    
  228.     Dim DV As New RSP.Variable
  229.     DV.Init Range("DecVar")
  230.     DV.NonNegative
  231.     PR.Variables.Add DV
  232.    
  233.     Dim OBJ As New RSP.Function
  234.     OBJ.Init Range("ObjFunc")
  235.     OBJ.FunctionType = Function_Type_Objective
  236.     PR.Functions.Add OBJ
  237.    
  238.     Dim CNST As New RSP.Function
  239.     CNST.Init Range("LHSRef")
  240.     CNST.Relation Cons_Rel_GE, Range("RHSRef")
  241.     PR.Functions.Add CNST
  242.  
  243.     Dim CNST2 As New RSP.Function
  244.     CNST2.Init Range("ObjFunc").Offset(0, -1)
  245.     CNST2.Relation Cons_Rel_LE, finishtime
  246.     PR.Functions.Add CNST2
  247.  
  248.     PR.Engine = PR.Engines("Standard LP/Quadratic")
  249.     PR.Solver.Optimize (Solve_Type_Solve)
  250.    
  251.     Dim result As Integer
  252.     result = PR.Solver.OptimizeStatus
  253.     If result = 5 Then
  254.         MsgBox "Problem infeasible."
  255.     End If
  256.    
  257.     SolveProjectNetworkMax = Range("ObjFunc").Offset(0, -1)
  258.    
  259.     Worksheets("Project Network").Activate
  260. End Function
  261.  
  262. Sub GetMinValues()
  263.     Dim r As Range, r2 As Range, numvar As Integer, i As Integer
  264.     Dim r3 As Range
  265.    
  266.     Worksheets("Solution Table").Cells.Clear
  267.     Set r = Worksheets("Project Network").Range("D1")
  268.     Set r2 = Range(r.Offset(0, 1), r.Offset(0, 1).End(xlToRight))
  269.     numvar = Application.WorksheetFunction.Max(r2)
  270.     Set r3 = Worksheets("Solution Table").Range("A1")
  271.     r3.Value = "Activity"
  272.     r3.Offset(0, 1) = "ES"
  273.     r3.Offset(0, 2) = "EF"
  274.     'Earlies start and Earliest finish
  275.    
  276.     'Two nodes for each activity
  277.     For i = 1 To numvar / 2
  278.         r3.Offset(i, 0) = i - 1
  279.         r3.Offset(i, 1) = r.Offset(1, 2 * i - 1)
  280.         r3.Offset(i, 2) = r.Offset(1, 2 * i)
  281.     Next i
  282.  
  283. End Sub
  284.  
  285.  
  286. Sub GetMaxValues()
  287.     Dim r As Range, r2 As Range, numvar As Integer, i As Integer
  288.     Dim r3 As Range
  289.    
  290.     Set r = Worksheets("Project Network").Range("D1")
  291.     Set r2 = Range(r.Offset(0, 1), r.Offset(0, 1).End(xlToRight))
  292.     numvar = Application.WorksheetFunction.Max(r2)
  293.     Set r3 = Worksheets("Solution Table").Range("A1")
  294.     r3.Offset(0, 3) = "LS"
  295.     r3.Offset(0, 4) = "LF"
  296.     r3.Offset(0, 5) = "Critical"
  297.    
  298.     For i = 1 To numvar / 2
  299.         r3.Offset(i, 3) = r.Offset(1, 2 * i - 1)
  300.         r3.Offset(i, 4) = r.Offset(1, 2 * i)
  301.         If r3.Offset(i, 1) = r3.Offset(i, 3) Then
  302.             'Earliest start is same at latest start, it's part of critical path
  303.             r3.Offset(i, 5) = 1
  304.         Else
  305.             r3.Offset(i, 5) = 0
  306.         End If
  307.     Next i
  308.  
  309. End Sub
  310.  
  311.  
  312. Function GetAOAarcs()
  313.     Dim AONarcs() As Integer, AOAarcs() As Integer
  314.     Dim numNodes As Integer, n2 As Integer, i As Integer, j As Integer, sz As Integer, sz2 As Integer
  315.     Dim r As Range, rng As Range, IsPred() As Boolean, Pred() As Integer, checklist As String
  316.    
  317.     Set r = Worksheets("InputTable").Range("A1")
  318.     numNodes = Range(r.Offset(1, 0), r.End(xlDown)).Rows.Count
  319.    
  320.     ReDim IsPred(numNodes)
  321.     sz = 0
  322.     For i = 1 To numNodes
  323.         Set rng = r.Offset(i, 2)
  324.         Call GetPred(rng, Pred, n2)
  325.         For j = 0 To n2 - 1
  326.             sz = sz + 1
  327.             ReDim Preserve AONarcs(2, 1 To sz)
  328.             Worksheets("AON Arcs Table").Range("A1").Offset(sz, 0) = Pred(j)
  329.             Worksheets("AON Arcs Table").Range("A1").Offset(sz, 1) = i
  330.             AONarcs(1, sz) = Pred(j)
  331.             AONarcs(2, sz) = i
  332.             IsPred(Pred(j)) = True
  333.         Next j
  334.     Next i
  335.    
  336.    
  337.     For i = 1 To numNodes
  338.         If Not IsPred(i) Then
  339.             sz = sz + 1
  340.             ReDim Preserve AONarcs(2, 1 To sz)
  341.             Worksheets("AON Arcs Table").Range("A1").Offset(sz, 0) = i
  342.             Worksheets("AON Arcs Table").Range("A1").Offset(sz, 1) = numNodes + 1
  343.             AONarcs(1, sz) = i
  344.             AONarcs(2, sz) = numNodes + 1
  345.         End If
  346.     Next i
  347.    
  348.     ReDim AOAarcs(numNodes + 1 + sz, 3)
  349.     For i = 0 To numNodes + 1
  350.         AOAarcs(i, 0) = i
  351.         AOAarcs(i, 1) = 2 * i + 1
  352.         AOAarcs(i, 2) = 2 * i + 2
  353.         If i <> 0 And i <> numNodes + 1 Then
  354.             AOAarcs(i, 3) = r.Offset(i, 3)
  355.         Else
  356.             AOAarcs(i, 3) = 0
  357.         End If
  358.     Next i
  359.    
  360.    
  361.     sz2 = numNodes + 1
  362.     For i = 1 To sz
  363.         sz2 = sz2 + 1
  364.         AOAarcs(sz2, 0) = -1
  365.         AOAarcs(sz2, 1) = AOAarcs(AONarcs(1, i), 2)
  366.         AOAarcs(sz2, 2) = AOAarcs(AONarcs(2, i), 1)
  367.         AOAarcs(sz2, 3) = 0
  368.     Next i
  369.    
  370.     checklist = ""
  371.     For i = 0 To sz2
  372.         If i <= Worksheets("InputTable").Range("A2").End(xlDown) + 1 Then
  373.             Worksheets("AOA Arcs Table").Range("A2").Offset(i, 0) = i
  374.         Else
  375.             Worksheets("AOA Arcs Table").Range("A2").Offset(i, 0).Value = "D"
  376.             Worksheets("AOA Arcs Table").Range("A2").Offset(i, 3) = 0
  377.         End If
  378.         Worksheets("AOA Arcs Table").Range("A2").Offset(i, 1) = AOAarcs(i, 1)
  379.         Worksheets("AOA Arcs Table").Range("A2").Offset(i, 2) = AOAarcs(i, 2)
  380.        
  381.         If i = 0 Then
  382.             Worksheets("AOA Arcs Table").Range("A2").Offset(i, 3) = 0
  383.         ElseIf Not IsEmpty(Worksheets("InputTable").Range("A2").Offset(i - 1, 3)) Then
  384.             Worksheets("AOA Arcs Table").Range("A2").Offset(i, 3) = Worksheets("InputTable").Range("A2").Offset(i - 1, 3)
  385.         Else
  386.             Worksheets("AOA Arcs Table").Range("A2").Offset(i, 3) = 0
  387.         End If
  388.        
  389.     Next i
  390.    
  391.     GetAOAarcs = AOAarcs
  392.  
  393. End Function
  394.  
  395.  
  396. Sub GetPred(r As Range, P() As Integer, sz As Integer)
  397.     Dim str() As String, i As Integer, numNodes As Integer
  398.    
  399.     If r.Value = "-" Then
  400.         ReDim P(1) As Integer
  401.         P(0) = 0
  402.         sz = 1
  403.     Else
  404.         str = Split(r, ",")
  405.         numNodes = UBound(str) - LBound(str) + 1
  406.         ReDim P(numNodes) As Integer
  407.        
  408.         For i = 0 To numNodes - 1
  409.             If IsNumeric(str(i)) Then
  410.                 P(i) = CInt(str(i))
  411.             Else
  412.                 MsgBox "Predecessors should be numeric values!"
  413.                 End
  414.             End If
  415.         Next i
  416.         sz = numNodes
  417.     End If
  418.    
  419. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement