Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Clustering... The hard way!

Clustering... The hard way!

Scheduled Pinned Locked Moved The Weird and The Wonderful
8 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Geoff Bolton
    wrote on last edited by
    #1

    Okay, hello Coding Horrors!!! A while ago I stumbled upon this delightful piece of code, which appears in a much larger monster that is (thankfully) obsolete. This loop also has a number of shorter variants covering the case of 7, 6, 5, 4 etc cameras. It cracked me up in any case, and I'd like to share it. To this day, I haven't worked out whether the person who wrote it was a fool, a genius, or both! Enjoy =)

    //if there are 8 cameras, clustering 15 out of 28 pairs
    if (pairCount>=28)
    {
    // compute the distances among points
    errBounce = FLOAT_MAX;
    for (i1=0; i1<pairCount-14; i1++)
    {
    for (i2=i1+1; i2<pairCount-13; i2++)
    {
    for (i3=i2+1; i3<pairCount-12; i3++)
    {
    for (i4=i3+1; i4<pairCount-11; i4++)
    {
    for (i5=i4+1; i5<pairCount-10; i5++)
    {
    for (i6=i5+1; i6<pairCount-9; i6++)
    {
    for (i7=i6+1; i7<pairCount-8; i7++)
    {
    for (i8=i7+1; i8<pairCount-7; i8++)
    {
    for (i9=i8+1; i9<pairCount-6; i9++)
    {
    for (i10=i9+1; i10<pairCount-5; i10++)
    {
    for (i11=i10+1; i11<pairCount-4; i11++)
    {
    for (i12=i11+1; i12<pairCount-3; i12++)
    {
    for (i13=i12+1; i13<pairCount-2; i13++)
    {
    for (i14=i13+1; i14<pairCount-1; i14++)
    {
    for (i15=i14+1; i15<pairCount; i15++)
    {
    centroid[0] = (estBounce[i1][0] + estBounce[i2][0] + estBounce[i3][0] + estBounce[i4][0] + estBounce[i5][0] +
    estBounce[i6][0] + estBounce[i7][0] + estBounce[i8][0] + estBounce[i9][0] + estBounce[i10][0] +
    estBounce[i11][0] + estBounce[i12][0] + estBounce[i13][0] + estBounce[i14][0] + estBounce[i15][0])/15.0;
    centroid[1] = (estBounce[i1][1] + estBounce[i2][1] + estBounce[i3][1] + estBounce[i4][1] + estBounce[i5][1] +
    estBounce[i6][1] + estBounce[i7][1] + estBounce[i8][1] + estBounce[i9][1] + estBounce[i10][1] +
    estBounce[i11][1] + estBounce[i12][1] + estBounce[i13][1] + estBounce[i14][1] + estBounce[i15][1])/1

    S L J S D 7 Replies Last reply
    0
    • G Geoff Bolton

      Okay, hello Coding Horrors!!! A while ago I stumbled upon this delightful piece of code, which appears in a much larger monster that is (thankfully) obsolete. This loop also has a number of shorter variants covering the case of 7, 6, 5, 4 etc cameras. It cracked me up in any case, and I'd like to share it. To this day, I haven't worked out whether the person who wrote it was a fool, a genius, or both! Enjoy =)

      //if there are 8 cameras, clustering 15 out of 28 pairs
      if (pairCount>=28)
      {
      // compute the distances among points
      errBounce = FLOAT_MAX;
      for (i1=0; i1<pairCount-14; i1++)
      {
      for (i2=i1+1; i2<pairCount-13; i2++)
      {
      for (i3=i2+1; i3<pairCount-12; i3++)
      {
      for (i4=i3+1; i4<pairCount-11; i4++)
      {
      for (i5=i4+1; i5<pairCount-10; i5++)
      {
      for (i6=i5+1; i6<pairCount-9; i6++)
      {
      for (i7=i6+1; i7<pairCount-8; i7++)
      {
      for (i8=i7+1; i8<pairCount-7; i8++)
      {
      for (i9=i8+1; i9<pairCount-6; i9++)
      {
      for (i10=i9+1; i10<pairCount-5; i10++)
      {
      for (i11=i10+1; i11<pairCount-4; i11++)
      {
      for (i12=i11+1; i12<pairCount-3; i12++)
      {
      for (i13=i12+1; i13<pairCount-2; i13++)
      {
      for (i14=i13+1; i14<pairCount-1; i14++)
      {
      for (i15=i14+1; i15<pairCount; i15++)
      {
      centroid[0] = (estBounce[i1][0] + estBounce[i2][0] + estBounce[i3][0] + estBounce[i4][0] + estBounce[i5][0] +
      estBounce[i6][0] + estBounce[i7][0] + estBounce[i8][0] + estBounce[i9][0] + estBounce[i10][0] +
      estBounce[i11][0] + estBounce[i12][0] + estBounce[i13][0] + estBounce[i14][0] + estBounce[i15][0])/15.0;
      centroid[1] = (estBounce[i1][1] + estBounce[i2][1] + estBounce[i3][1] + estBounce[i4][1] + estBounce[i5][1] +
      estBounce[i6][1] + estBounce[i7][1] + estBounce[i8][1] + estBounce[i9][1] + estBounce[i10][1] +
      estBounce[i11][1] + estBounce[i12][1] + estBounce[i13][1] + estBounce[i14][1] + estBounce[i15][1])/1

      S Offline
      S Offline
      Super Lloyd
      wrote on last edited by
      #2

      Geoff Bolton wrote:

      To this day, I haven't worked out whether the person who wrote it was a fool, a genius, or both

      Granted I only glanced at the code for, like, 1 minute, but I haven't figured it either! :laugh:

      A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

      1 Reply Last reply
      0
      • G Geoff Bolton

        Okay, hello Coding Horrors!!! A while ago I stumbled upon this delightful piece of code, which appears in a much larger monster that is (thankfully) obsolete. This loop also has a number of shorter variants covering the case of 7, 6, 5, 4 etc cameras. It cracked me up in any case, and I'd like to share it. To this day, I haven't worked out whether the person who wrote it was a fool, a genius, or both! Enjoy =)

        //if there are 8 cameras, clustering 15 out of 28 pairs
        if (pairCount>=28)
        {
        // compute the distances among points
        errBounce = FLOAT_MAX;
        for (i1=0; i1<pairCount-14; i1++)
        {
        for (i2=i1+1; i2<pairCount-13; i2++)
        {
        for (i3=i2+1; i3<pairCount-12; i3++)
        {
        for (i4=i3+1; i4<pairCount-11; i4++)
        {
        for (i5=i4+1; i5<pairCount-10; i5++)
        {
        for (i6=i5+1; i6<pairCount-9; i6++)
        {
        for (i7=i6+1; i7<pairCount-8; i7++)
        {
        for (i8=i7+1; i8<pairCount-7; i8++)
        {
        for (i9=i8+1; i9<pairCount-6; i9++)
        {
        for (i10=i9+1; i10<pairCount-5; i10++)
        {
        for (i11=i10+1; i11<pairCount-4; i11++)
        {
        for (i12=i11+1; i12<pairCount-3; i12++)
        {
        for (i13=i12+1; i13<pairCount-2; i13++)
        {
        for (i14=i13+1; i14<pairCount-1; i14++)
        {
        for (i15=i14+1; i15<pairCount; i15++)
        {
        centroid[0] = (estBounce[i1][0] + estBounce[i2][0] + estBounce[i3][0] + estBounce[i4][0] + estBounce[i5][0] +
        estBounce[i6][0] + estBounce[i7][0] + estBounce[i8][0] + estBounce[i9][0] + estBounce[i10][0] +
        estBounce[i11][0] + estBounce[i12][0] + estBounce[i13][0] + estBounce[i14][0] + estBounce[i15][0])/15.0;
        centroid[1] = (estBounce[i1][1] + estBounce[i2][1] + estBounce[i3][1] + estBounce[i4][1] + estBounce[i5][1] +
        estBounce[i6][1] + estBounce[i7][1] + estBounce[i8][1] + estBounce[i9][1] + estBounce[i10][1] +
        estBounce[i11][1] + estBounce[i12][1] + estBounce[i13][1] + estBounce[i14][1] + estBounce[i15][1])/1

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        I dont think that code will terminate ;P

        xacc.ide
        IronScheme - 1.0 RC 1 - out now!
        ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth Edition

        1 Reply Last reply
        0
        • G Geoff Bolton

          Okay, hello Coding Horrors!!! A while ago I stumbled upon this delightful piece of code, which appears in a much larger monster that is (thankfully) obsolete. This loop also has a number of shorter variants covering the case of 7, 6, 5, 4 etc cameras. It cracked me up in any case, and I'd like to share it. To this day, I haven't worked out whether the person who wrote it was a fool, a genius, or both! Enjoy =)

          //if there are 8 cameras, clustering 15 out of 28 pairs
          if (pairCount>=28)
          {
          // compute the distances among points
          errBounce = FLOAT_MAX;
          for (i1=0; i1<pairCount-14; i1++)
          {
          for (i2=i1+1; i2<pairCount-13; i2++)
          {
          for (i3=i2+1; i3<pairCount-12; i3++)
          {
          for (i4=i3+1; i4<pairCount-11; i4++)
          {
          for (i5=i4+1; i5<pairCount-10; i5++)
          {
          for (i6=i5+1; i6<pairCount-9; i6++)
          {
          for (i7=i6+1; i7<pairCount-8; i7++)
          {
          for (i8=i7+1; i8<pairCount-7; i8++)
          {
          for (i9=i8+1; i9<pairCount-6; i9++)
          {
          for (i10=i9+1; i10<pairCount-5; i10++)
          {
          for (i11=i10+1; i11<pairCount-4; i11++)
          {
          for (i12=i11+1; i12<pairCount-3; i12++)
          {
          for (i13=i12+1; i13<pairCount-2; i13++)
          {
          for (i14=i13+1; i14<pairCount-1; i14++)
          {
          for (i15=i14+1; i15<pairCount; i15++)
          {
          centroid[0] = (estBounce[i1][0] + estBounce[i2][0] + estBounce[i3][0] + estBounce[i4][0] + estBounce[i5][0] +
          estBounce[i6][0] + estBounce[i7][0] + estBounce[i8][0] + estBounce[i9][0] + estBounce[i10][0] +
          estBounce[i11][0] + estBounce[i12][0] + estBounce[i13][0] + estBounce[i14][0] + estBounce[i15][0])/15.0;
          centroid[1] = (estBounce[i1][1] + estBounce[i2][1] + estBounce[i3][1] + estBounce[i4][1] + estBounce[i5][1] +
          estBounce[i6][1] + estBounce[i7][1] + estBounce[i8][1] + estBounce[i9][1] + estBounce[i10][1] +
          estBounce[i11][1] + estBounce[i12][1] + estBounce[i13][1] + estBounce[i14][1] + estBounce[i15][1])/1

          J Offline
          J Offline
          Jeroen De Dauw
          wrote on last edited by
          #4

          I'd like to see how this guy would create software to manage a global system. How many TiB would the source be? :laugh:

          Jeroen De Dauw
          Blog ; Wiki

          1 Reply Last reply
          0
          • G Geoff Bolton

            Okay, hello Coding Horrors!!! A while ago I stumbled upon this delightful piece of code, which appears in a much larger monster that is (thankfully) obsolete. This loop also has a number of shorter variants covering the case of 7, 6, 5, 4 etc cameras. It cracked me up in any case, and I'd like to share it. To this day, I haven't worked out whether the person who wrote it was a fool, a genius, or both! Enjoy =)

            //if there are 8 cameras, clustering 15 out of 28 pairs
            if (pairCount>=28)
            {
            // compute the distances among points
            errBounce = FLOAT_MAX;
            for (i1=0; i1<pairCount-14; i1++)
            {
            for (i2=i1+1; i2<pairCount-13; i2++)
            {
            for (i3=i2+1; i3<pairCount-12; i3++)
            {
            for (i4=i3+1; i4<pairCount-11; i4++)
            {
            for (i5=i4+1; i5<pairCount-10; i5++)
            {
            for (i6=i5+1; i6<pairCount-9; i6++)
            {
            for (i7=i6+1; i7<pairCount-8; i7++)
            {
            for (i8=i7+1; i8<pairCount-7; i8++)
            {
            for (i9=i8+1; i9<pairCount-6; i9++)
            {
            for (i10=i9+1; i10<pairCount-5; i10++)
            {
            for (i11=i10+1; i11<pairCount-4; i11++)
            {
            for (i12=i11+1; i12<pairCount-3; i12++)
            {
            for (i13=i12+1; i13<pairCount-2; i13++)
            {
            for (i14=i13+1; i14<pairCount-1; i14++)
            {
            for (i15=i14+1; i15<pairCount; i15++)
            {
            centroid[0] = (estBounce[i1][0] + estBounce[i2][0] + estBounce[i3][0] + estBounce[i4][0] + estBounce[i5][0] +
            estBounce[i6][0] + estBounce[i7][0] + estBounce[i8][0] + estBounce[i9][0] + estBounce[i10][0] +
            estBounce[i11][0] + estBounce[i12][0] + estBounce[i13][0] + estBounce[i14][0] + estBounce[i15][0])/15.0;
            centroid[1] = (estBounce[i1][1] + estBounce[i2][1] + estBounce[i3][1] + estBounce[i4][1] + estBounce[i5][1] +
            estBounce[i6][1] + estBounce[i7][1] + estBounce[i8][1] + estBounce[i9][1] + estBounce[i10][1] +
            estBounce[i11][1] + estBounce[i12][1] + estBounce[i13][1] + estBounce[i14][1] + estBounce[i15][1])/1

            S Offline
            S Offline
            supercat9
            wrote on last edited by
            #5

            The for() loops seem a rather verbose way of generating all 37,442,160 combinations of 15 pairs chosen from 28, but I wouldn't say they're necessarily horrible. If it is indeed necessary to enumerate all the combinations, nested 'for' loops seem a verbose but time-efficient way to do it. The w1..w15 calculations seem icky, though. Much better to compute the sum of dis1..dis15, and subtract one item from that computation.

            1 Reply Last reply
            0
            • G Geoff Bolton

              Okay, hello Coding Horrors!!! A while ago I stumbled upon this delightful piece of code, which appears in a much larger monster that is (thankfully) obsolete. This loop also has a number of shorter variants covering the case of 7, 6, 5, 4 etc cameras. It cracked me up in any case, and I'd like to share it. To this day, I haven't worked out whether the person who wrote it was a fool, a genius, or both! Enjoy =)

              //if there are 8 cameras, clustering 15 out of 28 pairs
              if (pairCount>=28)
              {
              // compute the distances among points
              errBounce = FLOAT_MAX;
              for (i1=0; i1<pairCount-14; i1++)
              {
              for (i2=i1+1; i2<pairCount-13; i2++)
              {
              for (i3=i2+1; i3<pairCount-12; i3++)
              {
              for (i4=i3+1; i4<pairCount-11; i4++)
              {
              for (i5=i4+1; i5<pairCount-10; i5++)
              {
              for (i6=i5+1; i6<pairCount-9; i6++)
              {
              for (i7=i6+1; i7<pairCount-8; i7++)
              {
              for (i8=i7+1; i8<pairCount-7; i8++)
              {
              for (i9=i8+1; i9<pairCount-6; i9++)
              {
              for (i10=i9+1; i10<pairCount-5; i10++)
              {
              for (i11=i10+1; i11<pairCount-4; i11++)
              {
              for (i12=i11+1; i12<pairCount-3; i12++)
              {
              for (i13=i12+1; i13<pairCount-2; i13++)
              {
              for (i14=i13+1; i14<pairCount-1; i14++)
              {
              for (i15=i14+1; i15<pairCount; i15++)
              {
              centroid[0] = (estBounce[i1][0] + estBounce[i2][0] + estBounce[i3][0] + estBounce[i4][0] + estBounce[i5][0] +
              estBounce[i6][0] + estBounce[i7][0] + estBounce[i8][0] + estBounce[i9][0] + estBounce[i10][0] +
              estBounce[i11][0] + estBounce[i12][0] + estBounce[i13][0] + estBounce[i14][0] + estBounce[i15][0])/15.0;
              centroid[1] = (estBounce[i1][1] + estBounce[i2][1] + estBounce[i3][1] + estBounce[i4][1] + estBounce[i5][1] +
              estBounce[i6][1] + estBounce[i7][1] + estBounce[i8][1] + estBounce[i9][1] + estBounce[i10][1] +
              estBounce[i11][1] + estBounce[i12][1] + estBounce[i13][1] + estBounce[i14][1] + estBounce[i15][1])/1

              D Offline
              D Offline
              dybs
              wrote on last edited by
              #6

              Seems like a classic case for recursion to me...but I'm too tired to both trying to code it now... :)

              The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen

              1 Reply Last reply
              0
              • G Geoff Bolton

                Okay, hello Coding Horrors!!! A while ago I stumbled upon this delightful piece of code, which appears in a much larger monster that is (thankfully) obsolete. This loop also has a number of shorter variants covering the case of 7, 6, 5, 4 etc cameras. It cracked me up in any case, and I'd like to share it. To this day, I haven't worked out whether the person who wrote it was a fool, a genius, or both! Enjoy =)

                //if there are 8 cameras, clustering 15 out of 28 pairs
                if (pairCount>=28)
                {
                // compute the distances among points
                errBounce = FLOAT_MAX;
                for (i1=0; i1<pairCount-14; i1++)
                {
                for (i2=i1+1; i2<pairCount-13; i2++)
                {
                for (i3=i2+1; i3<pairCount-12; i3++)
                {
                for (i4=i3+1; i4<pairCount-11; i4++)
                {
                for (i5=i4+1; i5<pairCount-10; i5++)
                {
                for (i6=i5+1; i6<pairCount-9; i6++)
                {
                for (i7=i6+1; i7<pairCount-8; i7++)
                {
                for (i8=i7+1; i8<pairCount-7; i8++)
                {
                for (i9=i8+1; i9<pairCount-6; i9++)
                {
                for (i10=i9+1; i10<pairCount-5; i10++)
                {
                for (i11=i10+1; i11<pairCount-4; i11++)
                {
                for (i12=i11+1; i12<pairCount-3; i12++)
                {
                for (i13=i12+1; i13<pairCount-2; i13++)
                {
                for (i14=i13+1; i14<pairCount-1; i14++)
                {
                for (i15=i14+1; i15<pairCount; i15++)
                {
                centroid[0] = (estBounce[i1][0] + estBounce[i2][0] + estBounce[i3][0] + estBounce[i4][0] + estBounce[i5][0] +
                estBounce[i6][0] + estBounce[i7][0] + estBounce[i8][0] + estBounce[i9][0] + estBounce[i10][0] +
                estBounce[i11][0] + estBounce[i12][0] + estBounce[i13][0] + estBounce[i14][0] + estBounce[i15][0])/15.0;
                centroid[1] = (estBounce[i1][1] + estBounce[i2][1] + estBounce[i3][1] + estBounce[i4][1] + estBounce[i5][1] +
                estBounce[i6][1] + estBounce[i7][1] + estBounce[i8][1] + estBounce[i9][1] + estBounce[i10][1] +
                estBounce[i11][1] + estBounce[i12][1] + estBounce[i13][1] + estBounce[i14][1] + estBounce[i15][1])/1

                L Offline
                L Offline
                Lutoslaw
                wrote on last edited by
                #7

                Auch! It is terrible. I'm sure this can be reduced to a O(n^2) complexity with a smart algorithm and some precalculations/caching. If not, a simple recursive method can do it. :zzz:

                Greetings - Jacek

                1 Reply Last reply
                0
                • G Geoff Bolton

                  Okay, hello Coding Horrors!!! A while ago I stumbled upon this delightful piece of code, which appears in a much larger monster that is (thankfully) obsolete. This loop also has a number of shorter variants covering the case of 7, 6, 5, 4 etc cameras. It cracked me up in any case, and I'd like to share it. To this day, I haven't worked out whether the person who wrote it was a fool, a genius, or both! Enjoy =)

                  //if there are 8 cameras, clustering 15 out of 28 pairs
                  if (pairCount>=28)
                  {
                  // compute the distances among points
                  errBounce = FLOAT_MAX;
                  for (i1=0; i1<pairCount-14; i1++)
                  {
                  for (i2=i1+1; i2<pairCount-13; i2++)
                  {
                  for (i3=i2+1; i3<pairCount-12; i3++)
                  {
                  for (i4=i3+1; i4<pairCount-11; i4++)
                  {
                  for (i5=i4+1; i5<pairCount-10; i5++)
                  {
                  for (i6=i5+1; i6<pairCount-9; i6++)
                  {
                  for (i7=i6+1; i7<pairCount-8; i7++)
                  {
                  for (i8=i7+1; i8<pairCount-7; i8++)
                  {
                  for (i9=i8+1; i9<pairCount-6; i9++)
                  {
                  for (i10=i9+1; i10<pairCount-5; i10++)
                  {
                  for (i11=i10+1; i11<pairCount-4; i11++)
                  {
                  for (i12=i11+1; i12<pairCount-3; i12++)
                  {
                  for (i13=i12+1; i13<pairCount-2; i13++)
                  {
                  for (i14=i13+1; i14<pairCount-1; i14++)
                  {
                  for (i15=i14+1; i15<pairCount; i15++)
                  {
                  centroid[0] = (estBounce[i1][0] + estBounce[i2][0] + estBounce[i3][0] + estBounce[i4][0] + estBounce[i5][0] +
                  estBounce[i6][0] + estBounce[i7][0] + estBounce[i8][0] + estBounce[i9][0] + estBounce[i10][0] +
                  estBounce[i11][0] + estBounce[i12][0] + estBounce[i13][0] + estBounce[i14][0] + estBounce[i15][0])/15.0;
                  centroid[1] = (estBounce[i1][1] + estBounce[i2][1] + estBounce[i3][1] + estBounce[i4][1] + estBounce[i5][1] +
                  estBounce[i6][1] + estBounce[i7][1] + estBounce[i8][1] + estBounce[i9][1] + estBounce[i10][1] +
                  estBounce[i11][1] + estBounce[i12][1] + estBounce[i13][1] + estBounce[i14][1] + estBounce[i15][1])/1

                  J Offline
                  J Offline
                  James Lonero
                  wrote on last edited by
                  #8

                  My God. It looks like some of the old stuff lying around at my company.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups