Visual Basic for Applications macro that runs a simulation with moving blocks in Microsoft Excel. Blocks are randomly connected and disconnected based on a set of rules and parameters.

© All Rights Reserved

1 views

Visual Basic for Applications macro that runs a simulation with moving blocks in Microsoft Excel. Blocks are randomly connected and disconnected based on a set of rules and parameters.

© All Rights Reserved

You are on page 1of 4

Option Explicit

Sub simu()

Dim nCols As Integer, nRows As Integer, NSim As Integer, strng As String, i As I

nteger, j As Integer, N As Integer

Dim edgecells, conctvt As Integer, totalcon As Integer, nMoves As Integer, nFill

ed As Integer

Dim a As Single, b As Single, c As Single, d As Single, e As Single, f As Single

, g As Single, h As Single, k As Single

Open "C:\Documents and Settings\Kevin\Desktop\simlog.txt" For Output As #1

Print #1, "Started at " & Now

Print #1, ""

Print #1, "Iteration"; " "; "Average connectivity"; " "; "Number of moves"

; " "; "Filled cell count"

'format area

nCols = 30

nRows = 30

strng = ConvertToLetter(1) & ":" & ConvertToLetter(nCols)

With Columns(strng)

.ColumnWidth = 2.05

.NumberFormat = ";;;"

End With

'initialize border

Range(Cells(1, 1), Cells(1, nCols)).Interior.ThemeColor = xlThemeColorDark1

Range(Cells(1, 1), Cells(1, nCols)).Interior.TintAndShade = -0.25 'gray

Range(Cells(1, 1), Cells(1, nCols)).Value = 0

Range(Cells(1, nCols), Cells(nRows, nCols)).Interior.ThemeColor = xlThemeColorDa

rk1

Range(Cells(1, nCols), Cells(nRows, nCols)).Interior.TintAndShade = -0.25

Range(Cells(1, nCols), Cells(nRows, nCols)).Value = 0

Range(Cells(nRows, nCols), Cells(nRows, 1)).Interior.ThemeColor = xlThemeColorDa

rk1

Range(Cells(nRows, nCols), Cells(nRows, 1)).Interior.TintAndShade = -0.25

Range(Cells(nRows, nCols), Cells(nRows, 1)).Value = 0

Range(Cells(nRows, 1), Cells(1, 1)).Interior.ThemeColor = xlThemeColorDark1

Range(Cells(nRows, 1), Cells(1, 1)).Interior.TintAndShade = -0.25

Range(Cells(nRows, 1), Cells(1, 1)).Value = 0

'initialize cells

For i = 2 To nRows - 1

For j = 2 To nCols - 1

If Rnd() > 0.5 Then 'fill the cell

Cells(i, j).Interior.ThemeColor = xlThemeColorLight1 'black cell

Cells(i, j).Value = 1 'occupy cell

Else 'do not fill

Cells(i, j).Interior.ThemeColor = xlThemeColorDark1 'white cell

Cells(i, j).Value = 0 'empty cell

End If

Next j

Next i

'run simulation N times, excluding edge cells

NSim = 20

For N = 1 To NSim

totalcon = 0

nFilled = 0

nMoves = 0

'move cells randomly

For i = 2 To nRows - 1

For j = 2 To nCols - 1

If Cells(i, j).Value = 1 Then 'if current cell is occupied

Cells(i, j).Select 'calculate number of connected cells (including d

iagonals)

With ActiveCell

.FormulaR1C1 = "=R[-1]C[-1]+R[-1]C+R[-1]C[1]+RC[-1]+RC[1]+R[1]C[

-1]+R[1]C+R[1]C[1]"

conctvt = .Value

.Value = 1

edgecells = -4 * (.Offset(-1, -1).Interior.TintAndShade + .Offse

t(-1, 0).Interior.TintAndShade + .Offset(-1, 1).Interior.TintAndShade + .Offset(

0, -1).Interior.TintAndShade + .Offset(0, 1).Interior.TintAndShade + .Offset(1,

-1).Interior.TintAndShade + .Offset(1, 0).Interior.TintAndShade + .Offset(1, 1).

Interior.TintAndShade)

End With

edgecells = Round(edgecells, 0) 'rounding correction

'probability of move is inversely proportional to degree of connecti

vity

'no move if completely surrounded by filled cells and edge cells

If Rnd < (WorksheetFunction.Ceiling((8 - conctvt - edgecells) / 8, 1

) * Exp(-conctvt)) Then 'choose to move

'randomize the relative chances of moving into the 8 adjacent ce

