mattloper / chumpy

MIT License
195 stars 118 forks source link

The nondeterministic computation order of Jacobian #46

Open xchen-cs opened 2 years ago

xchen-cs commented 2 years ago

The computation order of Jacobian in Chumpy is nondeterministic. This in cases leads to different optimization results from run to run, since the numerical approximation errors are dependent on the computation order. Specifically, the function dr_wrt() in class Ch has the following code:

for k in set(self.dterms).intersection(propnames.union(set(self.__dict__.keys()))):
    ...

The set is an unordered container in python, and what's even worse for science computation is that its access order is nondeterministic. A quick fix to this could be adding a call to sorted():

for k in sorted(set(self.dterms).intersection(propnames.union(set(self.__dict__.keys())))):
    ...