What is the difference between afferent couplings and efferent couplings of a class? - oop

Code quality metric tool like Sonar does provide the ability to drill down to a class and find out the number of:
Afferent (incoming) couplings
Efferent (outgoing) couplings
What are these two parameters? Can you please describe with a simple contrived example?

According to wikipedia:
Afferent Couplings (Ca): The number of classes in other packages that depend upon classes within the package is an indicator of the package's responsibility. Afferent = incoming.
Efferent Couplings (Ce): The number of classes in other packages that the classes in the package depend upon is an indicator of the package's dependence on externalities. Efferent = outgoing.
So, if you have classes (or packages or whatever) with the following structure:
class Foo {
Quux q;
}
class Bar {
Quux q;
}
class Quux {
// ...
}
Then Foo and Bar each have one efferent coupling, and Quux has two afferent couplings.

Since you mentioned Sonar, here is the definition provided by their documentation page
Afferent couplings : A class afferent couplings is a measure of how
many other classes use the specific class.
Efferent couplings : A class efferent couplings is a measure of how
many different classes are used by the specific class.

Coupling is a measure of dependencies.
Afferent Coupling :
Who depends on you.
Measure of how many other packages use a specific package.
Incoming dependencies.
Efferent Coupling :
Who do you depend on.
Measure of how many different packages are used by a specific package.
Outgoing dependencies.

In the booke fundamentals of software architecture these two concepts are used to calculate the instability of a class or package. The formula ce/ce+ca will give you the insrability (buginess factor).

Other users have already given answers from the definition perspective. Here, I have given an example of Afferent Coupling (Ca) and Efferent Coupling (Ce) for package-level granularity.
Suppose, package1 has 3 classes (i.e., A, B, C) and package2 has 2 classes (i.e., P, Q). For package1, class A has 2 out-going edges, class B has 1 out-going edge, and class C has 1 in-coming edge. Similarly, for package2, class P has 2 in-coming edges, and class Q has 1 in-coming and 1 out-going edge. [see the figure].
To calculate the Ca for a package, count the number of classes out of the package that has dependencies on it. You can proceed one by one for all classes of that package and then, take the union of dependent classes.
To calculate the Ce for a package, count the number of classes dependent on other packages inside the analyzed package. Count for all classes of that package and then, take the union.
So, the calculation will be as follows--
package1
Ca = {} union {} union {Q}
= {Q}
= 1
Ce = {P, Q} union {P} union {}
= {P, Q,}
= 2
package2
Ca = {A, B} union {A}
= {A, B}
= 2
Ce = {} union {C}
= {C}
= 1
Afferent Coupling and Efferent Coupling are used to calculate the Instability (I) of a package also. Instability is the ratio between Efferent Coupling (Ce) and the total package coupling (Ce + Ca). The formula is
I = Ce / (Ce + Ca)
The value of instability is between 0 to 1. The value towards 1 means unstable package (i.e., prone to change) and the value towards 0 means stable package.
For this example,
Instability for package1 = 2/(1+2) = 0.67 and,
Instability for package2 = 1/(2+1) = 0.33
So, we can conclude that package1 is more unstable than package2 by measuring the afferent and efferent coupling.
For additional info you may check this paper also.

Related

In CGAL, can one convert a triangulation in more than three dimensions to a polytope?

If this question would be more appropriate on a related site, let me know, and I'd be happy to move it.
I have 165 vertices in ℤ11, all of which are at a distance of √8 from the origin and are extreme points on their corresponding convex hull. CGAL is able to calculate their d-dimensional triangulation in only 133 minutes on my laptop using just under a gigabyte of RAM.
Magma manages a similar 66 vertex case quite quickly, and, crucially for my application, it returns an actual polytope instead of a triangulation. Thus, I can view each d-dimensional face as a single object which can be bounded by an arbitrary number of vertices.
Additionally, although less essential to my application, I can also use Graph : TorPol -> GrphUnd to calculate all the topological information regarding how those faces are connected, and then AutomorphismGroup : Grph -> GrpPerm, ... to find the corresponding automorphism group of that cell structure.
Unfortunately, when applied to the original polytope, Magma's AutomorphismGroup : TorPol -> GrpMat only returns subgroups of GLd(ℤ), instead of the full automorphism group G, which is what I'm truly hoping to calculate. As a matrix group, G ∉ GL11(ℤ), but is instead ∈ GL11(𝔸), where 𝔸 represents the algebraic numbers. In general, I won't need the full algebraic closure of the rationals, ℚ̅, but just some field extension. However, I could make use of any non-trivially powerful representation of G.
With two days of calculation, Magma can manage the 165 vertex case, but is only able to provide information about the polytope's original 165 vertices, 10-facets, and volume. However, attempting to enumerate the d-faces, for any 2 ≤ d < 10, quickly consumes the 256 GB of RAM I have at my disposal.
CGAL's triangulation, on the other hand, only calculates collections of d-simplices, all of which have d + 1 vertices. It seems possible to derive the same facial information from such a triangulation, but I haven't thought of an easy way to code that up.
Am I missing something obvious in CGAL? Do you have any suggestions for alternative ways to calculate the polytope's face information, or to find the full automorphism group of my set of points?
You can use the package Combinatorial maps in CGAL, that is able to represent polytopes in nD. A combinatorial map describes all cells and all incidence and adjacency relations between the cells.
In this package, there is an undocumented method are_cc_isomorphic allowing to test if an isomorphism exist from two starting points. I think you can use this method from all possible pair of starting points to find all automorphisms.
Unfortunatly, there is no method to build a combinatorial map from a dD triangulation. Such method exists in 3D (cf. this file). It can be extended in dD.

