Browse Source

wariantowanie z przeskokiem maxu bitow

a.binder 9 years ago
parent
commit
cb3198039e

+ 51 - 3
SE/dev/php-cli/PE_wariant_bit_table.xsl

@@ -65,6 +65,48 @@
         <xsl:value-of select=" substring($bit,number($pos),1)"/>
     </xsl:function>
     
+    
+    <xsl:function name="bp:next_bit_max">
+       <!-- for ($i = 1; $i < 1024; $i++) {
+                $bin = decbin($i);
+                $array = str_split($bin);
+                $sum = array_sum($array);
+                if ($sum <= 3) echo str_pad($bin, 10, "0", STR_PAD_LEFT) . " = " . $i . "\n";
+        }-->
+        <xsl:param name="variant" required="yes"/>
+        <xsl:param name="bit_limit" required="yes"/>
+        <xsl:param name="no_more_variants" required="yes"/>
+        <xsl:param name="loop" required="no"/>
+        <xsl:variable name="variant" select="number($variant) + 1"/><!-- dodajemy 1 aby sprawdzic jak wyglada -->
+        <xsl:variable name="curr_bit" select="bp:decimalToBit($variant)"/>
+        <xsl:variable name="digits">
+            <xsl:for-each select="1 to string-length($curr_bit)">
+                <xsl:if test="substring($curr_bit,.,1) = '1'">
+                    <xsl:value-of select="number(1)"/>
+                </xsl:if>
+            </xsl:for-each>
+        </xsl:variable>
+        <xsl:variable name="cur_bit_len" select=" string-length($digits)"/>
+        <xsl:choose>
+            <xsl:when test="number($loop) &gt; 1024">
+                <!--<xsl:message>#91 loop:<xsl:value-of select="$loop"/>; hit returning $variant=<xsl:value-of select="$variant"/>; $curr_bit=<xsl:value-of select="$curr_bit"/>;</xsl:message>-->
+                <xsl:value-of select="$variant"/>
+            </xsl:when>
+            <xsl:when test="number($cur_bit_len)= number($no_more_variants)">
+                <!--<xsl:message>#90 brak kolejnych wariantow $variant=<xsl:value-of select="$variant"/>;$bit_limit=<xsl:value-of select="$bit_limit"/>; $curr_bit=<xsl:value-of select="$curr_bit"/></xsl:message>-->
+                <xsl:value-of select="$variant"/>
+            </xsl:when>
+            <xsl:when test="number($cur_bit_len) &lt;= number($bit_limit)">
+               <xsl:value-of select="$variant"/> 
+            </xsl:when>
+            <xsl:when test="number($cur_bit_len) &gt;  number($bit_limit)">
+                <!--<xsl:variable name="next_bit" select="bp:decimalToBit($variant+1)"/>-->
+                <!--<xsl:message>#104 next <xsl:value-of select="$variant"/></xsl:message>-->
+                <xsl:value-of select="bp:next_bit_max($variant + 1,$bit_limit,$no_more_variants,$loop+1)"/>
+            </xsl:when>
+        </xsl:choose>
+    </xsl:function>
+    
     <xsl:template name="PE_wariant_bit_table">
         <xsl:param name="wariant_id" required="yes"/>
         <xsl:param name="elements_cnt" required="yes"/>
@@ -125,12 +167,12 @@
     
     
     <xsl:template name="PE_wariant">
-        <xsl:param name="elements_cnt" select="20"/>
-        <xsl:param name="bit_max" select="63"/>
+        <xsl:param name="elements_cnt" select="10"/>
+        <xsl:param name="bit_max" select="3"/>
         <xsl:param name="EconomicDistanceCut2"/><!-- tabela do podstawiania danych i wyliczen -->
         <PE_wariant_bit_table>
             <!--<xsl:variable name="no_more_variants" select=" math:power(2,$elements_cnt)"/>-->
-            <xsl:variable name="no_more_variants" select="100"/>
+            <xsl:variable name="no_more_variants" select="6"/>
             <xsl:attribute name="no_more_variants" select="$no_more_variants"/>
             <xsl:for-each select="1 to ($no_more_variants+1)">
             <xsl:variable name="wariant_id" select="."/>
