artofscience / SAOR

Sequential Approximate Optimization Repository
GNU General Public License v3.0
5 stars 1 forks source link

Implements mixed variables using sets. #65

Closed MaxvdKolk closed 2 years ago

MaxvdKolk commented 2 years ago

This explores an implementation of the mixed intervening variable class. Although the original implementation works well, I felt it was a bit hard to grasp directly what is going on, especially as it uses boolean masks/indexing to get specific variables out and holds the variable sets as lists that can be empty.

An alternative might be the following, where the boolean indexing is changed to sets explicitly. So, in stead of maintaining arrays of booleans to extract the responses, e.g. self.all_resp[responses], we store those indices directly in a set. The conversion to sets automatically ensures uniqueness and provides routines to add/remove entries (as compared to toggling the boolean indexes to True or False).

To make the idea of a mapping from responses to variable sets more explicit, we might adopt a dictionary for this, rather than the lists of (possibly empty) variable arrays. Before, we would have for n responses, variables = [np.empty(0, dtype=int)] * n, where for a response r the array would be updated accordingly, e.g. variables[r] = [0, 1, 4]. The dictionary is fairly similar of course, where we can still assign variables[r] = ..., however, rather than storing the empty lists, we can remove the key-value pair from the dictionary.

The downside of this implementation is that we work with sets, and need to convert these sets to lists before indexing into arrays (see evaluate_for_each_response function).


The tests work and the interface is the same, so it is merely related to the specific implementation. If you think this helps clarifying the implementation of the mixed intervening variable sets, we might merge this. Otherwise, we might want to clarify the original code somewhat more, especially for people who are reading the code the first time.