Gurobi- How to use indicator constraints in version 7.0?

The newest version of Gurobi allows for the use of indicator constraints but I can't figure out from the user manual how to implement these with the Matlab API.
Unfortunately, indicator constraints are not supported by the Gurobi MATLAB and R interfaces. These interfaces use a matrix representation. For example, for a linear program in canonical form:
max ct x
Ax = b
x ≥ 0
The interface takes the matrix A and vectors b and c, and returns the optimal solution. Unfortunately, this means that high-level representations like piecewise linear functions or indicator constraints are beyond the scope of the MATLAB and R interfaces. They are available for Python, if that helps.

Schemata survival in genetic algorithm

I'm having some problems in solving this exercise about schemata in genetic algorithms. Suppose I have the following situation, where three parents {1101, 0101, 0001} have, respectively, fitness {0.7, 4.3, 3.5} with respect to an unknown fitness function. The question is: which schemata will have the highest survival probability in the case of a maximization problem? The possible answers I had been given are: { ** 01}, {0 *** }, {***1} and {*101}.
Thank you in advance!
For the general case the schema theorem states that the schema with above average fitness, short defining length and lower order is more likely to survive.
For a schema H:
the order o(H) = number of fixed bit (e.g. o({01*0*}) = 3)
the defining length δ(H) = distance between the first and the last fixed bits (e.g. δ({*0*10}) = 3)
the probability of a gene not being changed is (1 - p) where p is the mutation probability. So the probability a schema H survives under mutation is S(H) = (1-p) ^ o(H)
...but this isn't the general case.
Every individual matches the two schemas {**01} and {***1}.
No matter what parent is selected for crossover / copy (these operations are fitness-dependent), children will match (at least before mutation) both schemas (with 100% probability).
Assuming mutation is applied gene by gene, for a schema H to survive, all fixed bits must remain unchanged. So {***1} is more likely to survive (has lower order).

Optimization Algorithm vs Regression Models

Currently, I'm dealing with forecasting problems. I have a reference that used linear function to represent the input and output data.
y = po + p1.x1 + p2.x2
Both of x1 and x2 are known input; y is output; p0, p1, and p2 are the coefficient. Then, he used all the training data and Least Square Estimation (LSE) method to find the optimal coefficient (p0, p1, p2) to build the model.
My question is if he already used the LSE algorithm, can I try to improve his method by using any optimization algorithm (PSO or GA for example) to try find better coefficient value?
You answered this yourself:
Blockquote Then, he used all the training data and Least Square Estimation (LSE) method to find the optimal coefficient (p0, p1, p2) to build the model.
Because a linear-model is quite easy to optimize, the LSE method obtained a global optimum (ignoring subtle rounding-errors and early-stopping/tolerance errors). Without changing the model, there is no gain in terms of using other coefficients, independent on the usage of meta-heuristics lika GA.
So you may modify the model, or add additional data (feature-engineering: e.g. product of two variables; kernel-methods).
One thing to try: Support-Vector machines. These are also convex and can be trained efficiently (with not too much data). They are also designed to work well with kernels. An additional advantage (compared with more complex models: e.g. non-convex): they are quite good regarding generalization which seems to be important here because you don't have much data (sounds like a very small dataset).
See also #ayhan's comment!

All pairs maximally disjoint paths algorithm

Suppose that there are multiple source destination pairs in an undirected graph. I want to generate disjoint paths for multiple pairs. What would be the complexity of such problem? Is there any polynomial heuristic for finding edge-disjoint paths for these pairs? (i.e. path between s1 and d1 should not have any common edges with the path between s2 and d2)
This looks like a variant of the multi-commodity flow problem: http://en.wikipedia.org/wiki/Multi-commodity_flow_problem
Treat each source/sink pair as a new commodity, and give your edges unit weights to enforce disjoint paths. Now search the literature for approximations to this class of MCFP with unit capacities.
Your problem is NP-hard, even for the case of two sources and two sinks. It becomes polynomially solvable if you stop caring which source matches with which sink.