@@ -141,12 +183,15 @@
                             </test>-->
                 <xsl:variable name="bit" select=" string-join(bp:decimalToBitFill(.,$elements_cnt),'')"/>
                 <xsl:variable name="bit2" select=" string-join(bp:decimalToBit($wariant_id),'')"/>
+                <xsl:variable name="bp:next_bit_max" select="bp:next_bit_max($wariant_id,$bit_max,$elements_cnt,0)"/>
                 
             <xsl:variable name="PE_wariant_bit_table">
                 <PE_wariant_bit_table>
                     <xsl:attribute name="wariant_id" select="."/>
                     <xsl:attribute name="bit" select="$bit" />
                     <xsl:attribute name="bit2" select="$bit2" />
+                    <bp:next_bit_max><xsl:attribute name="bit_od"  select=" string-join(bp:decimalToBitFill($bp:next_bit_max,$elements_cnt),'')"/>
+                        <xsl:copy-of select="$bp:next_bit_max"/></bp:next_bit_max>
                     <xsl:call-template name="PE_wariant_bit_table">
                         <xsl:with-param name="wariant_id" select="$wariant_id"/>
                         <xsl:with-param name="elements_cnt" select="$elements_cnt"/>
@@ -156,6 +201,9 @@
                 </PE_wariant_bit_table>
             </xsl:variable>
             <xsl:choose>
+                <xsl:when test="$bp:next_bit_max = $no_more_variants">
+                    No More variants by $bp:next_bit_max hits!
+                </xsl:when>
                 <xsl:when test="$PE_wariant_bit_table/PE_wariant_bit_table/variant[@bit_limit_hit]">
                     <PE_wariant_bit_table>
                         <xsl:attribute name="wariant_id" select="$wariant_id"/>

+ 1 - 0
SE/dev/php-cli/build_edge_paths_with_PE_controll_loop.xsl

@@ -23,6 +23,7 @@
     <xsl:template match="edge_joins_filled_paths">
         <project basedir="." name="edge_paths_with_PE_controll_loop.xml" default="edge_paths_with_PE_controll_loop">
             <target name="edge_paths_with_PE_controll_loop">
+                <record name="build.log" loglevel="verbose" append="true"/>
                 <xsl:comment>test sort nie dziala</xsl:comment>
                 <delete dir="{$edge_paths_with_PE_controll_temp}"/>
                 <delete dir="{$edge_paths_with_PE_controll_temp.alias}"/>

+ 148 - 95
SE/dev/php-cli/edge_paths_with_PE_controll.xsl

@@ -142,90 +142,89 @@
             
         
         <!-- rozmieszczamy prawidlowo PE na danej trasie -->
