Skip to content

Commit e1d890f

Browse files
committed
Using prebuilt scenarios
1 parent d126d06 commit e1d890f

File tree

4 files changed

+46
-35
lines changed

4 files changed

+46
-35
lines changed

src/negmas/gb/components/acceptance.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,10 @@ class ACLast(AcceptancePolicy):
244244
alpha: float = 1.0
245245
beta: float = 0.0
246246

247-
def after_proposing(self, state: GBState, offer: Outcome | None):
247+
def after_proposing(
248+
self, state: GBState, offer: Outcome | None, dest: str | None = None
249+
):
250+
return super().after_proposing(state, offer, dest)
248251
if not self.negotiator or not self.negotiator.ufun:
249252
return
250253
self.last_offer_util = float(self.negotiator.ufun(offer))
@@ -267,9 +270,15 @@ class AcceptBetween(AcceptancePolicy):
267270

268271
min: float
269272
max: float = 1.0
273+
rational: bool = True
270274

271275
def __call__(self, state, offer, source):
272-
if self.max >= state.relative_time >= self.min:
276+
if self.max >= state.relative_time >= self.min and (
277+
not self.rational
278+
or self.negotiator.ufun.is_not_worse( # type: ignore
279+
offer, None
280+
)
281+
):
273282
return ResponseType.ACCEPT_OFFER
274283
return ResponseType.REJECT_OFFER
275284

