File wellmet/voronoi.py changed (mode: 100644) (index 05813bf..307bf40) |
... |
... |
class ContactVoronoi: |
576 |
576 |
self._valid_outsides = set() #č zabyvá se tím bezprostředně integrovačka |
self._valid_outsides = set() #č zabyvá se tím bezprostředně integrovačka |
577 |
577 |
|
|
578 |
578 |
|
|
579 |
|
Nodes pipeline: |
|
580 |
|
0. Sampling: generate coordinates |
|
581 |
|
1. _store_masked(): Filter out, create CandyNodes |
|
582 |
|
with d2 and (optionally) imask assigned |
|
583 |
|
2. assign weights (w) such as sum(w)=1 over entire domain |
|
584 |
|
3. score(): assign so called "score" |
|
|
579 |
|
# Nodes pipeline: |
|
580 |
|
# 0. Sampling: generate coordinates |
|
581 |
|
# 1. _store_masked(): Filter out, create CandyNodes |
|
582 |
|
# with d2 and (optionally) imask assigned |
|
583 |
|
# 2. assign weights (w) such as sum(w)=1 over entire domain |
|
584 |
|
# 3. score(): assign so called "score" |
585 |
585 |
""" |
""" |
586 |
586 |
|
|
587 |
587 |
|
|
|
... |
... |
class ContactVoronoi: |
697 |
697 |
#č zda nejsou změny u sady, doporučenou k integraci |
#č zda nejsou změny u sady, doporučenou k integraci |
698 |
698 |
|
|
699 |
699 |
#č co všechno se tu může dít? |
#č co všechno se tu může dít? |
700 |
|
#č 1. pár není, neboť smyčka tam přídavá všechny možné kombinace. |
|
|
700 |
|
#č -1. pár není, neboť smyčka tam přídavá všechny možné kombinace. |
701 |
701 |
if couple_indices not in self.couples: |
if couple_indices not in self.couples: |
702 |
702 |
assert couple_indices not in self.mixed_couples |
assert couple_indices not in self.mixed_couples |
703 |
703 |
assert couple_indices not in self.red_couples |
assert couple_indices not in self.red_couples |
704 |
704 |
return -1 |
return -1 |
705 |
705 |
idx_list = self.couples[couple_indices] |
idx_list = self.couples[couple_indices] |
706 |
706 |
|
|
707 |
|
#č 2. seznam může být prazdný i když by neměl |
|
|
707 |
|
#č -2. seznam může být prazdný i když by neměl |
708 |
708 |
if not idx_list: |
if not idx_list: |
709 |
709 |
self.couples.pop(couple_indices) |
self.couples.pop(couple_indices) |
710 |
710 |
print("ContactVoronoi: empty list found for ", couple_indices) |
print("ContactVoronoi: empty list found for ", couple_indices) |
711 |
711 |
print(self.mixed_couples.pop(couple_indices, None)) |
print(self.mixed_couples.pop(couple_indices, None)) |
712 |
712 |
print(self.red_couples.pop(couple_indices, None)) |
print(self.red_couples.pop(couple_indices, None)) |
713 |
713 |
return -2 |
return -2 |
714 |
|
|
|
715 |
|
##č 2.5 v seznamu je jen chudý první bodík, který se ani neintegruje |
|
716 |
|
|
|
717 |
|
#č Pokud pár je červený, tak stačí zkontrolovat jen reprezentativní sadu |
|
718 |
714 |
|
|
|
715 |
|
#č 3. Pokud je pár červený, stačí zkontrolovat jen reprezentativní sadu |
|
716 |
|
if couple_indices in self.red_couples: |
|
717 |
|
idx = self.red_couples[couple_indices].idx |
|
718 |
|
status, nodes = self._check_nodes(idx) |
|
719 |
|
if status == 1: #č nic se nezměnilo |
|
720 |
|
return 3 |
|
721 |
|
#č idx už jsme zkontrolovali. |
|
722 |
|
idx_list.remove(idx) |
|
723 |
|
#č Dále seznam proženeme smyčkou _walk_through_couple_nodes() |
|
724 |
|
#č (i kdyby se tam poslal prazdný idx_list - nic se neděje) |
|
725 |
|
#č pokud bodíky se jen maliňko změnily (status 2 z _check_nodes), |
|
726 |
|
#č tak budou použity jako best_nodes a dostanou se zpatky do seznamu. |
|
727 |
|
#č Propadla-li sada kompletně (status 3), tak nodes budou None |
|
728 |
|
else: |
|
729 |
|
nodes = None |
719 |
730 |
|
|
720 |
|
#č u smíšených může být potřeba zkontorlovat všechy sady bodíků |
|
|
731 |
|
#č 4. (u smíšených) může být potřeba zkontorlovat všechy sady bodíků |
721 |
732 |
#č kvůli garancim skříňce |
#č kvůli garancim skříňce |
|
733 |
|
new_idxs, best_nodes = self._walk_through_couple_nodes(idx_list, nodes) |
|
734 |
|
if not new_idxs: |
|
735 |
|
self._invalidate_couple(couple_indices) |
|
736 |
|
return -5 |
|
737 |
|
else: |
|
738 |
|
self.couples[couple_indices] = new_idxs |
|
739 |
|
self._recommend_to_integration(best_nodes) |
|
740 |
|
return 4 |
|
741 |
|
|
|
742 |
|
|
722 |
743 |
|
|
|
744 |
|
def _walk_through_couple_nodes(self, idx_list, best_nodes=None): |
|
745 |
|
new_idxs = [] |
|
746 |
|
#č i kdyby se poslal prazdný idx_list - nic se neděje |
|
747 |
|
if best_nodes is not None: |
|
748 |
|
new_idxs.append(best_nodes.idx) |
|
749 |
|
best_score = best_nodes.score |
|
750 |
|
else: |
|
751 |
|
best_score = -np.inf |
|
752 |
|
|
|
753 |
|
|
|
754 |
|
sampling_needed = False |
723 |
755 |
|
|
|
756 |
|
for idx in idx_list: |
|
757 |
|
status, nodes = self._check_nodes(idx) |
|
758 |
|
if status != 1: #č 1 == nic se nezměnilo |
|
759 |
|
sampling_needed = True |
|
760 |
|
if status == 3: #č Padla celá sada bodů. |
|
761 |
|
continue |
|
762 |
|
|
|
763 |
|
new_idxs.append(idx) |
|
764 |
|
if nodes.score > best_score: |
|
765 |
|
best_nodes = nodes |
|
766 |
|
best_score = nodes.score |
|
767 |
|
|
|
768 |
|
if (best_nodes is not None) and sampling_needed: |
|
769 |
|
#č tp-čko se musí znovu vygenerovat na aktuálních vzorcích |
|
770 |
|
#č nepůjde je ukladat |
|
771 |
|
tp = TwoPoints(self.points, best_nodes.couple, workers=self.workers) |
|
772 |
|
nodes = self.sample_alike(tp, best_nodes, self.ns) |
|
773 |
|
if nodes is not None: |
|
774 |
|
new_idxs.append(nodes.idx) |
|
775 |
|
if nodes.score > best_score: |
|
776 |
|
best_nodes = nodes |
724 |
777 |
|
|
|
778 |
|
return new_idxs, best_nodes |
725 |
779 |
|
|
726 |
780 |
|
|
727 |
|
def _check_nodes(self, nodes): |
|
|
781 |
|
def _check_nodes(self, idx): |
728 |
782 |
"returns status, masked_nodes" |
"returns status, masked_nodes" |
729 |
783 |
#č co všechno se tu může dít? |
#č co všechno se tu může dít? |
730 |
784 |
#č 1. nic se nezměnilo, všechny bodíky pořad patří k páru |
#č 1. nic se nezměnilo, všechny bodíky pořad patří k páru |
|
... |
... |
class ContactVoronoi: |
733 |
787 |
|
|
734 |
788 |
#č bodíky musí mít konečné souřadnice - ony prošly KD stromem |
#č bodíky musí mít konečné souřadnice - ony prošly KD stromem |
735 |
789 |
#č vzorky - tím více |
#č vzorky - tím více |
|
790 |
|
nodes = self.nodes[idx] |
736 |
791 |
nodes_model = getattr(nodes, self.model_space) |
nodes_model = getattr(nodes, self.model_space) |
737 |
792 |
d3 = np.min(cdist(nodes_model, self.points[self._nsim:]), axis=1) |
d3 = np.min(cdist(nodes_model, self.points[self._nsim:]), axis=1) |
738 |
793 |
mask = nodes.d2 <= d3 |
mask = nodes.d2 <= d3 |
739 |
794 |
|
|
740 |
795 |
if not np.any(mask): |
if not np.any(mask): |
|
796 |
|
# -1 = 'outside', 0=success, 1=failure, 2=mix |
|
797 |
|
if nodes.event_id == 2: |
|
798 |
|
#č dáme zákazníkovi vědět před tím, než bodíky vyhodíme |
|
799 |
|
self.on_delete_mixed(idx) |
|
800 |
|
#č invalidace sady |
|
801 |
|
self.nodes.pop(idx) |
741 |
802 |
return 3, None |
return 3, None |
742 |
803 |
elif np.all(mask): |
elif np.all(mask): |
743 |
804 |
return 1, nodes |
return 1, nodes |
|
... |
... |
class ContactVoronoi: |
745 |
806 |
del nodes.p_inside |
del nodes.p_inside |
746 |
807 |
del nodes.p_fv |
del nodes.p_fv |
747 |
808 |
del nodes.p_fw |
del nodes.p_fw |
748 |
|
return 2, nodes[mask] |
|
|
809 |
|
masked_nodes = nodes[mask] |
|
810 |
|
# update score |
|
811 |
|
self.score(masked_nodes) |
|
812 |
|
self.nodes[idx] = masked_nodes |
|
813 |
|
#č nejdřív update, teprve poté informujeme kontrahenta |
|
814 |
|
if nodes.event_id == 2: |
|
815 |
|
self.on_update_mixed(idx) |
|
816 |
|
return 2, masked_nodes |
749 |
817 |
|
|
750 |
|
#č invalidace sady? |
|
751 |
|
# update score? |
|
|
818 |
|
## checklist |
|
819 |
|
#self.couples[tp.couple_indices].append(nodes.idx) |
752 |
820 |
|
|
753 |
821 |
|
|
754 |
|
def invalidate_couple(self, couple_indices): |
|
755 |
|
pass |
|
|
822 |
|
def _invalidate_couple(self, couple_indices): |
|
823 |
|
self.couples.pop(couple_indices) |
|
824 |
|
self.mixed_couples.pop(couple_indices, None) |
|
825 |
|
self.red_couples.pop(couple_indices, None) |
756 |
826 |
|
|
757 |
827 |
def onboard_couple(self, couple_indices, event_id, nis): |
def onboard_couple(self, couple_indices, event_id, nis): |
758 |
828 |
#č (i, j)? poprvé ta čísla vidíme |
#č (i, j)? poprvé ta čísla vidíme |
|
... |
... |
class ContactVoronoi: |
767 |
837 |
#č kontakt existuje, dáme vědět aktualizovačce |
#č kontakt existuje, dáme vědět aktualizovačce |
768 |
838 |
##č (snad jediné místo kde tuhle funkci voláme) |
##č (snad jediné místo kde tuhle funkci voláme) |
769 |
839 |
self._add_indices_to_update(j) |
self._add_indices_to_update(j) |
|
840 |
|
self.couples[couple_indices].append(nodes.idx) |
770 |
841 |
|
|
771 |
842 |
|
|
772 |
843 |
##nodes.w = 0 |
##nodes.w = 0 |
|
... |
... |
class ContactVoronoi: |
805 |
876 |
#č kontakt existuje, dáme vědět aktualizovačce |
#č kontakt existuje, dáme vědět aktualizovačce |
806 |
877 |
##č (druhé místo kde tuhle funkci voláme) |
##č (druhé místo kde tuhle funkci voláme) |
807 |
878 |
self._add_indices_to_update(j) |
self._add_indices_to_update(j) |
|
879 |
|
self.couples[couple_indices].append(nodes.idx) |
808 |
880 |
|
|
809 |
881 |
#č jdeme do adaptivního IS vzorkování |
#č jdeme do adaptivního IS vzorkování |
810 |
882 |
#č zde jenom vzorkujeme; |
#č zde jenom vzorkujeme; |
|
... |
... |
class ContactVoronoi: |
813 |
885 |
while nodes.score > best_score: |
while nodes.score > best_score: |
814 |
886 |
best_nodes = nodes |
best_nodes = nodes |
815 |
887 |
best_score = nodes.score |
best_score = nodes.score |
816 |
|
nodes = self.sample_alike(tp, nodes, nis) |
|
|
888 |
|
nodes = self.sample_alike(tp, nodes, nis) |
|
889 |
|
self.couples[couple_indices].append(nodes.idx) |
817 |
890 |
|
|
818 |
891 |
|
|
819 |
892 |
#č doporučíme k integraci |
#č doporučíme k integraci |
|
... |
... |
class ContactVoronoi: |
929 |
1002 |
|
|
930 |
1003 |
|
|
931 |
1004 |
## checklist |
## checklist |
932 |
|
self.couples[tp.couple_indices].append(nodes.idx) |
|
|
1005 |
|
#č _store_masked dělá na urovní nodes |
|
1006 |
|
#č zbytek nechť dělá kód vejš |
|
1007 |
|
#č michá se to u kontrol párů z minule |
|
1008 |
|
#self.couples[tp.couple_indices].append(nodes.idx) |
933 |
1009 |
#self.mixed_couples #č v této fázi nevíme, |
#self.mixed_couples #č v této fázi nevíme, |
934 |
1010 |
#self.red_couples #č zda je sada ta nejlepší |
#self.red_couples #č zda je sada ta nejlepší |
935 |
1011 |
#self._add_indices_to_update(j) #č vždyť já nevím, jestli se jedná o aktualizaci |
#self._add_indices_to_update(j) #č vždyť já nevím, jestli se jedná o aktualizaci |