-        <xsl:variable name="EconomicDistanceCut">
-            <EconomicDistanceCut>
-                <xsl:for-each select="$edge_joins_filled_path/edge_joins_filled_paths_xml/asText_XMl/LineString[not(@error)]">
-                        <distance>
-                            <xsl:attribute name="pos" select="position()"/>
-                            <xsl:attribute name="not_id" select="@not_id"/>
-                            <xsl:attribute name="ID_Join" select="@ID_Join"/>
-                            <xsl:attribute name="len" select="@len"/>
-                            <xsl:attribute name="id_path" select="@id_path"/>
-                            <xsl:variable name="Points" select="count(Points)"/>
-                            <xsl:attribute name="Points" select="$Points"/>
-                            <xsl:variable name="Ways" select="count(Ways)"/>
-                            <xsl:attribute name="Ways_count" select="$Ways"/> 
-                            <xsl:choose>
-                            <xsl:when test="$Ways or $Points">
-                                <xsl:attribute name="point_count" select="$Points + $Ways"/>
-                                <!--<xsl:attribute name="NotPoints" select="0"/>--><!-- do Grupowanai potrzebne aby nie liczyc tych w count -->
-                                <xsl:if test="Ways">
-                                    <!-- sprawdzamy jak sie dowiazuje i ile ma do najblizszego wezla -->
-                                        <xsl:for-each-group select="Ways" group-by="@ID_WAY">
-                                           <PathsCrossed>
-                                               <xsl:attribute name="ID_WAY" select="@ID_WAY"/><xsl:attribute name="debug" select="'created at #163 by parsing Ways'"/>
-                                            <xsl:variable name="Joining_ID_Join_from" select="parent::node()/@ID_Join"/>    
-                                            <xsl:choose>
-                                                <xsl:when test="$PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]/(bp:Path_Point)[@ID_Join= $Joining_ID_Join_from]/@pos">
-                                                    <xsl:variable name="NeighJoin_pos" select="$PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]/(bp:Path_Point)[@ID_Join= $Joining_ID_Join_from]/@pos"/>
-                                                    <xsl:variable name="NeighJoin_pos_distance" select="number($PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]/(bp:Path_Point)[@pos= $NeighJoin_pos]/@distance)"/>
-                                                    <xsl:attribute name="NeighJoin_pos" select="$NeighJoin_pos"/>
-                                                    <!--<xsl:attribute name="NeighJoin_pos_distance" select="$NeighJoin_pos_distance"/>-->
-                                                    <xsl:attribute name="Deb_Cur_ID_WAY" select="@ID_WAY"/>
-                                                    <xsl:attribute name="Joining_ID_Join_from" select="$Joining_ID_Join_from"/>
-                                                    
-                                                    <!-- chk w gore -->
-                                                    <xsl:variable name="NeighJoin_way_up_pos" select="max($PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]/(bp:Path_Point)[ number(@pos)  &lt; number($NeighJoin_pos) ]   /@pos)"/>
-                                                    <xsl:choose>
-                                                        <xsl:when test="$NeighJoin_way_up_pos">
-                                                            <xsl:attribute name="NeighJoin_way_up_pos" select="$NeighJoin_way_up_pos"/>
-                                                            <xsl:variable name="NeighJoin_way_up_ID_Join" select="$PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]/(bp:Path_Point)[ @pos=$NeighJoin_way_up_pos] /@ID_Join" />
-                                                            <xsl:variable name="NeighJoin_way_up_len"   select="number($NeighJoin_pos_distance) - number($PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]/(bp:Path_Point)[ @pos=$NeighJoin_way_up_pos] /@distance) "/>
-                                                            <xsl:attribute name="NeighJoin_way_up_len" select="$NeighJoin_way_up_len"/>
-                                                            <xsl:attribute name="NeighJoin_way_up_pos" select="$NeighJoin_way_up_pos"/>
-                                                            <xsl:attribute name="NeighJoin_way_up_ID_Join" select="$NeighJoin_way_up_ID_Join"/>
-                                                        </xsl:when>
-                                                        <xsl:otherwise>
-                                                            <xsl:attribute name="NeighJoin_way_up_len" select="-1"/>
-                                                        </xsl:otherwise>
-                                                    </xsl:choose>
-                                                    <xsl:variable name="NeighJoin_way_dn_pos" select="min($PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]/(bp:Path_Point)[ number(@pos)  &gt; number($NeighJoin_pos) ]   /@pos)"/>
-                                                    <xsl:choose>
-                                                        <xsl:when test="$NeighJoin_way_dn_pos">
-                                                            <xsl:variable name="NeighJoin_way_dn_ID_Join" select="$PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]/(bp:Path_Point)[ @pos=$NeighJoin_way_dn_pos] /@ID_Join"/>
-                                                            <xsl:variable name="NeighJoin_way_dn_len" select="number($PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]/(bp:Path_Point)[ @pos=$NeighJoin_way_dn_pos] /@distance) - number($NeighJoin_pos_distance) "/>
-                                                            <xsl:attribute name="NeighJoin_way_dn_pos" select="$NeighJoin_way_dn_pos"/>
-                                                            <xsl:attribute name="NeighJoin_way_dn_ID_Join" select="$NeighJoin_way_dn_ID_Join"/>
-                                                            <xsl:attribute name="NeighJoin_way_dn_len" select="$NeighJoin_way_dn_len"/>
-                                                        </xsl:when>
-                                                        <xsl:otherwise>
-                                                            <xsl:attribute name="NeighJoin_way_dn_len" select="-1"/>
-                                                        </xsl:otherwise>
-                                                    </xsl:choose>
-                                                    <!-- chk w dol -->
-                                                    <xsl:if test=" number($debug) &gt; 3">
-                                                            <debug186>
-                                                                <xsl:copy-of select="$PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]"/>
-                                                            </debug186>
-                                                    </xsl:if>
-                                                </xsl:when>
-                                                <xsl:otherwise>
-                                                    <xsl:comment>No connection to other roads found</xsl:comment>
-                                                </xsl:otherwise>
-                                            </xsl:choose>
-                                           </PathsCrossed>
-                                        </xsl:for-each-group>
-                                </xsl:if>
-                            </xsl:when>
-                            <xsl:otherwise><xsl:attribute name="NotPoints" select="1"/>
-                                <xsl:attribute name="point_count" select="0"/>
-                            </xsl:otherwise><!-- do Grupowanai potrzebne aby nie liczyc tych w count -->
-                        </xsl:choose>
-                            
-                        </distance>
-                    </xsl:for-each>
-            </EconomicDistanceCut>
-        </xsl:variable>
+            <xsl:variable name="EconomicDistanceCut">
+                <EconomicDistanceCut>
+                    <xsl:for-each select="$edge_joins_filled_path/edge_joins_filled_paths_xml/asText_XMl/LineString[not(@error)]">
+                            <distance>
+                                <xsl:attribute name="pos" select="position()"/>
+                                <xsl:attribute name="not_id" select="@not_id"/>
+                                <xsl:attribute name="ID_Join" select="@ID_Join"/>
+                                <xsl:attribute name="len" select="@len"/>
+                                <xsl:attribute name="id_path" select="@id_path"/>
+                                <xsl:variable name="Points" select="count(Points)"/>
+                                <xsl:attribute name="Points" select="$Points"/>
+                                <xsl:variable name="Ways" select="count(Ways)"/>
+                                <xsl:attribute name="Ways_count" select="$Ways"/> 
+                                <xsl:choose>
+                                <xsl:when test="$Ways or $Points">
+                                    <xsl:attribute name="point_count" select="$Points + $Ways"/>
+                                    <!--<xsl:attribute name="NotPoints" select="0"/>--><!-- do Grupowanai potrzebne aby nie liczyc tych w count -->
+                                    <xsl:if test="Ways">
+                                        <!-- sprawdzamy jak sie dowiazuje i ile ma do najblizszego wezla -->
+                                            <xsl:for-each-group select="Ways" group-by="@ID_WAY">
+                                               <PathsCrossed>
+                                                   <xsl:attribute name="ID_WAY" select="@ID_WAY"/><xsl:attribute name="debug" select="'created at #163 by parsing Ways'"/>
+                                                <xsl:variable name="Joining_ID_Join_from" select="parent::node()/@ID_Join"/>    
+                                                <xsl:choose>
+                                                    <xsl:when test="$PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]/(bp:Path_Point)[@ID_Join= $Joining_ID_Join_from]/@pos">
+                                                        <xsl:variable name="NeighJoin_pos" select="$PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]/(bp:Path_Point)[@ID_Join= $Joining_ID_Join_from]/@pos"/>
+                                                        <xsl:variable name="NeighJoin_pos_distance" select="number($PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]/(bp:Path_Point)[@pos= $NeighJoin_pos]/@distance)"/>
+                                                        <xsl:attribute name="NeighJoin_pos" select="$NeighJoin_pos"/>
+                                                        <!--<xsl:attribute name="NeighJoin_pos_distance" select="$NeighJoin_pos_distance"/>-->
+                                                        <xsl:attribute name="Deb_Cur_ID_WAY" select="@ID_WAY"/>
+                                                        <xsl:attribute name="Joining_ID_Join_from" select="$Joining_ID_Join_from"/>
+                                                        
+                                                        <!-- chk w gore -->
+                                                        <xsl:variable name="NeighJoin_way_up_pos" select="max($PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]/(bp:Path_Point)[ number(@pos)  &lt; number($NeighJoin_pos) ]   /@pos)"/>
+                                                        <xsl:choose>
+                                                            <xsl:when test="$NeighJoin_way_up_pos">
+                                                                <xsl:attribute name="NeighJoin_way_up_pos" select="$NeighJoin_way_up_pos"/>
+                                                                <xsl:variable name="NeighJoin_way_up_ID_Join" select="$PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]/(bp:Path_Point)[ @pos=$NeighJoin_way_up_pos] /@ID_Join" />
+                                                                <xsl:variable name="NeighJoin_way_up_len"   select="number($NeighJoin_pos_distance) - number($PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]/(bp:Path_Point)[ @pos=$NeighJoin_way_up_pos] /@distance) "/>
+                                                                <xsl:attribute name="NeighJoin_way_up_len" select="$NeighJoin_way_up_len"/>
+                                                                <xsl:attribute name="NeighJoin_way_up_pos" select="$NeighJoin_way_up_pos"/>
+                                                                <xsl:attribute name="NeighJoin_way_up_ID_Join" select="$NeighJoin_way_up_ID_Join"/>
+                                                            </xsl:when>
+                                                            <xsl:otherwise>
+                                                                <xsl:attribute name="NeighJoin_way_up_len" select="-1"/>
+                                                            </xsl:otherwise>
+                                                        </xsl:choose>
+                                                        <xsl:variable name="NeighJoin_way_dn_pos" select="min($PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]/(bp:Path_Point)[ number(@pos)  &gt; number($NeighJoin_pos) ]   /@pos)"/>
+                                                        <xsl:choose>
+                                                            <xsl:when test="$NeighJoin_way_dn_pos">
+                                                                <xsl:variable name="NeighJoin_way_dn_ID_Join" select="$PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]/(bp:Path_Point)[ @pos=$NeighJoin_way_dn_pos] /@ID_Join"/>
+                                                                <xsl:variable name="NeighJoin_way_dn_len" select="number($PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]/(bp:Path_Point)[ @pos=$NeighJoin_way_dn_pos] /@distance) - number($NeighJoin_pos_distance) "/>
+                                                                <xsl:attribute name="NeighJoin_way_dn_pos" select="$NeighJoin_way_dn_pos"/>
+                                                                <xsl:attribute name="NeighJoin_way_dn_ID_Join" select="$NeighJoin_way_dn_ID_Join"/>
+                                                                <xsl:attribute name="NeighJoin_way_dn_len" select="$NeighJoin_way_dn_len"/>
+                                                            </xsl:when>
+                                                            <xsl:otherwise>
+                                                                <xsl:attribute name="NeighJoin_way_dn_len" select="-1"/>
+                                                            </xsl:otherwise>
+                                                        </xsl:choose>
+                                                        <!-- chk w dol -->
+                                                        <xsl:if test=" number($debug) &gt; 3">
+                                                                <debug186>
+                                                                    <xsl:copy-of select="$PathsCrossed/PathsCrossed/Ways[@ID_WAY= current()/@ID_WAY]"/>
+                                                                </debug186>
+                                                        </xsl:if>
+                                                    </xsl:when>
+                                                    <xsl:otherwise>
+                                                        <xsl:comment>No connection to other roads found</xsl:comment>
+                                                    </xsl:otherwise>
+                                                </xsl:choose>
+                                               </PathsCrossed>
+                                            </xsl:for-each-group>
+                                    </xsl:if>
+                                </xsl:when>
+                                <xsl:otherwise><xsl:attribute name="NotPoints" select="1"/>
+                                    <xsl:attribute name="point_count" select="0"/>
+                                </xsl:otherwise><!-- do Grupowanai potrzebne aby nie liczyc tych w count -->
+                            </xsl:choose> 
+                            </distance>
+                        </xsl:for-each>
+                </EconomicDistanceCut>
+            </xsl:variable>
             <xsl:if test="$debug">
                 <debug_174>
                       <xsl:copy-of select="$EconomicDistanceCut"/>