@@ -283,9 +292,15 @@ class ACTime(AcceptancePolicy):
283292
"""
284293

285294
tau: float
295+
rational: bool = True
286296

287297
def __call__(self, state, offer, source):
288-
if state.relative_time >= self.tau:
298+
if state.relative_time >= self.tau and (
299+
not self.rational
300+
or self.negotiator.ufun.is_not_worse( # type: ignore
301+
offer, None
302+
)
303+
):
289304
return ResponseType.ACCEPT_OFFER
290305
return ResponseType.REJECT_OFFER
291306

src/negmas/inout.py

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,7 @@ def calc_extra_stats(
454454
bilateral_frontier_outcomes=fo,
455455
)
456456

457-
def serialize(
458-
self, python_class_identifier=PYTHON_CLASS_IDENTIFIER
459-
) -> dict[str, Any]:
457+
def serialize(self) -> dict[str, Any]:
460458
"""
461459
Converts the current scenario into a serializable dict.
462460
@@ -488,7 +486,7 @@ def adjust(
488486
return d
489487
if adjust_name and "name" in d:
490488
d["name"] = get_name(d["name"], default_name)
491-
if d.get(PYTHON_CLASS_IDENTIFIER, "").startswith("negmas."):
489+
if d.get(PYTHON_CLASS_IDENTIFIER, "").startswith("negobench."):
492490
d[PYTHON_CLASS_IDENTIFIER] = d[PYTHON_CLASS_IDENTIFIER].split(".")[-1]
493491
for old, new in rename.items():
494492
if old in d.keys():
@@ -512,23 +510,12 @@ def adjust(
512510

513511
domain = adjust(
514512
serialize(
515-
self.outcome_space,
516-
shorten_type_field=True,
517-
add_type_field=True,
518-
python_class_identifier=python_class_identifier,
513+
self.outcome_space, shorten_type_field=False, add_type_field=True
519514
),
520515
"domain",
521516
)
522517
ufuns = [
523-
adjust(
524-
serialize(
525-
u,
526-
shorten_type_field=True,
527-
add_type_field=True,
528-
python_class_identifier=python_class_identifier,
529-
),
530-
i,
531-
)
518+
adjust(serialize(u, shorten_type_field=False, add_type_field=True), i)
532519
for i, u in enumerate(self.ufuns)
533520
]
534521
return dict(domain=domain, ufuns=ufuns)
@@ -549,22 +536,23 @@ def to_json(self, folder: Path | str) -> None:
549536
"""
550537
self.dumpas(folder, "json")
551538

552-
def dumpas(
553-
self,
554-
folder: Path | str,
555-
type="yml",
556-
compact: bool = False,
557-
python_class_identifier=PYTHON_CLASS_IDENTIFIER,
558-
) -> None:
539+
def dumpas(self, folder: Path | str, type="yml", compact: bool = False) -> None:
559540
"""
560541
Dumps the scenario in the given file format.
561542
"""
543+
if type.startswith("."):
544+
type = type[1:]
562545
folder = Path(folder)
546+
if type == "xml":
547+
self.to_genius_folder(folder)
548+
return
563549
folder.mkdir(parents=True, exist_ok=True)
564-
serialized = self.serialize(python_class_identifier=python_class_identifier)
550+
serialized = self.serialize()
565551
dump(serialized["domain"], folder / f"{serialized['domain']['name']}.{type}")
566552
for u in serialized["ufuns"]:
567553
dump(u, folder / f"{u['name']}.{type}", sort_keys=True, compact=compact)
554+
if self.info:
555+
dump(self.info, folder / f"{INFO_FILE_NAME}.{type}")
568556

569557
def load_info_file(self, file: Path):
570558
if not file.is_file():
@@ -740,7 +728,8 @@ def from_yaml_files(
740728
_ = safe_parsing # yaml parsing is always safe
741729

742730
def adjust_type(d: dict, base: str = "negmas", domain=None) -> dict:
743-
d["type"] = f"{base}.{d['type']}"
731+
if "." not in d["type"]:
732+
d["type"] = f"{base}.{d['type']}"
744733
if domain is not None:
745734
d["outcome_space"] = domain
746735
return d
@@ -855,7 +844,7 @@ def load_genius_domain(
855844
safe_parsing=safe_parsing,
856845
ignore_discount=ignore_discount,
857846
ignore_reserved=ignore_reserved,
858-
name=str(ufname),
847+
name=Path(ufname).stem,
859848
)
860849
except Exception as e:
861850
raise OSError(
@@ -865,7 +854,7 @@ def load_genius_domain(
865854
agent_info.append(
866855
{
867856
"ufun": utility,
868-
"ufun_name": ufname,
857+
"ufun_name": Path(ufname).stem,
869858
"reserved_value_func": utility.reserved_value
870859
if utility is not None
871860
else float("-inf"),

src/negmas/preferences/base_ufun.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ def _get_hyperrects(ufun, max_utility, utiltype=float):
697697
info[a] = child.attrib.get(a, info[a])
698698
issue_info[issue_key].update(info)
699699
mytype = info["type"]
700-
# vtype = info["vtype"]
700+
info["vtype"]
701701
if domain_issues_dict is None:
702702
raise ValueError("unknown domain-issue-dict!!!")
703703

@@ -891,7 +891,8 @@ def from_genius(
891891
See ``from_xml_str`` for all the parameters
892892
893893
"""
894-
kwargs["name"] = str(file_name)
894+
if "name" not in kwargs:
895+
kwargs["name"] = str(Path(file_name).stem)
895896
with open(file_name) as f:
896897
xml_str = f.read()
897898
return cls.from_xml_str(xml_str=xml_str, **kwargs)

src/negmas/serialization.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ def good_field(k: str, v, objmem):
106106
def adjust_dict(d):
107107
if not isinstance(d, dict):
108108
return d
109+
if python_class_identifier in d.keys():
110+
if shorten_type_field and d[python_class_identifier].startswith("negmas."):
111+
d[python_class_identifier] = d[python_class_identifier].split(".")[-1]
109112
for a, b in zip(SPECIAL_FIELDS, SPECIAL_FIELDS_SHORT_NAMES):
110113
if a in d.keys():
111114
if b in d.keys() and d[b] != d[a]:
@@ -198,12 +201,15 @@ def convertwith(value, method, pass_identifier=False):
198201
return adjust_dict(converted)
199202

200203
if has(type(value)):
201-
converted = asdict(value, recurse=deep)
204+
clsname = get_full_type_name(value.__class__)
205+
if shorten_type_field and clsname.startswith("negmas."):
206+
clsname = clsname.split("n")[-1]
207+
converted = asdict(value, recurse=deep) | {python_class_identifier: clsname}
202208
if converted is not None:
203209
return serialize(
204210
converted,
205211
deep=deep,
206-
add_type_field=add_type_field,
212+
add_type_field=False,
207213
keep_private=keep_private,
208214
ignore_methods=ignore_methods,
209215
ignore_lambda=ignore_lambda,

0 commit comments

Comments
 (0)