lls (a,b,c,d,e,f,g,h)

'if cell is occupied, chance of moving into it becomes zero as (

1 + 1) Mod 2 = 0

'body cells are treated normally, TintAndShade = 0 -> does not a

ffect

'but edge cells will not be moved into, TintAndShade = -0.25 ->

(0 + 4 * -0.25 + 1) Mod 2 = 0

a = Rnd * ((Cells(i, j).Offset(-1, -1).Value + 4 * Cells(i, j).O

ffset(-1, -1).Interior.TintAndShade + 1) Mod 2)

b = Rnd * ((Cells(i, j).Offset(-1, 0).Value + 4 * Cells(i, j).Of

fset(-1, 0).Interior.TintAndShade + 1) Mod 2)

c = Rnd * ((Cells(i, j).Offset(-1, 1).Value + 4 * Cells(i, j).Of

fset(-1, 1).Interior.TintAndShade + 1) Mod 2)

d = Rnd * ((Cells(i, j).Offset(0, -1).Value + 4 * Cells(i, j).Of

fset(0, -1).Interior.TintAndShade + 1) Mod 2)

e = Rnd * ((Cells(i, j).Offset(0, 1).Value + 4 * Cells(i, j).Off

set(0, 1).Interior.TintAndShade + 1) Mod 2)

f = Rnd * ((Cells(i, j).Offset(1, -1).Value + 4 * Cells(i, j).Of

fset(1, -1).Interior.TintAndShade + 1) Mod 2)

g = Rnd * ((Cells(i, j).Offset(1, 0).Value + 4 * Cells(i, j).Off

set(1, 0).Interior.TintAndShade + 1) Mod 2)

h = Rnd * ((Cells(i, j).Offset(1, 1).Value + 4 * Cells(i, j).Off

set(1, 1).Interior.TintAndShade + 1) Mod 2)

k = 0 'find the cell with largest chance of being moved into

If k < a Then

k = a

End If

If k < b Then

k = b

End If

If k < c Then

k = c

End If

If k < d Then

k = d

End If

If k < e Then

k = e

End If

If k < f Then

k = f

End If

If k < g Then

k = g

End If

If k < h Then

k = h

End If

If k = a Then

Cells(i, j).Offset(-1, -1).Select

ElseIf k = b Then

Cells(i, j).Offset(-1, 0).Select

ElseIf k = c Then

Cells(i, j).Offset(-1, 1).Select

ElseIf k = d Then

Cells(i, j).Offset(0, -1).Select

ElseIf k = e Then

Cells(i, j).Offset(0, 1).Select

ElseIf k = f Then

Cells(i, j).Offset(1, -1).Select

ElseIf k = g Then

Cells(i, j).Offset(1, 0).Select

ElseIf k = h Then

Cells(i, j).Offset(1, 1).Select

End If

Selection.Interior.ThemeColor = xlThemeColorLight1 'make new cel

l black

Selection.Value = 1 'occupy new cell

Cells(i, j).Interior.ThemeColor = xlThemeColorDark1 'make old ce

ll white

Cells(i, j).Value = 0 'empty old cell

End If

nMoves = nMoves + 1

End If

Next j

Next i

'check cells

For i = 2 To nRows - 1

For j = 2 To nCols - 1

If Cells(i, j).Value = 1 Then 'if current cell is occupied

Cells(i, j).Select 'calculate number of connected cells (including d

iagonals)

With ActiveCell

.FormulaR1C1 = "=R[-1]C[-1]+R[-1]C+R[-1]C[1]+RC[-1]+RC[1]+R[1]C[

-1]+R[1]C+R[1]C[1]"

conctvt = .Value

.Value = 1

End With

totalcon = totalcon + conctvt 'running total

nFilled = nFilled + 1

End If

Next j

Next i

Print #1, N; " "; Round(totalcon / nFilled, 4); " "; nMov

es; " "; nFilled

Next N

Cells(nRows + 1, nCols + 1).ClearContents

Print #1, ""

Print #1, "Ended at " & Now

Close #1

End Sub

Function ConvertToLetter(iCol As Integer) As String

'adapted from http://support.microsoft.com/kb/833402

Dim iAlpha As Integer

Dim iRemainder As Integer

If iCol > 0 Then

iAlpha = Int((iCol - 1) / 26)

iRemainder = iCol - (iAlpha * 26)

End If

If iAlpha > 0 Then

ConvertToLetter = Chr(iAlpha + 64)

End If

If iRemainder > 0 Then

ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)

End If

End Function

