Global AI and Data Science

 View Only
  • 1.  already installed cplex and import cplexerror but still have syntax error

    Posted Wed December 21, 2022 11:29 AM

    I'm encountering an error in the following lines:

    import cplex from cplex.exceptions import CplexError def capAst_LP(prod, C, p, v, meta = None):

    # st = time.time()    
    
    try:
        my_prob = cplex.Cplex()
    
        my_obj = list(p) #including the 0th coordinate
        my_ub = [cplex.infinity for i in p] #omitting lb as 0
        my_colnames = ['z_'+str(i) for i in range(prod+1)]
        my_rhs = [1]+[0 for i in range(prod+1)]
        my_rownames = ['sum2one','capaciy'] + ['lpr_'+str(i+1) for i in range(prod)]
        my_sense = ''.join(['E']+['L' for i in range(prod+1)])
        # print my_obj
        # print my_ub
        # print my_colnames
        # print my_rhs
        # print my_rownames
        # print my_sense
    
        my_prob.objective.set_sense(my_prob.objective.sense.maximize)
        my_prob.variables.add(obj = my_obj, ub = my_ub, names = my_colnames)
        
        # print my_prob.variables.get_lower_bounds()
        # print my_prob.variables.get_upper_bounds()
        # print my_prob.variables.get_names()
    
        rows = []
        rows.append([range(prod+1),[1 for i in range(prod+1)]])
        vcoeff_vec = [-C]
        for i in range(1,prod+1):
            vcoeff_vec.append(round(v[0]*1.0/v[i],7))
        rows.append([range(prod+1),vcoeff_vec])
        for i in range(1,prod+1):
            inequ_vec = [-v[i]]
            for j in range(1,prod+1):
                if j==i:
                    inequ_vec.append(v[0])
                else:
                    inequ_vec.append(0)
            rows.append([range(prod+1),inequ_vec])
    
        # print rows
    
        my_prob.linear_constraints.add(lin_expr = rows,
                                    senses = my_sense,
                                    rhs = my_rhs,
                                    names = my_rownames)
    
        my_prob.set_log_stream(None)
        my_prob.set_error_stream(None)
        my_prob.set_warning_stream(None)
        my_prob.set_results_stream(None)
        st = time.time() 
        my_prob.solve()
    
    except CplexError, exc:
        print exc
        return
    

    except CplexError, exc: print exc return

    I have installed cplex and I'm using python 3.7. I will iterate the codes.

    File "", line 304 except cplexError, exc: ^ SyntaxError: invalid syntax

    Thanks,

    jesonzale



    ------------------------------
    oscar clark
    ------------------------------



    #AIandDSSkills
    #AICertificate
    #AIandDSSkills


  • 2.  RE: already installed cplex and import cplexerror but still have syntax error

    Posted Wed December 28, 2022 09:31 AM
    invalid syntax

    ------------------------------
    Jon Conner
    ------------------------------



  • 3.  RE: already installed cplex and import cplexerror but still have syntax error

    Posted Fri November 10, 2023 08:50 AM

    Hello,

    `except CplexError, exc:` is not valid syntax.  You should write `except CplexError as exc:`

    Also `print exc` is python2 syntax and should be replaced with `print(exc)`

    Regards,


    Paul





    ------------------------------
    Paul Shaw
    ------------------------------



  • 4.  RE: already installed cplex and import cplexerror but still have syntax error

    Posted Tue November 21, 2023 05:58 PM

    IBM CPLEX and are encountering syntax errors when using it, there could be a variety of reasons. Here are some common issues and suggestions to resolve them:

    1. Library Import Issues:

      • Ensure that you have imported the CPLEX library correctly in your code. For example, in Java, you might use import ilog.cplex.IloCplex;.
      • Verify that the CPLEX library is in your classpath. The classpath should include the path to the CPLEX JAR file.
    2. Correct Version:

      • Make sure that the version of the CPLEX library you have installed matches the version you are referencing in your code.
    3. Correct Syntax:

      • Ensure that you are using the correct syntax for the CPLEX functions. Check the official documentation for the correct usage of the methods and classes you are using.
    4. Environment Configuration:

      • Verify that your development environment is set up correctly. For example, if you are using an integrated development environment (IDE), check its configuration settings to make sure it recognizes the CPLEX library.
    5. Compilation Errors:

      • If you are facing syntax errors during compilation, review the error messages provided by the compiler. These messages usually give hints about the source of the issue.
    6. Code Sample:

      • If you can provide a specific code snippet that is causing the syntax error, I can help you identify any issues in the code.

    Here's a very basic example in Java that uses CPLEX for linear programming:

    import ilog.concert.IloNumVar; import ilog.concert.IloCplex; import ilog.cplex.IloCplex.CplexStatus; public class CplexExample { public static void main(String[] args) { try { IloCplex cplex = new IloCplex(); // Your optimization model setup goes here cplex.solve(); CplexStatus status = cplex.getCplexStatus(); System.out.println("Status: " + status); cplex.end(); } catch (Exception e) { e.printStackTrace(); } } }

    Coding is a difficult task for beginners because it is not like simple image editing



    ------------------------------
    Daniel Thomas
    ------------------------------



  • 5.  RE: already installed cplex and import cplexerror but still have syntax error

    Posted Wed November 22, 2023 02:44 AM

    The error "SyntaxError: invalid syntax" suggests that there's an incorrect syntax in the exception handling code. Specifically, the exception name should be CplexError, not cplexError. Here's the corrected code:

    except CplexError, exc:
        print exc
        return
    

    This should resolve the syntax error and allow the code to continue executing.



    ------------------------------
    mciky jiol
    ------------------------------



  • 6.  RE: already installed cplex and import cplexerror but still have syntax error

    Posted Mon November 27, 2023 08:47 AM

    The snippet you provided seems to be the exception handling part of Python, where you are trying to catch an exception called CplexError. In Python, when you want to access an exception object in an exception block, you use the as keyword instead of a comma. This may be the cause of the "Syntax Error: invalid syntax" error. Here is the corrected code: python  Copy  try: # Your code except CplexError as exc: print(exc) return Such code should work correctly and print out the exception when the CplexError exception occurs.



    ------------------------------
    子豪 张
    ------------------------------



  • 7.  RE: already installed cplex and import cplexerror but still have syntax error

    Posted Mon November 27, 2023 08:47 AM

    IBM CPLEX encountered a syntax error when using it for a variety of reasons. Here are some common problems and suggestions for solving them: Library import problem: Make sure that the CPLEX library is imported correctly in your code. For example, in Java, you can use the import ilog. Cplex. IloCplex;. Verify that the CPLEX library is in the classpath. The classpath should include the path to the CPLEX JAR file.Correct version: Make sure that the version of the CPLEX library that you installed matches the version that you referenced in your code. Correct syntax: Make sure you use the correct syntax for the CPLEX function. Check the official documentation for the correct usage of the methods and classes you are using. Environment configuration: Verify that your development environment is set up correctly. For example, if you are using an integrated development environment (IDE), check its configuration settings to make sure it recognizes the CPLEX library. Compilation error: If you encounter syntax errors during compilation, check the error messages provided by the compiler. These messages usually provide a hint about the source of the problem.



    ------------------------------
    子豪 张
    ------------------------------



  • 8.  RE: already installed cplex and import cplexerror but still have syntax error

    Posted Mon November 27, 2023 08:47 AM
    Looks like you've posted a snippet of Python code using the Cplex library, likely for solving an optimization problem. It seems to be a linear programming problem given the use of linear constraints and an objective function with a sense of maximization.
    Here's a breakdown of the code:
     
    1.Objective Function:
     
     
    2.my_obj: Represents the coefficients of the objective function.
    3.my_prob.objective.set_sense(my_prob.objective.sense.maximize): Sets the optimization sense to maximize.
     
     
    4.Variables:
     
     
    5.my_ub: Represents the upper bounds of the variables.
    6.my_colnames: Names of the variables.
     
     
    7.Linear Constraints:
     
     
    8.rows: Represents the coefficients and structure of the linear constraints.
    9.my_sense: Specifies the sense of each constraint (E for equality, L for less than or equal to).
    10.my_rhs: Right-hand side values of the constraints.
    11.my_rownames: Names of the constraints.
     
     
    12.Cplex Problem Initialization:
     
     
    13.my_prob: Initializes the Cplex optimization problem.
    14.Adds the objective function, variables, and linear constraints.
     
     
    15.Solution:
     
     
    16.my_prob.solve(): Solves the optimization problem.
     
     
    17.Error Handling:
     
     
    18.The code includes a try-except block to catch and print any CplexError that might occur during the solution process.
     
    Note: The code contains placeholders like p, prod, v, C which are not defined in the provided snippet. Make sure to define these variables with appropriate values before running the code. Additionally, the return statement at the end might need to be within a function.
    If you have specific questions about parts of the code or if you encounter any issues, feel free to ask!


    ------------------------------
    子豪 张
    ------------------------------