类型不匹配:在 Anylogic 中无法从双精度转换为类

类型不匹配:在 Anylogic 中无法从双精度转换为类

我对 Anylogic 中的 java 还很陌生,因此觉得这可能是一个快速解决方案。

我定义了一个名为 OperationResults 的类类型:

    public class OperationResults {
    private final double multiplicationResult;
    private final double additionResult;
    private final double subtractionResult;

    public OperationResults(double mulRes, double addRes, double subRes) {
        this.multiplicationResult = mulRes;
        this.additionResult = addRes;
        this.subtractionResult = subRes;
    }
}

我的模拟中的函数称为“callAPI”,它只是将两个双精度值(“a”和“b”)发送到我的 python API 代码。两组代码如下。我的“callAPI”函数:

    try {
        URL url = new URL("http://127.0.0.1:5000/operations");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setDoOutput(true);

        String jsonInputString = "{\"a\": " + a + ", \"b\": " + b + "}";
        try (OutputStream os = conn.getOutputStream()) {
            byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
            os.write(input, 0, input.length);
        }

        try (InputStream is = conn.getInputStream(); Scanner scanner = new Scanner(is)) {
            String jsonResponse = scanner.useDelimiter("\\A").next();
            JSONObject response = new JSONObject(jsonResponse);
            double multiplicationResult = response.getDouble("multiplicationResult");
            double additionResult = response.getDouble("additionResult");
            double subtractionResult = response.getDouble("subtractionResult");
            return new OperationResults(multiplicationResult, additionResult, subtractionResult);
        }
    } catch (Exception e) {
        e.printStackTrace();
        return new OperationResults(-1, -1, -1); // Error handling
    }

我的API Python代码:


app = Flask(__name__)

@app.route('/operations', methods=['POST'])
def operations():
    data = request.get_json(force=True)
    a = data['a']
    b = data['b']
    result_multiply = a * b
    result_addition = a + b
    result_subtraction = a - b
    return jsonify({
        'multiplicationResult': result_multiply,
        'additionResult': result_addition,
        'subtractionResult': result_subtraction
    })

if __name__ == '__main__':
    app.run(debug=True)

我的模拟中执行该功能的按钮如下:

for (Learner agent : learners) {
    double a = agent.edu1Resources;
    double b = agent.edu3Cost;

    // API call
    OperationResults results = callAPI(a, b);

    // Update agent attributes or handle results
    agent.multiplicationResult = results.multiplicationResult;
    agent.additionResult = results.additionResult;
    agent.subtractionResult = results.subtractionResult;

    // Print results for each agent
    traceln("Agent ID " + agent.getIndex() + ": edu1Resources * edu3Cost = " + results.multiplicationResult + 
            ", edu1Resources + edu3Cost = " + results.additionResult + 
            ", edu1Resources - edu3Cost = " + results.subtractionResult);
}

我收到的两个错误表明“类型不匹配:无法从‘OperationResults’类转换为双精度型”在里面功能代码和“类型不匹配:无法从双精度型转换为‘OperationResults’类”在里面纽扣代码。

我真的希望我的错误有一个简单的解释,因为其他一切似乎都没问题。非常感谢您的帮助。

这是我的堆栈跟踪:


    Type mismatch: cannot convert from double to Main.OperationResults
    at _2024_03_28.Main.executeShapeControlAction(Main.java:651)
    at _2024_03_28.Main$6.action(Main.java:695)
    at com.anylogic.engine.presentation.ShapeControl.executeAction(Unknown Source)
    at com.anylogic.engine.presentation.ShapeButton.executeUserAction(Unknown Source)
    at com.anylogic.engine.gui.ExperimentHost.executeUserAction(Unknown Source)
    at com.anylogic.engine.internal.webserver.f.onAction(Unknown Source)
    at com.anylogic.executor.basic.rest.BasicAnimationSessionController.lambda$null$3(BasicAnimationSessionController.java:71)
    at com.anylogic.engine.internal.webserver.e.acceptToAnimationSvgSession(Unknown Source)
    at com.anylogic.executor.basic.rest.BasicAnimationSessionController.lambda$startController$4(BasicAnimationSessionController.java:71)
    at spark.ResponseTransformerRouteImpl$1.handle(ResponseTransformerRouteImpl.java:47)
    at spark.http.matching.Routes.execute(Routes.java:61)
    at spark.http.matching.MatcherFilter.doFilter(MatcherFilter.java:134)
    at com.anylogic.engine.internal.webserver.config.ALServerFactory$1.lambda$0(Unknown Source)
    at org.eclipse.jetty.servlets.QoSFilter.doFilter(QoSFilter.java:202)
    at com.anylogic.engine.internal.webserver.config.ALServerFactory$1.doHandle(Unknown Source)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1598)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
    at org.eclipse.jetty.server.Server.handle(Server.java:516)
    at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:487)
    at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:732)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:479)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:277)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105)
    at org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:338)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:315)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:173)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131)
    at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:409)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:883)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1034)
    at java.base/java.lang.Thread.run(Thread.java:834)

答案1

我找到了解决方案。在我callAPI使用 AnyLogic 编写的函数中,我将预期输出类型从“double”更改为“其他”,然后声明OperationResults。这是在“返回值”选项下完成的。函数输出不是期望给出的函数输出,而是在主页上的类代码中我创建的double类类型中。OperationResults

相关内容