pacti-org / pacti

A package for compositional system analysis and design
https://www.pacti.org
BSD 3-Clause "New" or "Revised" License
19 stars 5 forks source link

Error with merge when inputs are not the same #241

Closed abadithela closed 1 year ago

abadithela commented 1 year ago

I get the following error when running the saved_results.ipynb notebook inside evaluating_perception in the review branch of the autonomous systems case study (branch 137-review-autonomous-case-study).

The control contracts are as follows:

# First we construct each contract individually, and then merge the controller contract:
## Control contract for pedestrian class
def control_contract_ped(lbounds_ped):
    lb_m, lb_c = lbounds_ped
    tp = "tp_ped"
    P = "P_ped"
    input_vars = [tp]
    output_vars = [P]
    assumptions = [f"{tp} <= 1", f"-{tp} <= -0.6"]
    guarantees = [f"-{P} + "+str(lb_m)+ f"{tp} <= "+str(-1*lb_c)]
    Cped = PolyhedralContract.from_string(input_vars=input_vars, output_vars=output_vars, assumptions=assumptions, guarantees=guarantees)
    return Cped

def control_contract_obj(lbounds_obj):
    lb_m, lb_c = lbounds_obj

    tp = "tp_obj"
    P = "P_obj"
    input_vars = [tp]
    output_vars = [P]
    assumptions = [f"{tp} <= 1", f"-{tp} <= -0.3"]
    guarantees = [f"-{P} + "+str(lb_m)+ f"{tp} <= "+str(-1*lb_c)]
    Cobj = PolyhedralContract.from_string(input_vars=input_vars, output_vars=output_vars, assumptions=assumptions, guarantees=guarantees)
    return Cobj

def control_contract_emp(lbounds_emp):
    lb_m, lb_c = lbounds_emp

    tp = "tp_emp"
    P = "P_emp"
    input_vars = [tp]
    output_vars = [P]
    assumptions = [f"{tp} <= 1", f"-{tp} <= -0.6"]
    guarantees = [f"-{P} + "+str(lb_m)+ f"{tp} <= "+str(-1*lb_c)]
    Cemp = PolyhedralContract.from_string(input_vars=input_vars, output_vars=output_vars, assumptions=assumptions, guarantees=guarantees)
    return Cemp

Cped = control_contract_ped(lbounds_ped)
Cobj = control_contract_obj(lbounds_obj)
Cemp = control_contract_emp(lbounds_emp)

print("Controller Contract for pedestrian class:\n" + str(Cped) + "\n")
print("Controller Contract for object class:\n" + str(Cobj) + "\n")
print("Controller Contract for empty class:\n" + str(Cemp) + "\n")

C_controller = Cped.merge(Cobj)
C_controller = C_controller.merge(Cemp)

print("Merged Controller Contract for all object classes:\n" + str(C_controller) + "\n")

And, I get the following error. Is there something obvious I am missing?

---------------------------------------------------------------------------
IncompatibleArgsError                     Traceback (most recent call last)
Cell In[17], line 2
      1 # Construct merger of contracts:
----> 2 C_controller = Cped.merge(Cobj)
      3 C_controller = C_controller.merge(Cemp)
      5 print("Merged Controller Contract for all object classes:\n" + str(C_controller) + "\n")

File ~/Documents/software/gear/src/pacti/iocontract/iocontract.py:731, in IoContract.merge(self, other)
    715 """
    716 Compute the merging operation for two contracts.
    717 
   (...)
    728     IncompatibleArgsError: trying to merge different contract types.
    729 """
    730 if isinstance(self, type(other)):
--> 731     raise IncompatibleArgsError("Asked to merge incompatible contracts")
    732 input_vars = list_union(self.inputvars, other.inputvars)
    733 output_vars = list_union(self.outputvars, other.outputvars)

IncompatibleArgsError: Asked to merge incompatible contracts
iincer commented 1 year ago

This is a bug in the merge operation that we addressed a few days ago. Could you merge the latest main on your branch? This should take care of the problem.

abadithela commented 1 year ago

The issue persists with the same error even after the merge from the main branch

abadithela commented 1 year ago

Fixed. Restarting the kernel after the merge was important and I had assumed that VS Code did that when I close and reopen notebooks. However, I explicitly tried it and then it worked.