@@ -284,6 +283,35 @@
             <debug_Cut2_183>
             <xsl:copy-of select="$EconomicDistanceCut2"/>
             </debug_Cut2_183>
+            <!-- narzucenie max ilosci grup oraz wyliczenie ile jest -->
+            <!--<xsl:variable name="$max_combinate_groups_elements"/>-->
+            
+            <xsl:variable name="max_combinate_groups_elements_for_group">
+                <xsl:variable name="elements_cnt" select="count($EconomicDistanceCut2/EconomicDistanceCut2/distance2[number(@point_count) &gt; number(0) ])"/>
+                <xsl:variable name="cost_dist_Cnt" select="count($EconomicDistanceCut2/EconomicDistanceCut2/distance2[ ( number(@cost_dist) &gt; number($PE_Price) ) or number(@len) &gt; ( number($PE_Price) div number($Cable_Price) )  ])"/>
+                <xsl:variable name="bit_max" select="round($len  div  ( $PE_Price div $Cable_Price ) + $cost_dist_Cnt  ) + 1"/>
+                
+                <xsl:choose>
+                    <xsl:when test="number($bit_max) &lt; number(15) "> <!-- do przyjecia przez procesor kombinacji-->
+                        <xsl:message>#296 max_combinate_groups_elements_for_group  number($bit_max=<xsl:value-of select="$bit_max"/>) &lt; number(15)</xsl:message>
+                        <xsl:choose>
+                            <xsl:when test="number($elements_cnt) &gt; 50">
+                                <xsl:message>#297 narzucam fixed liczbe 40 z uwagi na $bit_max=<xsl:value-of select="$bit_max"/> &lt; 15 ilosc max = $elements_cnt=<xsl:value-of select="$elements_cnt"/>; </xsl:message>
+                                <xsl:value-of select="40"/>
+                            </xsl:when>
+                            <xsl:otherwise>
+                                <xsl:message>#302 max_combinate_groups_elements_for_group setting to current count - number($elements_cnt=<xsl:value-of select="$elements_cnt"/>); </xsl:message>
+                                <xsl:value-of select="number($elements_cnt)"/>
+                            </xsl:otherwise>
+                        </xsl:choose>
+                    </xsl:when>
+                    <xsl:otherwise>
+                        <xsl:message>#309 max_combinate_groups_elements_for_group otherwise setting to default $max_combinate_groups_elements=<xsl:value-of select="$max_combinate_groups_elements"/>; </xsl:message>
+                        <xsl:value-of select="$max_combinate_groups_elements"/>
+                    </xsl:otherwise>
+                </xsl:choose>
+            </xsl:variable>
+            
             
             <xsl:variable name="EconomicDistanceCut2">
                 <EconomicDistanceCut2><xsl:attribute name="debug" select="'bez pustych punktow tylko grupowanie'"/>
