Reconfigurable Intelligent Surfaces (RIS) are transformative technologies for next-generation wireless communication, offering advanced control over electromagnetic wave propagation. While RIS have been extensively studied, Stacked Intelligent Metasurfaces (SIM), which extend the RIS concept to multi-layered systems, present significant modeling and optimization challenges. This work addresses these challenges by introducing a new optimization framework for heterogeneous SIM architectures that, compared to previous approaches, is based on a comprehensive model without relying on specific assumptions, allowing for a broader applicability of the results. To this end, we first present a model based on multi-port network theory for characterizing a general electromagnetic collaborative object (ECO) and derive a general framework for ECO optimization. We then introduce the SIM as an ECO with a specific architecture and provide insights into SIM optimization for various architectures, discussing the complexity in each case. Next, we analyze the impact of commonly used assumptions, and as a further contribution, we propose a backpropagation algorithm for implementing the gradient descent method for a simplified SIM configuration.