stevengj / nlopt

library for nonlinear optimization, wrapping many algorithms for global and local, constrained or unconstrained, optimization
Other
1.89k stars 588 forks source link

Error about "undefined: Func" and undefined: Mfunc #524

Closed ZQ1126 closed 1 year ago

ZQ1126 commented 1 year ago

COMPILE SCRIPT:

!/bin/sh

set -ex wget https://codeload.github.com/stevengj/nlopt/tar.gz/v2.7.0 tar -xzvf v2.7.0 cd nlopt-2.7.0 && mkdir build && cd build && cmake -DNLOPT_OCTAVE=Off -DNLOPT_MATLAB=Off -DNLOPT_GUILE=Off .. && make && sudo make install && cd ../..

After successful compilation (No error report), I ran a TEST SCRIPT, and it give the error messages like this:

github.com/go-nlopt/nlopt

../../../go/pkg/mod/github.com/go-nlopt/nlopt@v0.0.0-20230219125344-443d3362dcb5/cfunc_reg.go:13:20: undefined: Func ../../../go/pkg/mod/github.com/go-nlopt/nlopt@v0.0.0-20230219125344-443d3362dcb5/cfunc_reg.go:17:21: undefined: Mfunc ../../../go/pkg/mod/github.com/go-nlopt/nlopt@v0.0.0-20230219125344-443d3362dcb5/cfunc_reg.go:30:26: undefined: Func ../../../go/pkg/mod/github.com/go-nlopt/nlopt@v0.0.0-20230219125344-443d3362dcb5/cfunc_reg.go:31:32: undefined: Func ../../../go/pkg/mod/github.com/go-nlopt/nlopt@v0.0.0-20230219125344-443d3362dcb5/cfunc_reg.go:34:27: undefined: Mfunc ../../../go/pkg/mod/github.com/go-nlopt/nlopt@v0.0.0-20230219125344-443d3362dcb5/cfunc_reg.go:35:32: undefined: Mfunc

This is the TEST SCRIPT i use: package main

import ( "fmt" "github.com/go-nlopt/nlopt" )

func main() { // 创建一个优化器 opt := nlopt.Create(nlopt.LN_COBYLA, 2)

// 设置优化目标函数
opt.SetMinObjective(func(x []float64, grad []float64) float64 {
    return x[0]*x[0] + x[1]*x[1]
})

// 设置优化变量的上下界
opt.SetLowerBounds([]float64{-1, -1})
opt.SetUpperBounds([]float64{1, 1})

// 进行优化
x := []float64{0.5, 0.5}
minf, err := opt.Optimize(x)

if err != nil {
    fmt.Println("优化失败:", err)
} else {
    fmt.Println("最优解:", x)
    fmt.Println("最小值:", minf)
}

// 释放优化器资源
opt.Destroy()

}

I have tried all versions of nlopt and have reported this error. I have been stuck here for a long time. Can you give me some advice!!!

stevengj commented 1 year ago

The NLopt package doesn't include a Go API.

Maybe try https://github.com/go-nlopt/nlopt