Programming (and Learning) Self-Adaptive & Self-Organising Behaviour
with ScaFi
for Swarms, Edge-Cloud Ecosystems, and More
Roberto Casadei roby.casadei@unibo.it
Gianluca Aguzzi gianluca.aguzzi@unibo.it
Danilo Pianini danilo.pianini@unibo.it
Mirko Viroli mirko.viroli@unibo.it
Alma Mater Studiorum – Università di Bologna
Talk @ International Conference on Autonomic Computing and Self-Organizing Systems (ACSOS)
11/12/2023
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 1 / 48
Tutorial Quickstart
Contents
1 Tutorial Quickstart
2 Introduction
Context – Collective Adaptive Systems
Aggregate Computing
3 Playing with ScaFi!
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 2 / 48
Tutorial Quickstart
Tutorial references
Where do I start?
https://github.com/AggregateComputing/acsos-2023-scafi-tutorial:
repository with the examples/exercises proposed in the tutorial
Follow the README.md to run examples and setup a development environment.
ScaFi-Web [1] (a online web simulator for ScaFi programs)
https://scafi.github.io/web
https://tomcat-glad-muskox.ngrok-free.app/
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 3 / 48
Tutorial Quickstart
Frequently Asked Questions (FAQs) I
What is it all about? ScaFi is an aggregate programming language: it supports the
development of self-organising behaviours. It is integrated into Alchemist: this
allows to build simulations of networked systems that execute ScaFi programs.
Where do I start? Slide 3
How can I quickly experiment with the programming model?
ScaFi-Web [1] (a online web simulator for ScaFi programs)
https://scafi.github.io/web
https://tomcat-glad-muskox.ngrok-free.app/
Where do I learn more about the tools (ScaFi, Alchemist)?
https://scafi.github.io/
http://alchemistsimulator.github.io/
Where do I learn more about the theory/research on aggregate computing (AC)?
Check out relevant presentations and papers.
https://www.slideshare.net/RobertoCasadei/
aggregate-computing-research-an-overview – a quick overview of AC research
M. Viroli, J. Beal, F. Damiani, et al., “From distributed coordination to field calculus
and aggregate computing,” J. Log. Algebraic Methods Program., vol. 109, 2019. doi:
10.1016/j.jlamp.2019.100486 – a survey about AC, its history, and main
developments
What is the research context? Check out Slide 8–Slide 16
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 4 / 48
Tutorial Quickstart
Frequently Asked Questions (FAQs) II
What kind of systems can be programmed/developed with AC? Check out Slide 21
and Slide 8–Slide 16
Where do I get an overview of relevant literature about aggregate computing?
https://www.slideshare.net/RobertoCasadei/
aggregate-computing-research-an-overview
M. Viroli, J. Beal, F. Damiani, et al., “From distributed coordination to field calculus
and aggregate computing,” J. Log. Algebraic Methods Program., vol. 109, 2019. doi:
10.1016/j.jlamp.2019.100486
What is the key literature about ScaFi?
software: R. Casadei, M. Viroli, G. Aguzzi, et al., “Scafi: A scala DSL and toolkit for
aggregate programming,” SoftwareX, vol. 20, p. 101 248, 2022. doi:
10.1016/j.softx.2022.101248. [Online]. Available:
https://doi.org/10.1016/j.softx.2022.101248 [3]
calculus: G. Audrito, R. Casadei, F. Damiani, et al., “Computation against a
neighbour: Addressing large-scale distribution and adaptivity with functional
programming and scala,” Log. Methods Comput. Sci., vol. 19, no. 1, 2023. doi:
10.46298/lmcs-19(1:6)2023. [Online]. Available:
https://doi.org/10.46298/lmcs-19(1:6)2023 [4]
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 5 / 48
Introduction
Contents
1 Tutorial Quickstart
2 Introduction
Context – Collective Adaptive Systems
Aggregate Computing
3 Playing with ScaFi!
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 6 / 48
Introduction Context – Collective Adaptive Systems
Contents
1 Tutorial Quickstart
2 Introduction
Context – Collective Adaptive Systems
Aggregate Computing
3 Playing with ScaFi!
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 7 / 48
Introduction Context – Collective Adaptive Systems
Context
Modern IT systems are more and more complex
increasing availability of wearable/mobile/embedded/flying devices
increasing availability of heterogeneous wireless networks
increasing availability of computational resources (edge/fog/cloud computing)
increasing production of data everywhere and anytime
The challenge
Consider the worst-case possible scenario
zillion of devices unpredictably located and moving in a space
heterogeneous displacement, pervasive sensing/actuation
computational services are contextual (proximity-based) and dynamic
How can we program such systems?
What are the right abstractions?
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 8 / 48
Collective Adaptive Systems (CASs)
Systems composed of possibly large set of component executing a collective task
strongly relying on component interactions and showing inherent adaptivity.
Introduction Context – Collective Adaptive Systems
Examples: controlling a fleet of drones
Issues
Design techniques to reactively propagate information across the fleet
Create algorithms for higher-level programming of the fleet
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 10 / 48
Introduction Context – Collective Adaptive Systems
Examples: pedestrian steering in (smart)cities
Issue
Design self-organisation algorithms for people navigation
More generally, design map-based large-scale applications
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 11 / 48
Introduction Context – Collective Adaptive Systems
Examples: fine control of crowds
Issues
Evaluating the influence of environment in crowd formation
Evaluating the influence of ambient sensors, cameras and wearable devices
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 12 / 48
Introduction Context – Collective Adaptive Systems
Collective Adaptive Systems
Adaptation
A system is adaptive if it can change its behaviour depending on circumstances, in order
to better reach its goal
ú Adaptivess in intrisic to open complex system (like CASs)
Kind of adaptiveness, self-* properties
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 13 / 48
Introduction Context – Collective Adaptive Systems
Kind of adaptiveness
Self-adaptiveness: general level
self-adaptiveness ú the ultimate property we perceive from outside
the terms “self-*” recalls a property achieved in autonomy
subcases: self-managing, -governing, -maintenance, -control
Major level
internal properties related to overall system management
subcases: self-configuring, self-healing, self-optimising, self-protecting
defined in the context of autonomic computing
Primitive level
internal properties related to primitive aspects
subcases: self-awareness, context-awareness
means being able to properly perceive the own state, context, . . .
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 14 / 48
Introduction Context – Collective Adaptive Systems
The case of self-organisation
Self-organisation
The ability of creating spatial/temporal patterns out of the local interaction of individuals
Self-adaptive vs. self-organising
self-adaptive: a top-down way of achieving adaptiveness
ú we have the adaptation goal, and accordingly guide components
self-organising: a bottom-up way of achieving adaptiveness
ú the adaptive behaviour emerges from local interactions
The typical approach in large-scale CASs
Born in the context of swarm intelligence
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 15 / 48
Introduction Context – Collective Adaptive Systems
Good Abstraction for CASs?
Ideas
specify overall behaviour, not individual device program
abstract from the actual shape of interactions
automatically adapt to environment details
focus on how the global output pattern can be obtained from global inputs
focus on both spatial and temporal computing patterns
ú the ideas around macro-programming paradigms [5]!
Aggregate computing
Buzz
. . .
Ruccurent abstractions:
Ensembles & collective tasks
Self-organising information flows
Self-healing collective structures (e.g., gradients)
[5] R. Casadei, “Macroprogramming: Concepts, state of the art, and opportunities of macroscopic
behaviour modelling,” ACM Comput. Surv., vol. 55, no. 13s, 2023, issn: 0360-0300. doi: 10.1145/3579353
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 16 / 48
Introduction Aggregate Computing
Contents
1 Tutorial Quickstart
2 Introduction
Context – Collective Adaptive Systems
Aggregate Computing
3 Playing with ScaFi!
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 17 / 48
Introduction Aggregate Computing
Aggregate Computing (AC) in 1 Slide
Self-org-like computational model
interaction: repeated msg exchange with neighbours
behaviour: repeated execution of async rounds of sense
– compute – (inter)act
formal model of executions: event structures
abstraction: computational fields (dev/evt 7→ V)
formal core language: field calculus [2]
paradigm: functional, macro-programming
source destination
gradient distance
gradient
<=
+
dilate
width
37
10
def channel(source: Boolean, target: Boolean, width: Double) =
dilate(gradient(source) + gradient(target) <=
distance(source, target), width)
M. Viroli, J. Beal, F. Damiani, et al., “From distributed co-
ordination to field calculus and aggregate computing,” J. Log.
Algebraic Methods Program., vol. 109, 2019. doi: 10.1016/j.
jlamp.2019.100486
δ0
δ1
δ2
δ3
δ4
device
time
0
0 0
1 0
2 0
3 0
4
1
0 1
1 1
2 1
3 1
4 1
5
2
0 2
1 2
2 2
3
3
0 3
1 3
2 3
3 3
4 3
5
4
0 4
1 4
2
m
e
s
s
a
g
e
self-message
reboot
sensors
local functions
actuators
Application
Code
Developer
APIs
Field Calculus
Constructs
Resilient
Coordination
Operators
Device
Capabilities
functions rep
nbr
T
G
C
functions
communication state
Perception
Perception
summarize
average
regionMax
…
Action
Action State
State
Collective Behavior
Collective Behavior
distanceTo
broadcast
partition
…
timer
lowpass
recentTrue
…
collectivePerception
collectiveSummary
managementRegions
…
Crowd Management
Crowd Management
dangerousDensity crowdTracking
crowdWarning safeDispersal
restriction
self­stabilisation
J. Beal, D. Pianini, and M. Viroli, “Aggregate programming
for the internet of things,” IEEE Computer, vol. 48, no. 9,
pp. 22–30, 2015. doi: 10.1109/MC.2015.261
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 18 / 48
Aggregate Computing
Program the aggregate, not the individual device
○ Computing Machine ú an ensemble of devices as a single body, fading the actual
space
○ Elaboration process ú atomic manipulation of a collective data structure
(computational field)
○ Networked computation ú a proximity-based self-organising system hidden
“under-the-hood”
Introduction Aggregate Computing
Computational model – self-organisation-like execution
Continuous communication with neighbours only (údecentralisation)
Continous execution of async rounds of sense - compute - communicate
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 20 / 48
Introduction Aggregate Computing
Applicability: in a nutshell
Essentially, this approach can be used to program any system that can be recast to the
following
Structure: network of locally interacting agents
neighbouring relationship: e.g. based on network connectivity, physical distance, social
relationship, or whatever
dynamicity/openness: devices may move/fail, enter/exit the system at runtime, and
neighbourhoods may change
Interaction:
with neighbours: asynchronous message passing
with environment: via sensors and actuators
Behaviour: sense–compute–interact
sense: acquire context (messages from neighbours, values from sensors)
compute: mapping context to (inter-)action
interact: sending messages to neighbours and running actuation
So, best for applications that are:
progressive/long-running: require multiple coordinated steps/rounds of local
processing and action
Assuming this system model, AC is about how to specify the “compute” part so that
global emergent outcomes can be eventually attained
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 21 / 48
Introduction Aggregate Computing
Computational Fields: a static view
Traditionally a map: Space 7→ Values
possibly: evolving over time, dynamically injected, stabilising
smoothly adapting to very heterogeneous domains
more easily “understood” on continuous and flat spatial domains
ranging to booleans, reals, vectors, functions
boolean channel in 2D numeric partition in 2D real-valued gradient in 3D
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 22 / 48
Introduction Aggregate Computing
Computational Fields revisited: a dynamic view
A field as a space-time structure: φ : D 7→ V
event E: a triple hδ, t, pi – device δ, “firing” at time t in position p
events domain D: a coherent set of events (devices cannot move too fast)
field values V : any data value
ú computation abstracts from/adapts to the underlying event
ú scheduling of events is essentially exogenous
Time
Space
Domain
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 23 / 48
Introduction Aggregate Computing
Computational Fields revisited: a dynamic view
A field as a space-time structure: φ : D 7→ V
event E: a triple hδ, t, pi – device δ, “firing” at time t in position p
events domain D: a coherent set of events (devices cannot move too fast)
field values V : any data value
ú computation abstracts from/adapts to the underlying event
ú scheduling of events is essentially exogenous
Time
Space
Domain
Time
Space
Field







  



 
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 23 / 48
Introduction Aggregate Computing
Computational Fields revisited: a dynamic view
A field as a space-time structure: φ : D 7→ V
event E: a triple hδ, t, pi – device δ, “firing” at time t in position p
events domain D: a coherent set of events (devices cannot move too fast)
field values V : any data value
ú computation abstracts from/adapts to the underlying event
ú scheduling of events is essentially exogenous
Time
Space
Domain
Time
Space
Field







  



 
will later show only snapshots of fields in 2D space..
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 23 / 48
Introduction Aggregate Computing
Aggregate programming as a functional approach
Functionally composing fields
Inputs: sensor fields, Output: actuator field
Computation is a pure function over fields (time embeds state!)
ú for this to be practical/expressive we need a good programming language
source destination
gradient distance
gradient
=
+
dilate
width
37
10
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 24 / 48
Introduction Aggregate Computing
Preview
How do we want that computation to be expressed?
source, dest and width as inputs
gradient, distance and dilate as reusable functions
ú note we are reusing and composing global-level, aggregate specs
source destination
gradient distance
gradient
=
+
dilate
width
37
10
def channel(source: Boolean, dest: Boolean, width: Double): Boolean = {
dilate(gradient(source) + gradient(dest) = distance(source,dest), width)
}
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 25 / 48
Introduction Aggregate Computing
Field calculus model
Key idea
a sort of λ-calculus with “everything is a field” philosophy!
Syntax – Reduced
e ::= x v e(e1, . . . , en) rep(e0){e} nbr{e} (expr)
v ::=  standard-values  λ (value)
λ ::= f o (x)=e (functional value)
F ::= def f(x) {e} (function definition)
Few explanations
v includes numbers, booleans, strings,..
..tuples/vectors/maps/any-ADT (of expressions)
f is a user-defined function (the key aggregate computing abstraction)
o is a built-in local operator (pure math, local sensors,..)
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 26 / 48
Introduction Aggregate Computing
Intuition of global-level (denotational) semantics
The four main constructs at work
⇒ values, application, evolution, and interaction – in aggregate guise
e ::= . . . v e(e1, . . . , en) rep(e0){e} nbr{e}
0
(x)=x+1
true t0,1
()
0
1
+
-
1
-1
ef(0,1)
ef
rep
0
(x)=x+1
t
v0
t
v1
..
rep(0){(x)=x+1}
nbr d
e
nbr{e}
φd=[d1→v1,..,dn→vn]
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 27 / 48
Introduction Aggregate Computing
Intuition of global-level semantics – More
Value v
A field constant in space and time, mapping any event to v
Function application e(e1, . . . , en)
e evaluates to a field of functions, assume it ranges to λ1, . . . , λn
this naturally induces a partition of the domain D1, . . . , Dn
now, join the fields: ∀i, λi (e1, . . . , en) restricted in Di
Repetition rep(e0){eλ}
the value of e0 where the restricted domain “begins”
elsewhere, unary function eλ is applied to previous value at each device
Neighbouring field construction nbr{e}
at each event gathers most recent value of e in neighbours (in restriction)
..what is neighbour is orthogonal (i.e., physical proximity)
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 28 / 48
Introduction Aggregate Computing
Key aspects of the semantics: network model
Platform abstract model
A node state θ (value-tree) updated at asynchronous rounds
At the end of the round, θ is made accessible to the neighbourhood
A node state is updated “against” recently received neighbours’ trees
Neighbour trees
aka, message queue
Evaluation tree
Nodes send their
evaluation tree to
neighbours at the end
of each computation round
Node A
Node B
Node C
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 29 / 48
Introduction Aggregate Computing
Tree evaluation: pictorial semantics
Neighbour trees Evaluation tree
rep
if
nbr fun
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 30 / 48
Introduction Aggregate Computing
Tree evaluation: pictorial semantics
Neighbour trees Evaluation tree
rep
if
nbr fun
time
persistence
domain
restriction
neighbour
alignment
tree
expansion
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 30 / 48
Introduction Aggregate Computing
Core mechanisms in the operational semantics
Orthogonally..
evaluation proceeds recursively on expression and neighbour trees
neighbour trees may be discarded on-the-fly if not “aligned” (restriction)
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 31 / 48
Introduction Aggregate Computing
Core mechanisms in the operational semantics
Orthogonally..
evaluation proceeds recursively on expression and neighbour trees
neighbour trees may be discarded on-the-fly if not “aligned” (restriction)
Function application e(e1, . . . , en)
evaluates the body against a filtered set of neighbours . . .
..i.e., only those which evaluated e to the same result
Repetition rep(e0){eλ}
if a previous value-tree of mine is available, evaluates eλ on its root
otherwise, evaluates e0
Neighbouring field construction nbr{e}
gather values from neighbour trees currently aligned
add my current evaluation of e
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 31 / 48
Introduction Aggregate Computing
Operational semantics as a blueprint for platform support
Requirements
a notion of the neighbourhood must be defined — wireless connectivity, physical
proximity . . .
nodes execute in asynchronous rounds, and emit a “round result”
a node needs to have recent round results of neighbours
by construction we tolerate losses of messages
by construction we tolerate various round frequencies
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 32 / 48
Introduction Aggregate Computing
Operational semantics as a blueprint for platform support
Requirements
a notion of the neighbourhood must be defined — wireless connectivity, physical
proximity . . .
nodes execute in asynchronous rounds, and emit a “round result”
a node needs to have recent round results of neighbours
by construction we tolerate losses of messages
by construction we tolerate various round frequencies
Platform details are very orthogonal to our programming model!
the above requirements can be met by various platforms
programming remains mostly unaltered!
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 32 / 48
ScaFi (Scala Fields)
A Scala toolkit providing an internal domain-specific language, libraries, a
simulation environment, and runtime support for practical aggregate computing
systems development
Introduction Aggregate Computing
ScaFi: Organization
SCAFI-CORE
SPALA
(AC PLATFORM)
SCAFI-TESTS
AKKA-CORE AKKA-REMOTING
SCAFI-SIMULATOR
SCAFI-SIMULATOR-GUI
SCAFI-STDLIB-EXT
SCAFI-DISTRIBUTED
SCAFI-COMMONS
(space-time abstractions)
DEMOS
depends on
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 34 / 48
Introduction Aggregate Computing
ScaFi: Design
it.unibo.scafi
Incarnation
AggregateProgram
StandardSensors
def nbrRange(): D
def currentTime(): Time
...
SpatialAbstraction
P Space[E]
D
TimeAbstraction
Time
Core
CNAME ID
Context
def selfId: ID
def exports(): Iterable[(ID,Export)]
def sense[T](ls: CNAME): Option[T]
def nbrSense[T](ns: CNAME)(nbr:ID): Option[T]
Export
def root[A](): A
Core
Engine
ContextImpl ExportImpl
factory : EngineFactory
Semantics
ExecutionTemplate
def round(ctx: Context): Export
RoundVM
Language
Constructs
def nbr[A](exp: = A): A
def rep[A](init: =A)(f: (A)=A): A
...
StandardLibrary
BlockG BlockC
BlockS ...
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 35 / 48
Introduction Aggregate Computing
ScaFi: syntax as a core language/API
trait Constructs {
// the unique identifier of the local device
def mid(): ID
// applies fun to the previous result, or to init at the first call
def rep[A](init: A)(fun: (A) = A): A
// evaluation of expr at the currently-considered neighbour
def nbr[A](expr: = A): A
// accumulates available evaluations of expr, with acc/init monoid
def foldhood[A](init: = A)(acc: (A,A)=A)(expr: = A): A
// splits computation: th where cond is true, el everywhere/time else
def branch[A](cond: = Boolean)(th: = A)(el: = A): A
// perception of local sensor
def sense[A](name: CNAME): A
// perception of neighbourhood sensor
def nbrvar[A](name: CNAME): A
...
}
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 36 / 48
Introduction Aggregate Computing
ScaFi: setup an aggregate application
package experiments
// STEP 1 Choose an incarnation (simulated or real)
import it.unibo.scafi.incarnations.BasicSimulationIncarnation._
// STEP 2 Define the aggregate program including the right libraries
class MyProgram extends AggregateProgram with Libs {
// STEP 2.1 Define main logic of the program
override def main(): Any = ...
}
// STEP 3 Platform/Node setup (both in simulation/real)
// STEP 3.1 in case of ScaFi simulation:
object SimulationRunner extends Launcher {
Settings.Sim_ProgramClass = experiments.MyAggregateProgram
Settings.ShowConfigPanel = true
launch()
}
So let us start playing with ScaFi!! 
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 37 / 48
Playing with ScaFi!
Contents
1 Tutorial Quickstart
2 Introduction
Context – Collective Adaptive Systems
Aggregate Computing
3 Playing with ScaFi!
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 38 / 48
Playing with ScaFi!
An aggregate computing playground – ScaFi Web!
https://scafi.github.io/web/
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 39 / 48
Playing with ScaFi!
Guided Examples!
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 40 / 48
Playing with ScaFi!
Example 1: static constant field
// 1. Select the incarnation
import it.unibo.alchemist.model.scafi.ScafiIncarnationForAlchemist._
// 2. define your aggregate program by extending 'AggregateProgram'
class Example1 extends AggregateProgram {
// 3. define the main method of the aggregate computing script
override def main(): Int = 22
}
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 41 / 48
Playing with ScaFi!
Example 3: sensor query
mid() provides the device ID
// Code in the AggregateProgram's main method:
mid()
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 42 / 48
Playing with ScaFi!
Example 4: sensor query
sense(name) reads the current value of a sensor
sense[Boolean](source)
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 43 / 48
Playing with ScaFi!
Example 5: neighbour interaction
foldhood(i)(f)(e) aggregates with f the neighbours’ values for e
// *Plus version does not consider the device itself in the neighbourhood
foldhoodPlus(0)((a,b) = a + b)(nbr(1))
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 44 / 48
Playing with ScaFi!
Example 6: stateful computations
rep(i)(f) updates a value (initially i) through function f
rep(0){ _ + 1 }
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 45 / 48
Playing with ScaFi!
Example 8: self-healing gradient
Field of minimum distances from source nodes
rep(Double.PositiveInfinity)(distance =
mux(sense[Boolean](source)) {
0.0
} {
minHoodPlus(nbr(distance) + nbrRange)
}
)
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 46 / 48
Programming (and Learning) Self-Adaptive  Self-Organising Behaviour
with ScaFi
for Swarms, Edge-Cloud Ecosystems, and More
Roberto Casadei roby.casadei@unibo.it
Gianluca Aguzzi gianluca.aguzzi@unibo.it
Danilo Pianini danilo.pianini@unibo.it
Mirko Viroli mirko.viroli@unibo.it
Alma Mater Studiorum – Università di Bologna
Talk @ International Conference on Autonomic Computing and Self-Organizing Systems (ACSOS)
11/12/2023
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 47 / 48
References
References I
[1] G. Aguzzi, R. Casadei, N. Maltoni, D. Pianini, and M. Viroli, “Scafi-web: A web-based application
for field-based coordination programming,” in Coordination Models and Languages - 23rd IFIP WG
6.1 International Conference, COORDINATION 2021, Held as Part of the 16th International
Federated Conference on Distributed Computing Techniques, DisCoTec 2021, Valletta, Malta, June
14-18, 2021, Proceedings, F. Damiani and O. Dardha, Eds., ser. Lecture Notes in Computer Science,
vol. 12717, Springer, 2021, pp. 285–299. doi: 10.1007/978-3-030-78142-2_18. [Online]. Available:
https://doi.org/10.1007/978-3-030-78142-2_18.
[2] M. Viroli, J. Beal, F. Damiani, G. Audrito, R. Casadei, and D. Pianini, “From distributed
coordination to field calculus and aggregate computing,” J. Log. Algebraic Methods Program.,
vol. 109, 2019. doi: 10.1016/j.jlamp.2019.100486.
[3] R. Casadei, M. Viroli, G. Aguzzi, and D. Pianini, “Scafi: A scala DSL and toolkit for aggregate
programming,” SoftwareX, vol. 20, p. 101 248, 2022. doi: 10.1016/j.softx.2022.101248. [Online].
Available: https://doi.org/10.1016/j.softx.2022.101248.
[4] G. Audrito, R. Casadei, F. Damiani, and M. Viroli, “Computation against a neighbour: Addressing
large-scale distribution and adaptivity with functional programming and scala,” Log. Methods
Comput. Sci., vol. 19, no. 1, 2023. doi: 10.46298/lmcs-19(1:6)2023. [Online]. Available:
https://doi.org/10.46298/lmcs-19(1:6)2023.
[5] R. Casadei, “Macroprogramming: Concepts, state of the art, and opportunities of macroscopic
behaviour modelling,” ACM Comput. Surv., vol. 55, no. 13s, 2023, issn: 0360-0300. doi:
10.1145/3579353.
[6] J. Beal, D. Pianini, and M. Viroli, “Aggregate programming for the internet of things,” IEEE
Computer, vol. 48, no. 9, pp. 22–30, 2015. doi: 10.1109/MC.2015.261.
Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023