@@ -291,7 +319,7 @@
                         <distance2>
                             <xsl:copy-of select="@*"/>
                             <xsl:choose>
-                                <xsl:when test=" position()  mod $max_combinate_groups_elements = 0">  <!--- minus puste sum($EconomicDistanceCut/EconomicDistanceCut/distance[ number(@pos) &lt; number(current()/@pos) ]/number(@NotPoints)) ) --><!-- nabija grupe do przeliczenia-->
+                                <xsl:when test=" position()  mod $max_combinate_groups_elements_for_group = 0">  <!--- minus puste sum($EconomicDistanceCut/EconomicDistanceCut/distance[ number(@pos) &lt; number(current()/@pos) ]/number(@NotPoints)) ) --><!-- nabija grupe do przeliczenia-->
                                     <xsl:attribute name="PosCut3Flag" select="1"/>
                                 </xsl:when>
                                 <xsl:otherwise>
@@ -334,9 +362,11 @@
             <xsl:copy-of select="$EconomicDistanceCut2"/>
             </debug>-->
             
-            <xsl:variable name="elements_cnt" select="count($EconomicDistanceCut2/EconomicDistanceCut2/distance2[@point_count &gt; 0])"/><!-- liczba wezlow potencjalnych licbza PE-->
-            <!--<xsl:variable name="bit_max" select="round($len  div  ( $PE_Price div $Cable_Price ) + count($EconomicDistanceCut2/EconomicDistanceCut2/distance2[@cost_dist &gt; $PE_Price])  )"/>--><!-- max licbza PE-->
-            
+            <xsl:variable name="elements_cnt" select="count($EconomicDistanceCut2/EconomicDistanceCut2/distance2[number(@point_count) &gt; number(0) ])"/><!-- liczba wezlow potencjalnych licbza PE-->
+            <xsl:variable name="cost_dist_Cnt" select="count($EconomicDistanceCut2/EconomicDistanceCut2/distance2[ ( number(@cost_dist) &gt; number($PE_Price) ) or number(@len) &gt; ( number($PE_Price) div number($Cable_Price) )  ])"/>
+            <xsl:variable name="bit_max" select="round($len  div  ( $PE_Price div $Cable_Price ) + $cost_dist_Cnt  )"/>
+            <!-- max licbza PE-->
+            <xsl:message>#341Calc $cost_dist_Cnt=<xsl:value-of select="$cost_dist_Cnt"/>; $elements_cnt=<xsl:value-of select="$elements_cnt"/>; $bit_max=<xsl:value-of select="$bit_max"/> ; </xsl:message>
             
             
             <!-- pogrupujemy elementy co 50 metrow lub grupy ilosciowe -->
