机器学习和优化scipy

时间:2017-11-26 08:43:11

标签: python optimization scipy minimize

我有编码机学习和优化我的成本函数我使用package Server; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.util.ArrayList; import java.util.HashMap; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; import javax.xml.ws.Endpoint; @WebService @SOAPBinding(style=Style.RPC) public class ServerWST implements Runnable{ String fileName = "log_WST.txt"; File file = new File(fileName); FileWriter writer = null; static HashMap<String,HashMap<Integer,HashMap<String,String>>> outerMap = new HashMap<>(); static ArrayList<HashMap<String,String>> innerArr = new ArrayList<HashMap<String,String>>(); static ArrayList<HashMap<Integer,HashMap<String,String>>> outerArr = new ArrayList<HashMap<Integer,HashMap<String,String>>>(); static HashMap<String,Integer> outerDateIndexMap = new HashMap<String, Integer>(); static HashMap<Integer,Integer> innerRoomIndexMap = new HashMap<Integer,Integer>(); // this is for the booking ID implementation in the Book Room and the cancel Room methods and for changing the reservation in the upcoming assignment static ArrayList<String> components = new ArrayList<String>(); static HashMap<String, ArrayList<String>> bookingIDMap = new HashMap<String,ArrayList<String>>(); String bookingID = ""; int rr = 1500; static HashMap<ArrayList<String>, String> studentIDMap = new HashMap<ArrayList<String>, String>(); // ends here ---------------------------------------------------------------------------------------------- static HashMap<String,Integer> countRestriction = new HashMap<>(); // main method starts here public static void main(String args[] ) { UDPWST ut1 = new UDPWST(); Thread t = new Thread(ut1); t.start(); Object implementor = new ServerWST (); String address = "http://localhost:9002/ServerWST"; Endpoint.publish(address, implementor); } public void createRecord(String date, int roomNo, String timeSlot, String id) { innerArr.add(new HashMap<String, String>()); int innerIndex = innerArr.size() - 1; innerArr.get(innerIndex).put(timeSlot, id); outerArr.add(new HashMap<Integer, HashMap<String, String>>()); int outerIndex = outerArr.size() - 1; outerArr.get(outerIndex).put(roomNo, innerArr.get(innerIndex)); outerMap.put(date, outerArr.get(outerIndex)); innerRoomIndexMap.put(roomNo, innerIndex); outerDateIndexMap.put(date, outerIndex); } private void flush() { // TODO Auto-generated method stub } public String deleteRoom(String date, int roomNo, String timeSlot) { if (outerMap.get(date).get(roomNo).get(timeSlot) != null) { outerMap.get(date).get(roomNo).remove(timeSlot); System.out.println("Deletion Succesfull"); return "Deletion Succesfull"; } else { System.out.println("No such record found for the entries provided:"); return "No such record found for the entries provided:"; } } synchronized public String bookRoom(String date, int roomNo, String timeSlot,String id, String campusName) { if (campusName.startsWith("WST", 0)) { if (outerMap.get(date).get(roomNo).get(timeSlot) != null) { if (countRestriction.get(id) == null) countRestriction.put(id, 0); if (countRestriction.get(id) >= 3) { System.out.println("You cannot book this room because you have exceeded the maximum number of rooms that you can book per week."); } else if (outerMap.get(date).get(roomNo).get(timeSlot).equals("0")) { bookingID = "RR" + rr; components.add(date); components.add(roomNo + ""); components.add(timeSlot); components.add(id); bookingIDMap.put(bookingID, components); studentIDMap.put(components, id); rr++; outerMap.get(date).get(roomNo).replace(timeSlot, id); System.out.println("The room was booked by the student with the ID " + id + "and the booking ID is" + bookingID); countRestriction.replace(id, countRestriction.get(id) + 1); return bookingID; } } else { System.out.println("Sorry please try again... Could not book the room for you !!"); return ""; } } else if (campusName.startsWith("KKL", 0)) { DatagramSocket aSocket2 = null; // this is the part where i am implementing the booking room function in the other campus as well. try { aSocket2 = new DatagramSocket(); byte[] m = ("bROOM_" + date + "_" + roomNo + "_" + timeSlot + "_" + id + "_" + campusName).getBytes(); InetAddress aHost = InetAddress.getByName("localhost"); int serverPort = 1079; DatagramPacket request = new DatagramPacket(m, ("bROOM_" + date + "_" + roomNo + "_" + timeSlot + "_" + id + "_" + campusName).length(), aHost, serverPort); aSocket2.send(request); byte[] buffer = new byte[1000]; DatagramPacket reply = new DatagramPacket(buffer, buffer.length); aSocket2.receive(reply); System.out.println("Reply " + new String(reply.getData())); return new String(reply.getData()); } catch (SocketException e) { System.out.println("Socket: " + e.getMessage()); } catch (IOException e) { System.out.println("IO: DVL"); } finally { if (aSocket2 != null) aSocket2.close(); } } else if(campusName.startsWith("DVL", 0)) { DatagramSocket aSocket3 = null; try { aSocket3 = new DatagramSocket(); byte[] m = ("bROOM_" + date + "_" + roomNo + "_" + timeSlot + "_" + id + "_" + campusName).getBytes(); InetAddress aHost = InetAddress.getByName("localhost"); int serverPort = 1099; DatagramPacket request = new DatagramPacket(m, ("bROOM_" + date + "_" + roomNo + "_" + timeSlot + "_" + id + "_" + campusName).length(), aHost, serverPort); aSocket3.send(request); byte[] buffer = new byte[1000]; DatagramPacket reply = new DatagramPacket(buffer, buffer.length); aSocket3.receive(reply); System.out.println("Reply " + new String(reply.getData())); return new String(reply.getData()); } catch (SocketException e) { System.out.println("Socket: " + e.getMessage()); } catch (IOException e) { System.out.println("IO: DVL"); } finally { if (aSocket3 != null) aSocket3.close(); } } return ""; } public String getAvailableTimeSlotsWithoutT(String date) { int counter = 0; if(outerMap.get(date) != null){ for(int index : outerMap.get(date).keySet()) { for(String timeIndex : outerMap.get(date).get(index).keySet()) { if(outerMap.get(date).get(index).get(timeIndex)==null){ System.out.println("There is no value in the timeslot and hence nothing will be processed by WST"); } else if (outerMap.get(date).get(index).get(timeIndex).equals("0")){ counter++; } } } System.out.println("the number of records available in the WST campus are " + counter); return "WST" + counter; } else { System.out.println("there is nothing to show as their is no value for the timelots for those dates in WST campus."); return ""; } } public void getAvailabilityTimeSlotsWithT(String date){ DatagramSocket aSocket = null; DatagramSocket aSocket1 = null; String tempAvail = getAvailableTimeSlotsWithoutT(date); try { aSocket = new DatagramSocket(); byte[] m = ("gATSWT_" + date).getBytes(); InetAddress aHost = InetAddress.getByName("localhost"); int serverPort = 1079; DatagramPacket request = new DatagramPacket(m,("gATSWT_" + date).length(), aHost, serverPort); aSocket.send(request); byte[] buffer = new byte[1000]; DatagramPacket reply = new DatagramPacket(buffer, buffer.length); aSocket.receive(reply); System.out.println("Reply " + new String (reply.getData())); aSocket1 = new DatagramSocket(); byte[] m1 = ("gATSWT_" + date).getBytes(); InetAddress aHost1 = InetAddress.getByName("localhost"); int serverPort1 = 1099; DatagramPacket request1 = new DatagramPacket(m1,("gATSWT_" + date).length(), aHost1, serverPort1); aSocket1.send(request1); byte[] buffer1 = new byte[1000]; DatagramPacket reply1 = new DatagramPacket(buffer1, buffer1.length); aSocket1.receive(reply1); System.out.println("Reply " + new String (reply1.getData())); } catch(SocketException e) { System.out.println("Socket: " + e.getMessage()); } catch(IOException e) { System.out.println("IO: WST"); } finally { if(aSocket != null) aSocket.close(); } } @Override public void run() { // TODO Auto-generated method stub } synchronized public boolean cancelBooking(String bookingID) { String datec, roomNoc,timeSlotc; String tempStudentID = studentIDMap.get(bookingIDMap.get(bookingID)); datec = bookingIDMap.get(bookingID).get(0); roomNoc = bookingIDMap.get(bookingID).get(1).trim(); timeSlotc = bookingIDMap.get(bookingID).get(2); outerMap.get(datec).get(roomNoc).replace(timeSlotc, "0"); // this is to remove the booking ID from the bookingIDMAP HashMap bookingIDMap.remove(bookingID); return true; } synchronized public void changeBooking (String newDate, int newRoomNo, String newTimeSlot, String id, String bookingID, String newCampusName ) { // if(bookingIDMap.get(bookingID).get(3).equals("id") && outerMap.get(newDate).get(newRoomNo).get(newTimeSlot).equals("0")) // { String result = bookRoom( newDate, newRoomNo, newTimeSlot, id, newCampusName); if(result.equals("")) { System.out.println("the room could not be booked for some reason so there will be no change made to the previous room booking." + "\n"); } else { cancelBooking(bookingID); System.out.println("your booking with the booking ID " + bookingID + " has been cancelled and the new room with the room no" + newRoomNo + " date " + newDate + " timeSlot " + newTimeSlot + " has been booked"); } //} } } 并且scipy没有返回正确的答案。那我该怎么办?

代码:

scipy.optimize.minimum

这是说成功为假的消息 我做的一切都是正确的我不知道发生了什么

1 个答案:

答案 0 :(得分:0)

在以下情况下调试类似的东西很难:

    由于外部数据,
  • 代码无法重现
  • 问题甚至没有尝试解释这里优化的内容

有一些奇怪的设计决定:

  • 使用np.matrix - &gt;请使用np.array!
  • 不要调用jacobian gradientDescent

然后关于你的观察:

Iterations: 0
Function evaluations: 106
Gradient evaluations: 94

零迭代,同时进行如此多的功能评估是一个非常糟糕的迹象。有些东西很破碎。可能行搜索在上面疯狂,但这只是猜测。

现在什么坏了?:

  • 你的jacobian肯定坏了!
    • 我没有检查数学,但是:
      • 你的雅可比形状取决于变量数量固定时的样本数量 - &gt;不!这没有意义!

要做的步骤:

  • 使用jac=False运行
    • 如果工作:你的费用看起来不错
    • 如果不能正常工作:你的麻烦可能(没有证明)即使在
    • 开始
  • 修理jacobian!
  • 检查jacobian对check_grad

我想知道为什么你这里没有任何形状错误。当我试图模仿您的输入形状并玩弄样本大小时,我会这样做!