Programming (and Learning) Self-Adaptive & Self-Organising Behaviour with ScaFi for Swarms, Edge-Cloud Ecosystems, and More

  • 1.
    Programming (and Learning)Self-Adaptive & Self-Organising Behaviour with ScaFi for Swarms, Edge-Cloud Ecosystems, and More Roberto Casadei roby.casadei@unibo.it Gianluca Aguzzi gianluca.aguzzi@unibo.it Danilo Pianini danilo.pianini@unibo.it Mirko Viroli mirko.viroli@unibo.it Alma Mater Studiorum – Università di Bologna Talk @ International Conference on Autonomic Computing and Self-Organizing Systems (ACSOS) 11/12/2023 Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 1 / 48
  • 2.
    Tutorial Quickstart Contents 1 TutorialQuickstart 2 Introduction Context – Collective Adaptive Systems Aggregate Computing 3 Playing with ScaFi! Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 2 / 48
  • 3.
    Tutorial Quickstart Tutorial references Wheredo I start? https://github.com/AggregateComputing/acsos-2023-scafi-tutorial: repository with the examples/exercises proposed in the tutorial Follow the README.md to run examples and setup a development environment. ScaFi-Web [1] (a online web simulator for ScaFi programs) https://scafi.github.io/web https://tomcat-glad-muskox.ngrok-free.app/ Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 3 / 48
  • 4.
    Tutorial Quickstart Frequently AskedQuestions (FAQs) I What is it all about? ScaFi is an aggregate programming language: it supports the development of self-organising behaviours. It is integrated into Alchemist: this allows to build simulations of networked systems that execute ScaFi programs. Where do I start? Slide 3 How can I quickly experiment with the programming model? ScaFi-Web [1] (a online web simulator for ScaFi programs) https://scafi.github.io/web https://tomcat-glad-muskox.ngrok-free.app/ Where do I learn more about the tools (ScaFi, Alchemist)? https://scafi.github.io/ http://alchemistsimulator.github.io/ Where do I learn more about the theory/research on aggregate computing (AC)? Check out relevant presentations and papers. https://www.slideshare.net/RobertoCasadei/ aggregate-computing-research-an-overview – a quick overview of AC research M. Viroli, J. Beal, F. Damiani, et al., “From distributed coordination to field calculus and aggregate computing,” J. Log. Algebraic Methods Program., vol. 109, 2019. doi: 10.1016/j.jlamp.2019.100486 – a survey about AC, its history, and main developments What is the research context? Check out Slide 8–Slide 16 Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 4 / 48
  • 5.
    Tutorial Quickstart Frequently AskedQuestions (FAQs) II What kind of systems can be programmed/developed with AC? Check out Slide 21 and Slide 8–Slide 16 Where do I get an overview of relevant literature about aggregate computing? https://www.slideshare.net/RobertoCasadei/ aggregate-computing-research-an-overview M. Viroli, J. Beal, F. Damiani, et al., “From distributed coordination to field calculus and aggregate computing,” J. Log. Algebraic Methods Program., vol. 109, 2019. doi: 10.1016/j.jlamp.2019.100486 What is the key literature about ScaFi? software: R. Casadei, M. Viroli, G. Aguzzi, et al., “Scafi: A scala DSL and toolkit for aggregate programming,” SoftwareX, vol. 20, p. 101 248, 2022. doi: 10.1016/j.softx.2022.101248. [Online]. Available: https://doi.org/10.1016/j.softx.2022.101248 [3] calculus: G. Audrito, R. Casadei, F. Damiani, et al., “Computation against a neighbour: Addressing large-scale distribution and adaptivity with functional programming and scala,” Log. Methods Comput. Sci., vol. 19, no. 1, 2023. doi: 10.46298/lmcs-19(1:6)2023. [Online]. Available: https://doi.org/10.46298/lmcs-19(1:6)2023 [4] Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 5 / 48
  • 6.
    Introduction Contents 1 Tutorial Quickstart 2Introduction Context – Collective Adaptive Systems Aggregate Computing 3 Playing with ScaFi! Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 6 / 48
  • 7.
    Introduction Context –Collective Adaptive Systems Contents 1 Tutorial Quickstart 2 Introduction Context – Collective Adaptive Systems Aggregate Computing 3 Playing with ScaFi! Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 7 / 48
  • 8.
    Introduction Context –Collective Adaptive Systems Context Modern IT systems are more and more complex increasing availability of wearable/mobile/embedded/flying devices increasing availability of heterogeneous wireless networks increasing availability of computational resources (edge/fog/cloud computing) increasing production of data everywhere and anytime The challenge Consider the worst-case possible scenario zillion of devices unpredictably located and moving in a space heterogeneous displacement, pervasive sensing/actuation computational services are contextual (proximity-based) and dynamic How can we program such systems? What are the right abstractions? Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 8 / 48
  • 9.
    Collective Adaptive Systems(CASs) Systems composed of possibly large set of component executing a collective task strongly relying on component interactions and showing inherent adaptivity.
  • 10.
    Introduction Context –Collective Adaptive Systems Examples: controlling a fleet of drones Issues Design techniques to reactively propagate information across the fleet Create algorithms for higher-level programming of the fleet Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 10 / 48
  • 11.
    Introduction Context –Collective Adaptive Systems Examples: pedestrian steering in (smart)cities Issue Design self-organisation algorithms for people navigation More generally, design map-based large-scale applications Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 11 / 48
  • 12.
    Introduction Context –Collective Adaptive Systems Examples: fine control of crowds Issues Evaluating the influence of environment in crowd formation Evaluating the influence of ambient sensors, cameras and wearable devices Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 12 / 48
  • 13.
    Introduction Context –Collective Adaptive Systems Collective Adaptive Systems Adaptation A system is adaptive if it can change its behaviour depending on circumstances, in order to better reach its goal ú Adaptivess in intrisic to open complex system (like CASs) Kind of adaptiveness, self-* properties Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 13 / 48
  • 14.
    Introduction Context –Collective Adaptive Systems Kind of adaptiveness Self-adaptiveness: general level self-adaptiveness ú the ultimate property we perceive from outside the terms “self-*” recalls a property achieved in autonomy subcases: self-managing, -governing, -maintenance, -control Major level internal properties related to overall system management subcases: self-configuring, self-healing, self-optimising, self-protecting defined in the context of autonomic computing Primitive level internal properties related to primitive aspects subcases: self-awareness, context-awareness means being able to properly perceive the own state, context, . . . Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 14 / 48
  • 15.
    Introduction Context –Collective Adaptive Systems The case of self-organisation Self-organisation The ability of creating spatial/temporal patterns out of the local interaction of individuals Self-adaptive vs. self-organising self-adaptive: a top-down way of achieving adaptiveness ú we have the adaptation goal, and accordingly guide components self-organising: a bottom-up way of achieving adaptiveness ú the adaptive behaviour emerges from local interactions The typical approach in large-scale CASs Born in the context of swarm intelligence Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 15 / 48
  • 16.
    Introduction Context –Collective Adaptive Systems Good Abstraction for CASs? Ideas specify overall behaviour, not individual device program abstract from the actual shape of interactions automatically adapt to environment details focus on how the global output pattern can be obtained from global inputs focus on both spatial and temporal computing patterns ú the ideas around macro-programming paradigms [5]! Aggregate computing Buzz . . . Ruccurent abstractions: Ensembles & collective tasks Self-organising information flows Self-healing collective structures (e.g., gradients) [5] R. Casadei, “Macroprogramming: Concepts, state of the art, and opportunities of macroscopic behaviour modelling,” ACM Comput. Surv., vol. 55, no. 13s, 2023, issn: 0360-0300. doi: 10.1145/3579353 Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 16 / 48
  • 17.
    Introduction Aggregate Computing Contents 1Tutorial Quickstart 2 Introduction Context – Collective Adaptive Systems Aggregate Computing 3 Playing with ScaFi! Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 17 / 48
  • 18.
    Introduction Aggregate Computing AggregateComputing (AC) in 1 Slide Self-org-like computational model interaction: repeated msg exchange with neighbours behaviour: repeated execution of async rounds of sense – compute – (inter)act formal model of executions: event structures abstraction: computational fields (dev/evt 7→ V) formal core language: field calculus [2] paradigm: functional, macro-programming source destination gradient distance gradient <= + dilate width 37 10 def channel(source: Boolean, target: Boolean, width: Double) = dilate(gradient(source) + gradient(target) <= distance(source, target), width) M. Viroli, J. Beal, F. Damiani, et al., “From distributed co- ordination to field calculus and aggregate computing,” J. Log. Algebraic Methods Program., vol. 109, 2019. doi: 10.1016/j. jlamp.2019.100486 δ0 δ1 δ2 δ3 δ4 device time 0 0 0 1 0 2 0 3 0 4 1 0 1 1 1 2 1 3 1 4 1 5 2 0 2 1 2 2 2 3 3 0 3 1 3 2 3 3 3 4 3 5 4 0 4 1 4 2 m e s s a g e self-message reboot sensors local functions actuators Application Code Developer APIs Field Calculus Constructs Resilient Coordination Operators Device Capabilities functions rep nbr T G C functions communication state Perception Perception summarize average regionMax … Action Action State State Collective Behavior Collective Behavior distanceTo broadcast partition … timer lowpass recentTrue … collectivePerception collectiveSummary managementRegions … Crowd Management Crowd Management dangerousDensity crowdTracking crowdWarning safeDispersal restriction self­stabilisation J. Beal, D. Pianini, and M. Viroli, “Aggregate programming for the internet of things,” IEEE Computer, vol. 48, no. 9, pp. 22–30, 2015. doi: 10.1109/MC.2015.261 Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 18 / 48
  • 19.
    Aggregate Computing Program theaggregate, not the individual device ○ Computing Machine ú an ensemble of devices as a single body, fading the actual space ○ Elaboration process ú atomic manipulation of a collective data structure (computational field) ○ Networked computation ú a proximity-based self-organising system hidden “under-the-hood”
  • 20.
    Introduction Aggregate Computing Computationalmodel – self-organisation-like execution Continuous communication with neighbours only (údecentralisation) Continous execution of async rounds of sense - compute - communicate Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 20 / 48
  • 21.
    Introduction Aggregate Computing Applicability:in a nutshell Essentially, this approach can be used to program any system that can be recast to the following Structure: network of locally interacting agents neighbouring relationship: e.g. based on network connectivity, physical distance, social relationship, or whatever dynamicity/openness: devices may move/fail, enter/exit the system at runtime, and neighbourhoods may change Interaction: with neighbours: asynchronous message passing with environment: via sensors and actuators Behaviour: sense–compute–interact sense: acquire context (messages from neighbours, values from sensors) compute: mapping context to (inter-)action interact: sending messages to neighbours and running actuation So, best for applications that are: progressive/long-running: require multiple coordinated steps/rounds of local processing and action Assuming this system model, AC is about how to specify the “compute” part so that global emergent outcomes can be eventually attained Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 21 / 48
  • 22.
    Introduction Aggregate Computing ComputationalFields: a static view Traditionally a map: Space 7→ Values possibly: evolving over time, dynamically injected, stabilising smoothly adapting to very heterogeneous domains more easily “understood” on continuous and flat spatial domains ranging to booleans, reals, vectors, functions boolean channel in 2D numeric partition in 2D real-valued gradient in 3D Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 22 / 48
  • 23.
    Introduction Aggregate Computing ComputationalFields revisited: a dynamic view A field as a space-time structure: φ : D 7→ V event E: a triple hδ, t, pi – device δ, “firing” at time t in position p events domain D: a coherent set of events (devices cannot move too fast) field values V : any data value ú computation abstracts from/adapts to the underlying event ú scheduling of events is essentially exogenous Time Space Domain Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 23 / 48
  • 24.
    Introduction Aggregate Computing ComputationalFields revisited: a dynamic view A field as a space-time structure: φ : D 7→ V event E: a triple hδ, t, pi – device δ, “firing” at time t in position p events domain D: a coherent set of events (devices cannot move too fast) field values V : any data value ú computation abstracts from/adapts to the underlying event ú scheduling of events is essentially exogenous Time Space Domain Time Space Field                Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 23 / 48
  • 25.
    Introduction Aggregate Computing ComputationalFields revisited: a dynamic view A field as a space-time structure: φ : D 7→ V event E: a triple hδ, t, pi – device δ, “firing” at time t in position p events domain D: a coherent set of events (devices cannot move too fast) field values V : any data value ú computation abstracts from/adapts to the underlying event ú scheduling of events is essentially exogenous Time Space Domain Time Space Field                will later show only snapshots of fields in 2D space.. Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 23 / 48
  • 26.
    Introduction Aggregate Computing Aggregateprogramming as a functional approach Functionally composing fields Inputs: sensor fields, Output: actuator field Computation is a pure function over fields (time embeds state!) ú for this to be practical/expressive we need a good programming language source destination gradient distance gradient = + dilate width 37 10 Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 24 / 48
  • 27.
    Introduction Aggregate Computing Preview Howdo we want that computation to be expressed? source, dest and width as inputs gradient, distance and dilate as reusable functions ú note we are reusing and composing global-level, aggregate specs source destination gradient distance gradient = + dilate width 37 10 def channel(source: Boolean, dest: Boolean, width: Double): Boolean = { dilate(gradient(source) + gradient(dest) = distance(source,dest), width) } Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 25 / 48
  • 28.
    Introduction Aggregate Computing Fieldcalculus model Key idea a sort of λ-calculus with “everything is a field” philosophy! Syntax – Reduced e ::= x v e(e1, . . . , en) rep(e0){e} nbr{e} (expr) v ::= standard-values λ (value) λ ::= f o (x)=e (functional value) F ::= def f(x) {e} (function definition) Few explanations v includes numbers, booleans, strings,.. ..tuples/vectors/maps/any-ADT (of expressions) f is a user-defined function (the key aggregate computing abstraction) o is a built-in local operator (pure math, local sensors,..) Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 26 / 48
  • 29.
    Introduction Aggregate Computing Intuitionof global-level (denotational) semantics The four main constructs at work ⇒ values, application, evolution, and interaction – in aggregate guise e ::= . . . v e(e1, . . . , en) rep(e0){e} nbr{e} 0 (x)=x+1 true t0,1 () 0 1 + - 1 -1 ef(0,1) ef rep 0 (x)=x+1 t v0 t v1 .. rep(0){(x)=x+1} nbr d e nbr{e} φd=[d1→v1,..,dn→vn] Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 27 / 48
  • 30.
    Introduction Aggregate Computing Intuitionof global-level semantics – More Value v A field constant in space and time, mapping any event to v Function application e(e1, . . . , en) e evaluates to a field of functions, assume it ranges to λ1, . . . , λn this naturally induces a partition of the domain D1, . . . , Dn now, join the fields: ∀i, λi (e1, . . . , en) restricted in Di Repetition rep(e0){eλ} the value of e0 where the restricted domain “begins” elsewhere, unary function eλ is applied to previous value at each device Neighbouring field construction nbr{e} at each event gathers most recent value of e in neighbours (in restriction) ..what is neighbour is orthogonal (i.e., physical proximity) Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 28 / 48
  • 31.
    Introduction Aggregate Computing Keyaspects of the semantics: network model Platform abstract model A node state θ (value-tree) updated at asynchronous rounds At the end of the round, θ is made accessible to the neighbourhood A node state is updated “against” recently received neighbours’ trees Neighbour trees aka, message queue Evaluation tree Nodes send their evaluation tree to neighbours at the end of each computation round Node A Node B Node C Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 29 / 48
  • 32.
    Introduction Aggregate Computing Treeevaluation: pictorial semantics Neighbour trees Evaluation tree rep if nbr fun Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 30 / 48
  • 33.
    Introduction Aggregate Computing Treeevaluation: pictorial semantics Neighbour trees Evaluation tree rep if nbr fun time persistence domain restriction neighbour alignment tree expansion Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 30 / 48
  • 34.
    Introduction Aggregate Computing Coremechanisms in the operational semantics Orthogonally.. evaluation proceeds recursively on expression and neighbour trees neighbour trees may be discarded on-the-fly if not “aligned” (restriction) Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 31 / 48
  • 35.
    Introduction Aggregate Computing Coremechanisms in the operational semantics Orthogonally.. evaluation proceeds recursively on expression and neighbour trees neighbour trees may be discarded on-the-fly if not “aligned” (restriction) Function application e(e1, . . . , en) evaluates the body against a filtered set of neighbours . . . ..i.e., only those which evaluated e to the same result Repetition rep(e0){eλ} if a previous value-tree of mine is available, evaluates eλ on its root otherwise, evaluates e0 Neighbouring field construction nbr{e} gather values from neighbour trees currently aligned add my current evaluation of e Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 31 / 48
  • 36.
    Introduction Aggregate Computing Operationalsemantics as a blueprint for platform support Requirements a notion of the neighbourhood must be defined — wireless connectivity, physical proximity . . . nodes execute in asynchronous rounds, and emit a “round result” a node needs to have recent round results of neighbours by construction we tolerate losses of messages by construction we tolerate various round frequencies Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 32 / 48
  • 37.
    Introduction Aggregate Computing Operationalsemantics as a blueprint for platform support Requirements a notion of the neighbourhood must be defined — wireless connectivity, physical proximity . . . nodes execute in asynchronous rounds, and emit a “round result” a node needs to have recent round results of neighbours by construction we tolerate losses of messages by construction we tolerate various round frequencies Platform details are very orthogonal to our programming model! the above requirements can be met by various platforms programming remains mostly unaltered! Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 32 / 48
  • 38.
    ScaFi (Scala Fields) AScala toolkit providing an internal domain-specific language, libraries, a simulation environment, and runtime support for practical aggregate computing systems development
  • 39.
    Introduction Aggregate Computing ScaFi:Organization SCAFI-CORE SPALA (AC PLATFORM) SCAFI-TESTS AKKA-CORE AKKA-REMOTING SCAFI-SIMULATOR SCAFI-SIMULATOR-GUI SCAFI-STDLIB-EXT SCAFI-DISTRIBUTED SCAFI-COMMONS (space-time abstractions) DEMOS depends on Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 34 / 48
  • 40.
    Introduction Aggregate Computing ScaFi:Design it.unibo.scafi Incarnation AggregateProgram StandardSensors def nbrRange(): D def currentTime(): Time ... SpatialAbstraction P Space[E] D TimeAbstraction Time Core CNAME ID Context def selfId: ID def exports(): Iterable[(ID,Export)] def sense[T](ls: CNAME): Option[T] def nbrSense[T](ns: CNAME)(nbr:ID): Option[T] Export def root[A](): A Core Engine ContextImpl ExportImpl factory : EngineFactory Semantics ExecutionTemplate def round(ctx: Context): Export RoundVM Language Constructs def nbr[A](exp: = A): A def rep[A](init: =A)(f: (A)=A): A ... StandardLibrary BlockG BlockC BlockS ... Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 35 / 48
  • 41.
    Introduction Aggregate Computing ScaFi:syntax as a core language/API trait Constructs { // the unique identifier of the local device def mid(): ID // applies fun to the previous result, or to init at the first call def rep[A](init: A)(fun: (A) = A): A // evaluation of expr at the currently-considered neighbour def nbr[A](expr: = A): A // accumulates available evaluations of expr, with acc/init monoid def foldhood[A](init: = A)(acc: (A,A)=A)(expr: = A): A // splits computation: th where cond is true, el everywhere/time else def branch[A](cond: = Boolean)(th: = A)(el: = A): A // perception of local sensor def sense[A](name: CNAME): A // perception of neighbourhood sensor def nbrvar[A](name: CNAME): A ... } Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 36 / 48
  • 42.
    Introduction Aggregate Computing ScaFi:setup an aggregate application package experiments // STEP 1 Choose an incarnation (simulated or real) import it.unibo.scafi.incarnations.BasicSimulationIncarnation._ // STEP 2 Define the aggregate program including the right libraries class MyProgram extends AggregateProgram with Libs { // STEP 2.1 Define main logic of the program override def main(): Any = ... } // STEP 3 Platform/Node setup (both in simulation/real) // STEP 3.1 in case of ScaFi simulation: object SimulationRunner extends Launcher { Settings.Sim_ProgramClass = experiments.MyAggregateProgram Settings.ShowConfigPanel = true launch() } So let us start playing with ScaFi!! Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 37 / 48
  • 43.
    Playing with ScaFi! Contents 1Tutorial Quickstart 2 Introduction Context – Collective Adaptive Systems Aggregate Computing 3 Playing with ScaFi! Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 38 / 48
  • 44.
    Playing with ScaFi! Anaggregate computing playground – ScaFi Web! https://scafi.github.io/web/ Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 39 / 48
  • 45.
    Playing with ScaFi! GuidedExamples! Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 40 / 48
  • 46.
    Playing with ScaFi! Example1: static constant field // 1. Select the incarnation import it.unibo.alchemist.model.scafi.ScafiIncarnationForAlchemist._ // 2. define your aggregate program by extending 'AggregateProgram' class Example1 extends AggregateProgram { // 3. define the main method of the aggregate computing script override def main(): Int = 22 } Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 41 / 48
  • 47.
    Playing with ScaFi! Example3: sensor query mid() provides the device ID // Code in the AggregateProgram's main method: mid() Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 42 / 48
  • 48.
    Playing with ScaFi! Example4: sensor query sense(name) reads the current value of a sensor sense[Boolean](source) Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 43 / 48
  • 49.
    Playing with ScaFi! Example5: neighbour interaction foldhood(i)(f)(e) aggregates with f the neighbours’ values for e // *Plus version does not consider the device itself in the neighbourhood foldhoodPlus(0)((a,b) = a + b)(nbr(1)) Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 44 / 48
  • 50.
    Playing with ScaFi! Example6: stateful computations rep(i)(f) updates a value (initially i) through function f rep(0){ _ + 1 } Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 45 / 48
  • 51.
    Playing with ScaFi! Example8: self-healing gradient Field of minimum distances from source nodes rep(Double.PositiveInfinity)(distance = mux(sense[Boolean](source)) { 0.0 } { minHoodPlus(nbr(distance) + nbrRange) } ) Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 46 / 48
  • 52.
    Programming (and Learning)Self-Adaptive Self-Organising Behaviour with ScaFi for Swarms, Edge-Cloud Ecosystems, and More Roberto Casadei roby.casadei@unibo.it Gianluca Aguzzi gianluca.aguzzi@unibo.it Danilo Pianini danilo.pianini@unibo.it Mirko Viroli mirko.viroli@unibo.it Alma Mater Studiorum – Università di Bologna Talk @ International Conference on Autonomic Computing and Self-Organizing Systems (ACSOS) 11/12/2023 Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023 47 / 48
  • 53.
    References References I [1] G.Aguzzi, R. Casadei, N. Maltoni, D. Pianini, and M. Viroli, “Scafi-web: A web-based application for field-based coordination programming,” in Coordination Models and Languages - 23rd IFIP WG 6.1 International Conference, COORDINATION 2021, Held as Part of the 16th International Federated Conference on Distributed Computing Techniques, DisCoTec 2021, Valletta, Malta, June 14-18, 2021, Proceedings, F. Damiani and O. Dardha, Eds., ser. Lecture Notes in Computer Science, vol. 12717, Springer, 2021, pp. 285–299. doi: 10.1007/978-3-030-78142-2_18. [Online]. Available: https://doi.org/10.1007/978-3-030-78142-2_18. [2] M. Viroli, J. Beal, F. Damiani, G. Audrito, R. Casadei, and D. Pianini, “From distributed coordination to field calculus and aggregate computing,” J. Log. Algebraic Methods Program., vol. 109, 2019. doi: 10.1016/j.jlamp.2019.100486. [3] R. Casadei, M. Viroli, G. Aguzzi, and D. Pianini, “Scafi: A scala DSL and toolkit for aggregate programming,” SoftwareX, vol. 20, p. 101 248, 2022. doi: 10.1016/j.softx.2022.101248. [Online]. Available: https://doi.org/10.1016/j.softx.2022.101248. [4] G. Audrito, R. Casadei, F. Damiani, and M. Viroli, “Computation against a neighbour: Addressing large-scale distribution and adaptivity with functional programming and scala,” Log. Methods Comput. Sci., vol. 19, no. 1, 2023. doi: 10.46298/lmcs-19(1:6)2023. [Online]. Available: https://doi.org/10.46298/lmcs-19(1:6)2023. [5] R. Casadei, “Macroprogramming: Concepts, state of the art, and opportunities of macroscopic behaviour modelling,” ACM Comput. Surv., vol. 55, no. 13s, 2023, issn: 0360-0300. doi: 10.1145/3579353. [6] J. Beal, D. Pianini, and M. Viroli, “Aggregate programming for the internet of things,” IEEE Computer, vol. 48, no. 9, pp. 22–30, 2015. doi: 10.1109/MC.2015.261. Casedei (DISI, Univ. Bologna) Programming with ScaFi! 11/12/2023