@@ -346,7 +376,7 @@
                         <PointMatrixGroupPos>
                             <xsl:variable name="elements_cnt_Pos" select="count(current-group()[@point_count &gt; 0])"/> 
                             <xsl:variable name="countGr" select="count(current-group())"/>
-                            <xsl:variable name="bit_max_Pos" select="round(sum( current-group()/@disttopkt )  div  ( $PE_Price div 20 ) )"/>
+                            <xsl:variable name="bit_max_Pos" select="round(sum( current-group()/number(@disttopkt) )  div  ( number($PE_Price) div number($Cable_Price) ) )"/>
                             <xsl:message>Nowa grupa Ways:<xsl:value-of select="@PosCut3GrId"/>; distanceGr=<xsl:value-of select="@PosCut3GrId"/>;elements_cnt_Pos=<xsl:value-of select="$elements_cnt_Pos"/>;  @bit_max_Pos=<xsl:value-of select="$bit_max_Pos"/>; @id_path=<xsl:value-of select="@id_path"/>; countGr=<xsl:value-of select="$countGr"/>; point_count_Pos=<xsl:value-of select="sum(current-group()/@point_count)"/>; len_sum=<xsl:value-of select="sum(current-group()/@len)"/> ; distanceBeg/distanceEnd= <xsl:value-of select="min(current-group()/number(@distance))"/>/<xsl:value-of select="max(current-group()/number(@distance))"/>
                             </xsl:message>
                             <xsl:attribute name="distanceGr" select="@PosCut3GrId "/> <!-- bylo current-grouping-key() -->