- important java programsUploaded bySaravananofficial
- Shoe StoreUploaded bydeepi
- Perl Interview QuestionUploaded bytowid
- Things Ch3Uploaded byapi-3706602
- Small Hotel SystemUploaded byUrva Amir
- System Admin ProjectUploaded bysomaacy
- VT8-VB9-SuthepSUploaded byrscm75
- Visual Basic 1Uploaded byPankaj Shukla
- Chapter 2Uploaded byNathan Samuel Obinwafor Uzoma
- C String Handling TutorialUploaded byJay Ng
- VirusUploaded byAjay Kumar
- ADS ProjectsUploaded bynavneet604779
- Sourabh SinghUploaded bySunny Rekhi
- Lesson 2Uploaded byShreesha Rodricks
- Java DocUploaded byKarthik Sirimulla
- advanced abapUploaded byBooker Booker
- E Quiz SynopsisUploaded byApacetech Iluvb
- QTP MaterialUploaded byNadikattu Ravikishore
- Nt_sasqdsmple Question Paper 1 (1)Uploaded byMainak De
- Test JavaUploaded bynarendramahajangm
- Formulas to Count the Occurrences of Text, Characters, Or Words in Excel for MacUploaded byYang Yong
- 12. PreDefined Vb.net FunctionsUploaded byMohd Nafish
- C# QuestionsUploaded bySameerSrivastava
- Lecture 14Uploaded bySara Wong
- Download BeveragesUploaded byChad Precilla
- Clip BatchUploaded byDaniel Asandei
- Voice Based MAil AttachmentUploaded byshalini
- davechild_mysql.pdfUploaded bymiguel
- CoilUploaded byBenjamin Culkin
- “Clustering Based Data Ensemble Using Incremental Semi Supervised Clustering Method”Uploaded byCHEIF EDITOR

- AnnoyUploaded byDylan Tyler
- Much MathUploaded byDylan Tyler
- heeniUploaded byDylan Tyler
- Branch MacroUploaded byDylan Tyler
- Read Stream FileUploaded byDylan Tyler
- epitaxy mindmapUploaded byDylan Tyler
- Koala BearUploaded byDylan Tyler
- Write Stream FileUploaded byDylan Tyler
- Acoustic Cafe - Last Carnival (Violin, Piano)Uploaded bylirenyeo

- Lecture 1 - CpE 690 Introduction to VLSI DesignUploaded byjvandome
- BPEL TricksUploaded bymahesh_upadhyay
- Smartphone SurveyingUploaded byAlzhammer Manupac
- Hotel Management SystemUploaded byNupur Ahluwalia
- What is Inline FunctionUploaded byRavi Sun
- MPX16 User Guide v1.4Uploaded bySdfsfSdfsdf
- compiler design.docxUploaded bynatarajauni
- Important question of DSUploaded byPritam Saha
- RPi.GPIO-Cheat-Sheet.pdfUploaded byBrendan Keith
- VHDL_dsdUploaded byArulaalan Mahalingam
- FPRA USER GUIDE PERUUploaded byVictor Chacchi Ricra
- Simatic Step 7 v5.5 From s5 to s7Uploaded byondoy4925
- Conversion Exit in sap abapUploaded bySantosh Ravindra Nadagouda
- Qbasic TutorialUploaded byArun
- Apple.pro.Training.series.xsanUploaded byEdwin Sanchez
- BMC Remedy ManualUploaded byprofilmk
- XI31 SP4 Fixed Issues 1of2Uploaded byozyind
- SAP Performance TuningUploaded bynandha587y
- Important to Read to Remote Visual InspectionUploaded bydiegonr18salazar
- ThesisUploaded byNhan Nguyen
- Uart Gps Neo 6m (b)_lea 6 Neo 6 Max 6 Hardware Integration ManualUploaded bynion83
- b28236_Oracle® XML Developer's KitUploaded bysurajthapar
- LBP162dw_LBP214dw SpecSheetUploaded byNBS Marketing
- 462116-01 AA_1510 Excel ESI Command Codes Addendum_10 Feb 2009Uploaded byquyenducqdc
- Setup LogUploaded byAndhi Crist
- 6ES7414-4HR14-0AB0 ManualUploaded byingenira
- Card Explorer EngUploaded bybabirda
- Interfacing a Gps to an Lcd Using a Micro ControllerUploaded byKumari Sneh
- Bluetooth Keyboard BKB-6062 User Guide1Uploaded byfjmrf
- Seagate Wireless en InUploaded byDr.Ajmal Nasir

## Much more than documents.

Discover everything Scribd has to offer, including books and audiobooks from major publishers.

Cancel anytime.