1. A method for distributing, during a given time period, electricity flows in a system, the system including:

at least one energy production element and one energy consumption element of electricity from the following elements:

an electricity distribution network suitable for supplying and/or receiving energy from other elements of the system,

an electricity storage element suitable for receiving energy production from the at least one energy production element of

the system and/or for supplying energy to the at least one energy consumption element of the system,

a building suitable for consuming energy produced locally and/or from the distribution network and/or from a storage element,

and

a local electricity production source suitable for supplying energy to the distribution network and/or building and/or storage

element; and

a monitoring unit configured to measure a state of charge of each storage element and electrical powers produced and consumed

by each of the elements of the system,

wherein the method comprises:

initialisation steps including:

a) defining physical system modelling parameters, wherein the physical system modelling parameters are physical quantities

conditioning the energy consumption or production of the at least one energy production element and one energy consumption

element,

b) defining a model of the system, in the form of state representation using the physical system modelling parameters determined

in step a), wherein the model of the system is defined with the following state representation:

where xk and xk+1 are the states x of the system at times k and k+1, yk is an output parameter y of the system at the time k, uk is a control parameter at the time k, wk is a disturbance parameter w at the time k and A, B, G, C, D, F are constant matrices,

c) defining optimisation parameters for solving an optimisation problem, and

d) predefining the optimisation problem, over the given time period, for the distribution of the electricity flows of the

system using the model defined in step b), wherein predefining the optimisation problem includes modelling a behaviour of

the system for the given time period using the following equation:

Y=?x0+??+?wwhere x0 is an initial state of the system, Y, ? and ? are power, control and disturbance parameters of the system respectively, for the given time period, expressed in

matrix form, and ?, ? and ? are constant matrices wherein the elements are dependent on the constant matrices A, B, G, C,

D and F; and

iterative steps at successive updating times of the given time period, including:

e) measuring a state of charge of each electricity storage element, and electrical powers produced and consumed by each of

the various elements of the system,

f) updating a preview of the behaviour of the production and consumption elements of the system for another time period of

a same duration as a duration of the given time period and starting at a considered updating time,

g) defining a formulation of the optimisation problem for the another time period, wherein formulating the optimisation problem

comprises at least a definition of an objective function f(?) and updating of optimisation problem constraints, these constraints

being expressed in the form of the following inequality:

Ain??bin where ? is the control parameter for the given time period, Ain a matrix dependent on the matrix ?, and bin a matrix dependent on the matrices ? and ?, on minimum Ymin and maximum values Ymax of the output parameter Y, and on a maximum variation ??max of the control ?,

h) solving the optimisation problem in step g) by using a solver to generate a solution, and

i) applying electricity distribution controls in the system using the solution from step h), until a next iteration of the

successive updating times of the given time period.