@@ -441,9 +471,7 @@
                         <xsl:variable name="odpalam_liczenie_or_nie">
                             <xsl:choose>
                                 <xsl:when test="1=1"> <!-- bylo count($candidates/bp:Path_Point) &lt;= $max_combinate_groups_elements  and count($candidates/bp:Path_Point)&gt;1-->
-                                    <xsl:variable name="max_bit">
-                                        <xsl:for-each select="1 to count($candidates/bp:Path_Point)"><xsl:value-of select="1"/></xsl:for-each>
-                                    </xsl:variable>
+                                    
                                     <odpalam_liczenie>
                                         <!-- todo dolozyc poprzedni wezel dodany aby mogl od niego zawsze liczyc -->
                                         <!--<disabled>temporary blad after #410 OK - przeliczony wariant bit=000000010000 ( nr 4095;) 
@@ -461,11 +489,15 @@
                                                     <xsl:otherwise><xsl:value-of select="1"/></xsl:otherwise>
                                                 </xsl:choose>
                                             </xsl:variable>
-      
+                                        <xsl:variable name="max_possible_bit"><!-- produkuje 11111111 -->
+                                            <xsl:for-each select="1 to count($candidates/bp:Path_Point)"><xsl:value-of select="1"/></xsl:for-each>
+                                        </xsl:variable>
+                                        
                                         <xsl:call-template name="calculate_optimum">
                                             <xsl:with-param name="candidates" select="$candidates"/>
                                             <xsl:with-param name="elements_cnt" select="count($candidates/bp:Path_Point)"/>
