Skip to content

Merge implems - #5

Open
VincentAuriau wants to merge 11 commits into
mainfrom
merge-implems
Open

Merge implems#5
VincentAuriau wants to merge 11 commits into
mainfrom
merge-implems

Conversation

@VincentAuriau

Copy link
Copy Markdown
Collaborator

No description provided.

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @VincentAuriau, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request establishes a foundational framework for simulating complex decision-making processes and subsequently inferring the underlying utility functions. It introduces new capabilities for generating diverse synthetic datasets, significantly enhances the core logic for modeling decision-makers with a more flexible utility function representation, and integrates powerful optimization tools to estimate these utility functions from the generated data. These changes collectively improve the system's ability to model and analyze decision-making behaviors.

Highlights

  • New Data Generation Module: Introduced a SyntheticDataGenerator class to create synthetic preference and indifference data, simulating various decision-making scenarios based on underlying DecisionMaker models.
  • Refactored Decision Maker Logic: The DecisionMaker class has been significantly overhauled to utilize a more robust piecewise linear utility function representation, moving from a slope-based approach to one defined by coefficients and marginal weights. This includes new helper functions for utility calculation and indifference point determination.
  • Gurobi-based UTA Implementations: Added WorstUTA and UTASpaceDiameter classes, which provide Gurobi-powered solvers for Utility Theory Additive (UTA) models. These classes are designed to fit utility functions from generated preference and indifference data.
  • Minor Query Logic Adjustments: Small but important fixes were made in query.py to handle edge cases in query generation and introduce an epsilon for more precise comparisons within the BridgeQuery class.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces significant changes, including a new implementation for data generation, a refactoring of the DecisionMaker class to use piecewise linear utility functions, and new Gurobi-based models for fitting UTA parameters. While the changes add important functionality, I've found several critical issues that need to be addressed. There are potential ZeroDivisionError exceptions in python/decision_maker.py and python/distances.py, and a NameError in python/distances.py that will cause a crash. Additionally, there's a likely logic bug in python/data_generation.py. I've also noted opportunities to improve maintainability by addressing significant code duplication and use of mutable default arguments. Please review the detailed comments for suggestions on how to fix these issues.

Comment thread python/decision_maker.py
dv = du - u_j + self.break_point_y[criterion_j][i+1]
return i+1 - (dv / self.slopes[criterion_j][i])
dv = marginal_utility_difference_i - marginal_utility_value_j + max_bp_value
return self.breakpoints_x[break_point] + (dv / (max_bp_value - min_bp_value)) * (self.breakpoints_x[break_point+1] - self.breakpoints_x[break_point])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

There is a potential ZeroDivisionError here if max_bp_value - min_bp_value is zero. Since the utility function is only guaranteed to be non-decreasing, it's possible for two consecutive breakpoint utilities to be equal. You should add a check to prevent division by zero.

Comment thread python/distances.py

if sample_weight is not None:
self.solver.setObjective(
gp.quicksum(sigma_err[i] * sample_weight[i] for i in range(n_samples)),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The variable sigma_err is not defined in this scope, which will cause a NameError when sample_weight is provided. This appears to be a copy-paste error. This same issue exists in fit_from_indifferences (line 410) and in the UTASpaceDiameter class (lines 691 and 881).

Comment thread python/distances.py
for i in range(self.n_pieces):
if inflexions_x[i] <= val <= inflexions_x[i + 1]:
return coeffs[i] + (
(val - inflexions_x[i]) / (inflexions_x[i + 1] - inflexions_x[i])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The term (inflexions_x[i + 1] - inflexions_x[i]) could be zero, which would cause a ZeroDivisionError. Although _determine_inflexions attempts to handle cases where the min and max values are equal, it's still possible for a segment to have zero length with certain data distributions or due to floating-point inaccuracies. Please add a check to prevent division by zero. This issue is present in multiple places where this calculation is performed (e.g., lines 189, 365, 517, 655).

Comment thread python/decision_maker.py
return i + dv / self.slopes[criterion_j][i]
dv = - marginal_utility_difference_i + marginal_utility_value_j - min_bp_value

return self.breakpoints_x[break_point] + dv / (max_bp_value - min_bp_value) * (self.breakpoints_x[break_point+1] - self.breakpoints_x[break_point])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Similar to a previous comment, there is a potential ZeroDivisionError here if max_bp_value - min_bp_value is zero. A check should be added to handle this case.

Comment thread python/data_generation.py
x = np.around(
np.random.uniform(0, 1, self.n_criteria), decimals=self.decimals
)
ux = np.around(self.utility(x), decimals=self.decimals)[0]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The utility ux is always calculated for the first decision maker ([0]) instead of the current one in the loop (i). This is likely a bug. It should use [i] to be consistent with the logic for uy on line 155.

Suggested change
ux = np.around(self.utility(x), decimals=self.decimals)[0]
ux = np.around(self.utility(x), decimals=self.decimals)[i]

Comment thread python/data_generation.py
Comment on lines +53 to +56
# Useless now that we have clusters
populations = [0] * self.n_dms
if not isinstance(num_pairs, list):
num_pairs = [np.ceil(num_pairs / self.n_dms)] * self.n_dms

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There are a couple of issues here:

  1. The comment on line 53, "Useless now that we have clusters", is misleading as populations is used later in the method. It should be updated or removed.
  2. On line 56, num_pairs is calculated using np.ceil, which results in a list of floats. It's safer to convert these to integers using .astype(int) as done in generate_indifference_data on line 129 to avoid potential issues.
Suggested change
# Useless now that we have clusters
populations = [0] * self.n_dms
if not isinstance(num_pairs, list):
num_pairs = [np.ceil(num_pairs / self.n_dms)] * self.n_dms
# The `populations` list tracks the number of pairs generated for each DM cluster.
populations = [0] * self.n_dms
if not isinstance(num_pairs, list):
num_pairs = np.array([np.ceil(num_pairs / self.n_dms)] * self.n_dms).astype(int)

Comment thread python/distances.py
self.min, self.max, self.inflexions = self._determine_inflexions(X, Y)

if verbose == 0:
self.solver.params.outputflag = 0 # mode muet

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This comment is in French, while the rest of the codebase is in English. For consistency, it's better to use a single language for all comments. For example, 'silent mode'. This applies to other French comments in this file as well (e.g., line 179).

Suggested change
self.solver.params.outputflag = 0 # mode muet
self.solver.params.outputflag = 0 # silent mode

Comment thread python/distances.py
Comment on lines +420 to +423
print("Optimize")
self.solver.optimize()
self.status = self.solver.Status
print(self.status)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

These print statements will always execute. Consider making them conditional on the verbose parameter to allow for silent execution.

Comment thread python/distances.py
Comment on lines +714 to +719
"""for k, v in estimate_x.items():
estimate_x[k] = v.x
for k, v in estimate_y.items():
estimate_y[k] = v.x

return estimate_x, estimate_y"""

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block of commented-out code should be removed if it's no longer needed to keep the codebase clean.

Comment thread python/identify.py
Comment on lines +158 to +160
### ###
# Add Renormalization here #
### ### No newline at end of file

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This comment block appears to be a placeholder for future implementation. It would be more effective to replace it with a TODO comment explaining what needs to be done, or to create a ticket in your issue tracker.

Suggested change
### ###
# Add Renormalization here #
### ###
# TODO: Add Renormalization here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant