def _unorm(self, u: UtilityFunction, mn, mx): if not isinstance(u, LinearUtilityAggregationFunction) and not isinstance( u, LinearUtilityFunction ): return normalize(u, outcomes=Issue.enumerate(issues, max_n_outcomes=1000)) # _, mx = self._urange(u, issues) if mx < 0: return None u.weights = {k: _ / mx for k, _ in u.weights.items()} return u
def random( cls, issues: List["Issue"], reserved_value=(0.0, 1.0), normalized=True, max_n_outcomes: int = 10000, ): outcomes = (Issue.enumerate(issues) if Issue.num_outcomes(issues) <= max_n_outcomes else Issue.sample(issues, max_n_outcomes, with_replacement=False, fail_if_not_enough=False)) return UtilityFunction.generate_random(1, outcomes)[0]
def test_reading_writing_linear_ufun(tmp_path): from negmas.utilities import LinearUtilityAggregationFunction, UtilityFunction from negmas.outcomes import Issue base_folder = pkg_resources.resource_filename( "negmas", resource_name="tests/data/Laptop") _, agent_info, issues = load_genius_domain_from_folder( base_folder, keep_issue_names=True, keep_value_names=True, ) ufuns = [_["ufun"] for _ in agent_info] for ufun in ufuns: assert isinstance(ufun, LinearUtilityAggregationFunction) dst = tmp_path / "tmp.xml" print(dst) UtilityFunction.to_genius(ufun, issues=issues, file_name=dst) ufun2, _ = UtilityFunction.from_genius(dst) assert isinstance(ufun2, LinearUtilityAggregationFunction) for outcome in Issue.enumerate(issues): assert abs(ufun2(outcome) - ufun(outcome)) < 1e-3