-                                            <xsl:with-param name="max_bit" select="$max_bit"/>
+                                            <xsl:with-param name="max_bit" select="$bit_max_Pos"/>
+                                            <xsl:with-param name="max_possible_bit" select="$max_possible_bit"/>
                                             <xsl:with-param name="poprzednie_odpalam_liczenie_or_nie" select="$poprzednie_odpalam_liczenie_or_nie/poprzednie_odpalam_liczenie_or_nie"/>
                                             <xsl:with-param name="variant" select="$IfPathsCrossed"/>
                                         </xsl:call-template>
@@ -549,10 +581,30 @@
         <xsl:param name="elements_cnt" required="yes"/>
         <xsl:param name="variant"  select="1"/>
         <xsl:param name="max_bit" required="yes"/>
+        <xsl:param name="max_possible_bit" required="yes"/>
         <xsl:param name="best_cost"/><xsl:param name="best_variant"/>
         <xsl:param name="poprzednie_odpalam_liczenie_or_nie"/>
         <!-- do jednorazowego wyzwolenia przez fukcje, aby tylko raz odjelo wariant zerowy -->
         
+        <!-- znajduje kolejny korzystny wariant bit -->
+        <xsl:variable name="variant">
+            <xsl:choose>
+                <xsl:when test="number($max_bit) &gt;= number($elements_cnt)">
+                    <xsl:message>#576 $max_bit <xsl:value-of select="$max_bit"/> &gt;$elements_cnt=<xsl:value-of select="$elements_cnt"/>; in $variant=<xsl:value-of select="$variant"/>;</xsl:message>
+                    <xsl:value-of select="$variant"/>
+                </xsl:when>
+                <xsl:when test="number($max_bit) &gt; 0">
+                    
+                    <xsl:message>#80  try to find next $max_bit <xsl:value-of select="$max_bit"/> &gt;$elements_cnt=<xsl:value-of select="$elements_cnt"/>; in $variant=<xsl:value-of select="$variant"/>;</xsl:message>
+                    <xsl:variable name="variant_found" select="bp:next_bit_max($variant,$max_bit,$elements_cnt,0)"/>
+                    <xsl:value-of select="$variant_found"/>
+                    <xsl:message>...  $variant_found <xsl:value-of select="$variant_found"/>;</xsl:message>
+                </xsl:when>
+                <xsl:otherwise>
+                    <xsl:value-of select="$variant"/>
+                </xsl:otherwise>
+            </xsl:choose>
+        </xsl:variable>
       
         <xsl:variable name="bit" select=" string-join(bp:decimalToBitFill($variant,$elements_cnt),'')"/>
         
@@ -614,7 +666,7 @@
             <xsl:message>#688 (C$<xsl:value-of select="$cost_sum/costs/@Cable_Price"/>+P$<xsl:value-of select="$cost_sum/costs/@PE_Price"/> =<xsl:value-of select="$cost_sum/costs/@Price"/>) v:<xsl:value-of select="$variant"/>; b:<xsl:value-of select="$bit"/>; bestV=<xsl:value-of select="$best_variant"/>; bestC=<xsl:value-of select="$best_cost"/> </xsl:message>
         </xsl:if>
         <xsl:choose>
-            <xsl:when test="$bit = $max_bit">
+            <xsl:when test="$bit = $max_possible_bit">
                 <koniec_wariantow>
                     <xsl:message>#410 OK - cost:<xsl:value-of select="$best_cost"/>; bit=<xsl:value-of select="$best_variant"/> ( nr <xsl:value-of select="$variant"/>;) </xsl:message>
                     <xsl:attribute name="best_variant" select="$best_variant"/>
@@ -672,6 +724,7 @@
                     <xsl:call-template name="calculate_optimum">
                         <xsl:with-param name="candidates" select="$candidates"/>
                         <xsl:with-param name="max_bit" select="$max_bit"/>
+                        <xsl:with-param name="max_possible_bit" select="$max_possible_bit"/>
                         <xsl:with-param name="variant" select="$variant + 1"/>
                         <xsl:with-param name="elements_cnt" select="$elements_cnt"/>
                         <xsl:with-param name="best_cost" select="$best/best/@best_cost"/>