Nonlinear Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nurulshidanoni
    New Member
    • Sep 2007
    • 40

    Nonlinear Function

    DEAL ALL EXPERTS,

    How can i write random nonlinear function?
    Let say, the nonlinear is 2 x power of 3. But , I want the random function of nonlinear.

    Thank you.

    Ashida.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    I would work out how to do that, mathematically. Then use that to create an abstracted algorithm to be able to do an "any case" nonlinear function. I would use my algorithm to create pseudocode, and then turn that pseudocode into real code.

    That's all the help I can give you until you ask a more specific question about the implementation that you are writing.

    Comment

    • nurulshidanoni
      New Member
      • Sep 2007
      • 40

      #3
      Dear Sicarie,

      Actually, I want to generate a randomly any nonlinear function.

      [code=cpp]
      #include <iostream>
      #include <time.h>
      #include <stdio.h>
      #include <math.h>
      #include <stdlib.h>
      using namespace std;

      int main()
      {
      int i;
      int x[10];//data array

      for(i=1; i<=6; i++)
      {
      x[i]=i;

      A[i]=(2+pow(x[i],2));// (I WANT GENERATE RANDOMLY NONLINEAR FUNCTION)
      [/code]
      Did you know how to do it?

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by nurulshidanoni
        Dear Sicarie,

        Actually, I want to generate a randomly any nonlinear function.

        A[i]=(2+pow(x[i],2));// (I WANT GENERATE RANDOMLY NONLINEAR FUNCTION)
        Right, do you have an algorithm for creating that? You put a lot of headers in your code, why did you include the stdio.h when you are using C++?

        I would not worry about coding it yet, you need to figure out how to generate your 'randomly nonlinear function'. I'm not entirely sure, but I would imagine that a "nonlinear" function is something like x^2 or x^3, something that does not follow a constant rise-over-run. In that case, I'm not sure why you are using an array, and not sure what the purpose of the 2+ is above. I'd stick with pow(x,2) just to make sure you have the concept correct, then you can mess around with it more.

        Anyway, you need to better define "randomly nonlinear function" and come up with a way to create that. Or is the array the collection of points that will be used for the random points? In that case, you want a 2-d array.

        Try getting your algorithm down before you start coding, it will save you a lot of time.

        Comment

        • nurulshidanoni
          New Member
          • Sep 2007
          • 40

          #5
          Originally posted by sicarie
          Right, do you have an algorithm for creating that? You put a lot of headers in your code, why did you include the stdio.h when you are using C++?

          I would not worry about coding it yet, you need to figure out how to generate your 'randomly nonlinear function'. I'm not entirely sure, but I would imagine that a "nonlinear" function is something like x^2 or x^3, something that does not follow a constant rise-over-run. In that case, I'm not sure why you are using an array, and not sure what the purpose of the 2+ is above. I'd stick with pow(x,2) just to make sure you have the concept correct, then you can mess around with it more.

          Anyway, you need to better define "randomly nonlinear function" and come up with a way to create that. Or is the array the collection of points that will be used for the random points? In that case, you want a 2-d array.

          Try getting your algorithm down before you start coding, it will save you a lot of time.

          1. Why can't I put stdio.h when using c++, Sicarie?

          2. My algorithm is

          generate Initial Solution, s0
          Local Search , s*
          repeat
          perturbations*'
          Local search,s*
          repeat
          until termination condition met.

          3. I still dont understand how to generate randomly nonlinear function.

          4. "2+" means 2 + x^2

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by nurulshidanoni
            1. Why can't I put stdio.h when using c++, Sicarie?
            You can. It's a C header, though. If you're using C++, there is no reason to have it, as there are C++ methods defined to do those things, and there are a few compilers that won't allow that.
            Originally posted by nurulshidanoni
            2. My algorithm is

            generate Initial Solution, s0
            Local Search , s*
            repeat
            perturbations*'
            Local search,s*
            repeat
            until termination condition met.
            And see, I understand none of that. What is an initial solution s0? How do you find it? How do you do a local search? How long do you repeat for, ie - what are the conditions that are supposed to be met? What, specifically, is a perturbation? You need to flesh out your algorithm more.
            Originally posted by nurulshidanoni
            3. I still dont understand how to generate randomly nonlinear function.

            4. "2+" means 2 + x^2
            I don't either, based on your algorithm. Was my definition of a "randomly nonlinear function" above correct? Could you define it, please? As soon as you set very specific conditions, you can work to develop algorithms/code for those conditions.

            Comment

            • kreagan
              New Member
              • Aug 2007
              • 153

              #7
              Originally posted by nurulshidanoni
              1.
              2. My algorithm is
              generate Initial Solution, s0
              Local Search , s*
              repeat
              perturbations*'
              Local search,s*
              repeat
              until termination condition met.
              That's a very general discription of an algorithm. For example, what is your termination condition? I agree with Sicarie, what is perturbations. I searched the term on the internet. I found perturbation theory (definition I found on perturbation doesn't seem to apply), but that information seems very general too. Could you explain the contex of that word?

              Originally posted by nurulshidanoni
              3. I still dont understand how to generate randomly nonlinear function.

              4. "2+" means 2 + x^2


              There is nothing random with the expression you have above. I think Sicarie understand the equation but not where it came from. Where did you get the 2+ for example. Maybe you should try defining what randomly nonlinear function is. For example, do you mean non-linear time to complete? Maybe it might help to explain what the function is going to be used for. How random it must be or complex.

              Comment

              • nurulshidanoni
                New Member
                • Sep 2007
                • 40

                #8
                DEAR SICARIE AND KREAGAN,

                EXAMPLE, MY DATA IS LIKE BELOW


                WHERE x is quantity anfd f is a cost function:



                #include <iostream>
                #include <time.h>
                #include <stdio.h>
                #include <math.h>
                #include <stdlib.h>
                using namespace std;

                int main()
                {
                int i;
                int x[10];//data array
                int A[10],B[10],C[10],D[10],E[10],F[10];

                printf("\n Data for Cost Function and Quantity of Resource Allotted\n");

                for(i=1; i<=6; i++)
                {
                x[i]=i;

                A[i]=(2+pow(x[i],2));
                B[i]=(1+pow(x[i],3));
                C[i]=(2*pow(x[i],2)-1);
                D[i]=(3*pow(x[i],3));
                E[i]=(1+pow(x[i],2));
                F[i]=(3+pow(x[i],2));
                }
                printf("\n");
                for(i=1; i<=6; i++)
                {
                printf("x[%d]=%d A[%d]=%d\n", i, x[i], i, A[i]);
                }
                printf("\n");

                for(i=1; i<=6; i++)
                {
                printf("x[%d]=%d B[%d]=%d\n", i, x[i], i, B[i]);
                }
                printf("\n");

                for(i=1; i<=6; i++)
                {
                printf("x[%d]=%d C[%d]=%d\n", i, x[i], i, C[i]);
                }
                printf("\n");

                for(i=1; i<=6; i++)
                {
                printf("x[%d]=%d D[%d]=%d\n", i, x[i], i, D[i]);
                }
                printf("\n");

                for(i=1; i<=6; i++)
                {
                printf("x[%d]=%d E[%d]=%d\n", i, x[i], i, E[i]);
                }
                printf("\n");

                for(i=1; i<=6; i++)
                {
                printf("x[%d]=%d F[%d]=%d\n", i, x[i], i, F[i]);
                }
                }


                AND MY ALGORITHM IS;

                ITERATED LOCAL SEARCH

                Generate initial solution, s0
                Local search, s*'
                repeat
                Perturbation, s*
                Local search, s*'
                acceptance criterion, s*
                Until termination condition met


                Generate initial solution, so : choose any random number or by greedy heuristic
                Local search, s*: Find the nearest neigbour of cost function, which the cost is lower than the s0.
                Perturbation: is a method like swap, forward and backward the neighbourhood, IF dont find the lower cost from s*.
                Acceptance criterion: accept the neighbourhood if the cost is lower from before. If not back to do perturbation.

                FOR HE RANDOMLY NONLINEAR FUNCTION, I WANT TO CREATE A DATA THAT CONSISTS THE REAL NONLINEAR FUNCTION RANDOMLY LIKE,
                IF I WANT TO GENERATE RANDOM NUMBER FROM 1 TO 10, ITK VERY EASY, BUT FOR THE NONLINEAR FUNCTION IT CONSISTS AN EQUATION LIKE x^2.

                THANK YOU.

                Comment

                • sicarie
                  Recognized Expert Specialist
                  • Nov 2006
                  • 4677

                  #9
                  Looks like you have code and an algorithm (though I didn't look over them).

                  Did you have a question?

                  Comment

                  • nurulshidanoni
                    New Member
                    • Sep 2007
                    • 40

                    #10
                    Q: BELOW IS AN ILS CODE, BUT IT WAS FOR JAVA. I DONT KNOW HOW TO CHANGE INTO C++.

                    WITH THIS ALGORITHM, I WANT TO CALLED THE DATA THAT I WAS DO, TO GET THE SOLUTION.
                    [code=c]
                    /*ITERATED LOCAL SEARCH*/

                    /* Performs on ILS on the quantity passes in for ilsIteration iterations */
                    private static Quantity getQuantity ( Quantity sn )
                    {
                    Isolution sCurrent, s0 =sn;

                    // CRreate ILS instance

                    QuantityILS s = new QuantityILS; );

                    //Generate starting solution
                    sCurrent = s.localsearch( s0 );
                    for (int i = 0; i< ilsIterattion; i++)
                    {

                    //perturb
                    Isolution sPerturb = s.perturb ( scurrent );

                    //local search

                    Isolution sTemp = s.Localsearch(s perturb);

                    //acceptance criteria
                    if ( s. accept( sTemp ))
                    {
                    scurrent = stTemp;
                    }
                    }
                    return (quantity) (s.getBest());
                    }
                    }[/code]
                    Last edited by sicarie; Sep 27 '07, 12:44 PM. Reason: Code tags

                    Comment

                    • kreagan
                      New Member
                      • Aug 2007
                      • 153

                      #11
                      Originally posted by nurulshidanoni
                      DEAR SICARIE AND KREAGAN,

                      EXAMPLE, MY DATA IS LIKE BELOW


                      WHERE x is quantity anfd f is a cost function:


                      FOR HE RANDOMLY NONLINEAR FUNCTION, I WANT TO CREATE A DATA THAT CONSISTS THE REAL NONLINEAR FUNCTION RANDOMLY LIKE,
                      IF I WANT TO GENERATE RANDOM NUMBER FROM 1 TO 10, ITK VERY EASY, BUT FOR THE NONLINEAR FUNCTION IT CONSISTS AN EQUATION LIKE x^2.

                      THANK YOU.
                      [CODE=c]

                      int createFunction( double &exponent, double &offset, double &coeff)

                      exponent = random(100)+2;
                      offset = random(100) + 1;
                      coeff = random(100);

                      )

                      int useFunction( double x, double exponent, double offset, double coeff){
                      return ( ( coeff * pow(x,exponent) ) + offset))
                      }
                      [/CODE]

                      WRITING IN ALL CAPS IS RUDE. The functions above generates a non linear function.

                      Comment

                      • nurulshidanoni
                        New Member
                        • Sep 2007
                        • 40

                        #12
                        Why this program cannot compile?

                        #include <iostream>
                        #include <stdio.h>
                        using namespace std;


                        int createFunction( double &exponent, double &offset, double &coeff);

                        main()
                        {
                        exponent = random(100)+2;
                        offset = random(100) + 1;
                        coeff = random(100);

                        }

                        int useFunction( double x, double exponent, double offset, double coeff)
                        {
                        return ( ( coeff * pow(x,exponent) ) + offset));
                        }
                        ar.cpp
                        C:\Documents and Settings\Admini strator\Desktop \MASTER\11nonli near.cpp(10) : error C2065: 'exponent' : undeclared identifier
                        C:\Documents and Settings\Admini strator\Desktop \MASTER\11nonli near.cpp(10) : error C2065: 'random' : undeclared identifier
                        C:\Documents and Settings\Admini strator\Desktop \MASTER\11nonli near.cpp(11) : error C2065: 'offset' : undeclared identifier
                        C:\Documents and Settings\Admini strator\Desktop \MASTER\11nonli near.cpp(12) : error C2065: 'coeff' : undeclared identifier
                        C:\Documents and Settings\Admini strator\Desktop \MASTER\11nonli near.cpp(14) : warning C4508: 'main' : function should return a value; 'void' return type assumed
                        C:\Documents and Settings\Admini strator\Desktop \MASTER\11nonli near.cpp(18) : error C2065: 'pow' : undeclared identifier
                        C:\Documents and Settings\Admini strator\Desktop \MASTER\11nonli near.cpp(18) : error C2059: syntax error : ')'
                        Error executing cl.exe.

                        11nonlinear.exe - 6 error(s), 1 warning(s)

                        Comment

                        • Laharl
                          Recognized Expert Contributor
                          • Sep 2007
                          • 849

                          #13
                          I don't know the random(int) function, but as for the others:

                          Variables must be declared with their type in addition to their identifier, as int, unsigned int, long, etc.

                          You also must return an int as the end of your main, 0 is conventional unless you have a need to have another program check it.

                          You're also not using any functions from stdio.h, or iostream for that matter.

                          Comment

                          • nurulshidanoni
                            New Member
                            • Sep 2007
                            • 40

                            #14
                            How to convert from java to c++?


                            /*ITERATED LOCAL SEARCH*/

                            /* Performs on ILS on the quantity passes in for ilsIteration iterations */
                            private static Quantity getQuantity ( Quantity sn )
                            {
                            Isolution sCurrent, s0 =sn;

                            // CRreate ILS instance

                            QuantityILS s = new QuantityILS; );

                            //Generate starting solution
                            sCurrent = s.localsearch( s0 );
                            for (int i = 0; i< ilsIterattion; i++)
                            {

                            //perturb
                            Isolution sPerturb = s.perturb ( scurrent );

                            //local search

                            Isolution sTemp = s.Localsearch(s perturb);

                            //acceptance criteria
                            if ( s. accept( sTemp ))
                            {
                            scurrent = stTemp;
                            }
                            }
                            return (quantity) (s.getBest());
                            }
                            }

                            Comment

                            • Laharl
                              Recognized Expert Contributor
                              • Sep 2007
                              • 849

                              #15
                              You'll have to overload the assignment operator for Isolution, and you should probably pass a reference or to sn, quite possibly a const reference or pointer.

                              Functions in C++ cannot be static.

                              Also, I think you need to remove the new in the QuantityILS instance creation line, since new returns a pointer in C++, as far as I understand it, anyway.

                              Comment

                              Working...